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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273242742427524276242772427824279242802428124282242832428424285242862428724288242892429024291242922429324294242952429624297242982429924300243012430224303243042430524306243072430824309243102431124312243132431424315243162431724318243192432024321243222432324324243252432624327243282432924330243312433224333243342433524336243372433824339243402434124342243432434424345243462434724348243492435024351243522435324354243552435624357243582435924360243612436224363243642436524366243672436824369243702437124372243732437424375243762437724378243792438024381243822438324384243852438624387243882438924390243912439224393243942439524396243972439824399244002440124402244032440424405244062440724408244092441024411244122441324414244152441624417244182441924420244212442224423244242442524426244272442824429244302443124432244332443424435244362443724438244392444024441244422444324444244452444624447244482444924450244512445224453244542445524456244572445824459244602446124462244632446424465244662446724468244692447024471244722447324474244752447624477244782447924480244812448224483244842448524486244872448824489244902449124492244932449424495244962449724498244992450024501245022450324504245052450624507245082450924510245112451224513245142451524516245172451824519245202452124522245232452424525245262452724528245292453024531245322453324534245352453624537245382453924540245412454224543245442454524546245472454824549245502455124552245532455424555245562455724558245592456024561245622456324564245652456624567245682456924570245712457224573245742457524576245772457824579245802458124582245832458424585245862458724588245892459024591245922459324594245952459624597245982459924600246012460224603246042460524606246072460824609246102461124612246132461424615246162461724618246192462024621246222462324624246252462624627246282462924630246312463224633246342463524636246372463824639246402464124642246432464424645246462464724648246492465024651246522465324654246552465624657246582465924660246612466224663246642466524666246672466824669246702467124672246732467424675246762467724678246792468024681246822468324684246852468624687246882468924690246912469224693246942469524696246972469824699247002470124702247032470424705247062470724708247092471024711247122471324714247152471624717247182471924720247212472224723247242472524726247272472824729247302473124732247332473424735247362473724738247392474024741247422474324744247452474624747247482474924750247512475224753247542475524756247572475824759247602476124762247632476424765247662476724768247692477024771247722477324774247752477624777247782477924780247812478224783247842478524786247872478824789247902479124792247932479424795247962479724798247992480024801248022480324804248052480624807248082480924810248112481224813248142481524816248172481824819248202482124822248232482424825248262482724828248292483024831248322483324834248352483624837248382483924840248412484224843248442484524846248472484824849248502485124852248532485424855248562485724858248592486024861248622486324864248652486624867248682486924870248712487224873248742487524876248772487824879248802488124882248832488424885248862488724888248892489024891248922489324894248952489624897248982489924900249012490224903249042490524906249072490824909249102491124912249132491424915249162491724918249192492024921249222492324924249252492624927249282492924930249312493224933249342493524936249372493824939249402494124942249432494424945249462494724948249492495024951249522495324954249552495624957249582495924960249612496224963249642496524966249672496824969249702497124972249732497424975249762497724978249792498024981249822498324984249852498624987249882498924990249912499224993249942499524996249972499824999250002500125002250032500425005250062500725008250092501025011250122501325014250152501625017250182501925020250212502225023250242502525026250272502825029250302503125032250332503425035250362503725038250392504025041250422504325044250452504625047250482504925050250512505225053250542505525056250572505825059250602506125062250632506425065250662506725068250692507025071250722507325074250752507625077250782507925080250812508225083250842508525086250872508825089250902509125092250932509425095250962509725098250992510025101251022510325104251052510625107251082510925110251112511225113251142511525116251172511825119251202512125122251232512425125251262512725128251292513025131251322513325134251352513625137251382513925140251412514225143251442514525146251472514825149251502515125152251532515425155251562515725158251592516025161251622516325164251652516625167251682516925170251712517225173251742517525176251772517825179251802518125182251832518425185251862518725188251892519025191251922519325194251952519625197251982519925200252012520225203252042520525206252072520825209252102521125212252132521425215252162521725218252192522025221252222522325224252252522625227252282522925230252312523225233252342523525236252372523825239252402524125242252432524425245252462524725248252492525025251252522525325254252552525625257252582525925260252612526225263252642526525266252672526825269252702527125272252732527425275252762527725278252792528025281252822528325284252852528625287252882528925290252912529225293252942529525296252972529825299253002530125302253032530425305253062530725308253092531025311253122531325314253152531625317253182531925320253212532225323253242532525326253272532825329253302533125332253332533425335253362533725338253392534025341253422534325344253452534625347253482534925350253512535225353253542535525356253572535825359253602536125362253632536425365253662536725368253692537025371253722537325374253752537625377253782537925380253812538225383253842538525386253872538825389253902539125392253932539425395253962539725398253992540025401254022540325404254052540625407254082540925410254112541225413254142541525416254172541825419254202542125422254232542425425254262542725428254292543025431254322543325434254352543625437254382543925440254412544225443254442544525446254472544825449254502545125452254532545425455254562545725458254592546025461254622546325464254652546625467254682546925470254712547225473254742547525476254772547825479254802548125482254832548425485254862548725488254892549025491254922549325494254952549625497254982549925500255012550225503255042550525506255072550825509255102551125512255132551425515255162551725518255192552025521255222552325524255252552625527255282552925530255312553225533255342553525536255372553825539255402554125542255432554425545255462554725548255492555025551255522555325554255552555625557255582555925560255612556225563255642556525566255672556825569255702557125572255732557425575255762557725578255792558025581255822558325584255852558625587255882558925590255912559225593255942559525596255972559825599256002560125602256032560425605256062560725608256092561025611256122561325614256152561625617256182561925620256212562225623256242562525626256272562825629256302563125632256332563425635256362563725638256392564025641256422564325644256452564625647256482564925650256512565225653256542565525656256572565825659256602566125662256632566425665256662566725668256692567025671256722567325674256752567625677256782567925680256812568225683256842568525686256872568825689256902569125692256932569425695256962569725698256992570025701257022570325704257052570625707257082570925710257112571225713257142571525716257172571825719257202572125722257232572425725257262572725728257292573025731257322573325734257352573625737257382573925740257412574225743257442574525746257472574825749257502575125752257532575425755257562575725758257592576025761257622576325764257652576625767257682576925770257712577225773257742577525776257772577825779257802578125782257832578425785257862578725788257892579025791257922579325794257952579625797257982579925800258012580225803258042580525806258072580825809258102581125812258132581425815258162581725818258192582025821258222582325824258252582625827258282582925830258312583225833258342583525836258372583825839258402584125842258432584425845258462584725848258492585025851258522585325854258552585625857258582585925860258612586225863258642586525866258672586825869258702587125872258732587425875258762587725878258792588025881258822588325884258852588625887258882588925890258912589225893258942589525896258972589825899259002590125902259032590425905259062590725908259092591025911259122591325914259152591625917259182591925920259212592225923259242592525926259272592825929259302593125932259332593425935259362593725938259392594025941259422594325944259452594625947259482594925950259512595225953259542595525956259572595825959259602596125962259632596425965259662596725968259692597025971259722597325974259752597625977259782597925980259812598225983259842598525986259872598825989259902599125992259932599425995259962599725998259992600026001260022600326004260052600626007260082600926010260112601226013260142601526016260172601826019260202602126022260232602426025260262602726028260292603026031260322603326034260352603626037260382603926040260412604226043260442604526046260472604826049260502605126052260532605426055260562605726058260592606026061260622606326064260652606626067260682606926070260712607226073260742607526076260772607826079260802608126082260832608426085260862608726088260892609026091260922609326094260952609626097260982609926100261012610226103261042610526106261072610826109261102611126112261132611426115261162611726118261192612026121261222612326124261252612626127261282612926130261312613226133261342613526136261372613826139261402614126142261432614426145261462614726148261492615026151261522615326154261552615626157261582615926160261612616226163261642616526166261672616826169261702617126172261732617426175261762617726178261792618026181261822618326184261852618626187261882618926190261912619226193261942619526196261972619826199262002620126202262032620426205262062620726208262092621026211262122621326214262152621626217262182621926220262212622226223262242622526226262272622826229262302623126232262332623426235262362623726238262392624026241262422624326244262452624626247262482624926250262512625226253262542625526256262572625826259262602626126262262632626426265262662626726268262692627026271262722627326274262752627626277262782627926280262812628226283262842628526286262872628826289262902629126292262932629426295262962629726298262992630026301263022630326304263052630626307263082630926310263112631226313263142631526316263172631826319263202632126322263232632426325263262632726328263292633026331263322633326334263352633626337263382633926340263412634226343263442634526346263472634826349263502635126352263532635426355263562635726358263592636026361263622636326364263652636626367263682636926370263712637226373263742637526376263772637826379263802638126382263832638426385263862638726388263892639026391263922639326394263952639626397263982639926400264012640226403264042640526406264072640826409264102641126412264132641426415264162641726418264192642026421264222642326424264252642626427264282642926430264312643226433264342643526436264372643826439264402644126442264432644426445264462644726448264492645026451264522645326454264552645626457264582645926460264612646226463264642646526466264672646826469264702647126472264732647426475264762647726478264792648026481264822648326484264852648626487264882648926490264912649226493264942649526496264972649826499265002650126502265032650426505265062650726508265092651026511265122651326514265152651626517265182651926520265212652226523265242652526526265272652826529265302653126532265332653426535265362653726538265392654026541265422654326544265452654626547265482654926550265512655226553265542655526556265572655826559265602656126562265632656426565265662656726568265692657026571265722657326574265752657626577265782657926580265812658226583265842658526586265872658826589265902659126592265932659426595265962659726598265992660026601266022660326604266052660626607266082660926610266112661226613266142661526616266172661826619266202662126622266232662426625266262662726628266292663026631266322663326634266352663626637266382663926640266412664226643266442664526646266472664826649266502665126652266532665426655266562665726658266592666026661266622666326664266652666626667266682666926670266712667226673266742667526676266772667826679266802668126682266832668426685266862668726688266892669026691266922669326694266952669626697266982669926700267012670226703267042670526706267072670826709267102671126712267132671426715267162671726718267192672026721267222672326724267252672626727267282672926730267312673226733267342673526736267372673826739267402674126742267432674426745267462674726748267492675026751267522675326754267552675626757267582675926760267612676226763267642676526766267672676826769267702677126772267732677426775267762677726778267792678026781267822678326784267852678626787267882678926790267912679226793267942679526796267972679826799268002680126802268032680426805268062680726808268092681026811268122681326814268152681626817268182681926820268212682226823268242682526826268272682826829268302683126832268332683426835268362683726838268392684026841268422684326844268452684626847268482684926850268512685226853268542685526856268572685826859268602686126862268632686426865268662686726868268692687026871268722687326874268752687626877268782687926880268812688226883268842688526886268872688826889268902689126892268932689426895268962689726898268992690026901269022690326904269052690626907269082690926910269112691226913269142691526916269172691826919269202692126922269232692426925269262692726928269292693026931269322693326934269352693626937269382693926940269412694226943269442694526946269472694826949269502695126952269532695426955269562695726958269592696026961269622696326964269652696626967269682696926970269712697226973269742697526976269772697826979269802698126982269832698426985269862698726988269892699026991269922699326994269952699626997269982699927000270012700227003270042700527006270072700827009270102701127012270132701427015270162701727018270192702027021270222702327024270252702627027270282702927030270312703227033270342703527036270372703827039270402704127042270432704427045270462704727048270492705027051270522705327054270552705627057270582705927060270612706227063270642706527066270672706827069270702707127072270732707427075270762707727078270792708027081270822708327084270852708627087270882708927090270912709227093270942709527096270972709827099271002710127102271032710427105271062710727108271092711027111271122711327114271152711627117271182711927120271212712227123271242712527126271272712827129271302713127132271332713427135271362713727138271392714027141271422714327144271452714627147271482714927150271512715227153271542715527156271572715827159271602716127162271632716427165271662716727168271692717027171271722717327174271752717627177271782717927180271812718227183271842718527186271872718827189271902719127192271932719427195271962719727198271992720027201272022720327204272052720627207272082720927210272112721227213272142721527216272172721827219272202722127222272232722427225272262722727228272292723027231272322723327234272352723627237272382723927240272412724227243272442724527246272472724827249272502725127252272532725427255272562725727258272592726027261272622726327264272652726627267272682726927270272712727227273272742727527276272772727827279272802728127282272832728427285272862728727288272892729027291272922729327294272952729627297272982729927300273012730227303273042730527306273072730827309273102731127312273132731427315273162731727318273192732027321273222732327324273252732627327273282732927330273312733227333273342733527336273372733827339273402734127342273432734427345273462734727348273492735027351273522735327354273552735627357273582735927360273612736227363273642736527366273672736827369273702737127372273732737427375273762737727378273792738027381273822738327384273852738627387273882738927390273912739227393273942739527396273972739827399274002740127402274032740427405274062740727408274092741027411274122741327414274152741627417274182741927420274212742227423274242742527426274272742827429274302743127432274332743427435274362743727438274392744027441274422744327444274452744627447274482744927450274512745227453274542745527456274572745827459274602746127462274632746427465274662746727468274692747027471274722747327474274752747627477274782747927480274812748227483274842748527486274872748827489274902749127492274932749427495274962749727498274992750027501275022750327504275052750627507275082750927510275112751227513275142751527516275172751827519275202752127522275232752427525275262752727528275292753027531275322753327534275352753627537275382753927540275412754227543275442754527546275472754827549275502755127552275532755427555275562755727558275592756027561275622756327564275652756627567275682756927570275712757227573275742757527576275772757827579275802758127582275832758427585275862758727588275892759027591275922759327594275952759627597275982759927600276012760227603276042760527606276072760827609276102761127612276132761427615276162761727618276192762027621276222762327624276252762627627276282762927630276312763227633276342763527636276372763827639276402764127642276432764427645276462764727648276492765027651276522765327654276552765627657276582765927660276612766227663276642766527666276672766827669276702767127672276732767427675276762767727678276792768027681276822768327684276852768627687276882768927690276912769227693276942769527696276972769827699277002770127702277032770427705277062770727708277092771027711277122771327714277152771627717277182771927720277212772227723277242772527726277272772827729277302773127732277332773427735277362773727738277392774027741277422774327744277452774627747277482774927750277512775227753277542775527756277572775827759277602776127762277632776427765277662776727768277692777027771277722777327774277752777627777277782777927780277812778227783277842778527786277872778827789277902779127792277932779427795277962779727798277992780027801278022780327804278052780627807278082780927810278112781227813278142781527816278172781827819278202782127822278232782427825278262782727828278292783027831278322783327834278352783627837278382783927840278412784227843278442784527846278472784827849278502785127852278532785427855278562785727858278592786027861278622786327864278652786627867278682786927870278712787227873278742787527876278772787827879278802788127882278832788427885278862788727888278892789027891278922789327894278952789627897278982789927900279012790227903279042790527906279072790827909279102791127912279132791427915279162791727918279192792027921279222792327924279252792627927279282792927930279312793227933279342793527936279372793827939279402794127942279432794427945279462794727948279492795027951279522795327954279552795627957279582795927960279612796227963279642796527966279672796827969279702797127972279732797427975279762797727978279792798027981279822798327984279852798627987279882798927990279912799227993279942799527996279972799827999280002800128002280032800428005280062800728008280092801028011280122801328014280152801628017280182801928020280212802228023280242802528026280272802828029280302803128032280332803428035280362803728038280392804028041280422804328044280452804628047280482804928050280512805228053280542805528056280572805828059280602806128062280632806428065280662806728068280692807028071280722807328074280752807628077280782807928080280812808228083280842808528086280872808828089280902809128092280932809428095280962809728098280992810028101281022810328104281052810628107281082810928110281112811228113281142811528116281172811828119281202812128122281232812428125281262812728128281292813028131281322813328134281352813628137281382813928140281412814228143281442814528146281472814828149281502815128152281532815428155281562815728158281592816028161281622816328164281652816628167281682816928170281712817228173281742817528176281772817828179281802818128182281832818428185281862818728188281892819028191281922819328194281952819628197281982819928200282012820228203282042820528206282072820828209282102821128212282132821428215282162821728218282192822028221282222822328224282252822628227282282822928230282312823228233282342823528236282372823828239282402824128242282432824428245282462824728248282492825028251282522825328254282552825628257282582825928260282612826228263282642826528266282672826828269282702827128272282732827428275282762827728278282792828028281282822828328284282852828628287282882828928290282912829228293282942829528296282972829828299283002830128302283032830428305283062830728308283092831028311283122831328314283152831628317283182831928320283212832228323283242832528326283272832828329283302833128332283332833428335283362833728338283392834028341283422834328344283452834628347283482834928350283512835228353283542835528356283572835828359283602836128362283632836428365283662836728368283692837028371283722837328374283752837628377283782837928380283812838228383283842838528386283872838828389283902839128392283932839428395283962839728398283992840028401284022840328404284052840628407284082840928410284112841228413284142841528416284172841828419284202842128422284232842428425284262842728428284292843028431284322843328434284352843628437284382843928440284412844228443284442844528446284472844828449284502845128452284532845428455284562845728458284592846028461284622846328464284652846628467284682846928470284712847228473284742847528476284772847828479284802848128482284832848428485284862848728488284892849028491284922849328494284952849628497284982849928500285012850228503285042850528506285072850828509285102851128512285132851428515285162851728518285192852028521285222852328524285252852628527285282852928530285312853228533285342853528536285372853828539285402854128542285432854428545285462854728548285492855028551285522855328554285552855628557285582855928560285612856228563285642856528566285672856828569285702857128572285732857428575285762857728578285792858028581285822858328584285852858628587285882858928590285912859228593285942859528596285972859828599286002860128602286032860428605286062860728608286092861028611286122861328614286152861628617286182861928620286212862228623286242862528626286272862828629286302863128632286332863428635286362863728638286392864028641286422864328644286452864628647286482864928650286512865228653286542865528656286572865828659286602866128662286632866428665286662866728668286692867028671286722867328674286752867628677286782867928680286812868228683286842868528686286872868828689286902869128692286932869428695286962869728698286992870028701287022870328704287052870628707287082870928710287112871228713287142871528716287172871828719287202872128722287232872428725287262872728728287292873028731287322873328734287352873628737287382873928740287412874228743287442874528746287472874828749287502875128752287532875428755287562875728758287592876028761287622876328764287652876628767287682876928770287712877228773287742877528776287772877828779287802878128782287832878428785287862878728788287892879028791287922879328794287952879628797287982879928800288012880228803288042880528806288072880828809288102881128812288132881428815288162881728818288192882028821288222882328824288252882628827288282882928830288312883228833288342883528836288372883828839288402884128842288432884428845288462884728848288492885028851288522885328854288552885628857288582885928860288612886228863288642886528866288672886828869288702887128872288732887428875288762887728878288792888028881288822888328884288852888628887288882888928890288912889228893288942889528896288972889828899289002890128902289032890428905289062890728908289092891028911289122891328914289152891628917289182891928920289212892228923289242892528926289272892828929289302893128932289332893428935289362893728938289392894028941289422894328944289452894628947289482894928950289512895228953289542895528956289572895828959289602896128962289632896428965289662896728968289692897028971289722897328974289752897628977289782897928980289812898228983289842898528986289872898828989289902899128992289932899428995289962899728998289992900029001290022900329004290052900629007290082900929010290112901229013290142901529016290172901829019290202902129022290232902429025290262902729028290292903029031290322903329034290352903629037290382903929040290412904229043290442904529046290472904829049290502905129052290532905429055290562905729058290592906029061290622906329064290652906629067290682906929070290712907229073290742907529076290772907829079290802908129082290832908429085290862908729088290892909029091290922909329094290952909629097290982909929100291012910229103291042910529106291072910829109291102911129112291132911429115291162911729118291192912029121291222912329124291252912629127291282912929130291312913229133291342913529136291372913829139291402914129142291432914429145291462914729148291492915029151291522915329154291552915629157291582915929160291612916229163291642916529166291672916829169291702917129172291732917429175291762917729178291792918029181291822918329184291852918629187291882918929190291912919229193291942919529196291972919829199292002920129202292032920429205292062920729208292092921029211292122921329214292152921629217292182921929220292212922229223292242922529226292272922829229292302923129232292332923429235292362923729238292392924029241292422924329244292452924629247292482924929250292512925229253292542925529256292572925829259292602926129262292632926429265292662926729268292692927029271292722927329274292752927629277292782927929280292812928229283292842928529286292872928829289292902929129292292932929429295292962929729298292992930029301293022930329304293052930629307293082930929310293112931229313293142931529316293172931829319293202932129322293232932429325293262932729328293292933029331293322933329334293352933629337293382933929340293412934229343293442934529346293472934829349293502935129352293532935429355293562935729358293592936029361293622936329364293652936629367293682936929370293712937229373293742937529376293772937829379293802938129382293832938429385293862938729388293892939029391293922939329394293952939629397293982939929400294012940229403294042940529406294072940829409294102941129412294132941429415294162941729418294192942029421294222942329424294252942629427294282942929430294312943229433294342943529436294372943829439294402944129442294432944429445294462944729448294492945029451294522945329454294552945629457294582945929460294612946229463294642946529466294672946829469294702947129472294732947429475294762947729478294792948029481294822948329484294852948629487294882948929490294912949229493294942949529496294972949829499295002950129502295032950429505295062950729508295092951029511295122951329514295152951629517295182951929520295212952229523295242952529526295272952829529295302953129532295332953429535295362953729538295392954029541295422954329544295452954629547295482954929550295512955229553295542955529556295572955829559295602956129562295632956429565295662956729568295692957029571295722957329574295752957629577295782957929580295812958229583295842958529586295872958829589295902959129592295932959429595295962959729598295992960029601296022960329604296052960629607296082960929610296112961229613296142961529616296172961829619296202962129622296232962429625296262962729628296292963029631296322963329634296352963629637296382963929640296412964229643296442964529646296472964829649296502965129652296532965429655296562965729658296592966029661296622966329664296652966629667296682966929670296712967229673296742967529676296772967829679296802968129682296832968429685296862968729688296892969029691296922969329694296952969629697296982969929700297012970229703297042970529706297072970829709297102971129712297132971429715297162971729718297192972029721297222972329724297252972629727297282972929730297312973229733297342973529736297372973829739297402974129742297432974429745297462974729748297492975029751297522975329754297552975629757297582975929760297612976229763297642976529766297672976829769297702977129772297732977429775297762977729778297792978029781297822978329784297852978629787297882978929790297912979229793297942979529796297972979829799298002980129802298032980429805298062980729808298092981029811298122981329814298152981629817298182981929820298212982229823298242982529826298272982829829298302983129832298332983429835298362983729838298392984029841298422984329844298452984629847298482984929850298512985229853298542985529856298572985829859298602986129862298632986429865298662986729868298692987029871298722987329874298752987629877298782987929880298812988229883298842988529886298872988829889298902989129892298932989429895298962989729898298992990029901299022990329904299052990629907299082990929910299112991229913299142991529916299172991829919299202992129922299232992429925299262992729928299292993029931299322993329934299352993629937299382993929940299412994229943299442994529946299472994829949299502995129952299532995429955299562995729958299592996029961299622996329964299652996629967299682996929970299712997229973299742997529976299772997829979299802998129982299832998429985299862998729988299892999029991299922999329994299952999629997299982999930000300013000230003300043000530006300073000830009300103001130012300133001430015300163001730018300193002030021300223002330024300253002630027300283002930030300313003230033300343003530036300373003830039300403004130042300433004430045300463004730048300493005030051300523005330054300553005630057300583005930060300613006230063300643006530066300673006830069300703007130072300733007430075300763007730078300793008030081300823008330084300853008630087300883008930090300913009230093300943009530096300973009830099301003010130102301033010430105301063010730108301093011030111301123011330114301153011630117301183011930120301213012230123301243012530126301273012830129301303013130132301333013430135301363013730138301393014030141301423014330144301453014630147301483014930150301513015230153301543015530156301573015830159301603016130162301633016430165301663016730168301693017030171301723017330174301753017630177301783017930180301813018230183301843018530186301873018830189301903019130192301933019430195301963019730198301993020030201302023020330204302053020630207302083020930210302113021230213302143021530216302173021830219302203022130222302233022430225302263022730228302293023030231302323023330234302353023630237302383023930240302413024230243302443024530246302473024830249302503025130252302533025430255302563025730258302593026030261302623026330264302653026630267302683026930270302713027230273302743027530276302773027830279302803028130282302833028430285302863028730288302893029030291302923029330294302953029630297302983029930300303013030230303303043030530306303073030830309303103031130312303133031430315303163031730318303193032030321303223032330324303253032630327303283032930330303313033230333303343033530336303373033830339303403034130342303433034430345303463034730348303493035030351303523035330354303553035630357303583035930360303613036230363303643036530366303673036830369303703037130372303733037430375303763037730378303793038030381303823038330384303853038630387303883038930390303913039230393303943039530396303973039830399304003040130402304033040430405304063040730408304093041030411304123041330414304153041630417304183041930420304213042230423304243042530426304273042830429304303043130432304333043430435304363043730438304393044030441304423044330444304453044630447304483044930450304513045230453304543045530456304573045830459304603046130462304633046430465304663046730468304693047030471304723047330474304753047630477304783047930480304813048230483304843048530486304873048830489304903049130492304933049430495304963049730498304993050030501305023050330504305053050630507305083050930510305113051230513305143051530516305173051830519305203052130522305233052430525305263052730528305293053030531305323053330534305353053630537305383053930540305413054230543305443054530546305473054830549305503055130552305533055430555305563055730558305593056030561305623056330564305653056630567305683056930570305713057230573305743057530576305773057830579305803058130582305833058430585305863058730588305893059030591305923059330594305953059630597305983059930600306013060230603306043060530606306073060830609306103061130612306133061430615306163061730618306193062030621306223062330624306253062630627306283062930630306313063230633306343063530636306373063830639306403064130642306433064430645306463064730648306493065030651306523065330654306553065630657306583065930660306613066230663306643066530666306673066830669306703067130672306733067430675306763067730678306793068030681306823068330684306853068630687306883068930690306913069230693306943069530696306973069830699307003070130702307033070430705307063070730708307093071030711307123071330714307153071630717307183071930720307213072230723307243072530726307273072830729307303073130732307333073430735307363073730738307393074030741307423074330744307453074630747307483074930750307513075230753307543075530756307573075830759307603076130762307633076430765307663076730768307693077030771307723077330774307753077630777307783077930780307813078230783307843078530786307873078830789307903079130792307933079430795307963079730798307993080030801308023080330804308053080630807308083080930810308113081230813308143081530816308173081830819308203082130822308233082430825308263082730828308293083030831308323083330834308353083630837308383083930840308413084230843308443084530846308473084830849308503085130852308533085430855308563085730858308593086030861308623086330864308653086630867308683086930870308713087230873308743087530876308773087830879308803088130882308833088430885308863088730888308893089030891308923089330894308953089630897308983089930900309013090230903309043090530906309073090830909309103091130912309133091430915309163091730918309193092030921309223092330924309253092630927309283092930930309313093230933309343093530936309373093830939309403094130942309433094430945309463094730948309493095030951309523095330954309553095630957309583095930960309613096230963309643096530966309673096830969309703097130972309733097430975309763097730978309793098030981309823098330984309853098630987309883098930990309913099230993309943099530996309973099830999310003100131002310033100431005310063100731008310093101031011310123101331014310153101631017310183101931020310213102231023310243102531026310273102831029310303103131032310333103431035310363103731038310393104031041310423104331044310453104631047310483104931050310513105231053310543105531056310573105831059310603106131062310633106431065310663106731068310693107031071310723107331074310753107631077310783107931080310813108231083310843108531086310873108831089310903109131092310933109431095310963109731098310993110031101311023110331104311053110631107311083110931110311113111231113311143111531116311173111831119311203112131122311233112431125311263112731128311293113031131311323113331134311353113631137311383113931140311413114231143311443114531146311473114831149311503115131152311533115431155311563115731158311593116031161311623116331164311653116631167311683116931170311713117231173311743117531176311773117831179311803118131182311833118431185311863118731188311893119031191311923119331194311953119631197311983119931200312013120231203312043120531206312073120831209312103121131212312133121431215312163121731218312193122031221312223122331224312253122631227312283122931230312313123231233312343123531236312373123831239312403124131242312433124431245312463124731248312493125031251312523125331254312553125631257312583125931260312613126231263312643126531266312673126831269312703127131272312733127431275312763127731278312793128031281312823128331284312853128631287312883128931290312913129231293312943129531296312973129831299313003130131302313033130431305313063130731308313093131031311313123131331314313153131631317313183131931320313213132231323313243132531326313273132831329313303133131332313333133431335313363133731338313393134031341313423134331344313453134631347313483134931350313513135231353313543135531356313573135831359313603136131362313633136431365313663136731368313693137031371313723137331374313753137631377313783137931380313813138231383313843138531386313873138831389313903139131392313933139431395313963139731398313993140031401314023140331404314053140631407314083140931410314113141231413314143141531416314173141831419314203142131422314233142431425314263142731428314293143031431314323143331434314353143631437314383143931440314413144231443314443144531446314473144831449314503145131452314533145431455314563145731458314593146031461314623146331464314653146631467314683146931470314713147231473314743147531476314773147831479314803148131482314833148431485314863148731488314893149031491314923149331494314953149631497314983149931500315013150231503315043150531506315073150831509315103151131512315133151431515315163151731518315193152031521315223152331524315253152631527315283152931530315313153231533315343153531536315373153831539315403154131542315433154431545315463154731548315493155031551315523155331554315553155631557315583155931560315613156231563315643156531566315673156831569315703157131572315733157431575315763157731578315793158031581315823158331584315853158631587315883158931590315913159231593315943159531596315973159831599316003160131602316033160431605316063160731608316093161031611316123161331614316153161631617316183161931620316213162231623316243162531626316273162831629316303163131632316333163431635316363163731638316393164031641316423164331644316453164631647316483164931650316513165231653316543165531656316573165831659316603166131662316633166431665316663166731668316693167031671316723167331674316753167631677316783167931680316813168231683316843168531686316873168831689316903169131692316933169431695316963169731698316993170031701317023170331704317053170631707317083170931710317113171231713317143171531716317173171831719317203172131722317233172431725317263172731728317293173031731317323173331734317353173631737317383173931740317413174231743317443174531746317473174831749317503175131752317533175431755317563175731758317593176031761317623176331764317653176631767317683176931770317713177231773317743177531776317773177831779317803178131782317833178431785317863178731788317893179031791317923179331794317953179631797317983179931800318013180231803318043180531806318073180831809318103181131812318133181431815318163181731818318193182031821318223182331824318253182631827318283182931830318313183231833318343183531836318373183831839318403184131842318433184431845318463184731848318493185031851318523185331854318553185631857318583185931860318613186231863318643186531866318673186831869318703187131872318733187431875318763187731878318793188031881318823188331884318853188631887318883188931890318913189231893318943189531896318973189831899319003190131902319033190431905319063190731908319093191031911319123191331914319153191631917319183191931920319213192231923319243192531926319273192831929319303193131932319333193431935319363193731938319393194031941319423194331944319453194631947319483194931950319513195231953319543195531956319573195831959319603196131962319633196431965319663196731968319693197031971319723197331974319753197631977319783197931980319813198231983319843198531986319873198831989319903199131992319933199431995319963199731998319993200032001320023200332004320053200632007320083200932010320113201232013320143201532016320173201832019320203202132022320233202432025320263202732028320293203032031320323203332034320353203632037320383203932040320413204232043320443204532046320473204832049320503205132052320533205432055320563205732058320593206032061320623206332064320653206632067320683206932070320713207232073320743207532076320773207832079320803208132082320833208432085320863208732088320893209032091320923209332094320953209632097320983209932100321013210232103321043210532106321073210832109321103211132112321133211432115321163211732118321193212032121321223212332124321253212632127321283212932130321313213232133321343213532136321373213832139321403214132142321433214432145321463214732148321493215032151321523215332154321553215632157321583215932160321613216232163321643216532166321673216832169321703217132172321733217432175321763217732178321793218032181321823218332184321853218632187321883218932190321913219232193321943219532196321973219832199322003220132202322033220432205322063220732208322093221032211322123221332214322153221632217322183221932220322213222232223322243222532226322273222832229322303223132232322333223432235322363223732238322393224032241322423224332244322453224632247322483224932250322513225232253322543225532256322573225832259322603226132262322633226432265322663226732268322693227032271322723227332274322753227632277322783227932280322813228232283322843228532286322873228832289322903229132292322933229432295322963229732298322993230032301323023230332304323053230632307323083230932310323113231232313323143231532316323173231832319323203232132322323233232432325323263232732328323293233032331323323233332334323353233632337323383233932340323413234232343323443234532346323473234832349323503235132352323533235432355323563235732358323593236032361323623236332364323653236632367323683236932370323713237232373323743237532376323773237832379323803238132382323833238432385323863238732388323893239032391323923239332394323953239632397323983239932400324013240232403324043240532406324073240832409324103241132412324133241432415324163241732418324193242032421324223242332424324253242632427324283242932430324313243232433324343243532436324373243832439324403244132442324433244432445324463244732448324493245032451324523245332454324553245632457324583245932460324613246232463324643246532466324673246832469324703247132472324733247432475324763247732478324793248032481324823248332484324853248632487324883248932490324913249232493324943249532496324973249832499325003250132502325033250432505325063250732508325093251032511325123251332514325153251632517325183251932520325213252232523325243252532526325273252832529325303253132532325333253432535325363253732538325393254032541325423254332544325453254632547325483254932550325513255232553325543255532556325573255832559325603256132562325633256432565325663256732568325693257032571325723257332574325753257632577325783257932580325813258232583325843258532586325873258832589325903259132592325933259432595325963259732598325993260032601326023260332604326053260632607326083260932610326113261232613326143261532616326173261832619326203262132622326233262432625326263262732628326293263032631326323263332634326353263632637326383263932640326413264232643326443264532646326473264832649326503265132652326533265432655326563265732658326593266032661326623266332664326653266632667326683266932670326713267232673326743267532676326773267832679326803268132682326833268432685326863268732688326893269032691326923269332694326953269632697326983269932700327013270232703327043270532706327073270832709327103271132712327133271432715327163271732718327193272032721327223272332724327253272632727327283272932730327313273232733327343273532736327373273832739327403274132742327433274432745327463274732748327493275032751327523275332754327553275632757327583275932760327613276232763327643276532766327673276832769327703277132772327733277432775327763277732778327793278032781327823278332784327853278632787327883278932790327913279232793327943279532796327973279832799328003280132802328033280432805328063280732808328093281032811328123281332814328153281632817328183281932820328213282232823328243282532826328273282832829328303283132832328333283432835328363283732838328393284032841328423284332844328453284632847328483284932850328513285232853328543285532856328573285832859328603286132862328633286432865328663286732868328693287032871328723287332874328753287632877328783287932880328813288232883328843288532886328873288832889328903289132892328933289432895328963289732898328993290032901329023290332904329053290632907329083290932910329113291232913329143291532916329173291832919329203292132922329233292432925329263292732928329293293032931329323293332934329353293632937329383293932940329413294232943329443294532946329473294832949329503295132952329533295432955329563295732958329593296032961329623296332964329653296632967329683296932970329713297232973329743297532976329773297832979329803298132982329833298432985329863298732988329893299032991329923299332994329953299632997329983299933000330013300233003330043300533006330073300833009330103301133012330133301433015330163301733018330193302033021330223302333024330253302633027330283302933030330313303233033330343303533036330373303833039330403304133042330433304433045330463304733048330493305033051330523305333054330553305633057330583305933060330613306233063330643306533066330673306833069330703307133072330733307433075330763307733078330793308033081330823308333084330853308633087330883308933090330913309233093330943309533096330973309833099331003310133102331033310433105331063310733108331093311033111331123311333114331153311633117331183311933120331213312233123331243312533126331273312833129331303313133132331333313433135331363313733138331393314033141331423314333144331453314633147331483314933150331513315233153331543315533156331573315833159331603316133162331633316433165331663316733168331693317033171331723317333174331753317633177331783317933180331813318233183331843318533186331873318833189331903319133192331933319433195331963319733198331993320033201332023320333204332053320633207332083320933210332113321233213332143321533216332173321833219332203322133222332233322433225332263322733228332293323033231332323323333234332353323633237332383323933240332413324233243332443324533246332473324833249332503325133252332533325433255332563325733258332593326033261332623326333264332653326633267332683326933270332713327233273332743327533276332773327833279332803328133282332833328433285332863328733288332893329033291332923329333294332953329633297332983329933300333013330233303333043330533306333073330833309333103331133312333133331433315333163331733318333193332033321333223332333324333253332633327333283332933330333313333233333333343333533336333373333833339333403334133342333433334433345333463334733348333493335033351333523335333354333553335633357333583335933360333613336233363333643336533366333673336833369333703337133372333733337433375333763337733378333793338033381333823338333384333853338633387333883338933390333913339233393333943339533396333973339833399334003340133402334033340433405334063340733408334093341033411334123341333414334153341633417334183341933420334213342233423334243342533426334273342833429334303343133432334333343433435334363343733438334393344033441334423344333444334453344633447334483344933450334513345233453334543345533456334573345833459334603346133462334633346433465334663346733468334693347033471334723347333474334753347633477334783347933480334813348233483334843348533486334873348833489334903349133492334933349433495334963349733498334993350033501335023350333504335053350633507335083350933510335113351233513335143351533516335173351833519335203352133522335233352433525335263352733528335293353033531335323353333534335353353633537335383353933540335413354233543335443354533546335473354833549335503355133552335533355433555335563355733558335593356033561335623356333564335653356633567335683356933570335713357233573335743357533576335773357833579335803358133582335833358433585335863358733588335893359033591335923359333594335953359633597335983359933600336013360233603336043360533606336073360833609336103361133612336133361433615336163361733618336193362033621336223362333624336253362633627336283362933630336313363233633336343363533636336373363833639336403364133642336433364433645336463364733648336493365033651336523365333654336553365633657336583365933660336613366233663336643366533666336673366833669336703367133672336733367433675336763367733678336793368033681336823368333684336853368633687336883368933690336913369233693336943369533696336973369833699337003370133702337033370433705337063370733708337093371033711337123371333714337153371633717337183371933720337213372233723337243372533726337273372833729337303373133732337333373433735337363373733738337393374033741337423374333744337453374633747337483374933750337513375233753337543375533756337573375833759337603376133762337633376433765337663376733768337693377033771337723377333774337753377633777337783377933780337813378233783337843378533786337873378833789337903379133792337933379433795337963379733798337993380033801338023380333804338053380633807338083380933810338113381233813338143381533816338173381833819338203382133822338233382433825338263382733828338293383033831338323383333834338353383633837338383383933840338413384233843338443384533846338473384833849338503385133852338533385433855338563385733858338593386033861338623386333864338653386633867338683386933870338713387233873338743387533876338773387833879338803388133882338833388433885338863388733888338893389033891338923389333894338953389633897338983389933900339013390233903339043390533906339073390833909339103391133912339133391433915339163391733918339193392033921339223392333924339253392633927339283392933930339313393233933339343393533936339373393833939339403394133942339433394433945339463394733948339493395033951339523395333954339553395633957339583395933960339613396233963339643396533966339673396833969339703397133972339733397433975339763397733978339793398033981339823398333984339853398633987339883398933990339913399233993339943399533996339973399833999340003400134002340033400434005340063400734008340093401034011340123401334014340153401634017340183401934020340213402234023340243402534026340273402834029340303403134032340333403434035340363403734038340393404034041340423404334044340453404634047340483404934050340513405234053340543405534056340573405834059340603406134062340633406434065340663406734068340693407034071340723407334074340753407634077340783407934080340813408234083340843408534086340873408834089340903409134092340933409434095340963409734098340993410034101341023410334104341053410634107341083410934110341113411234113341143411534116341173411834119341203412134122341233412434125341263412734128341293413034131341323413334134341353413634137341383413934140341413414234143341443414534146341473414834149341503415134152341533415434155341563415734158341593416034161341623416334164341653416634167341683416934170341713417234173341743417534176341773417834179341803418134182341833418434185341863418734188341893419034191341923419334194341953419634197341983419934200342013420234203342043420534206342073420834209342103421134212342133421434215342163421734218342193422034221342223422334224342253422634227342283422934230342313423234233342343423534236342373423834239342403424134242342433424434245342463424734248342493425034251342523425334254342553425634257342583425934260342613426234263342643426534266342673426834269342703427134272342733427434275342763427734278342793428034281342823428334284342853428634287342883428934290342913429234293342943429534296342973429834299343003430134302343033430434305343063430734308343093431034311343123431334314343153431634317343183431934320343213432234323343243432534326343273432834329343303433134332343333433434335343363433734338343393434034341343423434334344343453434634347343483434934350343513435234353343543435534356343573435834359343603436134362343633436434365343663436734368343693437034371343723437334374343753437634377343783437934380343813438234383343843438534386343873438834389343903439134392343933439434395343963439734398343993440034401344023440334404344053440634407344083440934410344113441234413344143441534416344173441834419344203442134422344233442434425344263442734428344293443034431344323443334434344353443634437344383443934440344413444234443344443444534446344473444834449344503445134452344533445434455344563445734458344593446034461344623446334464344653446634467344683446934470344713447234473344743447534476344773447834479344803448134482344833448434485344863448734488344893449034491344923449334494344953449634497344983449934500345013450234503345043450534506345073450834509345103451134512345133451434515345163451734518345193452034521345223452334524345253452634527345283452934530345313453234533345343453534536345373453834539345403454134542345433454434545345463454734548345493455034551345523455334554345553455634557345583455934560345613456234563345643456534566345673456834569345703457134572345733457434575345763457734578345793458034581345823458334584345853458634587345883458934590345913459234593345943459534596345973459834599346003460134602346033460434605346063460734608346093461034611346123461334614346153461634617346183461934620346213462234623346243462534626346273462834629346303463134632346333463434635346363463734638346393464034641346423464334644346453464634647346483464934650346513465234653346543465534656346573465834659346603466134662346633466434665346663466734668346693467034671346723467334674346753467634677346783467934680346813468234683346843468534686346873468834689346903469134692346933469434695346963469734698346993470034701347023470334704347053470634707347083470934710347113471234713347143471534716347173471834719347203472134722347233472434725347263472734728347293473034731347323473334734347353473634737347383473934740347413474234743347443474534746347473474834749347503475134752347533475434755347563475734758347593476034761347623476334764347653476634767347683476934770347713477234773347743477534776347773477834779347803478134782347833478434785347863478734788347893479034791347923479334794347953479634797347983479934800348013480234803348043480534806348073480834809348103481134812348133481434815348163481734818348193482034821348223482334824348253482634827348283482934830348313483234833348343483534836348373483834839348403484134842348433484434845348463484734848348493485034851348523485334854348553485634857348583485934860348613486234863348643486534866348673486834869348703487134872348733487434875348763487734878348793488034881348823488334884348853488634887348883488934890348913489234893348943489534896348973489834899349003490134902349033490434905349063490734908349093491034911349123491334914349153491634917349183491934920349213492234923349243492534926349273492834929349303493134932349333493434935349363493734938349393494034941349423494334944349453494634947349483494934950349513495234953349543495534956349573495834959349603496134962349633496434965349663496734968349693497034971349723497334974349753497634977349783497934980349813498234983349843498534986349873498834989349903499134992349933499434995349963499734998349993500035001350023500335004350053500635007350083500935010350113501235013350143501535016350173501835019350203502135022350233502435025350263502735028350293503035031350323503335034350353503635037350383503935040350413504235043350443504535046350473504835049350503505135052350533505435055350563505735058350593506035061350623506335064350653506635067350683506935070350713507235073350743507535076350773507835079350803508135082350833508435085350863508735088350893509035091350923509335094350953509635097350983509935100351013510235103351043510535106351073510835109351103511135112351133511435115351163511735118351193512035121351223512335124351253512635127351283512935130351313513235133351343513535136351373513835139351403514135142351433514435145351463514735148351493515035151351523515335154351553515635157351583515935160351613516235163351643516535166351673516835169351703517135172351733517435175351763517735178351793518035181351823518335184351853518635187351883518935190351913519235193351943519535196351973519835199352003520135202352033520435205352063520735208352093521035211352123521335214352153521635217352183521935220352213522235223352243522535226352273522835229352303523135232352333523435235352363523735238352393524035241352423524335244352453524635247352483524935250352513525235253352543525535256352573525835259352603526135262352633526435265352663526735268352693527035271352723527335274352753527635277352783527935280352813528235283352843528535286352873528835289352903529135292352933529435295352963529735298352993530035301353023530335304353053530635307353083530935310353113531235313353143531535316353173531835319353203532135322353233532435325353263532735328353293533035331353323533335334353353533635337353383533935340353413534235343353443534535346353473534835349353503535135352353533535435355353563535735358353593536035361353623536335364353653536635367353683536935370353713537235373353743537535376353773537835379353803538135382353833538435385353863538735388353893539035391353923539335394353953539635397353983539935400354013540235403354043540535406354073540835409354103541135412354133541435415354163541735418354193542035421354223542335424354253542635427354283542935430354313543235433354343543535436354373543835439354403544135442354433544435445354463544735448354493545035451354523545335454354553545635457354583545935460354613546235463354643546535466354673546835469354703547135472354733547435475354763547735478354793548035481354823548335484354853548635487354883548935490354913549235493354943549535496354973549835499355003550135502355033550435505355063550735508355093551035511355123551335514355153551635517355183551935520355213552235523355243552535526355273552835529355303553135532355333553435535355363553735538355393554035541355423554335544355453554635547355483554935550355513555235553355543555535556355573555835559355603556135562355633556435565355663556735568355693557035571355723557335574355753557635577355783557935580355813558235583355843558535586355873558835589355903559135592355933559435595355963559735598355993560035601356023560335604356053560635607356083560935610356113561235613356143561535616356173561835619356203562135622356233562435625356263562735628356293563035631356323563335634356353563635637356383563935640356413564235643356443564535646356473564835649356503565135652356533565435655356563565735658356593566035661356623566335664356653566635667356683566935670356713567235673356743567535676356773567835679356803568135682356833568435685356863568735688356893569035691356923569335694356953569635697356983569935700357013570235703357043570535706357073570835709357103571135712357133571435715357163571735718357193572035721357223572335724357253572635727357283572935730357313573235733357343573535736357373573835739357403574135742357433574435745357463574735748357493575035751357523575335754357553575635757357583575935760357613576235763357643576535766357673576835769357703577135772357733577435775357763577735778357793578035781357823578335784357853578635787357883578935790357913579235793357943579535796357973579835799358003580135802358033580435805358063580735808358093581035811358123581335814358153581635817358183581935820358213582235823358243582535826358273582835829358303583135832358333583435835358363583735838358393584035841358423584335844358453584635847358483584935850358513585235853358543585535856358573585835859358603586135862358633586435865358663586735868358693587035871358723587335874358753587635877358783587935880358813588235883358843588535886358873588835889358903589135892358933589435895358963589735898358993590035901359023590335904359053590635907359083590935910359113591235913359143591535916359173591835919359203592135922359233592435925359263592735928359293593035931359323593335934359353593635937359383593935940359413594235943359443594535946359473594835949359503595135952359533595435955359563595735958359593596035961359623596335964359653596635967359683596935970359713597235973359743597535976359773597835979359803598135982359833598435985359863598735988359893599035991359923599335994359953599635997359983599936000360013600236003360043600536006360073600836009360103601136012360133601436015360163601736018360193602036021360223602336024360253602636027360283602936030360313603236033360343603536036360373603836039360403604136042360433604436045360463604736048360493605036051360523605336054360553605636057360583605936060360613606236063360643606536066360673606836069360703607136072360733607436075360763607736078360793608036081360823608336084360853608636087360883608936090360913609236093360943609536096360973609836099361003610136102361033610436105361063610736108361093611036111361123611336114361153611636117361183611936120361213612236123361243612536126361273612836129361303613136132361333613436135361363613736138361393614036141361423614336144361453614636147361483614936150361513615236153361543615536156361573615836159361603616136162361633616436165361663616736168361693617036171361723617336174361753617636177361783617936180361813618236183361843618536186361873618836189361903619136192361933619436195361963619736198361993620036201362023620336204362053620636207362083620936210362113621236213362143621536216362173621836219362203622136222362233622436225362263622736228362293623036231362323623336234362353623636237362383623936240362413624236243362443624536246362473624836249362503625136252362533625436255362563625736258362593626036261362623626336264362653626636267362683626936270362713627236273362743627536276362773627836279362803628136282362833628436285362863628736288362893629036291362923629336294362953629636297362983629936300363013630236303363043630536306363073630836309363103631136312363133631436315363163631736318363193632036321363223632336324363253632636327363283632936330363313633236333363343633536336363373633836339363403634136342363433634436345363463634736348363493635036351363523635336354363553635636357363583635936360363613636236363363643636536366363673636836369363703637136372363733637436375363763637736378363793638036381363823638336384363853638636387363883638936390363913639236393363943639536396363973639836399364003640136402364033640436405364063640736408364093641036411364123641336414364153641636417364183641936420364213642236423364243642536426364273642836429364303643136432364333643436435364363643736438364393644036441364423644336444364453644636447364483644936450364513645236453364543645536456364573645836459364603646136462364633646436465364663646736468364693647036471364723647336474364753647636477364783647936480364813648236483364843648536486364873648836489364903649136492364933649436495364963649736498364993650036501365023650336504365053650636507365083650936510365113651236513365143651536516365173651836519365203652136522365233652436525365263652736528365293653036531365323653336534365353653636537365383653936540365413654236543365443654536546365473654836549365503655136552365533655436555365563655736558365593656036561365623656336564365653656636567365683656936570365713657236573365743657536576365773657836579365803658136582365833658436585365863658736588365893659036591365923659336594365953659636597365983659936600366013660236603366043660536606366073660836609366103661136612366133661436615366163661736618366193662036621366223662336624366253662636627366283662936630366313663236633366343663536636366373663836639366403664136642366433664436645366463664736648366493665036651366523665336654366553665636657366583665936660366613666236663366643666536666366673666836669366703667136672366733667436675366763667736678366793668036681366823668336684366853668636687366883668936690366913669236693366943669536696366973669836699367003670136702367033670436705367063670736708367093671036711367123671336714367153671636717367183671936720367213672236723367243672536726367273672836729367303673136732367333673436735367363673736738367393674036741367423674336744367453674636747367483674936750367513675236753367543675536756367573675836759367603676136762367633676436765367663676736768367693677036771367723677336774367753677636777367783677936780367813678236783367843678536786367873678836789367903679136792367933679436795367963679736798367993680036801368023680336804368053680636807368083680936810368113681236813368143681536816368173681836819368203682136822368233682436825368263682736828368293683036831368323683336834368353683636837368383683936840368413684236843368443684536846368473684836849368503685136852368533685436855368563685736858368593686036861368623686336864368653686636867368683686936870368713687236873368743687536876368773687836879368803688136882368833688436885368863688736888368893689036891368923689336894368953689636897368983689936900369013690236903369043690536906369073690836909369103691136912369133691436915369163691736918369193692036921369223692336924369253692636927369283692936930369313693236933369343693536936369373693836939369403694136942369433694436945369463694736948369493695036951369523695336954369553695636957369583695936960369613696236963369643696536966369673696836969369703697136972369733697436975369763697736978369793698036981369823698336984369853698636987369883698936990369913699236993369943699536996369973699836999370003700137002370033700437005370063700737008370093701037011370123701337014370153701637017370183701937020370213702237023370243702537026370273702837029370303703137032370333703437035370363703737038370393704037041370423704337044370453704637047370483704937050370513705237053370543705537056370573705837059370603706137062370633706437065370663706737068370693707037071370723707337074370753707637077370783707937080370813708237083370843708537086370873708837089370903709137092370933709437095370963709737098370993710037101371023710337104371053710637107371083710937110371113711237113371143711537116371173711837119371203712137122371233712437125371263712737128371293713037131371323713337134371353713637137371383713937140371413714237143371443714537146371473714837149371503715137152371533715437155371563715737158371593716037161371623716337164371653716637167371683716937170371713717237173371743717537176371773717837179371803718137182371833718437185371863718737188371893719037191371923719337194371953719637197371983719937200372013720237203372043720537206372073720837209372103721137212372133721437215372163721737218372193722037221372223722337224372253722637227372283722937230372313723237233372343723537236372373723837239372403724137242372433724437245372463724737248372493725037251372523725337254372553725637257372583725937260372613726237263372643726537266372673726837269372703727137272372733727437275372763727737278372793728037281372823728337284372853728637287372883728937290372913729237293372943729537296372973729837299373003730137302373033730437305373063730737308373093731037311373123731337314373153731637317373183731937320373213732237323373243732537326373273732837329373303733137332373333733437335373363733737338373393734037341373423734337344373453734637347373483734937350373513735237353373543735537356373573735837359373603736137362373633736437365373663736737368373693737037371373723737337374373753737637377373783737937380373813738237383373843738537386373873738837389373903739137392373933739437395373963739737398373993740037401374023740337404374053740637407374083740937410374113741237413374143741537416374173741837419374203742137422374233742437425374263742737428374293743037431374323743337434374353743637437374383743937440374413744237443374443744537446374473744837449374503745137452374533745437455374563745737458374593746037461374623746337464374653746637467374683746937470374713747237473374743747537476374773747837479374803748137482374833748437485374863748737488374893749037491374923749337494374953749637497374983749937500375013750237503375043750537506375073750837509375103751137512375133751437515375163751737518375193752037521375223752337524375253752637527375283752937530375313753237533375343753537536375373753837539375403754137542375433754437545375463754737548375493755037551375523755337554375553755637557375583755937560375613756237563375643756537566375673756837569375703757137572375733757437575375763757737578375793758037581375823758337584375853758637587375883758937590375913759237593375943759537596375973759837599376003760137602376033760437605376063760737608376093761037611376123761337614376153761637617376183761937620376213762237623376243762537626376273762837629376303763137632376333763437635376363763737638376393764037641376423764337644376453764637647376483764937650376513765237653376543765537656376573765837659376603766137662376633766437665376663766737668376693767037671376723767337674376753767637677376783767937680376813768237683376843768537686376873768837689376903769137692376933769437695376963769737698376993770037701377023770337704377053770637707377083770937710377113771237713377143771537716377173771837719377203772137722377233772437725377263772737728377293773037731377323773337734377353773637737377383773937740377413774237743377443774537746377473774837749377503775137752377533775437755377563775737758377593776037761377623776337764377653776637767377683776937770377713777237773377743777537776377773777837779377803778137782377833778437785377863778737788377893779037791377923779337794377953779637797377983779937800378013780237803378043780537806378073780837809378103781137812378133781437815378163781737818378193782037821378223782337824378253782637827378283782937830378313783237833378343783537836378373783837839378403784137842378433784437845378463784737848378493785037851378523785337854378553785637857378583785937860378613786237863378643786537866378673786837869378703787137872378733787437875378763787737878378793788037881378823788337884378853788637887378883788937890378913789237893378943789537896378973789837899379003790137902379033790437905379063790737908379093791037911379123791337914379153791637917379183791937920379213792237923379243792537926379273792837929379303793137932379333793437935379363793737938379393794037941379423794337944379453794637947379483794937950379513795237953379543795537956379573795837959379603796137962379633796437965379663796737968379693797037971379723797337974379753797637977379783797937980379813798237983379843798537986379873798837989379903799137992379933799437995379963799737998379993800038001380023800338004380053800638007380083800938010380113801238013380143801538016380173801838019380203802138022380233802438025380263802738028380293803038031380323803338034380353803638037380383803938040380413804238043380443804538046380473804838049380503805138052380533805438055380563805738058380593806038061380623806338064380653806638067380683806938070380713807238073380743807538076380773807838079380803808138082380833808438085380863808738088380893809038091380923809338094380953809638097380983809938100381013810238103381043810538106381073810838109381103811138112381133811438115381163811738118381193812038121381223812338124381253812638127381283812938130381313813238133381343813538136381373813838139381403814138142381433814438145381463814738148381493815038151381523815338154381553815638157381583815938160381613816238163381643816538166381673816838169381703817138172381733817438175381763817738178381793818038181381823818338184381853818638187381883818938190381913819238193381943819538196381973819838199382003820138202382033820438205382063820738208382093821038211382123821338214382153821638217382183821938220382213822238223382243822538226382273822838229382303823138232382333823438235382363823738238382393824038241382423824338244382453824638247382483824938250382513825238253382543825538256382573825838259382603826138262382633826438265382663826738268382693827038271382723827338274382753827638277382783827938280382813828238283382843828538286382873828838289382903829138292382933829438295382963829738298382993830038301383023830338304383053830638307383083830938310383113831238313383143831538316383173831838319383203832138322383233832438325383263832738328383293833038331383323833338334383353833638337383383833938340383413834238343383443834538346383473834838349383503835138352383533835438355383563835738358383593836038361383623836338364383653836638367383683836938370383713837238373383743837538376383773837838379383803838138382383833838438385383863838738388383893839038391383923839338394383953839638397383983839938400384013840238403384043840538406384073840838409384103841138412384133841438415384163841738418384193842038421384223842338424384253842638427384283842938430384313843238433384343843538436384373843838439384403844138442384433844438445384463844738448384493845038451384523845338454384553845638457384583845938460384613846238463384643846538466384673846838469384703847138472384733847438475384763847738478384793848038481384823848338484384853848638487384883848938490384913849238493384943849538496384973849838499385003850138502385033850438505385063850738508385093851038511385123851338514385153851638517385183851938520385213852238523385243852538526385273852838529385303853138532385333853438535385363853738538385393854038541385423854338544385453854638547385483854938550385513855238553385543855538556385573855838559385603856138562385633856438565385663856738568385693857038571385723857338574385753857638577385783857938580385813858238583385843858538586385873858838589385903859138592385933859438595385963859738598385993860038601386023860338604386053860638607386083860938610386113861238613386143861538616386173861838619386203862138622386233862438625386263862738628386293863038631386323863338634386353863638637386383863938640386413864238643386443864538646386473864838649386503865138652386533865438655386563865738658386593866038661386623866338664386653866638667386683866938670386713867238673386743867538676386773867838679386803868138682386833868438685386863868738688386893869038691386923869338694386953869638697386983869938700387013870238703387043870538706387073870838709387103871138712387133871438715387163871738718387193872038721387223872338724387253872638727387283872938730387313873238733387343873538736387373873838739387403874138742387433874438745387463874738748387493875038751387523875338754387553875638757387583875938760387613876238763387643876538766387673876838769387703877138772387733877438775387763877738778387793878038781387823878338784387853878638787387883878938790387913879238793387943879538796387973879838799388003880138802388033880438805388063880738808388093881038811388123881338814388153881638817388183881938820388213882238823388243882538826388273882838829388303883138832388333883438835388363883738838388393884038841388423884338844388453884638847388483884938850388513885238853388543885538856388573885838859388603886138862388633886438865388663886738868388693887038871388723887338874388753887638877388783887938880388813888238883388843888538886388873888838889388903889138892388933889438895388963889738898388993890038901389023890338904389053890638907389083890938910389113891238913389143891538916389173891838919389203892138922389233892438925389263892738928389293893038931389323893338934389353893638937389383893938940389413894238943389443894538946389473894838949389503895138952389533895438955389563895738958389593896038961389623896338964389653896638967389683896938970389713897238973389743897538976389773897838979389803898138982389833898438985389863898738988389893899038991389923899338994389953899638997389983899939000390013900239003390043900539006390073900839009390103901139012390133901439015390163901739018390193902039021390223902339024390253902639027390283902939030390313903239033390343903539036390373903839039390403904139042390433904439045390463904739048390493905039051390523905339054390553905639057390583905939060390613906239063390643906539066390673906839069390703907139072390733907439075390763907739078390793908039081390823908339084390853908639087390883908939090390913909239093390943909539096390973909839099391003910139102391033910439105391063910739108391093911039111391123911339114391153911639117391183911939120391213912239123391243912539126391273912839129391303913139132391333913439135391363913739138391393914039141391423914339144391453914639147391483914939150391513915239153391543915539156391573915839159391603916139162391633916439165391663916739168391693917039171391723917339174391753917639177391783917939180391813918239183391843918539186391873918839189391903919139192391933919439195391963919739198391993920039201392023920339204392053920639207392083920939210392113921239213392143921539216392173921839219392203922139222392233922439225392263922739228392293923039231392323923339234392353923639237392383923939240392413924239243392443924539246392473924839249392503925139252392533925439255392563925739258392593926039261392623926339264392653926639267392683926939270392713927239273392743927539276392773927839279392803928139282392833928439285392863928739288392893929039291392923929339294392953929639297392983929939300393013930239303393043930539306393073930839309393103931139312393133931439315393163931739318393193932039321393223932339324393253932639327393283932939330393313933239333393343933539336393373933839339393403934139342393433934439345393463934739348393493935039351393523935339354393553935639357393583935939360393613936239363393643936539366393673936839369393703937139372393733937439375393763937739378393793938039381393823938339384393853938639387393883938939390393913939239393393943939539396393973939839399394003940139402394033940439405394063940739408394093941039411394123941339414394153941639417394183941939420394213942239423394243942539426394273942839429394303943139432394333943439435394363943739438394393944039441394423944339444394453944639447394483944939450394513945239453394543945539456394573945839459394603946139462394633946439465394663946739468394693947039471394723947339474394753947639477394783947939480394813948239483394843948539486394873948839489394903949139492394933949439495394963949739498394993950039501395023950339504395053950639507395083950939510395113951239513395143951539516395173951839519395203952139522395233952439525395263952739528395293953039531395323953339534395353953639537395383953939540395413954239543395443954539546395473954839549395503955139552395533955439555395563955739558395593956039561395623956339564395653956639567395683956939570395713957239573395743957539576395773957839579395803958139582395833958439585395863958739588395893959039591395923959339594395953959639597395983959939600396013960239603396043960539606396073960839609396103961139612396133961439615396163961739618396193962039621396223962339624396253962639627396283962939630396313963239633396343963539636396373963839639396403964139642396433964439645396463964739648396493965039651396523965339654396553965639657396583965939660396613966239663396643966539666396673966839669396703967139672396733967439675396763967739678396793968039681396823968339684396853968639687396883968939690396913969239693396943969539696396973969839699397003970139702397033970439705397063970739708397093971039711397123971339714397153971639717397183971939720397213972239723397243972539726397273972839729397303973139732397333973439735397363973739738397393974039741397423974339744397453974639747397483974939750397513975239753397543975539756397573975839759397603976139762397633976439765397663976739768397693977039771397723977339774397753977639777397783977939780397813978239783397843978539786397873978839789397903979139792397933979439795397963979739798397993980039801398023980339804398053980639807398083980939810398113981239813398143981539816398173981839819398203982139822398233982439825398263982739828398293983039831398323983339834398353983639837398383983939840398413984239843398443984539846398473984839849398503985139852398533985439855398563985739858398593986039861398623986339864398653986639867398683986939870398713987239873398743987539876398773987839879398803988139882398833988439885398863988739888398893989039891398923989339894398953989639897398983989939900399013990239903399043990539906399073990839909399103991139912399133991439915399163991739918399193992039921399223992339924399253992639927399283992939930399313993239933399343993539936399373993839939399403994139942399433994439945399463994739948399493995039951399523995339954399553995639957399583995939960399613996239963399643996539966399673996839969399703997139972399733997439975399763997739978399793998039981399823998339984399853998639987399883998939990399913999239993399943999539996399973999839999400004000140002400034000440005400064000740008400094001040011400124001340014400154001640017400184001940020400214002240023400244002540026400274002840029400304003140032400334003440035400364003740038400394004040041400424004340044400454004640047400484004940050400514005240053400544005540056400574005840059400604006140062400634006440065400664006740068400694007040071400724007340074400754007640077400784007940080400814008240083400844008540086400874008840089400904009140092400934009440095400964009740098400994010040101401024010340104401054010640107401084010940110401114011240113401144011540116401174011840119401204012140122401234012440125401264012740128401294013040131401324013340134401354013640137401384013940140401414014240143401444014540146401474014840149401504015140152401534015440155401564015740158401594016040161401624016340164401654016640167401684016940170401714017240173401744017540176401774017840179401804018140182401834018440185401864018740188401894019040191401924019340194401954019640197401984019940200402014020240203402044020540206402074020840209402104021140212402134021440215402164021740218402194022040221402224022340224402254022640227402284022940230402314023240233402344023540236402374023840239402404024140242402434024440245402464024740248402494025040251402524025340254402554025640257402584025940260402614026240263402644026540266402674026840269402704027140272402734027440275402764027740278402794028040281402824028340284402854028640287402884028940290402914029240293402944029540296402974029840299403004030140302403034030440305403064030740308403094031040311403124031340314403154031640317403184031940320403214032240323403244032540326403274032840329403304033140332403334033440335403364033740338403394034040341403424034340344403454034640347403484034940350403514035240353403544035540356403574035840359403604036140362403634036440365403664036740368403694037040371403724037340374403754037640377403784037940380403814038240383403844038540386403874038840389403904039140392403934039440395403964039740398403994040040401404024040340404404054040640407404084040940410404114041240413404144041540416404174041840419404204042140422404234042440425404264042740428404294043040431404324043340434404354043640437404384043940440404414044240443404444044540446404474044840449404504045140452404534045440455404564045740458404594046040461404624046340464404654046640467404684046940470404714047240473404744047540476404774047840479404804048140482404834048440485404864048740488404894049040491404924049340494404954049640497404984049940500405014050240503405044050540506405074050840509405104051140512405134051440515405164051740518405194052040521405224052340524405254052640527405284052940530405314053240533405344053540536405374053840539405404054140542405434054440545405464054740548405494055040551405524055340554405554055640557405584055940560405614056240563405644056540566405674056840569405704057140572405734057440575405764057740578405794058040581405824058340584405854058640587405884058940590405914059240593405944059540596405974059840599406004060140602406034060440605406064060740608406094061040611406124061340614406154061640617406184061940620406214062240623406244062540626406274062840629406304063140632406334063440635406364063740638406394064040641406424064340644406454064640647406484064940650406514065240653406544065540656406574065840659406604066140662406634066440665406664066740668406694067040671406724067340674406754067640677406784067940680406814068240683406844068540686406874068840689406904069140692406934069440695406964069740698406994070040701407024070340704407054070640707407084070940710407114071240713407144071540716407174071840719407204072140722407234072440725407264072740728407294073040731407324073340734407354073640737407384073940740407414074240743407444074540746407474074840749407504075140752407534075440755407564075740758407594076040761407624076340764407654076640767407684076940770407714077240773407744077540776407774077840779407804078140782407834078440785407864078740788407894079040791407924079340794407954079640797407984079940800408014080240803408044080540806408074080840809408104081140812408134081440815408164081740818408194082040821408224082340824408254082640827408284082940830408314083240833408344083540836408374083840839408404084140842408434084440845408464084740848408494085040851408524085340854408554085640857408584085940860408614086240863408644086540866408674086840869408704087140872408734087440875408764087740878408794088040881408824088340884408854088640887408884088940890408914089240893408944089540896408974089840899409004090140902409034090440905409064090740908409094091040911409124091340914409154091640917409184091940920409214092240923409244092540926409274092840929409304093140932409334093440935409364093740938409394094040941409424094340944409454094640947409484094940950409514095240953409544095540956409574095840959409604096140962409634096440965409664096740968409694097040971409724097340974409754097640977409784097940980409814098240983409844098540986409874098840989409904099140992409934099440995409964099740998409994100041001410024100341004410054100641007410084100941010410114101241013410144101541016410174101841019410204102141022410234102441025410264102741028410294103041031410324103341034410354103641037410384103941040410414104241043410444104541046410474104841049410504105141052410534105441055410564105741058410594106041061410624106341064410654106641067410684106941070410714107241073410744107541076410774107841079410804108141082410834108441085410864108741088410894109041091410924109341094410954109641097410984109941100411014110241103411044110541106411074110841109411104111141112411134111441115411164111741118411194112041121411224112341124411254112641127411284112941130411314113241133411344113541136411374113841139411404114141142411434114441145411464114741148411494115041151411524115341154411554115641157411584115941160411614116241163411644116541166411674116841169411704117141172411734117441175411764117741178411794118041181411824118341184411854118641187411884118941190411914119241193411944119541196411974119841199412004120141202412034120441205412064120741208412094121041211412124121341214412154121641217412184121941220412214122241223412244122541226412274122841229412304123141232412334123441235412364123741238412394124041241412424124341244412454124641247412484124941250412514125241253412544125541256412574125841259412604126141262412634126441265412664126741268412694127041271412724127341274412754127641277412784127941280412814128241283412844128541286412874128841289412904129141292412934129441295412964129741298412994130041301413024130341304413054130641307413084130941310413114131241313413144131541316413174131841319413204132141322413234132441325413264132741328413294133041331413324133341334413354133641337413384133941340413414134241343413444134541346413474134841349413504135141352413534135441355413564135741358413594136041361413624136341364413654136641367413684136941370413714137241373413744137541376413774137841379413804138141382413834138441385413864138741388413894139041391413924139341394413954139641397413984139941400414014140241403414044140541406414074140841409414104141141412414134141441415414164141741418414194142041421414224142341424414254142641427414284142941430414314143241433414344143541436414374143841439414404144141442414434144441445414464144741448414494145041451414524145341454414554145641457414584145941460414614146241463414644146541466414674146841469414704147141472414734147441475414764147741478414794148041481414824148341484414854148641487414884148941490414914149241493414944149541496414974149841499415004150141502415034150441505415064150741508415094151041511415124151341514415154151641517415184151941520415214152241523415244152541526415274152841529415304153141532415334153441535415364153741538415394154041541415424154341544415454154641547415484154941550415514155241553415544155541556415574155841559415604156141562415634156441565415664156741568415694157041571415724157341574415754157641577415784157941580415814158241583415844158541586415874158841589415904159141592415934159441595415964159741598415994160041601416024160341604416054160641607416084160941610416114161241613416144161541616416174161841619416204162141622416234162441625416264162741628416294163041631416324163341634416354163641637416384163941640416414164241643416444164541646416474164841649416504165141652416534165441655416564165741658416594166041661416624166341664416654166641667416684166941670416714167241673416744167541676416774167841679416804168141682416834168441685416864168741688416894169041691416924169341694416954169641697416984169941700417014170241703417044170541706417074170841709417104171141712417134171441715417164171741718417194172041721417224172341724417254172641727417284172941730417314173241733417344173541736417374173841739417404174141742417434174441745417464174741748417494175041751417524175341754417554175641757417584175941760417614176241763417644176541766417674176841769417704177141772417734177441775417764177741778417794178041781417824178341784417854178641787417884178941790417914179241793417944179541796417974179841799418004180141802418034180441805418064180741808418094181041811418124181341814418154181641817418184181941820418214182241823418244182541826418274182841829418304183141832418334183441835418364183741838418394184041841418424184341844418454184641847418484184941850418514185241853418544185541856418574185841859418604186141862418634186441865418664186741868418694187041871418724187341874418754187641877418784187941880418814188241883418844188541886418874188841889418904189141892418934189441895418964189741898418994190041901419024190341904419054190641907419084190941910419114191241913419144191541916419174191841919419204192141922419234192441925419264192741928419294193041931419324193341934419354193641937419384193941940419414194241943419444194541946419474194841949419504195141952419534195441955419564195741958419594196041961419624196341964419654196641967419684196941970419714197241973419744197541976419774197841979419804198141982419834198441985419864198741988419894199041991419924199341994419954199641997419984199942000420014200242003420044200542006420074200842009420104201142012420134201442015420164201742018420194202042021420224202342024420254202642027420284202942030420314203242033420344203542036420374203842039420404204142042420434204442045420464204742048420494205042051420524205342054420554205642057420584205942060420614206242063420644206542066420674206842069420704207142072420734207442075420764207742078420794208042081420824208342084420854208642087420884208942090420914209242093420944209542096420974209842099421004210142102421034210442105421064210742108421094211042111421124211342114421154211642117421184211942120421214212242123421244212542126421274212842129421304213142132421334213442135421364213742138421394214042141421424214342144421454214642147421484214942150421514215242153421544215542156421574215842159421604216142162421634216442165421664216742168421694217042171421724217342174421754217642177421784217942180421814218242183421844218542186421874218842189421904219142192421934219442195421964219742198421994220042201422024220342204422054220642207422084220942210422114221242213422144221542216422174221842219422204222142222422234222442225422264222742228422294223042231422324223342234422354223642237422384223942240422414224242243422444224542246422474224842249422504225142252422534225442255422564225742258422594226042261422624226342264422654226642267422684226942270422714227242273422744227542276422774227842279422804228142282422834228442285422864228742288422894229042291422924229342294422954229642297422984229942300423014230242303423044230542306423074230842309423104231142312423134231442315423164231742318423194232042321423224232342324423254232642327423284232942330423314233242333423344233542336423374233842339423404234142342423434234442345423464234742348423494235042351423524235342354423554235642357423584235942360423614236242363423644236542366423674236842369423704237142372423734237442375423764237742378423794238042381423824238342384423854238642387423884238942390423914239242393423944239542396423974239842399424004240142402424034240442405424064240742408424094241042411424124241342414424154241642417424184241942420424214242242423424244242542426424274242842429424304243142432424334243442435424364243742438424394244042441424424244342444424454244642447424484244942450424514245242453424544245542456424574245842459424604246142462424634246442465424664246742468424694247042471424724247342474424754247642477424784247942480424814248242483424844248542486424874248842489424904249142492424934249442495424964249742498424994250042501425024250342504425054250642507425084250942510425114251242513425144251542516425174251842519425204252142522425234252442525425264252742528425294253042531425324253342534425354253642537425384253942540425414254242543425444254542546425474254842549425504255142552425534255442555425564255742558425594256042561425624256342564425654256642567425684256942570425714257242573425744257542576425774257842579425804258142582425834258442585425864258742588425894259042591425924259342594425954259642597425984259942600426014260242603426044260542606426074260842609426104261142612426134261442615426164261742618426194262042621426224262342624426254262642627426284262942630426314263242633426344263542636426374263842639426404264142642426434264442645426464264742648426494265042651426524265342654426554265642657426584265942660426614266242663426644266542666426674266842669426704267142672426734267442675426764267742678426794268042681426824268342684426854268642687426884268942690426914269242693426944269542696426974269842699427004270142702427034270442705427064270742708427094271042711427124271342714427154271642717427184271942720427214272242723427244272542726427274272842729427304273142732427334273442735427364273742738427394274042741427424274342744427454274642747427484274942750427514275242753427544275542756427574275842759427604276142762427634276442765427664276742768427694277042771427724277342774427754277642777427784277942780427814278242783427844278542786427874278842789427904279142792427934279442795427964279742798427994280042801428024280342804428054280642807428084280942810428114281242813428144281542816428174281842819428204282142822428234282442825428264282742828428294283042831428324283342834428354283642837428384283942840428414284242843428444284542846428474284842849428504285142852428534285442855428564285742858428594286042861428624286342864428654286642867428684286942870428714287242873428744287542876428774287842879428804288142882428834288442885428864288742888428894289042891428924289342894428954289642897428984289942900429014290242903429044290542906429074290842909429104291142912429134291442915429164291742918429194292042921429224292342924429254292642927429284292942930429314293242933429344293542936429374293842939429404294142942429434294442945429464294742948429494295042951429524295342954429554295642957429584295942960429614296242963429644296542966429674296842969429704297142972429734297442975429764297742978429794298042981429824298342984429854298642987429884298942990429914299242993429944299542996429974299842999430004300143002430034300443005430064300743008430094301043011430124301343014430154301643017430184301943020430214302243023430244302543026430274302843029430304303143032430334303443035430364303743038430394304043041430424304343044430454304643047430484304943050430514305243053430544305543056430574305843059430604306143062430634306443065430664306743068430694307043071430724307343074430754307643077430784307943080430814308243083430844308543086430874308843089430904309143092430934309443095430964309743098430994310043101431024310343104431054310643107431084310943110431114311243113431144311543116431174311843119431204312143122431234312443125431264312743128431294313043131431324313343134431354313643137431384313943140431414314243143431444314543146431474314843149431504315143152431534315443155431564315743158431594316043161431624316343164431654316643167431684316943170431714317243173431744317543176431774317843179431804318143182431834318443185431864318743188431894319043191431924319343194431954319643197431984319943200432014320243203432044320543206432074320843209432104321143212432134321443215432164321743218432194322043221432224322343224432254322643227432284322943230432314323243233432344323543236432374323843239432404324143242432434324443245432464324743248432494325043251432524325343254432554325643257432584325943260432614326243263432644326543266432674326843269432704327143272432734327443275432764327743278432794328043281432824328343284432854328643287432884328943290432914329243293432944329543296432974329843299433004330143302433034330443305433064330743308433094331043311433124331343314433154331643317433184331943320433214332243323433244332543326433274332843329433304333143332433334333443335433364333743338433394334043341433424334343344433454334643347433484334943350433514335243353433544335543356433574335843359433604336143362433634336443365433664336743368433694337043371433724337343374433754337643377433784337943380433814338243383433844338543386433874338843389433904339143392433934339443395433964339743398433994340043401434024340343404434054340643407434084340943410434114341243413434144341543416434174341843419434204342143422434234342443425434264342743428434294343043431434324343343434434354343643437434384343943440434414344243443434444344543446434474344843449434504345143452434534345443455434564345743458434594346043461434624346343464434654346643467434684346943470434714347243473434744347543476434774347843479434804348143482434834348443485434864348743488434894349043491434924349343494434954349643497434984349943500435014350243503435044350543506435074350843509435104351143512435134351443515435164351743518435194352043521435224352343524435254352643527435284352943530435314353243533435344353543536435374353843539435404354143542435434354443545435464354743548435494355043551435524355343554435554355643557435584355943560435614356243563435644356543566435674356843569435704357143572435734357443575435764357743578435794358043581435824358343584435854358643587435884358943590435914359243593435944359543596435974359843599436004360143602436034360443605436064360743608436094361043611436124361343614436154361643617436184361943620436214362243623436244362543626436274362843629436304363143632436334363443635436364363743638436394364043641436424364343644436454364643647436484364943650436514365243653436544365543656436574365843659436604366143662436634366443665436664366743668436694367043671436724367343674436754367643677436784367943680436814368243683436844368543686436874368843689436904369143692436934369443695436964369743698436994370043701437024370343704437054370643707437084370943710437114371243713437144371543716437174371843719437204372143722437234372443725437264372743728437294373043731437324373343734437354373643737437384373943740437414374243743437444374543746437474374843749437504375143752437534375443755437564375743758437594376043761437624376343764437654376643767437684376943770437714377243773437744377543776437774377843779437804378143782437834378443785437864378743788437894379043791437924379343794437954379643797437984379943800438014380243803438044380543806438074380843809438104381143812438134381443815438164381743818438194382043821438224382343824438254382643827438284382943830438314383243833438344383543836438374383843839438404384143842438434384443845438464384743848438494385043851438524385343854438554385643857438584385943860438614386243863438644386543866438674386843869438704387143872438734387443875438764387743878438794388043881438824388343884438854388643887438884388943890438914389243893438944389543896438974389843899439004390143902439034390443905439064390743908439094391043911439124391343914439154391643917439184391943920439214392243923439244392543926439274392843929439304393143932439334393443935439364393743938439394394043941439424394343944439454394643947439484394943950439514395243953439544395543956439574395843959439604396143962439634396443965439664396743968439694397043971439724397343974439754397643977439784397943980439814398243983439844398543986439874398843989439904399143992439934399443995439964399743998439994400044001440024400344004440054400644007440084400944010440114401244013440144401544016440174401844019440204402144022440234402444025440264402744028440294403044031440324403344034440354403644037440384403944040440414404244043440444404544046440474404844049440504405144052440534405444055440564405744058440594406044061440624406344064440654406644067440684406944070440714407244073440744407544076440774407844079440804408144082440834408444085440864408744088440894409044091440924409344094440954409644097440984409944100441014410244103441044410544106441074410844109441104411144112441134411444115441164411744118441194412044121441224412344124441254412644127441284412944130441314413244133441344413544136441374413844139441404414144142441434414444145441464414744148441494415044151441524415344154441554415644157441584415944160441614416244163441644416544166441674416844169441704417144172441734417444175441764417744178441794418044181441824418344184441854418644187441884418944190441914419244193441944419544196441974419844199442004420144202442034420444205442064420744208442094421044211442124421344214442154421644217442184421944220442214422244223442244422544226442274422844229442304423144232442334423444235442364423744238442394424044241442424424344244442454424644247442484424944250442514425244253442544425544256442574425844259442604426144262442634426444265442664426744268442694427044271442724427344274442754427644277442784427944280442814428244283442844428544286442874428844289442904429144292442934429444295442964429744298442994430044301443024430344304443054430644307443084430944310443114431244313443144431544316443174431844319443204432144322443234432444325443264432744328443294433044331443324433344334443354433644337443384433944340443414434244343443444434544346443474434844349443504435144352443534435444355443564435744358443594436044361443624436344364443654436644367443684436944370443714437244373443744437544376443774437844379443804438144382443834438444385443864438744388443894439044391443924439344394443954439644397443984439944400444014440244403444044440544406444074440844409444104441144412444134441444415444164441744418444194442044421444224442344424444254442644427444284442944430444314443244433444344443544436444374443844439444404444144442444434444444445444464444744448444494445044451444524445344454444554445644457444584445944460444614446244463444644446544466444674446844469444704447144472444734447444475444764447744478444794448044481444824448344484444854448644487444884448944490444914449244493444944449544496444974449844499445004450144502445034450444505445064450744508445094451044511445124451344514445154451644517445184451944520445214452244523445244452544526445274452844529445304453144532445334453444535445364453744538445394454044541445424454344544445454454644547445484454944550445514455244553445544455544556445574455844559445604456144562445634456444565445664456744568445694457044571445724457344574445754457644577445784457944580445814458244583445844458544586445874458844589445904459144592445934459444595445964459744598445994460044601446024460344604446054460644607446084460944610446114461244613446144461544616446174461844619446204462144622446234462444625446264462744628446294463044631446324463344634446354463644637446384463944640446414464244643446444464544646446474464844649446504465144652446534465444655446564465744658446594466044661446624466344664446654466644667446684466944670446714467244673446744467544676446774467844679446804468144682446834468444685446864468744688446894469044691446924469344694446954469644697446984469944700447014470244703447044470544706447074470844709447104471144712447134471444715447164471744718447194472044721447224472344724447254472644727447284472944730447314473244733447344473544736447374473844739447404474144742447434474444745447464474744748447494475044751447524475344754447554475644757447584475944760447614476244763447644476544766447674476844769447704477144772447734477444775447764477744778447794478044781447824478344784447854478644787447884478944790447914479244793447944479544796447974479844799448004480144802448034480444805448064480744808448094481044811448124481344814448154481644817448184481944820448214482244823448244482544826448274482844829448304483144832448334483444835448364483744838448394484044841448424484344844448454484644847448484484944850448514485244853448544485544856448574485844859448604486144862448634486444865448664486744868448694487044871448724487344874448754487644877448784487944880448814488244883448844488544886448874488844889448904489144892448934489444895448964489744898448994490044901449024490344904449054490644907449084490944910449114491244913449144491544916449174491844919449204492144922449234492444925449264492744928449294493044931449324493344934449354493644937449384493944940449414494244943449444494544946449474494844949449504495144952449534495444955449564495744958449594496044961449624496344964449654496644967449684496944970449714497244973449744497544976449774497844979449804498144982449834498444985449864498744988449894499044991449924499344994449954499644997449984499945000450014500245003450044500545006450074500845009450104501145012450134501445015450164501745018450194502045021450224502345024450254502645027450284502945030450314503245033450344503545036450374503845039450404504145042450434504445045450464504745048450494505045051450524505345054450554505645057450584505945060450614506245063450644506545066450674506845069450704507145072450734507445075450764507745078450794508045081450824508345084450854508645087450884508945090450914509245093450944509545096450974509845099451004510145102451034510445105451064510745108451094511045111451124511345114451154511645117451184511945120451214512245123451244512545126451274512845129451304513145132451334513445135451364513745138451394514045141451424514345144451454514645147451484514945150451514515245153451544515545156451574515845159451604516145162451634516445165451664516745168451694517045171451724517345174451754517645177451784517945180451814518245183451844518545186451874518845189451904519145192451934519445195451964519745198451994520045201452024520345204452054520645207452084520945210452114521245213452144521545216452174521845219452204522145222452234522445225452264522745228452294523045231452324523345234452354523645237452384523945240452414524245243452444524545246452474524845249452504525145252452534525445255452564525745258452594526045261452624526345264452654526645267452684526945270452714527245273452744527545276452774527845279452804528145282452834528445285452864528745288452894529045291452924529345294452954529645297452984529945300453014530245303453044530545306453074530845309453104531145312453134531445315453164531745318453194532045321453224532345324453254532645327453284532945330453314533245333453344533545336453374533845339453404534145342453434534445345453464534745348453494535045351453524535345354453554535645357453584535945360453614536245363453644536545366453674536845369453704537145372453734537445375453764537745378453794538045381453824538345384453854538645387453884538945390453914539245393453944539545396453974539845399454004540145402454034540445405454064540745408454094541045411454124541345414454154541645417454184541945420454214542245423454244542545426454274542845429454304543145432454334543445435454364543745438454394544045441454424544345444454454544645447454484544945450454514545245453454544545545456454574545845459454604546145462454634546445465454664546745468454694547045471454724547345474454754547645477454784547945480454814548245483454844548545486454874548845489454904549145492454934549445495454964549745498454994550045501455024550345504455054550645507455084550945510455114551245513455144551545516455174551845519455204552145522455234552445525455264552745528455294553045531455324553345534455354553645537455384553945540455414554245543455444554545546455474554845549455504555145552455534555445555455564555745558455594556045561455624556345564455654556645567455684556945570455714557245573455744557545576455774557845579455804558145582455834558445585455864558745588455894559045591455924559345594455954559645597455984559945600456014560245603456044560545606456074560845609456104561145612456134561445615456164561745618456194562045621456224562345624456254562645627456284562945630456314563245633456344563545636456374563845639456404564145642456434564445645456464564745648456494565045651456524565345654456554565645657456584565945660456614566245663456644566545666456674566845669456704567145672456734567445675456764567745678456794568045681456824568345684456854568645687456884568945690456914569245693456944569545696456974569845699457004570145702457034570445705457064570745708457094571045711457124571345714457154571645717457184571945720457214572245723457244572545726457274572845729457304573145732457334573445735457364573745738457394574045741457424574345744457454574645747457484574945750457514575245753457544575545756457574575845759457604576145762457634576445765457664576745768457694577045771457724577345774457754577645777457784577945780457814578245783457844578545786457874578845789457904579145792457934579445795457964579745798457994580045801458024580345804458054580645807458084580945810458114581245813458144581545816458174581845819458204582145822458234582445825458264582745828458294583045831458324583345834458354583645837458384583945840458414584245843458444584545846458474584845849458504585145852458534585445855458564585745858458594586045861458624586345864458654586645867458684586945870458714587245873458744587545876458774587845879458804588145882458834588445885458864588745888458894589045891458924589345894458954589645897458984589945900459014590245903459044590545906459074590845909459104591145912459134591445915459164591745918459194592045921459224592345924459254592645927459284592945930459314593245933459344593545936459374593845939459404594145942459434594445945459464594745948459494595045951459524595345954459554595645957459584595945960459614596245963459644596545966459674596845969459704597145972459734597445975459764597745978459794598045981459824598345984459854598645987459884598945990459914599245993459944599545996459974599845999460004600146002460034600446005460064600746008460094601046011460124601346014460154601646017460184601946020460214602246023460244602546026460274602846029460304603146032460334603446035460364603746038460394604046041460424604346044460454604646047460484604946050460514605246053460544605546056460574605846059460604606146062460634606446065460664606746068460694607046071460724607346074460754607646077460784607946080460814608246083460844608546086460874608846089460904609146092460934609446095460964609746098460994610046101461024610346104461054610646107461084610946110461114611246113461144611546116461174611846119461204612146122461234612446125461264612746128461294613046131461324613346134461354613646137461384613946140461414614246143461444614546146461474614846149461504615146152461534615446155461564615746158461594616046161461624616346164461654616646167461684616946170461714617246173461744617546176461774617846179461804618146182461834618446185461864618746188461894619046191461924619346194461954619646197461984619946200462014620246203462044620546206462074620846209462104621146212462134621446215462164621746218462194622046221462224622346224462254622646227462284622946230462314623246233462344623546236462374623846239462404624146242462434624446245462464624746248462494625046251462524625346254462554625646257462584625946260462614626246263462644626546266462674626846269462704627146272462734627446275462764627746278462794628046281462824628346284462854628646287462884628946290462914629246293462944629546296462974629846299463004630146302463034630446305463064630746308463094631046311463124631346314463154631646317463184631946320463214632246323463244632546326463274632846329463304633146332463334633446335463364633746338463394634046341463424634346344463454634646347463484634946350463514635246353463544635546356463574635846359463604636146362463634636446365463664636746368463694637046371463724637346374463754637646377463784637946380463814638246383463844638546386463874638846389463904639146392463934639446395463964639746398463994640046401464024640346404464054640646407464084640946410464114641246413464144641546416464174641846419464204642146422464234642446425464264642746428464294643046431464324643346434464354643646437464384643946440464414644246443464444644546446464474644846449464504645146452464534645446455464564645746458464594646046461464624646346464464654646646467464684646946470464714647246473464744647546476464774647846479464804648146482464834648446485464864648746488464894649046491464924649346494464954649646497464984649946500465014650246503465044650546506465074650846509465104651146512465134651446515465164651746518465194652046521465224652346524465254652646527465284652946530465314653246533465344653546536465374653846539465404654146542465434654446545465464654746548465494655046551465524655346554465554655646557465584655946560465614656246563465644656546566465674656846569465704657146572465734657446575465764657746578465794658046581465824658346584465854658646587465884658946590465914659246593465944659546596465974659846599466004660146602466034660446605466064660746608466094661046611466124661346614466154661646617466184661946620466214662246623466244662546626466274662846629466304663146632466334663446635466364663746638466394664046641466424664346644466454664646647466484664946650466514665246653466544665546656466574665846659466604666146662466634666446665466664666746668466694667046671466724667346674466754667646677466784667946680466814668246683466844668546686466874668846689466904669146692466934669446695466964669746698466994670046701467024670346704467054670646707467084670946710467114671246713467144671546716467174671846719467204672146722467234672446725467264672746728467294673046731467324673346734467354673646737467384673946740467414674246743467444674546746467474674846749467504675146752467534675446755467564675746758467594676046761467624676346764467654676646767467684676946770467714677246773467744677546776467774677846779467804678146782467834678446785467864678746788467894679046791467924679346794467954679646797467984679946800468014680246803468044680546806468074680846809468104681146812468134681446815468164681746818468194682046821468224682346824468254682646827468284682946830468314683246833468344683546836468374683846839468404684146842468434684446845468464684746848468494685046851468524685346854468554685646857468584685946860468614686246863468644686546866468674686846869468704687146872468734687446875468764687746878468794688046881468824688346884468854688646887468884688946890468914689246893468944689546896468974689846899469004690146902469034690446905469064690746908469094691046911469124691346914469154691646917469184691946920469214692246923469244692546926469274692846929469304693146932469334693446935469364693746938469394694046941469424694346944469454694646947469484694946950469514695246953469544695546956469574695846959469604696146962469634696446965469664696746968469694697046971469724697346974469754697646977469784697946980469814698246983469844698546986469874698846989469904699146992469934699446995469964699746998469994700047001470024700347004470054700647007470084700947010470114701247013470144701547016470174701847019470204702147022470234702447025470264702747028470294703047031470324703347034470354703647037470384703947040470414704247043470444704547046470474704847049470504705147052470534705447055470564705747058470594706047061470624706347064470654706647067470684706947070470714707247073470744707547076470774707847079470804708147082470834708447085470864708747088470894709047091470924709347094470954709647097470984709947100471014710247103471044710547106471074710847109471104711147112471134711447115471164711747118471194712047121471224712347124471254712647127471284712947130471314713247133471344713547136471374713847139471404714147142471434714447145471464714747148471494715047151471524715347154471554715647157471584715947160471614716247163471644716547166471674716847169471704717147172471734717447175471764717747178471794718047181471824718347184471854718647187471884718947190471914719247193471944719547196471974719847199472004720147202472034720447205472064720747208472094721047211472124721347214472154721647217472184721947220472214722247223472244722547226472274722847229472304723147232472334723447235472364723747238472394724047241472424724347244472454724647247472484724947250472514725247253472544725547256472574725847259472604726147262472634726447265472664726747268472694727047271472724727347274472754727647277472784727947280472814728247283472844728547286472874728847289472904729147292472934729447295472964729747298472994730047301473024730347304473054730647307473084730947310473114731247313473144731547316473174731847319473204732147322473234732447325473264732747328473294733047331473324733347334473354733647337473384733947340473414734247343473444734547346473474734847349473504735147352473534735447355473564735747358473594736047361473624736347364473654736647367473684736947370473714737247373473744737547376473774737847379473804738147382473834738447385473864738747388473894739047391473924739347394473954739647397473984739947400474014740247403474044740547406474074740847409474104741147412474134741447415474164741747418474194742047421474224742347424474254742647427474284742947430474314743247433474344743547436474374743847439474404744147442474434744447445474464744747448474494745047451474524745347454474554745647457474584745947460474614746247463474644746547466474674746847469474704747147472474734747447475474764747747478474794748047481474824748347484474854748647487474884748947490474914749247493474944749547496474974749847499475004750147502475034750447505475064750747508475094751047511475124751347514475154751647517475184751947520475214752247523475244752547526475274752847529475304753147532475334753447535475364753747538475394754047541475424754347544475454754647547475484754947550475514755247553475544755547556475574755847559475604756147562475634756447565475664756747568475694757047571475724757347574475754757647577475784757947580475814758247583475844758547586475874758847589475904759147592475934759447595475964759747598475994760047601476024760347604476054760647607476084760947610476114761247613476144761547616476174761847619476204762147622476234762447625476264762747628476294763047631476324763347634476354763647637476384763947640476414764247643476444764547646476474764847649476504765147652476534765447655476564765747658476594766047661476624766347664476654766647667476684766947670476714767247673476744767547676476774767847679476804768147682476834768447685476864768747688476894769047691476924769347694476954769647697476984769947700477014770247703477044770547706477074770847709477104771147712477134771447715477164771747718477194772047721477224772347724477254772647727477284772947730477314773247733477344773547736477374773847739477404774147742477434774447745477464774747748477494775047751477524775347754477554775647757477584775947760477614776247763477644776547766477674776847769477704777147772477734777447775477764777747778477794778047781477824778347784477854778647787477884778947790477914779247793477944779547796477974779847799478004780147802478034780447805478064780747808478094781047811478124781347814478154781647817478184781947820478214782247823478244782547826478274782847829478304783147832478334783447835478364783747838478394784047841478424784347844478454784647847478484784947850478514785247853478544785547856478574785847859478604786147862478634786447865478664786747868478694787047871478724787347874478754787647877478784787947880478814788247883478844788547886478874788847889478904789147892478934789447895478964789747898478994790047901479024790347904479054790647907479084790947910479114791247913479144791547916479174791847919479204792147922479234792447925479264792747928479294793047931479324793347934479354793647937479384793947940479414794247943479444794547946479474794847949479504795147952479534795447955479564795747958479594796047961479624796347964479654796647967479684796947970479714797247973479744797547976479774797847979479804798147982479834798447985479864798747988479894799047991479924799347994479954799647997479984799948000480014800248003480044800548006480074800848009480104801148012480134801448015480164801748018480194802048021480224802348024480254802648027480284802948030480314803248033480344803548036480374803848039480404804148042480434804448045480464804748048480494805048051480524805348054480554805648057480584805948060480614806248063480644806548066480674806848069480704807148072480734807448075480764807748078480794808048081480824808348084480854808648087480884808948090480914809248093480944809548096480974809848099481004810148102481034810448105481064810748108481094811048111481124811348114481154811648117481184811948120481214812248123481244812548126481274812848129481304813148132481334813448135481364813748138481394814048141481424814348144481454814648147481484814948150481514815248153481544815548156481574815848159481604816148162481634816448165481664816748168481694817048171481724817348174481754817648177481784817948180481814818248183481844818548186481874818848189481904819148192481934819448195481964819748198481994820048201482024820348204482054820648207482084820948210482114821248213482144821548216482174821848219482204822148222482234822448225482264822748228482294823048231482324823348234482354823648237482384823948240482414824248243482444824548246482474824848249482504825148252482534825448255482564825748258482594826048261482624826348264482654826648267482684826948270482714827248273482744827548276482774827848279482804828148282482834828448285482864828748288482894829048291482924829348294482954829648297482984829948300483014830248303483044830548306483074830848309483104831148312483134831448315483164831748318483194832048321483224832348324483254832648327483284832948330483314833248333483344833548336483374833848339483404834148342483434834448345483464834748348483494835048351483524835348354483554835648357483584835948360483614836248363483644836548366483674836848369483704837148372483734837448375483764837748378483794838048381483824838348384483854838648387483884838948390483914839248393483944839548396483974839848399484004840148402484034840448405484064840748408484094841048411484124841348414484154841648417484184841948420484214842248423484244842548426484274842848429484304843148432484334843448435484364843748438484394844048441484424844348444484454844648447484484844948450484514845248453484544845548456484574845848459484604846148462484634846448465484664846748468484694847048471484724847348474484754847648477484784847948480484814848248483484844848548486484874848848489484904849148492484934849448495484964849748498484994850048501485024850348504485054850648507485084850948510485114851248513485144851548516485174851848519485204852148522485234852448525485264852748528485294853048531485324853348534485354853648537485384853948540485414854248543485444854548546485474854848549485504855148552485534855448555485564855748558485594856048561485624856348564485654856648567485684856948570485714857248573485744857548576485774857848579485804858148582485834858448585485864858748588485894859048591485924859348594485954859648597485984859948600486014860248603486044860548606486074860848609486104861148612486134861448615486164861748618486194862048621486224862348624486254862648627486284862948630486314863248633486344863548636486374863848639486404864148642486434864448645486464864748648486494865048651486524865348654486554865648657486584865948660486614866248663486644866548666486674866848669486704867148672486734867448675486764867748678486794868048681486824868348684486854868648687486884868948690486914869248693486944869548696486974869848699487004870148702487034870448705487064870748708487094871048711487124871348714487154871648717487184871948720487214872248723487244872548726487274872848729487304873148732487334873448735487364873748738487394874048741487424874348744487454874648747487484874948750487514875248753487544875548756487574875848759487604876148762487634876448765487664876748768487694877048771487724877348774487754877648777487784877948780487814878248783487844878548786487874878848789487904879148792487934879448795487964879748798487994880048801488024880348804488054880648807488084880948810488114881248813488144881548816488174881848819488204882148822488234882448825488264882748828488294883048831488324883348834488354883648837488384883948840488414884248843488444884548846488474884848849488504885148852488534885448855488564885748858488594886048861488624886348864488654886648867488684886948870488714887248873488744887548876488774887848879488804888148882488834888448885488864888748888488894889048891488924889348894488954889648897488984889948900489014890248903489044890548906489074890848909489104891148912489134891448915489164891748918489194892048921489224892348924489254892648927489284892948930489314893248933489344893548936489374893848939489404894148942489434894448945489464894748948489494895048951489524895348954489554895648957489584895948960489614896248963489644896548966489674896848969489704897148972489734897448975489764897748978489794898048981489824898348984489854898648987489884898948990489914899248993489944899548996489974899848999490004900149002490034900449005490064900749008490094901049011490124901349014490154901649017490184901949020490214902249023490244902549026490274902849029490304903149032490334903449035490364903749038490394904049041490424904349044490454904649047490484904949050490514905249053490544905549056490574905849059490604906149062490634906449065490664906749068490694907049071490724907349074490754907649077490784907949080490814908249083490844908549086490874908849089490904909149092490934909449095490964909749098490994910049101491024910349104491054910649107491084910949110491114911249113491144911549116491174911849119491204912149122491234912449125491264912749128491294913049131491324913349134491354913649137491384913949140491414914249143491444914549146491474914849149491504915149152491534915449155491564915749158491594916049161491624916349164491654916649167491684916949170491714917249173491744917549176491774917849179491804918149182491834918449185491864918749188491894919049191491924919349194491954919649197491984919949200492014920249203492044920549206492074920849209492104921149212492134921449215492164921749218492194922049221492224922349224492254922649227492284922949230492314923249233492344923549236492374923849239492404924149242492434924449245492464924749248492494925049251492524925349254492554925649257492584925949260492614926249263492644926549266492674926849269492704927149272492734927449275492764927749278492794928049281492824928349284492854928649287492884928949290492914929249293492944929549296492974929849299493004930149302493034930449305493064930749308493094931049311493124931349314493154931649317493184931949320493214932249323493244932549326493274932849329493304933149332493334933449335493364933749338493394934049341493424934349344493454934649347493484934949350493514935249353493544935549356493574935849359493604936149362493634936449365493664936749368493694937049371493724937349374493754937649377493784937949380493814938249383493844938549386493874938849389493904939149392493934939449395493964939749398493994940049401494024940349404494054940649407494084940949410494114941249413494144941549416494174941849419494204942149422494234942449425494264942749428494294943049431494324943349434494354943649437494384943949440494414944249443494444944549446494474944849449494504945149452494534945449455494564945749458494594946049461494624946349464494654946649467494684946949470494714947249473494744947549476494774947849479494804948149482494834948449485494864948749488494894949049491494924949349494494954949649497494984949949500495014950249503495044950549506495074950849509495104951149512495134951449515495164951749518495194952049521495224952349524495254952649527495284952949530495314953249533495344953549536495374953849539495404954149542495434954449545495464954749548495494955049551495524955349554495554955649557495584955949560495614956249563495644956549566495674956849569495704957149572495734957449575495764957749578495794958049581495824958349584495854958649587495884958949590495914959249593495944959549596495974959849599496004960149602496034960449605496064960749608496094961049611496124961349614496154961649617496184961949620496214962249623496244962549626496274962849629496304963149632496334963449635496364963749638496394964049641496424964349644496454964649647496484964949650496514965249653496544965549656496574965849659496604966149662496634966449665496664966749668496694967049671496724967349674496754967649677496784967949680496814968249683496844968549686496874968849689496904969149692496934969449695496964969749698496994970049701497024970349704497054970649707497084970949710497114971249713497144971549716497174971849719497204972149722497234972449725497264972749728497294973049731497324973349734497354973649737497384973949740497414974249743497444974549746497474974849749497504975149752497534975449755497564975749758497594976049761497624976349764497654976649767497684976949770497714977249773497744977549776497774977849779497804978149782497834978449785497864978749788497894979049791497924979349794497954979649797497984979949800498014980249803498044980549806498074980849809498104981149812498134981449815498164981749818498194982049821498224982349824498254982649827498284982949830498314983249833498344983549836498374983849839498404984149842498434984449845498464984749848498494985049851498524985349854498554985649857498584985949860498614986249863498644986549866498674986849869498704987149872498734987449875498764987749878498794988049881498824988349884498854988649887498884988949890498914989249893498944989549896498974989849899499004990149902499034990449905499064990749908499094991049911499124991349914499154991649917499184991949920499214992249923499244992549926499274992849929499304993149932499334993449935499364993749938499394994049941499424994349944499454994649947499484994949950499514995249953499544995549956499574995849959499604996149962499634996449965499664996749968499694997049971499724997349974499754997649977499784997949980499814998249983499844998549986499874998849989499904999149992499934999449995499964999749998499995000050001500025000350004500055000650007500085000950010500115001250013500145001550016500175001850019500205002150022500235002450025500265002750028500295003050031500325003350034500355003650037500385003950040500415004250043500445004550046500475004850049500505005150052500535005450055500565005750058500595006050061500625006350064500655006650067500685006950070500715007250073500745007550076500775007850079500805008150082500835008450085500865008750088500895009050091500925009350094500955009650097500985009950100501015010250103501045010550106501075010850109501105011150112501135011450115501165011750118501195012050121501225012350124501255012650127501285012950130501315013250133501345013550136501375013850139501405014150142501435014450145501465014750148501495015050151501525015350154501555015650157501585015950160501615016250163501645016550166501675016850169501705017150172501735017450175501765017750178501795018050181501825018350184501855018650187501885018950190501915019250193501945019550196501975019850199502005020150202502035020450205502065020750208502095021050211502125021350214502155021650217502185021950220502215022250223502245022550226502275022850229502305023150232502335023450235502365023750238502395024050241502425024350244502455024650247502485024950250502515025250253502545025550256502575025850259502605026150262502635026450265502665026750268502695027050271502725027350274502755027650277502785027950280502815028250283502845028550286502875028850289502905029150292502935029450295502965029750298502995030050301503025030350304503055030650307503085030950310503115031250313503145031550316503175031850319503205032150322503235032450325503265032750328503295033050331503325033350334503355033650337503385033950340503415034250343503445034550346503475034850349503505035150352503535035450355503565035750358503595036050361503625036350364503655036650367503685036950370503715037250373503745037550376503775037850379503805038150382503835038450385503865038750388503895039050391503925039350394503955039650397503985039950400504015040250403504045040550406504075040850409504105041150412504135041450415504165041750418504195042050421504225042350424504255042650427504285042950430504315043250433504345043550436504375043850439504405044150442504435044450445504465044750448504495045050451504525045350454504555045650457504585045950460504615046250463504645046550466504675046850469504705047150472504735047450475504765047750478504795048050481504825048350484504855048650487504885048950490504915049250493504945049550496504975049850499505005050150502505035050450505505065050750508505095051050511505125051350514505155051650517505185051950520505215052250523505245052550526505275052850529505305053150532505335053450535505365053750538505395054050541505425054350544505455054650547505485054950550505515055250553505545055550556505575055850559505605056150562505635056450565505665056750568505695057050571505725057350574505755057650577505785057950580505815058250583505845058550586505875058850589505905059150592505935059450595505965059750598505995060050601506025060350604506055060650607506085060950610506115061250613506145061550616506175061850619506205062150622506235062450625506265062750628506295063050631506325063350634506355063650637506385063950640506415064250643506445064550646506475064850649506505065150652506535065450655506565065750658506595066050661506625066350664506655066650667506685066950670506715067250673506745067550676506775067850679506805068150682506835068450685506865068750688506895069050691506925069350694506955069650697506985069950700507015070250703507045070550706507075070850709507105071150712507135071450715507165071750718507195072050721507225072350724507255072650727507285072950730507315073250733507345073550736507375073850739507405074150742507435074450745507465074750748507495075050751507525075350754507555075650757507585075950760507615076250763507645076550766507675076850769507705077150772507735077450775507765077750778507795078050781507825078350784507855078650787507885078950790507915079250793507945079550796507975079850799508005080150802508035080450805508065080750808508095081050811508125081350814508155081650817508185081950820508215082250823508245082550826508275082850829508305083150832508335083450835508365083750838508395084050841508425084350844508455084650847508485084950850508515085250853508545085550856508575085850859508605086150862508635086450865508665086750868508695087050871508725087350874508755087650877508785087950880508815088250883508845088550886508875088850889508905089150892508935089450895508965089750898508995090050901509025090350904509055090650907509085090950910509115091250913509145091550916509175091850919509205092150922509235092450925509265092750928509295093050931509325093350934509355093650937509385093950940509415094250943509445094550946509475094850949509505095150952509535095450955509565095750958509595096050961509625096350964509655096650967509685096950970509715097250973509745097550976509775097850979509805098150982509835098450985509865098750988509895099050991509925099350994509955099650997509985099951000510015100251003510045100551006510075100851009510105101151012510135101451015510165101751018510195102051021510225102351024510255102651027510285102951030510315103251033510345103551036510375103851039510405104151042510435104451045510465104751048510495105051051510525105351054510555105651057510585105951060510615106251063510645106551066510675106851069510705107151072510735107451075510765107751078510795108051081510825108351084510855108651087510885108951090510915109251093510945109551096510975109851099511005110151102511035110451105511065110751108511095111051111511125111351114511155111651117511185111951120511215112251123511245112551126511275112851129511305113151132511335113451135511365113751138511395114051141511425114351144511455114651147511485114951150511515115251153511545115551156511575115851159511605116151162511635116451165511665116751168511695117051171511725117351174511755117651177511785117951180511815118251183511845118551186511875118851189511905119151192511935119451195511965119751198511995120051201512025120351204512055120651207512085120951210512115121251213512145121551216512175121851219512205122151222512235122451225512265122751228512295123051231512325123351234512355123651237512385123951240512415124251243512445124551246512475124851249512505125151252512535125451255512565125751258512595126051261512625126351264512655126651267512685126951270512715127251273512745127551276512775127851279512805128151282512835128451285512865128751288512895129051291512925129351294512955129651297512985129951300513015130251303513045130551306513075130851309513105131151312513135131451315513165131751318513195132051321513225132351324513255132651327513285132951330513315133251333513345133551336513375133851339513405134151342513435134451345513465134751348513495135051351513525135351354513555135651357513585135951360513615136251363513645136551366513675136851369513705137151372513735137451375513765137751378513795138051381513825138351384513855138651387513885138951390513915139251393513945139551396513975139851399514005140151402514035140451405514065140751408514095141051411514125141351414514155141651417514185141951420514215142251423514245142551426514275142851429514305143151432514335143451435514365143751438514395144051441514425144351444514455144651447514485144951450514515145251453514545145551456514575145851459514605146151462514635146451465514665146751468514695147051471514725147351474514755147651477514785147951480514815148251483514845148551486514875148851489514905149151492514935149451495514965149751498514995150051501515025150351504515055150651507515085150951510515115151251513515145151551516515175151851519515205152151522515235152451525515265152751528515295153051531515325153351534515355153651537515385153951540515415154251543515445154551546515475154851549515505155151552515535155451555515565155751558515595156051561515625156351564515655156651567515685156951570515715157251573515745157551576515775157851579515805158151582515835158451585515865158751588515895159051591515925159351594515955159651597515985159951600516015160251603516045160551606516075160851609516105161151612516135161451615516165161751618516195162051621516225162351624516255162651627516285162951630516315163251633516345163551636516375163851639516405164151642516435164451645516465164751648516495165051651516525165351654516555165651657516585165951660516615166251663516645166551666516675166851669516705167151672516735167451675516765167751678516795168051681516825168351684516855168651687516885168951690516915169251693516945169551696516975169851699517005170151702517035170451705517065170751708517095171051711517125171351714517155171651717517185171951720517215172251723517245172551726517275172851729517305173151732517335173451735517365173751738517395174051741517425174351744517455174651747517485174951750517515175251753517545175551756517575175851759517605176151762517635176451765517665176751768517695177051771517725177351774517755177651777517785177951780517815178251783517845178551786517875178851789517905179151792517935179451795517965179751798517995180051801518025180351804518055180651807518085180951810518115181251813518145181551816518175181851819518205182151822518235182451825518265182751828518295183051831518325183351834518355183651837518385183951840518415184251843518445184551846518475184851849518505185151852518535185451855518565185751858518595186051861518625186351864518655186651867518685186951870518715187251873518745187551876518775187851879518805188151882518835188451885518865188751888518895189051891518925189351894518955189651897518985189951900519015190251903519045190551906519075190851909519105191151912519135191451915519165191751918519195192051921519225192351924519255192651927519285192951930519315193251933519345193551936519375193851939519405194151942519435194451945519465194751948519495195051951519525195351954519555195651957519585195951960519615196251963519645196551966519675196851969519705197151972519735197451975519765197751978519795198051981519825198351984519855198651987519885198951990519915199251993519945199551996519975199851999520005200152002520035200452005520065200752008520095201052011520125201352014520155201652017520185201952020520215202252023520245202552026520275202852029520305203152032520335203452035520365203752038520395204052041520425204352044520455204652047520485204952050520515205252053520545205552056520575205852059520605206152062520635206452065520665206752068520695207052071520725207352074520755207652077520785207952080520815208252083520845208552086520875208852089520905209152092520935209452095520965209752098520995210052101521025210352104521055210652107521085210952110521115211252113521145211552116521175211852119521205212152122521235212452125521265212752128521295213052131521325213352134521355213652137521385213952140521415214252143521445214552146521475214852149521505215152152521535215452155521565215752158521595216052161521625216352164521655216652167521685216952170521715217252173521745217552176521775217852179521805218152182521835218452185521865218752188521895219052191521925219352194521955219652197521985219952200522015220252203522045220552206522075220852209522105221152212522135221452215522165221752218522195222052221522225222352224522255222652227522285222952230522315223252233522345223552236522375223852239522405224152242522435224452245522465224752248522495225052251522525225352254522555225652257522585225952260522615226252263522645226552266522675226852269522705227152272522735227452275522765227752278522795228052281522825228352284522855228652287522885228952290522915229252293522945229552296522975229852299523005230152302523035230452305523065230752308523095231052311523125231352314523155231652317523185231952320523215232252323523245232552326523275232852329523305233152332523335233452335523365233752338523395234052341523425234352344523455234652347523485234952350523515235252353523545235552356523575235852359523605236152362523635236452365523665236752368523695237052371523725237352374523755237652377523785237952380523815238252383523845238552386523875238852389523905239152392523935239452395523965239752398523995240052401524025240352404524055240652407524085240952410524115241252413524145241552416524175241852419524205242152422524235242452425524265242752428524295243052431524325243352434524355243652437524385243952440524415244252443524445244552446524475244852449524505245152452524535245452455524565245752458524595246052461524625246352464524655246652467524685246952470524715247252473524745247552476524775247852479524805248152482524835248452485524865248752488524895249052491524925249352494524955249652497524985249952500525015250252503525045250552506525075250852509525105251152512525135251452515525165251752518525195252052521525225252352524525255252652527525285252952530525315253252533525345253552536525375253852539525405254152542525435254452545525465254752548525495255052551525525255352554525555255652557525585255952560525615256252563525645256552566525675256852569525705257152572525735257452575525765257752578525795258052581525825258352584525855258652587525885258952590525915259252593525945259552596525975259852599526005260152602526035260452605526065260752608526095261052611526125261352614526155261652617526185261952620526215262252623526245262552626526275262852629526305263152632526335263452635526365263752638526395264052641526425264352644526455264652647526485264952650526515265252653526545265552656526575265852659526605266152662526635266452665526665266752668526695267052671526725267352674526755267652677526785267952680526815268252683526845268552686526875268852689526905269152692526935269452695526965269752698526995270052701527025270352704527055270652707527085270952710527115271252713527145271552716527175271852719527205272152722527235272452725527265272752728527295273052731527325273352734527355273652737527385273952740527415274252743527445274552746527475274852749527505275152752527535275452755527565275752758527595276052761527625276352764527655276652767527685276952770527715277252773527745277552776527775277852779527805278152782527835278452785527865278752788527895279052791527925279352794527955279652797527985279952800528015280252803528045280552806528075280852809528105281152812528135281452815528165281752818528195282052821528225282352824528255282652827528285282952830528315283252833528345283552836528375283852839528405284152842528435284452845528465284752848528495285052851528525285352854528555285652857528585285952860528615286252863528645286552866528675286852869528705287152872528735287452875528765287752878528795288052881528825288352884528855288652887528885288952890528915289252893528945289552896528975289852899529005290152902529035290452905529065290752908529095291052911529125291352914529155291652917529185291952920529215292252923529245292552926529275292852929529305293152932529335293452935529365293752938529395294052941529425294352944529455294652947529485294952950529515295252953529545295552956529575295852959529605296152962529635296452965529665296752968529695297052971529725297352974529755297652977529785297952980529815298252983529845298552986529875298852989529905299152992529935299452995529965299752998529995300053001530025300353004530055300653007530085300953010530115301253013530145301553016530175301853019530205302153022530235302453025530265302753028530295303053031530325303353034530355303653037530385303953040530415304253043530445304553046530475304853049530505305153052530535305453055530565305753058530595306053061530625306353064530655306653067530685306953070530715307253073530745307553076530775307853079530805308153082530835308453085530865308753088530895309053091530925309353094530955309653097530985309953100531015310253103531045310553106531075310853109531105311153112531135311453115531165311753118531195312053121531225312353124531255312653127531285312953130531315313253133531345313553136531375313853139531405314153142531435314453145531465314753148531495315053151531525315353154531555315653157531585315953160531615316253163531645316553166531675316853169531705317153172531735317453175531765317753178531795318053181531825318353184531855318653187531885318953190531915319253193531945319553196531975319853199532005320153202532035320453205532065320753208532095321053211532125321353214532155321653217532185321953220532215322253223532245322553226532275322853229532305323153232532335323453235532365323753238532395324053241532425324353244532455324653247532485324953250532515325253253532545325553256532575325853259532605326153262532635326453265532665326753268532695327053271532725327353274532755327653277532785327953280532815328253283532845328553286532875328853289532905329153292532935329453295532965329753298532995330053301533025330353304533055330653307533085330953310533115331253313533145331553316533175331853319533205332153322533235332453325533265332753328533295333053331533325333353334533355333653337533385333953340533415334253343533445334553346533475334853349533505335153352533535335453355533565335753358533595336053361533625336353364533655336653367533685336953370533715337253373533745337553376533775337853379533805338153382533835338453385533865338753388533895339053391533925339353394533955339653397533985339953400534015340253403534045340553406534075340853409534105341153412534135341453415534165341753418534195342053421534225342353424534255342653427534285342953430534315343253433534345343553436534375343853439534405344153442534435344453445534465344753448534495345053451534525345353454534555345653457534585345953460534615346253463534645346553466534675346853469534705347153472534735347453475534765347753478534795348053481534825348353484534855348653487534885348953490534915349253493534945349553496534975349853499535005350153502535035350453505535065350753508535095351053511535125351353514535155351653517535185351953520535215352253523535245352553526535275352853529535305353153532535335353453535535365353753538535395354053541535425354353544535455354653547535485354953550535515355253553535545355553556535575355853559535605356153562535635356453565535665356753568535695357053571535725357353574535755357653577535785357953580535815358253583535845358553586535875358853589535905359153592535935359453595535965359753598535995360053601536025360353604536055360653607536085360953610536115361253613536145361553616536175361853619536205362153622536235362453625536265362753628536295363053631536325363353634536355363653637536385363953640536415364253643536445364553646536475364853649536505365153652536535365453655536565365753658536595366053661536625366353664536655366653667536685366953670536715367253673536745367553676536775367853679536805368153682536835368453685536865368753688536895369053691536925369353694536955369653697536985369953700537015370253703537045370553706537075370853709537105371153712537135371453715537165371753718537195372053721537225372353724537255372653727537285372953730537315373253733537345373553736537375373853739537405374153742537435374453745537465374753748537495375053751537525375353754537555375653757537585375953760537615376253763537645376553766537675376853769537705377153772537735377453775537765377753778537795378053781537825378353784537855378653787537885378953790537915379253793537945379553796537975379853799538005380153802538035380453805538065380753808538095381053811538125381353814538155381653817538185381953820538215382253823538245382553826538275382853829538305383153832538335383453835538365383753838538395384053841538425384353844538455384653847538485384953850538515385253853538545385553856538575385853859538605386153862538635386453865538665386753868538695387053871538725387353874538755387653877538785387953880538815388253883538845388553886538875388853889538905389153892538935389453895538965389753898538995390053901539025390353904539055390653907539085390953910539115391253913539145391553916539175391853919539205392153922539235392453925539265392753928539295393053931539325393353934539355393653937539385393953940539415394253943539445394553946539475394853949539505395153952539535395453955539565395753958539595396053961539625396353964539655396653967539685396953970539715397253973539745397553976539775397853979539805398153982539835398453985539865398753988539895399053991539925399353994539955399653997539985399954000540015400254003540045400554006540075400854009540105401154012540135401454015540165401754018540195402054021540225402354024540255402654027540285402954030540315403254033540345403554036540375403854039540405404154042540435404454045540465404754048540495405054051540525405354054540555405654057540585405954060540615406254063540645406554066540675406854069540705407154072540735407454075540765407754078540795408054081540825408354084540855408654087540885408954090540915409254093540945409554096540975409854099541005410154102541035410454105541065410754108541095411054111541125411354114541155411654117541185411954120541215412254123541245412554126541275412854129541305413154132541335413454135541365413754138541395414054141541425414354144541455414654147541485414954150541515415254153541545415554156541575415854159541605416154162541635416454165541665416754168541695417054171541725417354174541755417654177541785417954180541815418254183541845418554186541875418854189541905419154192541935419454195541965419754198541995420054201542025420354204542055420654207542085420954210542115421254213542145421554216542175421854219542205422154222542235422454225542265422754228542295423054231542325423354234542355423654237542385423954240542415424254243542445424554246542475424854249542505425154252542535425454255542565425754258542595426054261542625426354264542655426654267542685426954270542715427254273542745427554276542775427854279542805428154282542835428454285542865428754288542895429054291542925429354294542955429654297542985429954300543015430254303543045430554306543075430854309543105431154312543135431454315543165431754318543195432054321543225432354324543255432654327543285432954330543315433254333543345433554336543375433854339543405434154342543435434454345543465434754348543495435054351543525435354354543555435654357543585435954360543615436254363543645436554366543675436854369543705437154372543735437454375543765437754378543795438054381543825438354384543855438654387543885438954390543915439254393543945439554396543975439854399544005440154402544035440454405544065440754408544095441054411544125441354414544155441654417544185441954420544215442254423544245442554426544275442854429544305443154432544335443454435544365443754438544395444054441544425444354444544455444654447544485444954450544515445254453544545445554456544575445854459544605446154462544635446454465544665446754468544695447054471544725447354474544755447654477544785447954480544815448254483544845448554486544875448854489544905449154492544935449454495544965449754498544995450054501545025450354504545055450654507545085450954510545115451254513545145451554516545175451854519545205452154522545235452454525545265452754528545295453054531545325453354534545355453654537545385453954540545415454254543545445454554546545475454854549545505455154552545535455454555545565455754558545595456054561545625456354564545655456654567545685456954570545715457254573545745457554576545775457854579545805458154582545835458454585545865458754588545895459054591545925459354594545955459654597545985459954600546015460254603546045460554606546075460854609546105461154612546135461454615546165461754618546195462054621546225462354624546255462654627546285462954630546315463254633546345463554636546375463854639546405464154642546435464454645546465464754648546495465054651546525465354654546555465654657546585465954660546615466254663546645466554666546675466854669546705467154672546735467454675546765467754678546795468054681546825468354684546855468654687546885468954690546915469254693546945469554696546975469854699547005470154702547035470454705547065470754708547095471054711547125471354714547155471654717547185471954720547215472254723547245472554726547275472854729547305473154732547335473454735547365473754738547395474054741547425474354744547455474654747547485474954750547515475254753547545475554756547575475854759547605476154762547635476454765547665476754768547695477054771547725477354774547755477654777547785477954780547815478254783547845478554786547875478854789547905479154792547935479454795547965479754798547995480054801548025480354804548055480654807548085480954810548115481254813548145481554816548175481854819548205482154822548235482454825548265482754828548295483054831548325483354834548355483654837548385483954840548415484254843548445484554846548475484854849548505485154852548535485454855548565485754858548595486054861548625486354864548655486654867548685486954870548715487254873548745487554876548775487854879548805488154882548835488454885548865488754888548895489054891548925489354894548955489654897548985489954900549015490254903549045490554906549075490854909549105491154912549135491454915549165491754918549195492054921549225492354924549255492654927549285492954930549315493254933549345493554936549375493854939549405494154942549435494454945549465494754948549495495054951549525495354954549555495654957549585495954960549615496254963549645496554966549675496854969549705497154972549735497454975549765497754978549795498054981549825498354984549855498654987549885498954990549915499254993549945499554996549975499854999550005500155002550035500455005550065500755008550095501055011550125501355014550155501655017550185501955020550215502255023550245502555026550275502855029550305503155032550335503455035550365503755038550395504055041550425504355044550455504655047550485504955050550515505255053550545505555056550575505855059550605506155062550635506455065550665506755068550695507055071550725507355074550755507655077550785507955080550815508255083550845508555086550875508855089550905509155092550935509455095550965509755098550995510055101551025510355104551055510655107551085510955110551115511255113551145511555116551175511855119551205512155122551235512455125551265512755128551295513055131551325513355134551355513655137551385513955140551415514255143551445514555146551475514855149551505515155152551535515455155551565515755158551595516055161551625516355164551655516655167551685516955170551715517255173551745517555176551775517855179551805518155182551835518455185551865518755188551895519055191551925519355194551955519655197551985519955200552015520255203552045520555206552075520855209552105521155212552135521455215552165521755218552195522055221552225522355224552255522655227552285522955230552315523255233552345523555236552375523855239552405524155242552435524455245552465524755248552495525055251552525525355254552555525655257552585525955260552615526255263552645526555266552675526855269552705527155272552735527455275552765527755278552795528055281552825528355284552855528655287552885528955290552915529255293552945529555296552975529855299553005530155302553035530455305553065530755308553095531055311553125531355314553155531655317553185531955320553215532255323553245532555326553275532855329553305533155332553335533455335553365533755338553395534055341553425534355344553455534655347553485534955350553515535255353553545535555356553575535855359553605536155362553635536455365553665536755368553695537055371553725537355374553755537655377553785537955380553815538255383553845538555386553875538855389553905539155392553935539455395553965539755398553995540055401554025540355404554055540655407554085540955410554115541255413554145541555416554175541855419554205542155422554235542455425554265542755428554295543055431554325543355434554355543655437554385543955440554415544255443554445544555446554475544855449554505545155452554535545455455554565545755458554595546055461554625546355464554655546655467554685546955470554715547255473554745547555476554775547855479554805548155482554835548455485554865548755488554895549055491554925549355494554955549655497554985549955500555015550255503555045550555506555075550855509555105551155512555135551455515555165551755518555195552055521555225552355524555255552655527555285552955530555315553255533555345553555536555375553855539555405554155542555435554455545555465554755548555495555055551555525555355554555555555655557555585555955560555615556255563555645556555566555675556855569555705557155572555735557455575555765557755578555795558055581555825558355584555855558655587555885558955590555915559255593555945559555596555975559855599556005560155602556035560455605556065560755608556095561055611556125561355614556155561655617556185561955620556215562255623556245562555626556275562855629556305563155632556335563455635556365563755638556395564055641556425564355644556455564655647556485564955650556515565255653556545565555656556575565855659556605566155662556635566455665556665566755668556695567055671556725567355674556755567655677556785567955680556815568255683556845568555686556875568855689556905569155692556935569455695556965569755698556995570055701557025570355704557055570655707557085570955710557115571255713557145571555716557175571855719557205572155722557235572455725557265572755728557295573055731557325573355734557355573655737557385573955740557415574255743557445574555746557475574855749557505575155752557535575455755557565575755758557595576055761557625576355764557655576655767557685576955770557715577255773557745577555776557775577855779557805578155782557835578455785557865578755788557895579055791557925579355794557955579655797557985579955800558015580255803558045580555806558075580855809558105581155812558135581455815558165581755818558195582055821558225582355824558255582655827558285582955830558315583255833558345583555836558375583855839558405584155842558435584455845558465584755848558495585055851558525585355854558555585655857558585585955860558615586255863558645586555866558675586855869558705587155872558735587455875558765587755878558795588055881558825588355884558855588655887558885588955890558915589255893558945589555896558975589855899559005590155902559035590455905559065590755908559095591055911559125591355914559155591655917559185591955920559215592255923559245592555926559275592855929559305593155932559335593455935559365593755938559395594055941559425594355944559455594655947559485594955950559515595255953559545595555956559575595855959559605596155962559635596455965559665596755968559695597055971559725597355974559755597655977559785597955980559815598255983559845598555986559875598855989559905599155992559935599455995559965599755998559995600056001560025600356004560055600656007560085600956010560115601256013560145601556016560175601856019560205602156022560235602456025560265602756028560295603056031560325603356034560355603656037560385603956040560415604256043560445604556046560475604856049560505605156052560535605456055560565605756058560595606056061560625606356064560655606656067560685606956070560715607256073560745607556076560775607856079560805608156082560835608456085560865608756088560895609056091560925609356094560955609656097560985609956100561015610256103561045610556106561075610856109561105611156112561135611456115561165611756118561195612056121561225612356124561255612656127561285612956130561315613256133561345613556136561375613856139561405614156142561435614456145561465614756148561495615056151561525615356154561555615656157561585615956160561615616256163561645616556166561675616856169561705617156172561735617456175561765617756178561795618056181561825618356184561855618656187561885618956190561915619256193561945619556196561975619856199562005620156202562035620456205562065620756208562095621056211562125621356214562155621656217562185621956220562215622256223562245622556226562275622856229562305623156232562335623456235562365623756238562395624056241562425624356244562455624656247562485624956250562515625256253562545625556256562575625856259562605626156262562635626456265562665626756268562695627056271562725627356274562755627656277562785627956280562815628256283562845628556286562875628856289562905629156292562935629456295562965629756298562995630056301563025630356304563055630656307563085630956310563115631256313563145631556316563175631856319563205632156322563235632456325563265632756328563295633056331563325633356334563355633656337563385633956340563415634256343563445634556346563475634856349563505635156352563535635456355563565635756358563595636056361563625636356364563655636656367563685636956370563715637256373563745637556376563775637856379563805638156382563835638456385563865638756388563895639056391563925639356394563955639656397563985639956400564015640256403564045640556406564075640856409564105641156412564135641456415564165641756418564195642056421564225642356424564255642656427564285642956430564315643256433564345643556436564375643856439564405644156442564435644456445564465644756448564495645056451564525645356454564555645656457564585645956460564615646256463564645646556466564675646856469564705647156472564735647456475564765647756478564795648056481564825648356484564855648656487564885648956490564915649256493564945649556496564975649856499565005650156502565035650456505565065650756508565095651056511565125651356514565155651656517565185651956520565215652256523565245652556526565275652856529565305653156532565335653456535565365653756538565395654056541565425654356544565455654656547565485654956550565515655256553565545655556556565575655856559565605656156562565635656456565565665656756568565695657056571565725657356574565755657656577565785657956580565815658256583565845658556586565875658856589565905659156592565935659456595565965659756598565995660056601566025660356604566055660656607566085660956610566115661256613566145661556616566175661856619566205662156622566235662456625566265662756628566295663056631566325663356634566355663656637566385663956640566415664256643566445664556646566475664856649566505665156652566535665456655566565665756658566595666056661566625666356664566655666656667566685666956670566715667256673566745667556676566775667856679566805668156682566835668456685566865668756688566895669056691566925669356694566955669656697566985669956700567015670256703567045670556706567075670856709567105671156712567135671456715567165671756718567195672056721567225672356724567255672656727567285672956730567315673256733567345673556736567375673856739567405674156742567435674456745567465674756748567495675056751567525675356754567555675656757567585675956760567615676256763567645676556766567675676856769567705677156772567735677456775567765677756778567795678056781567825678356784567855678656787567885678956790567915679256793567945679556796567975679856799568005680156802568035680456805568065680756808568095681056811568125681356814568155681656817568185681956820568215682256823568245682556826568275682856829568305683156832568335683456835568365683756838568395684056841568425684356844568455684656847568485684956850568515685256853568545685556856568575685856859568605686156862568635686456865568665686756868568695687056871568725687356874568755687656877568785687956880568815688256883568845688556886568875688856889568905689156892568935689456895568965689756898568995690056901569025690356904569055690656907569085690956910569115691256913569145691556916569175691856919569205692156922569235692456925569265692756928569295693056931569325693356934569355693656937569385693956940569415694256943569445694556946569475694856949569505695156952569535695456955569565695756958569595696056961569625696356964569655696656967569685696956970569715697256973569745697556976569775697856979569805698156982569835698456985569865698756988569895699056991569925699356994569955699656997569985699957000570015700257003570045700557006570075700857009570105701157012570135701457015570165701757018570195702057021570225702357024570255702657027570285702957030570315703257033570345703557036570375703857039570405704157042570435704457045570465704757048570495705057051570525705357054570555705657057570585705957060570615706257063570645706557066570675706857069570705707157072570735707457075570765707757078570795708057081570825708357084570855708657087570885708957090570915709257093570945709557096570975709857099571005710157102571035710457105571065710757108571095711057111571125711357114571155711657117571185711957120571215712257123571245712557126571275712857129571305713157132571335713457135571365713757138571395714057141571425714357144571455714657147571485714957150571515715257153571545715557156571575715857159571605716157162571635716457165571665716757168571695717057171571725717357174571755717657177571785717957180571815718257183571845718557186571875718857189571905719157192571935719457195571965719757198571995720057201572025720357204572055720657207572085720957210572115721257213572145721557216572175721857219572205722157222572235722457225572265722757228572295723057231572325723357234572355723657237572385723957240572415724257243572445724557246572475724857249572505725157252572535725457255572565725757258572595726057261572625726357264572655726657267572685726957270572715727257273572745727557276572775727857279572805728157282572835728457285572865728757288572895729057291572925729357294572955729657297572985729957300573015730257303573045730557306573075730857309573105731157312573135731457315573165731757318573195732057321573225732357324573255732657327573285732957330573315733257333573345733557336573375733857339573405734157342573435734457345573465734757348573495735057351573525735357354573555735657357573585735957360573615736257363573645736557366573675736857369573705737157372573735737457375573765737757378573795738057381573825738357384573855738657387573885738957390573915739257393573945739557396573975739857399574005740157402574035740457405574065740757408574095741057411574125741357414574155741657417574185741957420574215742257423574245742557426574275742857429574305743157432574335743457435574365743757438574395744057441574425744357444574455744657447574485744957450574515745257453574545745557456574575745857459574605746157462574635746457465574665746757468574695747057471574725747357474574755747657477574785747957480574815748257483574845748557486574875748857489574905749157492574935749457495574965749757498574995750057501575025750357504575055750657507575085750957510575115751257513575145751557516575175751857519575205752157522575235752457525575265752757528575295753057531575325753357534575355753657537575385753957540575415754257543575445754557546575475754857549575505755157552575535755457555575565755757558575595756057561575625756357564575655756657567575685756957570575715757257573575745757557576575775757857579575805758157582575835758457585575865758757588575895759057591575925759357594575955759657597575985759957600576015760257603576045760557606576075760857609576105761157612576135761457615576165761757618576195762057621576225762357624576255762657627576285762957630576315763257633576345763557636576375763857639576405764157642576435764457645576465764757648576495765057651576525765357654576555765657657576585765957660576615766257663576645766557666576675766857669576705767157672576735767457675576765767757678576795768057681576825768357684576855768657687576885768957690576915769257693576945769557696576975769857699577005770157702577035770457705577065770757708577095771057711577125771357714577155771657717577185771957720577215772257723577245772557726577275772857729577305773157732577335773457735577365773757738577395774057741577425774357744577455774657747577485774957750577515775257753577545775557756577575775857759577605776157762577635776457765577665776757768577695777057771577725777357774577755777657777577785777957780577815778257783577845778557786577875778857789577905779157792577935779457795577965779757798577995780057801578025780357804578055780657807578085780957810578115781257813578145781557816578175781857819578205782157822578235782457825578265782757828578295783057831578325783357834578355783657837578385783957840578415784257843578445784557846578475784857849578505785157852578535785457855578565785757858578595786057861578625786357864578655786657867578685786957870578715787257873578745787557876578775787857879578805788157882578835788457885578865788757888578895789057891578925789357894578955789657897578985789957900579015790257903579045790557906579075790857909579105791157912579135791457915579165791757918579195792057921579225792357924579255792657927579285792957930579315793257933579345793557936579375793857939579405794157942579435794457945579465794757948579495795057951579525795357954579555795657957579585795957960579615796257963579645796557966579675796857969579705797157972579735797457975579765797757978579795798057981579825798357984579855798657987579885798957990579915799257993579945799557996579975799857999580005800158002580035800458005580065800758008580095801058011580125801358014580155801658017580185801958020580215802258023580245802558026580275802858029580305803158032580335803458035580365803758038580395804058041580425804358044580455804658047580485804958050580515805258053580545805558056580575805858059580605806158062580635806458065580665806758068580695807058071580725807358074580755807658077580785807958080580815808258083580845808558086580875808858089580905809158092580935809458095580965809758098580995810058101581025810358104581055810658107581085810958110581115811258113581145811558116581175811858119581205812158122581235812458125581265812758128581295813058131581325813358134581355813658137581385813958140581415814258143581445814558146581475814858149581505815158152581535815458155581565815758158581595816058161581625816358164581655816658167581685816958170581715817258173581745817558176581775817858179581805818158182581835818458185581865818758188581895819058191581925819358194581955819658197581985819958200582015820258203582045820558206582075820858209582105821158212582135821458215582165821758218582195822058221582225822358224582255822658227582285822958230582315823258233582345823558236582375823858239582405824158242582435824458245582465824758248582495825058251582525825358254582555825658257582585825958260582615826258263582645826558266582675826858269582705827158272582735827458275582765827758278582795828058281582825828358284582855828658287582885828958290582915829258293582945829558296582975829858299583005830158302583035830458305583065830758308583095831058311583125831358314583155831658317583185831958320583215832258323583245832558326583275832858329583305833158332583335833458335583365833758338583395834058341583425834358344583455834658347583485834958350583515835258353583545835558356583575835858359583605836158362583635836458365583665836758368583695837058371583725837358374583755837658377583785837958380583815838258383583845838558386583875838858389583905839158392583935839458395583965839758398583995840058401584025840358404584055840658407584085840958410584115841258413584145841558416584175841858419584205842158422584235842458425584265842758428584295843058431584325843358434584355843658437584385843958440584415844258443584445844558446584475844858449584505845158452584535845458455584565845758458584595846058461584625846358464584655846658467584685846958470584715847258473584745847558476584775847858479584805848158482584835848458485584865848758488584895849058491584925849358494584955849658497584985849958500585015850258503585045850558506585075850858509585105851158512585135851458515585165851758518585195852058521585225852358524585255852658527585285852958530585315853258533585345853558536585375853858539585405854158542585435854458545585465854758548585495855058551585525855358554585555855658557585585855958560585615856258563585645856558566585675856858569585705857158572585735857458575585765857758578585795858058581585825858358584585855858658587585885858958590585915859258593585945859558596585975859858599586005860158602586035860458605586065860758608586095861058611586125861358614586155861658617586185861958620586215862258623586245862558626586275862858629586305863158632586335863458635586365863758638586395864058641586425864358644586455864658647586485864958650586515865258653586545865558656586575865858659586605866158662586635866458665586665866758668586695867058671586725867358674586755867658677586785867958680586815868258683586845868558686586875868858689586905869158692586935869458695586965869758698586995870058701587025870358704587055870658707587085870958710587115871258713587145871558716587175871858719587205872158722587235872458725587265872758728587295873058731587325873358734587355873658737587385873958740587415874258743587445874558746587475874858749587505875158752587535875458755587565875758758587595876058761587625876358764587655876658767587685876958770587715877258773587745877558776587775877858779587805878158782587835878458785587865878758788587895879058791587925879358794587955879658797587985879958800588015880258803588045880558806588075880858809588105881158812588135881458815588165881758818588195882058821588225882358824588255882658827588285882958830588315883258833588345883558836588375883858839588405884158842588435884458845588465884758848588495885058851588525885358854588555885658857588585885958860588615886258863588645886558866588675886858869588705887158872588735887458875588765887758878588795888058881588825888358884588855888658887588885888958890588915889258893588945889558896588975889858899589005890158902589035890458905589065890758908589095891058911589125891358914589155891658917589185891958920589215892258923589245892558926589275892858929589305893158932589335893458935589365893758938589395894058941589425894358944589455894658947589485894958950589515895258953589545895558956589575895858959589605896158962589635896458965589665896758968589695897058971589725897358974589755897658977589785897958980589815898258983589845898558986589875898858989589905899158992589935899458995589965899758998589995900059001590025900359004590055900659007590085900959010590115901259013590145901559016590175901859019590205902159022590235902459025590265902759028590295903059031590325903359034590355903659037590385903959040590415904259043590445904559046590475904859049590505905159052590535905459055590565905759058590595906059061590625906359064590655906659067590685906959070590715907259073590745907559076590775907859079590805908159082590835908459085590865908759088590895909059091590925909359094590955909659097590985909959100591015910259103591045910559106591075910859109591105911159112591135911459115591165911759118591195912059121591225912359124591255912659127591285912959130591315913259133591345913559136591375913859139591405914159142591435914459145591465914759148591495915059151591525915359154591555915659157591585915959160591615916259163591645916559166591675916859169591705917159172591735917459175591765917759178591795918059181591825918359184591855918659187591885918959190591915919259193591945919559196591975919859199592005920159202592035920459205592065920759208592095921059211592125921359214592155921659217592185921959220592215922259223592245922559226592275922859229592305923159232592335923459235592365923759238592395924059241592425924359244592455924659247592485924959250592515925259253592545925559256592575925859259592605926159262592635926459265592665926759268592695927059271592725927359274592755927659277592785927959280592815928259283592845928559286592875928859289592905929159292592935929459295592965929759298592995930059301593025930359304593055930659307593085930959310593115931259313593145931559316593175931859319593205932159322593235932459325593265932759328593295933059331593325933359334593355933659337593385933959340593415934259343593445934559346593475934859349593505935159352593535935459355593565935759358593595936059361593625936359364593655936659367593685936959370593715937259373593745937559376593775937859379593805938159382593835938459385593865938759388593895939059391593925939359394593955939659397593985939959400594015940259403594045940559406594075940859409594105941159412594135941459415594165941759418594195942059421594225942359424594255942659427594285942959430594315943259433594345943559436594375943859439594405944159442594435944459445594465944759448594495945059451594525945359454594555945659457594585945959460594615946259463594645946559466594675946859469594705947159472594735947459475594765947759478594795948059481594825948359484594855948659487594885948959490594915949259493594945949559496594975949859499595005950159502595035950459505595065950759508595095951059511595125951359514595155951659517595185951959520595215952259523595245952559526595275952859529595305953159532595335953459535595365953759538595395954059541595425954359544595455954659547595485954959550595515955259553595545955559556595575955859559595605956159562595635956459565595665956759568595695957059571595725957359574595755957659577595785957959580595815958259583595845958559586595875958859589595905959159592595935959459595595965959759598595995960059601596025960359604596055960659607596085960959610596115961259613596145961559616596175961859619596205962159622596235962459625596265962759628596295963059631596325963359634596355963659637596385963959640596415964259643596445964559646596475964859649596505965159652596535965459655596565965759658596595966059661596625966359664596655966659667596685966959670596715967259673596745967559676596775967859679596805968159682596835968459685596865968759688596895969059691596925969359694596955969659697596985969959700597015970259703597045970559706597075970859709597105971159712597135971459715597165971759718597195972059721597225972359724597255972659727597285972959730597315973259733597345973559736597375973859739597405974159742597435974459745597465974759748597495975059751597525975359754597555975659757597585975959760597615976259763597645976559766597675976859769597705977159772597735977459775597765977759778597795978059781597825978359784597855978659787597885978959790597915979259793597945979559796597975979859799598005980159802598035980459805598065980759808598095981059811598125981359814598155981659817598185981959820598215982259823598245982559826598275982859829598305983159832598335983459835598365983759838598395984059841598425984359844598455984659847598485984959850598515985259853598545985559856598575985859859598605986159862598635986459865598665986759868598695987059871598725987359874598755987659877598785987959880598815988259883598845988559886598875988859889598905989159892598935989459895598965989759898598995990059901599025990359904599055990659907599085990959910599115991259913599145991559916599175991859919599205992159922599235992459925599265992759928599295993059931599325993359934599355993659937599385993959940599415994259943599445994559946599475994859949599505995159952599535995459955599565995759958599595996059961599625996359964599655996659967599685996959970599715997259973599745997559976599775997859979599805998159982599835998459985599865998759988599895999059991599925999359994599955999659997599985999960000600016000260003600046000560006600076000860009600106001160012600136001460015600166001760018600196002060021600226002360024600256002660027600286002960030600316003260033600346003560036600376003860039600406004160042600436004460045600466004760048600496005060051600526005360054600556005660057600586005960060600616006260063600646006560066600676006860069600706007160072600736007460075600766007760078600796008060081600826008360084600856008660087600886008960090600916009260093600946009560096600976009860099601006010160102601036010460105601066010760108601096011060111601126011360114601156011660117601186011960120601216012260123601246012560126601276012860129601306013160132601336013460135601366013760138601396014060141601426014360144601456014660147601486014960150601516015260153601546015560156601576015860159601606016160162601636016460165601666016760168601696017060171601726017360174601756017660177601786017960180601816018260183601846018560186601876018860189601906019160192601936019460195601966019760198601996020060201602026020360204602056020660207602086020960210602116021260213602146021560216602176021860219602206022160222602236022460225602266022760228602296023060231602326023360234602356023660237602386023960240602416024260243602446024560246602476024860249602506025160252602536025460255602566025760258602596026060261602626026360264602656026660267602686026960270602716027260273602746027560276602776027860279602806028160282602836028460285602866028760288602896029060291602926029360294602956029660297602986029960300603016030260303603046030560306603076030860309603106031160312603136031460315603166031760318603196032060321603226032360324603256032660327603286032960330603316033260333603346033560336603376033860339603406034160342603436034460345603466034760348603496035060351603526035360354603556035660357603586035960360603616036260363603646036560366603676036860369603706037160372603736037460375603766037760378603796038060381603826038360384603856038660387603886038960390603916039260393603946039560396603976039860399604006040160402604036040460405604066040760408604096041060411604126041360414604156041660417604186041960420604216042260423604246042560426604276042860429604306043160432604336043460435604366043760438604396044060441604426044360444604456044660447604486044960450604516045260453604546045560456604576045860459604606046160462604636046460465604666046760468604696047060471604726047360474604756047660477604786047960480604816048260483604846048560486604876048860489604906049160492604936049460495604966049760498604996050060501605026050360504605056050660507605086050960510605116051260513605146051560516605176051860519605206052160522605236052460525605266052760528605296053060531605326053360534605356053660537605386053960540605416054260543605446054560546605476054860549605506055160552605536055460555605566055760558605596056060561605626056360564605656056660567605686056960570605716057260573605746057560576605776057860579605806058160582605836058460585605866058760588605896059060591605926059360594605956059660597605986059960600606016060260603606046060560606606076060860609606106061160612606136061460615606166061760618606196062060621606226062360624606256062660627606286062960630606316063260633606346063560636606376063860639606406064160642606436064460645606466064760648606496065060651606526065360654606556065660657606586065960660606616066260663606646066560666606676066860669606706067160672606736067460675606766067760678606796068060681606826068360684606856068660687606886068960690606916069260693606946069560696606976069860699607006070160702607036070460705607066070760708607096071060711607126071360714607156071660717607186071960720607216072260723607246072560726607276072860729607306073160732607336073460735607366073760738607396074060741607426074360744607456074660747607486074960750607516075260753607546075560756607576075860759607606076160762607636076460765607666076760768607696077060771607726077360774607756077660777607786077960780607816078260783607846078560786607876078860789607906079160792607936079460795607966079760798607996080060801608026080360804608056080660807608086080960810608116081260813608146081560816608176081860819608206082160822608236082460825608266082760828608296083060831608326083360834608356083660837608386083960840608416084260843608446084560846608476084860849608506085160852608536085460855608566085760858
  1. From 65f57e56fcf9d40383718f0bcd9e6f95a34ca1aa 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. ---
  342. arch/arm/include/asm/irqflags.h | 16 +-
  343. arch/arm/kernel/fiqasm.S | 4 +
  344. drivers/usb/Makefile | 1 +
  345. drivers/usb/core/generic.c | 1 +
  346. drivers/usb/core/hub.c | 2 +-
  347. drivers/usb/core/message.c | 79 +
  348. drivers/usb/core/otg_whitelist.h | 114 +-
  349. drivers/usb/gadget/file_storage.c | 3676 ++++++++++
  350. drivers/usb/host/Kconfig | 13 +
  351. drivers/usb/host/Makefile | 2 +
  352. drivers/usb/host/dwc_common_port/Makefile | 58 +
  353. drivers/usb/host/dwc_common_port/Makefile.fbsd | 17 +
  354. drivers/usb/host/dwc_common_port/Makefile.linux | 49 +
  355. drivers/usb/host/dwc_common_port/changes.txt | 174 +
  356. drivers/usb/host/dwc_common_port/doc/doxygen.cfg | 270 +
  357. drivers/usb/host/dwc_common_port/dwc_cc.c | 532 ++
  358. drivers/usb/host/dwc_common_port/dwc_cc.h | 224 +
  359. drivers/usb/host/dwc_common_port/dwc_common_fbsd.c | 1308 ++++
  360. .../usb/host/dwc_common_port/dwc_common_linux.c | 1418 ++++
  361. drivers/usb/host/dwc_common_port/dwc_common_nbsd.c | 1275 ++++
  362. drivers/usb/host/dwc_common_port/dwc_crypto.c | 308 +
  363. drivers/usb/host/dwc_common_port/dwc_crypto.h | 111 +
  364. drivers/usb/host/dwc_common_port/dwc_dh.c | 291 +
  365. drivers/usb/host/dwc_common_port/dwc_dh.h | 106 +
  366. drivers/usb/host/dwc_common_port/dwc_list.h | 594 ++
  367. drivers/usb/host/dwc_common_port/dwc_mem.c | 245 +
  368. drivers/usb/host/dwc_common_port/dwc_modpow.c | 636 ++
  369. drivers/usb/host/dwc_common_port/dwc_modpow.h | 34 +
  370. drivers/usb/host/dwc_common_port/dwc_notifier.c | 319 +
  371. drivers/usb/host/dwc_common_port/dwc_notifier.h | 122 +
  372. drivers/usb/host/dwc_common_port/dwc_os.h | 1276 ++++
  373. drivers/usb/host/dwc_common_port/usb.h | 946 +++
  374. drivers/usb/host/dwc_otg/Makefile | 82 +
  375. drivers/usb/host/dwc_otg/doc/doxygen.cfg | 224 +
  376. drivers/usb/host/dwc_otg/dummy_audio.c | 1574 +++++
  377. drivers/usb/host/dwc_otg/dwc_cfi_common.h | 142 +
  378. drivers/usb/host/dwc_otg/dwc_otg_adp.c | 854 +++
  379. drivers/usb/host/dwc_otg/dwc_otg_adp.h | 80 +
  380. drivers/usb/host/dwc_otg/dwc_otg_attr.c | 1210 ++++
  381. drivers/usb/host/dwc_otg/dwc_otg_attr.h | 89 +
  382. drivers/usb/host/dwc_otg/dwc_otg_cfi.c | 1876 +++++
  383. drivers/usb/host/dwc_otg/dwc_otg_cfi.h | 320 +
  384. drivers/usb/host/dwc_otg/dwc_otg_cil.c | 7141 ++++++++++++++++++++
  385. drivers/usb/host/dwc_otg/dwc_otg_cil.h | 1464 ++++
  386. drivers/usb/host/dwc_otg/dwc_otg_cil_intr.c | 1594 +++++
  387. drivers/usb/host/dwc_otg/dwc_otg_core_if.h | 705 ++
  388. drivers/usb/host/dwc_otg/dwc_otg_dbg.h | 117 +
  389. drivers/usb/host/dwc_otg/dwc_otg_driver.c | 1757 +++++
  390. drivers/usb/host/dwc_otg/dwc_otg_driver.h | 86 +
  391. drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.c | 1355 ++++
  392. drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.h | 370 +
  393. drivers/usb/host/dwc_otg/dwc_otg_fiq_stub.S | 80 +
  394. drivers/usb/host/dwc_otg/dwc_otg_hcd.c | 4260 ++++++++++++
  395. drivers/usb/host/dwc_otg/dwc_otg_hcd.h | 868 +++
  396. drivers/usb/host/dwc_otg/dwc_otg_hcd_ddma.c | 1139 ++++
  397. drivers/usb/host/dwc_otg/dwc_otg_hcd_if.h | 417 ++
  398. drivers/usb/host/dwc_otg/dwc_otg_hcd_intr.c | 2727 ++++++++
  399. drivers/usb/host/dwc_otg/dwc_otg_hcd_linux.c | 1005 +++
  400. drivers/usb/host/dwc_otg/dwc_otg_hcd_queue.c | 963 +++
  401. drivers/usb/host/dwc_otg/dwc_otg_os_dep.h | 188 +
  402. drivers/usb/host/dwc_otg/dwc_otg_pcd.c | 2725 ++++++++
  403. drivers/usb/host/dwc_otg/dwc_otg_pcd.h | 273 +
  404. drivers/usb/host/dwc_otg/dwc_otg_pcd_if.h | 361 +
  405. drivers/usb/host/dwc_otg/dwc_otg_pcd_intr.c | 5148 ++++++++++++++
  406. drivers/usb/host/dwc_otg/dwc_otg_pcd_linux.c | 1280 ++++
  407. drivers/usb/host/dwc_otg/dwc_otg_regs.h | 2550 +++++++
  408. drivers/usb/host/dwc_otg/test/Makefile | 16 +
  409. drivers/usb/host/dwc_otg/test/dwc_otg_test.pm | 337 +
  410. drivers/usb/host/dwc_otg/test/test_mod_param.pl | 133 +
  411. drivers/usb/host/dwc_otg/test/test_sysfs.pl | 193 +
  412. 70 files changed, 59908 insertions(+), 16 deletions(-)
  413. create mode 100644 drivers/usb/gadget/file_storage.c
  414. create mode 100644 drivers/usb/host/dwc_common_port/Makefile
  415. create mode 100644 drivers/usb/host/dwc_common_port/Makefile.fbsd
  416. create mode 100644 drivers/usb/host/dwc_common_port/Makefile.linux
  417. create mode 100644 drivers/usb/host/dwc_common_port/changes.txt
  418. create mode 100644 drivers/usb/host/dwc_common_port/doc/doxygen.cfg
  419. create mode 100644 drivers/usb/host/dwc_common_port/dwc_cc.c
  420. create mode 100644 drivers/usb/host/dwc_common_port/dwc_cc.h
  421. create mode 100644 drivers/usb/host/dwc_common_port/dwc_common_fbsd.c
  422. create mode 100644 drivers/usb/host/dwc_common_port/dwc_common_linux.c
  423. create mode 100644 drivers/usb/host/dwc_common_port/dwc_common_nbsd.c
  424. create mode 100644 drivers/usb/host/dwc_common_port/dwc_crypto.c
  425. create mode 100644 drivers/usb/host/dwc_common_port/dwc_crypto.h
  426. create mode 100644 drivers/usb/host/dwc_common_port/dwc_dh.c
  427. create mode 100644 drivers/usb/host/dwc_common_port/dwc_dh.h
  428. create mode 100644 drivers/usb/host/dwc_common_port/dwc_list.h
  429. create mode 100644 drivers/usb/host/dwc_common_port/dwc_mem.c
  430. create mode 100644 drivers/usb/host/dwc_common_port/dwc_modpow.c
  431. create mode 100644 drivers/usb/host/dwc_common_port/dwc_modpow.h
  432. create mode 100644 drivers/usb/host/dwc_common_port/dwc_notifier.c
  433. create mode 100644 drivers/usb/host/dwc_common_port/dwc_notifier.h
  434. create mode 100644 drivers/usb/host/dwc_common_port/dwc_os.h
  435. create mode 100644 drivers/usb/host/dwc_common_port/usb.h
  436. create mode 100644 drivers/usb/host/dwc_otg/Makefile
  437. create mode 100644 drivers/usb/host/dwc_otg/doc/doxygen.cfg
  438. create mode 100644 drivers/usb/host/dwc_otg/dummy_audio.c
  439. create mode 100644 drivers/usb/host/dwc_otg/dwc_cfi_common.h
  440. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_adp.c
  441. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_adp.h
  442. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_attr.c
  443. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_attr.h
  444. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_cfi.c
  445. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_cfi.h
  446. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_cil.c
  447. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_cil.h
  448. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_cil_intr.c
  449. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_core_if.h
  450. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_dbg.h
  451. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_driver.c
  452. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_driver.h
  453. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.c
  454. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.h
  455. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_fiq_stub.S
  456. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_hcd.c
  457. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_hcd.h
  458. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_hcd_ddma.c
  459. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_hcd_if.h
  460. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_hcd_intr.c
  461. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_hcd_linux.c
  462. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_hcd_queue.c
  463. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_os_dep.h
  464. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_pcd.c
  465. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_pcd.h
  466. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_pcd_if.h
  467. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_pcd_intr.c
  468. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_pcd_linux.c
  469. create mode 100644 drivers/usb/host/dwc_otg/dwc_otg_regs.h
  470. create mode 100644 drivers/usb/host/dwc_otg/test/Makefile
  471. create mode 100644 drivers/usb/host/dwc_otg/test/dwc_otg_test.pm
  472. create mode 100644 drivers/usb/host/dwc_otg/test/test_mod_param.pl
  473. create mode 100644 drivers/usb/host/dwc_otg/test/test_sysfs.pl
  474. --- a/arch/arm/include/asm/irqflags.h
  475. +++ b/arch/arm/include/asm/irqflags.h
  476. @@ -162,13 +162,23 @@ static inline unsigned long arch_local_s
  477. }
  478. /*
  479. - * restore saved IRQ & FIQ state
  480. + * restore saved IRQ state
  481. */
  482. #define arch_local_irq_restore arch_local_irq_restore
  483. static inline void arch_local_irq_restore(unsigned long flags)
  484. {
  485. - asm volatile(
  486. - " msr " IRQMASK_REG_NAME_W ", %0 @ local_irq_restore"
  487. + unsigned long temp = 0;
  488. + flags &= ~(1 << 6);
  489. + asm volatile (
  490. + " mrs %0, cpsr"
  491. + : "=r" (temp)
  492. + :
  493. + : "memory", "cc");
  494. + /* Preserve FIQ bit */
  495. + temp &= (1 << 6);
  496. + flags = flags | temp;
  497. + asm volatile (
  498. + " msr cpsr_c, %0 @ local_irq_restore"
  499. :
  500. : "r" (flags)
  501. : "memory", "cc");
  502. --- a/arch/arm/kernel/fiqasm.S
  503. +++ b/arch/arm/kernel/fiqasm.S
  504. @@ -47,3 +47,7 @@ ENTRY(__get_fiq_regs)
  505. mov r0, r0 @ avoid hazard prior to ARMv4
  506. ret lr
  507. ENDPROC(__get_fiq_regs)
  508. +
  509. +ENTRY(__FIQ_Branch)
  510. + mov pc, r8
  511. +ENDPROC(__FIQ_Branch)
  512. --- a/drivers/usb/Makefile
  513. +++ b/drivers/usb/Makefile
  514. @@ -7,6 +7,7 @@
  515. obj-$(CONFIG_USB) += core/
  516. obj-$(CONFIG_USB_SUPPORT) += phy/
  517. +obj-$(CONFIG_USB_DWCOTG) += host/
  518. obj-$(CONFIG_USB_DWC3) += dwc3/
  519. obj-$(CONFIG_USB_DWC2) += dwc2/
  520. obj-$(CONFIG_USB_ISP1760) += isp1760/
  521. --- a/drivers/usb/core/generic.c
  522. +++ b/drivers/usb/core/generic.c
  523. @@ -152,6 +152,7 @@ int usb_choose_configuration(struct usb_
  524. dev_warn(&udev->dev,
  525. "no configuration chosen from %d choice%s\n",
  526. num_configs, plural(num_configs));
  527. + dev_warn(&udev->dev, "No support over %dmA\n", udev->bus_mA);
  528. }
  529. return i;
  530. }
  531. --- a/drivers/usb/core/hub.c
  532. +++ b/drivers/usb/core/hub.c
  533. @@ -5068,7 +5068,7 @@ static void port_event(struct usb_hub *h
  534. if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
  535. u16 status = 0, unused;
  536. - dev_dbg(&port_dev->dev, "over-current change\n");
  537. + dev_notice(&port_dev->dev, "over-current change\n");
  538. usb_clear_port_feature(hdev, port1,
  539. USB_PORT_FEAT_C_OVER_CURRENT);
  540. msleep(100); /* Cool down */
  541. --- a/drivers/usb/core/message.c
  542. +++ b/drivers/usb/core/message.c
  543. @@ -1912,6 +1912,85 @@ free_interfaces:
  544. if (cp->string == NULL &&
  545. !(dev->quirks & USB_QUIRK_CONFIG_INTF_STRINGS))
  546. cp->string = usb_cache_string(dev, cp->desc.iConfiguration);
  547. +/* Uncomment this define to enable the HS Electrical Test support */
  548. +#define DWC_HS_ELECT_TST 1
  549. +#ifdef DWC_HS_ELECT_TST
  550. + /* Here we implement the HS Electrical Test support. The
  551. + * tester uses a vendor ID of 0x1A0A to indicate we should
  552. + * run a special test sequence. The product ID tells us
  553. + * which sequence to run. We invoke the test sequence by
  554. + * sending a non-standard SetFeature command to our root
  555. + * hub port. Our dwc_otg_hcd_hub_control() routine will
  556. + * recognize the command and perform the desired test
  557. + * sequence.
  558. + */
  559. + if (dev->descriptor.idVendor == 0x1A0A) {
  560. + /* HSOTG Electrical Test */
  561. + dev_warn(&dev->dev, "VID from HSOTG Electrical Test Fixture\n");
  562. +
  563. + if (dev->bus && dev->bus->root_hub) {
  564. + struct usb_device *hdev = dev->bus->root_hub;
  565. + dev_warn(&dev->dev, "Got PID 0x%x\n", dev->descriptor.idProduct);
  566. +
  567. + switch (dev->descriptor.idProduct) {
  568. + case 0x0101: /* TEST_SE0_NAK */
  569. + dev_warn(&dev->dev, "TEST_SE0_NAK\n");
  570. + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  571. + USB_REQ_SET_FEATURE, USB_RT_PORT,
  572. + USB_PORT_FEAT_TEST, 0x300, NULL, 0, HZ);
  573. + break;
  574. +
  575. + case 0x0102: /* TEST_J */
  576. + dev_warn(&dev->dev, "TEST_J\n");
  577. + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  578. + USB_REQ_SET_FEATURE, USB_RT_PORT,
  579. + USB_PORT_FEAT_TEST, 0x100, NULL, 0, HZ);
  580. + break;
  581. +
  582. + case 0x0103: /* TEST_K */
  583. + dev_warn(&dev->dev, "TEST_K\n");
  584. + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  585. + USB_REQ_SET_FEATURE, USB_RT_PORT,
  586. + USB_PORT_FEAT_TEST, 0x200, NULL, 0, HZ);
  587. + break;
  588. +
  589. + case 0x0104: /* TEST_PACKET */
  590. + dev_warn(&dev->dev, "TEST_PACKET\n");
  591. + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  592. + USB_REQ_SET_FEATURE, USB_RT_PORT,
  593. + USB_PORT_FEAT_TEST, 0x400, NULL, 0, HZ);
  594. + break;
  595. +
  596. + case 0x0105: /* TEST_FORCE_ENABLE */
  597. + dev_warn(&dev->dev, "TEST_FORCE_ENABLE\n");
  598. + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  599. + USB_REQ_SET_FEATURE, USB_RT_PORT,
  600. + USB_PORT_FEAT_TEST, 0x500, NULL, 0, HZ);
  601. + break;
  602. +
  603. + case 0x0106: /* HS_HOST_PORT_SUSPEND_RESUME */
  604. + dev_warn(&dev->dev, "HS_HOST_PORT_SUSPEND_RESUME\n");
  605. + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  606. + USB_REQ_SET_FEATURE, USB_RT_PORT,
  607. + USB_PORT_FEAT_TEST, 0x600, NULL, 0, 40 * HZ);
  608. + break;
  609. +
  610. + case 0x0107: /* SINGLE_STEP_GET_DEVICE_DESCRIPTOR setup */
  611. + dev_warn(&dev->dev, "SINGLE_STEP_GET_DEVICE_DESCRIPTOR setup\n");
  612. + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  613. + USB_REQ_SET_FEATURE, USB_RT_PORT,
  614. + USB_PORT_FEAT_TEST, 0x700, NULL, 0, 40 * HZ);
  615. + break;
  616. +
  617. + case 0x0108: /* SINGLE_STEP_GET_DEVICE_DESCRIPTOR execute */
  618. + dev_warn(&dev->dev, "SINGLE_STEP_GET_DEVICE_DESCRIPTOR execute\n");
  619. + usb_control_msg(hdev, usb_sndctrlpipe(hdev, 0),
  620. + USB_REQ_SET_FEATURE, USB_RT_PORT,
  621. + USB_PORT_FEAT_TEST, 0x800, NULL, 0, 40 * HZ);
  622. + }
  623. + }
  624. + }
  625. +#endif /* DWC_HS_ELECT_TST */
  626. /* Now that the interfaces are installed, re-enable LPM. */
  627. usb_unlocked_enable_lpm(dev);
  628. --- a/drivers/usb/core/otg_whitelist.h
  629. +++ b/drivers/usb/core/otg_whitelist.h
  630. @@ -19,33 +19,82 @@
  631. static struct usb_device_id whitelist_table[] = {
  632. /* hubs are optional in OTG, but very handy ... */
  633. +#define CERT_WITHOUT_HUBS
  634. +#if defined(CERT_WITHOUT_HUBS)
  635. +{ USB_DEVICE( 0x0000, 0x0000 ), }, /* Root HUB Only*/
  636. +#else
  637. { USB_DEVICE_INFO(USB_CLASS_HUB, 0, 0), },
  638. { USB_DEVICE_INFO(USB_CLASS_HUB, 0, 1), },
  639. +{ USB_DEVICE_INFO(USB_CLASS_HUB, 0, 2), },
  640. +#endif
  641. #ifdef CONFIG_USB_PRINTER /* ignoring nonstatic linkage! */
  642. /* FIXME actually, printers are NOT supposed to use device classes;
  643. * they're supposed to use interface classes...
  644. */
  645. -{ USB_DEVICE_INFO(7, 1, 1) },
  646. -{ USB_DEVICE_INFO(7, 1, 2) },
  647. -{ USB_DEVICE_INFO(7, 1, 3) },
  648. +//{ USB_DEVICE_INFO(7, 1, 1) },
  649. +//{ USB_DEVICE_INFO(7, 1, 2) },
  650. +//{ USB_DEVICE_INFO(7, 1, 3) },
  651. #endif
  652. #ifdef CONFIG_USB_NET_CDCETHER
  653. /* Linux-USB CDC Ethernet gadget */
  654. -{ USB_DEVICE(0x0525, 0xa4a1), },
  655. +//{ USB_DEVICE(0x0525, 0xa4a1), },
  656. /* Linux-USB CDC Ethernet + RNDIS gadget */
  657. -{ USB_DEVICE(0x0525, 0xa4a2), },
  658. +//{ USB_DEVICE(0x0525, 0xa4a2), },
  659. #endif
  660. #if IS_ENABLED(CONFIG_USB_TEST)
  661. /* gadget zero, for testing */
  662. -{ USB_DEVICE(0x0525, 0xa4a0), },
  663. +//{ USB_DEVICE(0x0525, 0xa4a0), },
  664. #endif
  665. +/* OPT Tester */
  666. +{ USB_DEVICE( 0x1a0a, 0x0101 ), }, /* TEST_SE0_NAK */
  667. +{ USB_DEVICE( 0x1a0a, 0x0102 ), }, /* Test_J */
  668. +{ USB_DEVICE( 0x1a0a, 0x0103 ), }, /* Test_K */
  669. +{ USB_DEVICE( 0x1a0a, 0x0104 ), }, /* Test_PACKET */
  670. +{ USB_DEVICE( 0x1a0a, 0x0105 ), }, /* Test_FORCE_ENABLE */
  671. +{ USB_DEVICE( 0x1a0a, 0x0106 ), }, /* HS_PORT_SUSPEND_RESUME */
  672. +{ USB_DEVICE( 0x1a0a, 0x0107 ), }, /* SINGLE_STEP_GET_DESCRIPTOR setup */
  673. +{ USB_DEVICE( 0x1a0a, 0x0108 ), }, /* SINGLE_STEP_GET_DESCRIPTOR execute */
  674. +
  675. +/* Sony cameras */
  676. +{ USB_DEVICE_VER(0x054c,0x0010,0x0410, 0x0500), },
  677. +
  678. +/* Memory Devices */
  679. +//{ USB_DEVICE( 0x0781, 0x5150 ), }, /* SanDisk */
  680. +//{ USB_DEVICE( 0x05DC, 0x0080 ), }, /* Lexar */
  681. +//{ USB_DEVICE( 0x4146, 0x9281 ), }, /* IOMEGA */
  682. +//{ USB_DEVICE( 0x067b, 0x2507 ), }, /* Hammer 20GB External HD */
  683. +{ USB_DEVICE( 0x0EA0, 0x2168 ), }, /* Ours Technology Inc. (BUFFALO ClipDrive)*/
  684. +//{ USB_DEVICE( 0x0457, 0x0150 ), }, /* Silicon Integrated Systems Corp. */
  685. +
  686. +/* HP Printers */
  687. +//{ USB_DEVICE( 0x03F0, 0x1102 ), }, /* HP Photosmart 245 */
  688. +//{ USB_DEVICE( 0x03F0, 0x1302 ), }, /* HP Photosmart 370 Series */
  689. +
  690. +/* Speakers */
  691. +//{ USB_DEVICE( 0x0499, 0x3002 ), }, /* YAMAHA YST-MS35D USB Speakers */
  692. +//{ USB_DEVICE( 0x0672, 0x1041 ), }, /* Labtec USB Headset */
  693. +
  694. { } /* Terminating entry */
  695. };
  696. +static inline void report_errors(struct usb_device *dev)
  697. +{
  698. + /* OTG MESSAGE: report errors here, customize to match your product */
  699. + dev_info(&dev->dev, "device Vendor:%04x Product:%04x is not supported\n",
  700. + le16_to_cpu(dev->descriptor.idVendor),
  701. + le16_to_cpu(dev->descriptor.idProduct));
  702. + if (USB_CLASS_HUB == dev->descriptor.bDeviceClass){
  703. + dev_printk(KERN_CRIT, &dev->dev, "Unsupported Hub Topology\n");
  704. + } else {
  705. + dev_printk(KERN_CRIT, &dev->dev, "Attached Device is not Supported\n");
  706. + }
  707. +}
  708. +
  709. +
  710. static int is_targeted(struct usb_device *dev)
  711. {
  712. struct usb_device_id *id = whitelist_table;
  713. @@ -95,16 +144,57 @@ static int is_targeted(struct usb_device
  714. continue;
  715. return 1;
  716. - }
  717. + /* NOTE: can't use usb_match_id() since interface caches
  718. + * aren't set up yet. this is cut/paste from that code.
  719. + */
  720. + for (id = whitelist_table; id->match_flags; id++) {
  721. +#ifdef DEBUG
  722. + dev_dbg(&dev->dev,
  723. + "ID: V:%04x P:%04x DC:%04x SC:%04x PR:%04x \n",
  724. + id->idVendor,
  725. + id->idProduct,
  726. + id->bDeviceClass,
  727. + id->bDeviceSubClass,
  728. + id->bDeviceProtocol);
  729. +#endif
  730. - /* add other match criteria here ... */
  731. + if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
  732. + id->idVendor != le16_to_cpu(dev->descriptor.idVendor))
  733. + continue;
  734. +
  735. + if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
  736. + id->idProduct != le16_to_cpu(dev->descriptor.idProduct))
  737. + continue;
  738. +
  739. + /* No need to test id->bcdDevice_lo != 0, since 0 is never
  740. + greater than any unsigned number. */
  741. + if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
  742. + (id->bcdDevice_lo > le16_to_cpu(dev->descriptor.bcdDevice)))
  743. + continue;
  744. +
  745. + if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
  746. + (id->bcdDevice_hi < le16_to_cpu(dev->descriptor.bcdDevice)))
  747. + continue;
  748. +
  749. + if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
  750. + (id->bDeviceClass != dev->descriptor.bDeviceClass))
  751. + continue;
  752. +
  753. + if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
  754. + (id->bDeviceSubClass != dev->descriptor.bDeviceSubClass))
  755. + continue;
  756. +
  757. + if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
  758. + (id->bDeviceProtocol != dev->descriptor.bDeviceProtocol))
  759. + continue;
  760. + return 1;
  761. + }
  762. + }
  763. - /* OTG MESSAGE: report errors here, customize to match your product */
  764. - dev_err(&dev->dev, "device v%04x p%04x is not supported\n",
  765. - le16_to_cpu(dev->descriptor.idVendor),
  766. - le16_to_cpu(dev->descriptor.idProduct));
  767. + /* add other match criteria here ... */
  768. + report_errors(dev);
  769. return 0;
  770. }
  771. --- /dev/null
  772. +++ b/drivers/usb/gadget/file_storage.c
  773. @@ -0,0 +1,3676 @@
  774. +/*
  775. + * file_storage.c -- File-backed USB Storage Gadget, for USB development
  776. + *
  777. + * Copyright (C) 2003-2008 Alan Stern
  778. + * All rights reserved.
  779. + *
  780. + * Redistribution and use in source and binary forms, with or without
  781. + * modification, are permitted provided that the following conditions
  782. + * are met:
  783. + * 1. Redistributions of source code must retain the above copyright
  784. + * notice, this list of conditions, and the following disclaimer,
  785. + * without modification.
  786. + * 2. Redistributions in binary form must reproduce the above copyright
  787. + * notice, this list of conditions and the following disclaimer in the
  788. + * documentation and/or other materials provided with the distribution.
  789. + * 3. The names of the above-listed copyright holders may not be used
  790. + * to endorse or promote products derived from this software without
  791. + * specific prior written permission.
  792. + *
  793. + * ALTERNATIVELY, this software may be distributed under the terms of the
  794. + * GNU General Public License ("GPL") as published by the Free Software
  795. + * Foundation, either version 2 of that License or (at your option) any
  796. + * later version.
  797. + *
  798. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  799. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  800. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  801. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  802. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  803. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  804. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  805. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  806. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  807. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  808. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  809. + */
  810. +
  811. +
  812. +/*
  813. + * The File-backed Storage Gadget acts as a USB Mass Storage device,
  814. + * appearing to the host as a disk drive or as a CD-ROM drive. In addition
  815. + * to providing an example of a genuinely useful gadget driver for a USB
  816. + * device, it also illustrates a technique of double-buffering for increased
  817. + * throughput. Last but not least, it gives an easy way to probe the
  818. + * behavior of the Mass Storage drivers in a USB host.
  819. + *
  820. + * Backing storage is provided by a regular file or a block device, specified
  821. + * by the "file" module parameter. Access can be limited to read-only by
  822. + * setting the optional "ro" module parameter. (For CD-ROM emulation,
  823. + * access is always read-only.) The gadget will indicate that it has
  824. + * removable media if the optional "removable" module parameter is set.
  825. + *
  826. + * The gadget supports the Control-Bulk (CB), Control-Bulk-Interrupt (CBI),
  827. + * and Bulk-Only (also known as Bulk-Bulk-Bulk or BBB) transports, selected
  828. + * by the optional "transport" module parameter. It also supports the
  829. + * following protocols: RBC (0x01), ATAPI or SFF-8020i (0x02), QIC-157 (0c03),
  830. + * UFI (0x04), SFF-8070i (0x05), and transparent SCSI (0x06), selected by
  831. + * the optional "protocol" module parameter. In addition, the default
  832. + * Vendor ID, Product ID, release number and serial number can be overridden.
  833. + *
  834. + * There is support for multiple logical units (LUNs), each of which has
  835. + * its own backing file. The number of LUNs can be set using the optional
  836. + * "luns" module parameter (anywhere from 1 to 8), and the corresponding
  837. + * files are specified using comma-separated lists for "file" and "ro".
  838. + * The default number of LUNs is taken from the number of "file" elements;
  839. + * it is 1 if "file" is not given. If "removable" is not set then a backing
  840. + * file must be specified for each LUN. If it is set, then an unspecified
  841. + * or empty backing filename means the LUN's medium is not loaded. Ideally
  842. + * each LUN would be settable independently as a disk drive or a CD-ROM
  843. + * drive, but currently all LUNs have to be the same type. The CD-ROM
  844. + * emulation includes a single data track and no audio tracks; hence there
  845. + * need be only one backing file per LUN.
  846. + *
  847. + * Requirements are modest; only a bulk-in and a bulk-out endpoint are
  848. + * needed (an interrupt-out endpoint is also needed for CBI). The memory
  849. + * requirement amounts to two 16K buffers, size configurable by a parameter.
  850. + * Support is included for both full-speed and high-speed operation.
  851. + *
  852. + * Note that the driver is slightly non-portable in that it assumes a
  853. + * single memory/DMA buffer will be useable for bulk-in, bulk-out, and
  854. + * interrupt-in endpoints. With most device controllers this isn't an
  855. + * issue, but there may be some with hardware restrictions that prevent
  856. + * a buffer from being used by more than one endpoint.
  857. + *
  858. + * Module options:
  859. + *
  860. + * file=filename[,filename...]
  861. + * Required if "removable" is not set, names of
  862. + * the files or block devices used for
  863. + * backing storage
  864. + * serial=HHHH... Required serial number (string of hex chars)
  865. + * ro=b[,b...] Default false, booleans for read-only access
  866. + * removable Default false, boolean for removable media
  867. + * luns=N Default N = number of filenames, number of
  868. + * LUNs to support
  869. + * nofua=b[,b...] Default false, booleans for ignore FUA flag
  870. + * in SCSI WRITE(10,12) commands
  871. + * stall Default determined according to the type of
  872. + * USB device controller (usually true),
  873. + * boolean to permit the driver to halt
  874. + * bulk endpoints
  875. + * cdrom Default false, boolean for whether to emulate
  876. + * a CD-ROM drive
  877. + * transport=XXX Default BBB, transport name (CB, CBI, or BBB)
  878. + * protocol=YYY Default SCSI, protocol name (RBC, 8020 or
  879. + * ATAPI, QIC, UFI, 8070, or SCSI;
  880. + * also 1 - 6)
  881. + * vendor=0xVVVV Default 0x0525 (NetChip), USB Vendor ID
  882. + * product=0xPPPP Default 0xa4a5 (FSG), USB Product ID
  883. + * release=0xRRRR Override the USB release number (bcdDevice)
  884. + * buflen=N Default N=16384, buffer size used (will be
  885. + * rounded down to a multiple of
  886. + * PAGE_CACHE_SIZE)
  887. + *
  888. + * If CONFIG_USB_FILE_STORAGE_TEST is not set, only the "file", "serial", "ro",
  889. + * "removable", "luns", "nofua", "stall", and "cdrom" options are available;
  890. + * default values are used for everything else.
  891. + *
  892. + * The pathnames of the backing files and the ro settings are available in
  893. + * the attribute files "file", "nofua", and "ro" in the lun<n> subdirectory of
  894. + * the gadget's sysfs directory. If the "removable" option is set, writing to
  895. + * these files will simulate ejecting/loading the medium (writing an empty
  896. + * line means eject) and adjusting a write-enable tab. Changes to the ro
  897. + * setting are not allowed when the medium is loaded or if CD-ROM emulation
  898. + * is being used.
  899. + *
  900. + * This gadget driver is heavily based on "Gadget Zero" by David Brownell.
  901. + * The driver's SCSI command interface was based on the "Information
  902. + * technology - Small Computer System Interface - 2" document from
  903. + * X3T9.2 Project 375D, Revision 10L, 7-SEP-93, available at
  904. + * <http://www.t10.org/ftp/t10/drafts/s2/s2-r10l.pdf>. The single exception
  905. + * is opcode 0x23 (READ FORMAT CAPACITIES), which was based on the
  906. + * "Universal Serial Bus Mass Storage Class UFI Command Specification"
  907. + * document, Revision 1.0, December 14, 1998, available at
  908. + * <http://www.usb.org/developers/devclass_docs/usbmass-ufi10.pdf>.
  909. + */
  910. +
  911. +
  912. +/*
  913. + * Driver Design
  914. + *
  915. + * The FSG driver is fairly straightforward. There is a main kernel
  916. + * thread that handles most of the work. Interrupt routines field
  917. + * callbacks from the controller driver: bulk- and interrupt-request
  918. + * completion notifications, endpoint-0 events, and disconnect events.
  919. + * Completion events are passed to the main thread by wakeup calls. Many
  920. + * ep0 requests are handled at interrupt time, but SetInterface,
  921. + * SetConfiguration, and device reset requests are forwarded to the
  922. + * thread in the form of "exceptions" using SIGUSR1 signals (since they
  923. + * should interrupt any ongoing file I/O operations).
  924. + *
  925. + * The thread's main routine implements the standard command/data/status
  926. + * parts of a SCSI interaction. It and its subroutines are full of tests
  927. + * for pending signals/exceptions -- all this polling is necessary since
  928. + * the kernel has no setjmp/longjmp equivalents. (Maybe this is an
  929. + * indication that the driver really wants to be running in userspace.)
  930. + * An important point is that so long as the thread is alive it keeps an
  931. + * open reference to the backing file. This will prevent unmounting
  932. + * the backing file's underlying filesystem and could cause problems
  933. + * during system shutdown, for example. To prevent such problems, the
  934. + * thread catches INT, TERM, and KILL signals and converts them into
  935. + * an EXIT exception.
  936. + *
  937. + * In normal operation the main thread is started during the gadget's
  938. + * fsg_bind() callback and stopped during fsg_unbind(). But it can also
  939. + * exit when it receives a signal, and there's no point leaving the
  940. + * gadget running when the thread is dead. So just before the thread
  941. + * exits, it deregisters the gadget driver. This makes things a little
  942. + * tricky: The driver is deregistered at two places, and the exiting
  943. + * thread can indirectly call fsg_unbind() which in turn can tell the
  944. + * thread to exit. The first problem is resolved through the use of the
  945. + * REGISTERED atomic bitflag; the driver will only be deregistered once.
  946. + * The second problem is resolved by having fsg_unbind() check
  947. + * fsg->state; it won't try to stop the thread if the state is already
  948. + * FSG_STATE_TERMINATED.
  949. + *
  950. + * To provide maximum throughput, the driver uses a circular pipeline of
  951. + * buffer heads (struct fsg_buffhd). In principle the pipeline can be
  952. + * arbitrarily long; in practice the benefits don't justify having more
  953. + * than 2 stages (i.e., double buffering). But it helps to think of the
  954. + * pipeline as being a long one. Each buffer head contains a bulk-in and
  955. + * a bulk-out request pointer (since the buffer can be used for both
  956. + * output and input -- directions always are given from the host's
  957. + * point of view) as well as a pointer to the buffer and various state
  958. + * variables.
  959. + *
  960. + * Use of the pipeline follows a simple protocol. There is a variable
  961. + * (fsg->next_buffhd_to_fill) that points to the next buffer head to use.
  962. + * At any time that buffer head may still be in use from an earlier
  963. + * request, so each buffer head has a state variable indicating whether
  964. + * it is EMPTY, FULL, or BUSY. Typical use involves waiting for the
  965. + * buffer head to be EMPTY, filling the buffer either by file I/O or by
  966. + * USB I/O (during which the buffer head is BUSY), and marking the buffer
  967. + * head FULL when the I/O is complete. Then the buffer will be emptied
  968. + * (again possibly by USB I/O, during which it is marked BUSY) and
  969. + * finally marked EMPTY again (possibly by a completion routine).
  970. + *
  971. + * A module parameter tells the driver to avoid stalling the bulk
  972. + * endpoints wherever the transport specification allows. This is
  973. + * necessary for some UDCs like the SuperH, which cannot reliably clear a
  974. + * halt on a bulk endpoint. However, under certain circumstances the
  975. + * Bulk-only specification requires a stall. In such cases the driver
  976. + * will halt the endpoint and set a flag indicating that it should clear
  977. + * the halt in software during the next device reset. Hopefully this
  978. + * will permit everything to work correctly. Furthermore, although the
  979. + * specification allows the bulk-out endpoint to halt when the host sends
  980. + * too much data, implementing this would cause an unavoidable race.
  981. + * The driver will always use the "no-stall" approach for OUT transfers.
  982. + *
  983. + * One subtle point concerns sending status-stage responses for ep0
  984. + * requests. Some of these requests, such as device reset, can involve
  985. + * interrupting an ongoing file I/O operation, which might take an
  986. + * arbitrarily long time. During that delay the host might give up on
  987. + * the original ep0 request and issue a new one. When that happens the
  988. + * driver should not notify the host about completion of the original
  989. + * request, as the host will no longer be waiting for it. So the driver
  990. + * assigns to each ep0 request a unique tag, and it keeps track of the
  991. + * tag value of the request associated with a long-running exception
  992. + * (device-reset, interface-change, or configuration-change). When the
  993. + * exception handler is finished, the status-stage response is submitted
  994. + * only if the current ep0 request tag is equal to the exception request
  995. + * tag. Thus only the most recently received ep0 request will get a
  996. + * status-stage response.
  997. + *
  998. + * Warning: This driver source file is too long. It ought to be split up
  999. + * into a header file plus about 3 separate .c files, to handle the details
  1000. + * of the Gadget, USB Mass Storage, and SCSI protocols.
  1001. + */
  1002. +
  1003. +
  1004. +/* #define VERBOSE_DEBUG */
  1005. +/* #define DUMP_MSGS */
  1006. +
  1007. +
  1008. +#include <linux/blkdev.h>
  1009. +#include <linux/completion.h>
  1010. +#include <linux/dcache.h>
  1011. +#include <linux/delay.h>
  1012. +#include <linux/device.h>
  1013. +#include <linux/fcntl.h>
  1014. +#include <linux/file.h>
  1015. +#include <linux/fs.h>
  1016. +#include <linux/kref.h>
  1017. +#include <linux/kthread.h>
  1018. +#include <linux/limits.h>
  1019. +#include <linux/module.h>
  1020. +#include <linux/rwsem.h>
  1021. +#include <linux/slab.h>
  1022. +#include <linux/spinlock.h>
  1023. +#include <linux/string.h>
  1024. +#include <linux/freezer.h>
  1025. +#include <linux/utsname.h>
  1026. +
  1027. +#include <linux/usb/ch9.h>
  1028. +#include <linux/usb/gadget.h>
  1029. +
  1030. +#include "gadget_chips.h"
  1031. +
  1032. +
  1033. +
  1034. +/*
  1035. + * Kbuild is not very cooperative with respect to linking separately
  1036. + * compiled library objects into one module. So for now we won't use
  1037. + * separate compilation ... ensuring init/exit sections work to shrink
  1038. + * the runtime footprint, and giving us at least some parts of what
  1039. + * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
  1040. + */
  1041. +#include "usbstring.c"
  1042. +#include "config.c"
  1043. +#include "epautoconf.c"
  1044. +
  1045. +/*-------------------------------------------------------------------------*/
  1046. +
  1047. +#define DRIVER_DESC "File-backed Storage Gadget"
  1048. +#define DRIVER_NAME "g_file_storage"
  1049. +#define DRIVER_VERSION "1 September 2010"
  1050. +
  1051. +static char fsg_string_manufacturer[64];
  1052. +static const char fsg_string_product[] = DRIVER_DESC;
  1053. +static const char fsg_string_config[] = "Self-powered";
  1054. +static const char fsg_string_interface[] = "Mass Storage";
  1055. +
  1056. +
  1057. +#include "storage_common.c"
  1058. +
  1059. +
  1060. +MODULE_DESCRIPTION(DRIVER_DESC);
  1061. +MODULE_AUTHOR("Alan Stern");
  1062. +MODULE_LICENSE("Dual BSD/GPL");
  1063. +
  1064. +/*
  1065. + * This driver assumes self-powered hardware and has no way for users to
  1066. + * trigger remote wakeup. It uses autoconfiguration to select endpoints
  1067. + * and endpoint addresses.
  1068. + */
  1069. +
  1070. +
  1071. +/*-------------------------------------------------------------------------*/
  1072. +
  1073. +
  1074. +/* Encapsulate the module parameter settings */
  1075. +
  1076. +static struct {
  1077. + char *file[FSG_MAX_LUNS];
  1078. + char *serial;
  1079. + bool ro[FSG_MAX_LUNS];
  1080. + bool nofua[FSG_MAX_LUNS];
  1081. + unsigned int num_filenames;
  1082. + unsigned int num_ros;
  1083. + unsigned int num_nofuas;
  1084. + unsigned int nluns;
  1085. +
  1086. + bool removable;
  1087. + bool can_stall;
  1088. + bool cdrom;
  1089. +
  1090. + char *transport_parm;
  1091. + char *protocol_parm;
  1092. + unsigned short vendor;
  1093. + unsigned short product;
  1094. + unsigned short release;
  1095. + unsigned int buflen;
  1096. +
  1097. + int transport_type;
  1098. + char *transport_name;
  1099. + int protocol_type;
  1100. + char *protocol_name;
  1101. +
  1102. +} mod_data = { // Default values
  1103. + .transport_parm = "BBB",
  1104. + .protocol_parm = "SCSI",
  1105. + .removable = 0,
  1106. + .can_stall = 1,
  1107. + .cdrom = 0,
  1108. + .vendor = FSG_VENDOR_ID,
  1109. + .product = FSG_PRODUCT_ID,
  1110. + .release = 0xffff, // Use controller chip type
  1111. + .buflen = 16384,
  1112. + };
  1113. +
  1114. +
  1115. +module_param_array_named(file, mod_data.file, charp, &mod_data.num_filenames,
  1116. + S_IRUGO);
  1117. +MODULE_PARM_DESC(file, "names of backing files or devices");
  1118. +
  1119. +module_param_named(serial, mod_data.serial, charp, S_IRUGO);
  1120. +MODULE_PARM_DESC(serial, "USB serial number");
  1121. +
  1122. +module_param_array_named(ro, mod_data.ro, bool, &mod_data.num_ros, S_IRUGO);
  1123. +MODULE_PARM_DESC(ro, "true to force read-only");
  1124. +
  1125. +module_param_array_named(nofua, mod_data.nofua, bool, &mod_data.num_nofuas,
  1126. + S_IRUGO);
  1127. +MODULE_PARM_DESC(nofua, "true to ignore SCSI WRITE(10,12) FUA bit");
  1128. +
  1129. +module_param_named(luns, mod_data.nluns, uint, S_IRUGO);
  1130. +MODULE_PARM_DESC(luns, "number of LUNs");
  1131. +
  1132. +module_param_named(removable, mod_data.removable, bool, S_IRUGO);
  1133. +MODULE_PARM_DESC(removable, "true to simulate removable media");
  1134. +
  1135. +module_param_named(stall, mod_data.can_stall, bool, S_IRUGO);
  1136. +MODULE_PARM_DESC(stall, "false to prevent bulk stalls");
  1137. +
  1138. +module_param_named(cdrom, mod_data.cdrom, bool, S_IRUGO);
  1139. +MODULE_PARM_DESC(cdrom, "true to emulate cdrom instead of disk");
  1140. +
  1141. +/* In the non-TEST version, only the module parameters listed above
  1142. + * are available. */
  1143. +#ifdef CONFIG_USB_FILE_STORAGE_TEST
  1144. +
  1145. +module_param_named(transport, mod_data.transport_parm, charp, S_IRUGO);
  1146. +MODULE_PARM_DESC(transport, "type of transport (BBB, CBI, or CB)");
  1147. +
  1148. +module_param_named(protocol, mod_data.protocol_parm, charp, S_IRUGO);
  1149. +MODULE_PARM_DESC(protocol, "type of protocol (RBC, 8020, QIC, UFI, "
  1150. + "8070, or SCSI)");
  1151. +
  1152. +module_param_named(vendor, mod_data.vendor, ushort, S_IRUGO);
  1153. +MODULE_PARM_DESC(vendor, "USB Vendor ID");
  1154. +
  1155. +module_param_named(product, mod_data.product, ushort, S_IRUGO);
  1156. +MODULE_PARM_DESC(product, "USB Product ID");
  1157. +
  1158. +module_param_named(release, mod_data.release, ushort, S_IRUGO);
  1159. +MODULE_PARM_DESC(release, "USB release number");
  1160. +
  1161. +module_param_named(buflen, mod_data.buflen, uint, S_IRUGO);
  1162. +MODULE_PARM_DESC(buflen, "I/O buffer size");
  1163. +
  1164. +#endif /* CONFIG_USB_FILE_STORAGE_TEST */
  1165. +
  1166. +
  1167. +/*
  1168. + * These definitions will permit the compiler to avoid generating code for
  1169. + * parts of the driver that aren't used in the non-TEST version. Even gcc
  1170. + * can recognize when a test of a constant expression yields a dead code
  1171. + * path.
  1172. + */
  1173. +
  1174. +#ifdef CONFIG_USB_FILE_STORAGE_TEST
  1175. +
  1176. +#define transport_is_bbb() (mod_data.transport_type == USB_PR_BULK)
  1177. +#define transport_is_cbi() (mod_data.transport_type == USB_PR_CBI)
  1178. +#define protocol_is_scsi() (mod_data.protocol_type == USB_SC_SCSI)
  1179. +
  1180. +#else
  1181. +
  1182. +#define transport_is_bbb() 1
  1183. +#define transport_is_cbi() 0
  1184. +#define protocol_is_scsi() 1
  1185. +
  1186. +#endif /* CONFIG_USB_FILE_STORAGE_TEST */
  1187. +
  1188. +
  1189. +/*-------------------------------------------------------------------------*/
  1190. +
  1191. +
  1192. +struct fsg_dev {
  1193. + /* lock protects: state, all the req_busy's, and cbbuf_cmnd */
  1194. + spinlock_t lock;
  1195. + struct usb_gadget *gadget;
  1196. +
  1197. + /* filesem protects: backing files in use */
  1198. + struct rw_semaphore filesem;
  1199. +
  1200. + /* reference counting: wait until all LUNs are released */
  1201. + struct kref ref;
  1202. +
  1203. + struct usb_ep *ep0; // Handy copy of gadget->ep0
  1204. + struct usb_request *ep0req; // For control responses
  1205. + unsigned int ep0_req_tag;
  1206. + const char *ep0req_name;
  1207. +
  1208. + struct usb_request *intreq; // For interrupt responses
  1209. + int intreq_busy;
  1210. + struct fsg_buffhd *intr_buffhd;
  1211. +
  1212. + unsigned int bulk_out_maxpacket;
  1213. + enum fsg_state state; // For exception handling
  1214. + unsigned int exception_req_tag;
  1215. +
  1216. + u8 config, new_config;
  1217. +
  1218. + unsigned int running : 1;
  1219. + unsigned int bulk_in_enabled : 1;
  1220. + unsigned int bulk_out_enabled : 1;
  1221. + unsigned int intr_in_enabled : 1;
  1222. + unsigned int phase_error : 1;
  1223. + unsigned int short_packet_received : 1;
  1224. + unsigned int bad_lun_okay : 1;
  1225. +
  1226. + unsigned long atomic_bitflags;
  1227. +#define REGISTERED 0
  1228. +#define IGNORE_BULK_OUT 1
  1229. +#define SUSPENDED 2
  1230. +
  1231. + struct usb_ep *bulk_in;
  1232. + struct usb_ep *bulk_out;
  1233. + struct usb_ep *intr_in;
  1234. +
  1235. + struct fsg_buffhd *next_buffhd_to_fill;
  1236. + struct fsg_buffhd *next_buffhd_to_drain;
  1237. +
  1238. + int thread_wakeup_needed;
  1239. + struct completion thread_notifier;
  1240. + struct task_struct *thread_task;
  1241. +
  1242. + int cmnd_size;
  1243. + u8 cmnd[MAX_COMMAND_SIZE];
  1244. + enum data_direction data_dir;
  1245. + u32 data_size;
  1246. + u32 data_size_from_cmnd;
  1247. + u32 tag;
  1248. + unsigned int lun;
  1249. + u32 residue;
  1250. + u32 usb_amount_left;
  1251. +
  1252. + /* The CB protocol offers no way for a host to know when a command
  1253. + * has completed. As a result the next command may arrive early,
  1254. + * and we will still have to handle it. For that reason we need
  1255. + * a buffer to store new commands when using CB (or CBI, which
  1256. + * does not oblige a host to wait for command completion either). */
  1257. + int cbbuf_cmnd_size;
  1258. + u8 cbbuf_cmnd[MAX_COMMAND_SIZE];
  1259. +
  1260. + unsigned int nluns;
  1261. + struct fsg_lun *luns;
  1262. + struct fsg_lun *curlun;
  1263. + /* Must be the last entry */
  1264. + struct fsg_buffhd buffhds[];
  1265. +};
  1266. +
  1267. +typedef void (*fsg_routine_t)(struct fsg_dev *);
  1268. +
  1269. +static int exception_in_progress(struct fsg_dev *fsg)
  1270. +{
  1271. + return (fsg->state > FSG_STATE_IDLE);
  1272. +}
  1273. +
  1274. +/* Make bulk-out requests be divisible by the maxpacket size */
  1275. +static void set_bulk_out_req_length(struct fsg_dev *fsg,
  1276. + struct fsg_buffhd *bh, unsigned int length)
  1277. +{
  1278. + unsigned int rem;
  1279. +
  1280. + bh->bulk_out_intended_length = length;
  1281. + rem = length % fsg->bulk_out_maxpacket;
  1282. + if (rem > 0)
  1283. + length += fsg->bulk_out_maxpacket - rem;
  1284. + bh->outreq->length = length;
  1285. +}
  1286. +
  1287. +static struct fsg_dev *the_fsg;
  1288. +static struct usb_gadget_driver fsg_driver;
  1289. +
  1290. +
  1291. +/*-------------------------------------------------------------------------*/
  1292. +
  1293. +static int fsg_set_halt(struct fsg_dev *fsg, struct usb_ep *ep)
  1294. +{
  1295. + const char *name;
  1296. +
  1297. + if (ep == fsg->bulk_in)
  1298. + name = "bulk-in";
  1299. + else if (ep == fsg->bulk_out)
  1300. + name = "bulk-out";
  1301. + else
  1302. + name = ep->name;
  1303. + DBG(fsg, "%s set halt\n", name);
  1304. + return usb_ep_set_halt(ep);
  1305. +}
  1306. +
  1307. +
  1308. +/*-------------------------------------------------------------------------*/
  1309. +
  1310. +/*
  1311. + * DESCRIPTORS ... most are static, but strings and (full) configuration
  1312. + * descriptors are built on demand. Also the (static) config and interface
  1313. + * descriptors are adjusted during fsg_bind().
  1314. + */
  1315. +
  1316. +/* There is only one configuration. */
  1317. +#define CONFIG_VALUE 1
  1318. +
  1319. +static struct usb_device_descriptor
  1320. +device_desc = {
  1321. + .bLength = sizeof device_desc,
  1322. + .bDescriptorType = USB_DT_DEVICE,
  1323. +
  1324. + .bcdUSB = cpu_to_le16(0x0200),
  1325. + .bDeviceClass = USB_CLASS_PER_INTERFACE,
  1326. +
  1327. + /* The next three values can be overridden by module parameters */
  1328. + .idVendor = cpu_to_le16(FSG_VENDOR_ID),
  1329. + .idProduct = cpu_to_le16(FSG_PRODUCT_ID),
  1330. + .bcdDevice = cpu_to_le16(0xffff),
  1331. +
  1332. + .iManufacturer = FSG_STRING_MANUFACTURER,
  1333. + .iProduct = FSG_STRING_PRODUCT,
  1334. + .iSerialNumber = FSG_STRING_SERIAL,
  1335. + .bNumConfigurations = 1,
  1336. +};
  1337. +
  1338. +static struct usb_config_descriptor
  1339. +config_desc = {
  1340. + .bLength = sizeof config_desc,
  1341. + .bDescriptorType = USB_DT_CONFIG,
  1342. +
  1343. + /* wTotalLength computed by usb_gadget_config_buf() */
  1344. + .bNumInterfaces = 1,
  1345. + .bConfigurationValue = CONFIG_VALUE,
  1346. + .iConfiguration = FSG_STRING_CONFIG,
  1347. + .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
  1348. + .bMaxPower = CONFIG_USB_GADGET_VBUS_DRAW / 2,
  1349. +};
  1350. +
  1351. +
  1352. +static struct usb_qualifier_descriptor
  1353. +dev_qualifier = {
  1354. + .bLength = sizeof dev_qualifier,
  1355. + .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
  1356. +
  1357. + .bcdUSB = cpu_to_le16(0x0200),
  1358. + .bDeviceClass = USB_CLASS_PER_INTERFACE,
  1359. +
  1360. + .bNumConfigurations = 1,
  1361. +};
  1362. +
  1363. +static int populate_bos(struct fsg_dev *fsg, u8 *buf)
  1364. +{
  1365. + memcpy(buf, &fsg_bos_desc, USB_DT_BOS_SIZE);
  1366. + buf += USB_DT_BOS_SIZE;
  1367. +
  1368. + memcpy(buf, &fsg_ext_cap_desc, USB_DT_USB_EXT_CAP_SIZE);
  1369. + buf += USB_DT_USB_EXT_CAP_SIZE;
  1370. +
  1371. + memcpy(buf, &fsg_ss_cap_desc, USB_DT_USB_SS_CAP_SIZE);
  1372. +
  1373. + return USB_DT_BOS_SIZE + USB_DT_USB_SS_CAP_SIZE
  1374. + + USB_DT_USB_EXT_CAP_SIZE;
  1375. +}
  1376. +
  1377. +/*
  1378. + * Config descriptors must agree with the code that sets configurations
  1379. + * and with code managing interfaces and their altsettings. They must
  1380. + * also handle different speeds and other-speed requests.
  1381. + */
  1382. +static int populate_config_buf(struct usb_gadget *gadget,
  1383. + u8 *buf, u8 type, unsigned index)
  1384. +{
  1385. + enum usb_device_speed speed = gadget->speed;
  1386. + int len;
  1387. + const struct usb_descriptor_header **function;
  1388. +
  1389. + if (index > 0)
  1390. + return -EINVAL;
  1391. +
  1392. + if (gadget_is_dualspeed(gadget) && type == USB_DT_OTHER_SPEED_CONFIG)
  1393. + speed = (USB_SPEED_FULL + USB_SPEED_HIGH) - speed;
  1394. + function = gadget_is_dualspeed(gadget) && speed == USB_SPEED_HIGH
  1395. + ? (const struct usb_descriptor_header **)fsg_hs_function
  1396. + : (const struct usb_descriptor_header **)fsg_fs_function;
  1397. +
  1398. + /* for now, don't advertise srp-only devices */
  1399. + if (!gadget_is_otg(gadget))
  1400. + function++;
  1401. +
  1402. + len = usb_gadget_config_buf(&config_desc, buf, EP0_BUFSIZE, function);
  1403. + ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
  1404. + return len;
  1405. +}
  1406. +
  1407. +
  1408. +/*-------------------------------------------------------------------------*/
  1409. +
  1410. +/* These routines may be called in process context or in_irq */
  1411. +
  1412. +/* Caller must hold fsg->lock */
  1413. +static void wakeup_thread(struct fsg_dev *fsg)
  1414. +{
  1415. + /* Tell the main thread that something has happened */
  1416. + fsg->thread_wakeup_needed = 1;
  1417. + if (fsg->thread_task)
  1418. + wake_up_process(fsg->thread_task);
  1419. +}
  1420. +
  1421. +
  1422. +static void raise_exception(struct fsg_dev *fsg, enum fsg_state new_state)
  1423. +{
  1424. + unsigned long flags;
  1425. +
  1426. + /* Do nothing if a higher-priority exception is already in progress.
  1427. + * If a lower-or-equal priority exception is in progress, preempt it
  1428. + * and notify the main thread by sending it a signal. */
  1429. + spin_lock_irqsave(&fsg->lock, flags);
  1430. + if (fsg->state <= new_state) {
  1431. + fsg->exception_req_tag = fsg->ep0_req_tag;
  1432. + fsg->state = new_state;
  1433. + if (fsg->thread_task)
  1434. + send_sig_info(SIGUSR1, SEND_SIG_FORCED,
  1435. + fsg->thread_task);
  1436. + }
  1437. + spin_unlock_irqrestore(&fsg->lock, flags);
  1438. +}
  1439. +
  1440. +
  1441. +/*-------------------------------------------------------------------------*/
  1442. +
  1443. +/* The disconnect callback and ep0 routines. These always run in_irq,
  1444. + * except that ep0_queue() is called in the main thread to acknowledge
  1445. + * completion of various requests: set config, set interface, and
  1446. + * Bulk-only device reset. */
  1447. +
  1448. +static void fsg_disconnect(struct usb_gadget *gadget)
  1449. +{
  1450. + struct fsg_dev *fsg = get_gadget_data(gadget);
  1451. +
  1452. + DBG(fsg, "disconnect or port reset\n");
  1453. + raise_exception(fsg, FSG_STATE_DISCONNECT);
  1454. +}
  1455. +
  1456. +
  1457. +static int ep0_queue(struct fsg_dev *fsg)
  1458. +{
  1459. + int rc;
  1460. +
  1461. + rc = usb_ep_queue(fsg->ep0, fsg->ep0req, GFP_ATOMIC);
  1462. + if (rc != 0 && rc != -ESHUTDOWN) {
  1463. +
  1464. + /* We can't do much more than wait for a reset */
  1465. + WARNING(fsg, "error in submission: %s --> %d\n",
  1466. + fsg->ep0->name, rc);
  1467. + }
  1468. + return rc;
  1469. +}
  1470. +
  1471. +static void ep0_complete(struct usb_ep *ep, struct usb_request *req)
  1472. +{
  1473. + struct fsg_dev *fsg = ep->driver_data;
  1474. +
  1475. + if (req->actual > 0)
  1476. + dump_msg(fsg, fsg->ep0req_name, req->buf, req->actual);
  1477. + if (req->status || req->actual != req->length)
  1478. + DBG(fsg, "%s --> %d, %u/%u\n", __func__,
  1479. + req->status, req->actual, req->length);
  1480. + if (req->status == -ECONNRESET) // Request was cancelled
  1481. + usb_ep_fifo_flush(ep);
  1482. +
  1483. + if (req->status == 0 && req->context)
  1484. + ((fsg_routine_t) (req->context))(fsg);
  1485. +}
  1486. +
  1487. +
  1488. +/*-------------------------------------------------------------------------*/
  1489. +
  1490. +/* Bulk and interrupt endpoint completion handlers.
  1491. + * These always run in_irq. */
  1492. +
  1493. +static void bulk_in_complete(struct usb_ep *ep, struct usb_request *req)
  1494. +{
  1495. + struct fsg_dev *fsg = ep->driver_data;
  1496. + struct fsg_buffhd *bh = req->context;
  1497. +
  1498. + if (req->status || req->actual != req->length)
  1499. + DBG(fsg, "%s --> %d, %u/%u\n", __func__,
  1500. + req->status, req->actual, req->length);
  1501. + if (req->status == -ECONNRESET) // Request was cancelled
  1502. + usb_ep_fifo_flush(ep);
  1503. +
  1504. + /* Hold the lock while we update the request and buffer states */
  1505. + smp_wmb();
  1506. + spin_lock(&fsg->lock);
  1507. + bh->inreq_busy = 0;
  1508. + bh->state = BUF_STATE_EMPTY;
  1509. + wakeup_thread(fsg);
  1510. + spin_unlock(&fsg->lock);
  1511. +}
  1512. +
  1513. +static void bulk_out_complete(struct usb_ep *ep, struct usb_request *req)
  1514. +{
  1515. + struct fsg_dev *fsg = ep->driver_data;
  1516. + struct fsg_buffhd *bh = req->context;
  1517. +
  1518. + dump_msg(fsg, "bulk-out", req->buf, req->actual);
  1519. + if (req->status || req->actual != bh->bulk_out_intended_length)
  1520. + DBG(fsg, "%s --> %d, %u/%u\n", __func__,
  1521. + req->status, req->actual,
  1522. + bh->bulk_out_intended_length);
  1523. + if (req->status == -ECONNRESET) // Request was cancelled
  1524. + usb_ep_fifo_flush(ep);
  1525. +
  1526. + /* Hold the lock while we update the request and buffer states */
  1527. + smp_wmb();
  1528. + spin_lock(&fsg->lock);
  1529. + bh->outreq_busy = 0;
  1530. + bh->state = BUF_STATE_FULL;
  1531. + wakeup_thread(fsg);
  1532. + spin_unlock(&fsg->lock);
  1533. +}
  1534. +
  1535. +
  1536. +#ifdef CONFIG_USB_FILE_STORAGE_TEST
  1537. +static void intr_in_complete(struct usb_ep *ep, struct usb_request *req)
  1538. +{
  1539. + struct fsg_dev *fsg = ep->driver_data;
  1540. + struct fsg_buffhd *bh = req->context;
  1541. +
  1542. + if (req->status || req->actual != req->length)
  1543. + DBG(fsg, "%s --> %d, %u/%u\n", __func__,
  1544. + req->status, req->actual, req->length);
  1545. + if (req->status == -ECONNRESET) // Request was cancelled
  1546. + usb_ep_fifo_flush(ep);
  1547. +
  1548. + /* Hold the lock while we update the request and buffer states */
  1549. + smp_wmb();
  1550. + spin_lock(&fsg->lock);
  1551. + fsg->intreq_busy = 0;
  1552. + bh->state = BUF_STATE_EMPTY;
  1553. + wakeup_thread(fsg);
  1554. + spin_unlock(&fsg->lock);
  1555. +}
  1556. +
  1557. +#else
  1558. +static void intr_in_complete(struct usb_ep *ep, struct usb_request *req)
  1559. +{}
  1560. +#endif /* CONFIG_USB_FILE_STORAGE_TEST */
  1561. +
  1562. +
  1563. +/*-------------------------------------------------------------------------*/
  1564. +
  1565. +/* Ep0 class-specific handlers. These always run in_irq. */
  1566. +
  1567. +#ifdef CONFIG_USB_FILE_STORAGE_TEST
  1568. +static void received_cbi_adsc(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  1569. +{
  1570. + struct usb_request *req = fsg->ep0req;
  1571. + static u8 cbi_reset_cmnd[6] = {
  1572. + SEND_DIAGNOSTIC, 4, 0xff, 0xff, 0xff, 0xff};
  1573. +
  1574. + /* Error in command transfer? */
  1575. + if (req->status || req->length != req->actual ||
  1576. + req->actual < 6 || req->actual > MAX_COMMAND_SIZE) {
  1577. +
  1578. + /* Not all controllers allow a protocol stall after
  1579. + * receiving control-out data, but we'll try anyway. */
  1580. + fsg_set_halt(fsg, fsg->ep0);
  1581. + return; // Wait for reset
  1582. + }
  1583. +
  1584. + /* Is it the special reset command? */
  1585. + if (req->actual >= sizeof cbi_reset_cmnd &&
  1586. + memcmp(req->buf, cbi_reset_cmnd,
  1587. + sizeof cbi_reset_cmnd) == 0) {
  1588. +
  1589. + /* Raise an exception to stop the current operation
  1590. + * and reinitialize our state. */
  1591. + DBG(fsg, "cbi reset request\n");
  1592. + raise_exception(fsg, FSG_STATE_RESET);
  1593. + return;
  1594. + }
  1595. +
  1596. + VDBG(fsg, "CB[I] accept device-specific command\n");
  1597. + spin_lock(&fsg->lock);
  1598. +
  1599. + /* Save the command for later */
  1600. + if (fsg->cbbuf_cmnd_size)
  1601. + WARNING(fsg, "CB[I] overwriting previous command\n");
  1602. + fsg->cbbuf_cmnd_size = req->actual;
  1603. + memcpy(fsg->cbbuf_cmnd, req->buf, fsg->cbbuf_cmnd_size);
  1604. +
  1605. + wakeup_thread(fsg);
  1606. + spin_unlock(&fsg->lock);
  1607. +}
  1608. +
  1609. +#else
  1610. +static void received_cbi_adsc(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  1611. +{}
  1612. +#endif /* CONFIG_USB_FILE_STORAGE_TEST */
  1613. +
  1614. +
  1615. +static int class_setup_req(struct fsg_dev *fsg,
  1616. + const struct usb_ctrlrequest *ctrl)
  1617. +{
  1618. + struct usb_request *req = fsg->ep0req;
  1619. + int value = -EOPNOTSUPP;
  1620. + u16 w_index = le16_to_cpu(ctrl->wIndex);
  1621. + u16 w_value = le16_to_cpu(ctrl->wValue);
  1622. + u16 w_length = le16_to_cpu(ctrl->wLength);
  1623. +
  1624. + if (!fsg->config)
  1625. + return value;
  1626. +
  1627. + /* Handle Bulk-only class-specific requests */
  1628. + if (transport_is_bbb()) {
  1629. + switch (ctrl->bRequest) {
  1630. +
  1631. + case US_BULK_RESET_REQUEST:
  1632. + if (ctrl->bRequestType != (USB_DIR_OUT |
  1633. + USB_TYPE_CLASS | USB_RECIP_INTERFACE))
  1634. + break;
  1635. + if (w_index != 0 || w_value != 0 || w_length != 0) {
  1636. + value = -EDOM;
  1637. + break;
  1638. + }
  1639. +
  1640. + /* Raise an exception to stop the current operation
  1641. + * and reinitialize our state. */
  1642. + DBG(fsg, "bulk reset request\n");
  1643. + raise_exception(fsg, FSG_STATE_RESET);
  1644. + value = DELAYED_STATUS;
  1645. + break;
  1646. +
  1647. + case US_BULK_GET_MAX_LUN:
  1648. + if (ctrl->bRequestType != (USB_DIR_IN |
  1649. + USB_TYPE_CLASS | USB_RECIP_INTERFACE))
  1650. + break;
  1651. + if (w_index != 0 || w_value != 0 || w_length != 1) {
  1652. + value = -EDOM;
  1653. + break;
  1654. + }
  1655. + VDBG(fsg, "get max LUN\n");
  1656. + *(u8 *) req->buf = fsg->nluns - 1;
  1657. + value = 1;
  1658. + break;
  1659. + }
  1660. + }
  1661. +
  1662. + /* Handle CBI class-specific requests */
  1663. + else {
  1664. + switch (ctrl->bRequest) {
  1665. +
  1666. + case USB_CBI_ADSC_REQUEST:
  1667. + if (ctrl->bRequestType != (USB_DIR_OUT |
  1668. + USB_TYPE_CLASS | USB_RECIP_INTERFACE))
  1669. + break;
  1670. + if (w_index != 0 || w_value != 0) {
  1671. + value = -EDOM;
  1672. + break;
  1673. + }
  1674. + if (w_length > MAX_COMMAND_SIZE) {
  1675. + value = -EOVERFLOW;
  1676. + break;
  1677. + }
  1678. + value = w_length;
  1679. + fsg->ep0req->context = received_cbi_adsc;
  1680. + break;
  1681. + }
  1682. + }
  1683. +
  1684. + if (value == -EOPNOTSUPP)
  1685. + VDBG(fsg,
  1686. + "unknown class-specific control req "
  1687. + "%02x.%02x v%04x i%04x l%u\n",
  1688. + ctrl->bRequestType, ctrl->bRequest,
  1689. + le16_to_cpu(ctrl->wValue), w_index, w_length);
  1690. + return value;
  1691. +}
  1692. +
  1693. +
  1694. +/*-------------------------------------------------------------------------*/
  1695. +
  1696. +/* Ep0 standard request handlers. These always run in_irq. */
  1697. +
  1698. +static int standard_setup_req(struct fsg_dev *fsg,
  1699. + const struct usb_ctrlrequest *ctrl)
  1700. +{
  1701. + struct usb_request *req = fsg->ep0req;
  1702. + int value = -EOPNOTSUPP;
  1703. + u16 w_index = le16_to_cpu(ctrl->wIndex);
  1704. + u16 w_value = le16_to_cpu(ctrl->wValue);
  1705. +
  1706. + /* Usually this just stores reply data in the pre-allocated ep0 buffer,
  1707. + * but config change events will also reconfigure hardware. */
  1708. + switch (ctrl->bRequest) {
  1709. +
  1710. + case USB_REQ_GET_DESCRIPTOR:
  1711. + if (ctrl->bRequestType != (USB_DIR_IN | USB_TYPE_STANDARD |
  1712. + USB_RECIP_DEVICE))
  1713. + break;
  1714. + switch (w_value >> 8) {
  1715. +
  1716. + case USB_DT_DEVICE:
  1717. + VDBG(fsg, "get device descriptor\n");
  1718. + device_desc.bMaxPacketSize0 = fsg->ep0->maxpacket;
  1719. + value = sizeof device_desc;
  1720. + memcpy(req->buf, &device_desc, value);
  1721. + break;
  1722. + case USB_DT_DEVICE_QUALIFIER:
  1723. + VDBG(fsg, "get device qualifier\n");
  1724. + if (!gadget_is_dualspeed(fsg->gadget) ||
  1725. + fsg->gadget->speed == USB_SPEED_SUPER)
  1726. + break;
  1727. + /*
  1728. + * Assume ep0 uses the same maxpacket value for both
  1729. + * speeds
  1730. + */
  1731. + dev_qualifier.bMaxPacketSize0 = fsg->ep0->maxpacket;
  1732. + value = sizeof dev_qualifier;
  1733. + memcpy(req->buf, &dev_qualifier, value);
  1734. + break;
  1735. +
  1736. + case USB_DT_OTHER_SPEED_CONFIG:
  1737. + VDBG(fsg, "get other-speed config descriptor\n");
  1738. + if (!gadget_is_dualspeed(fsg->gadget) ||
  1739. + fsg->gadget->speed == USB_SPEED_SUPER)
  1740. + break;
  1741. + goto get_config;
  1742. + case USB_DT_CONFIG:
  1743. + VDBG(fsg, "get configuration descriptor\n");
  1744. +get_config:
  1745. + value = populate_config_buf(fsg->gadget,
  1746. + req->buf,
  1747. + w_value >> 8,
  1748. + w_value & 0xff);
  1749. + break;
  1750. +
  1751. + case USB_DT_STRING:
  1752. + VDBG(fsg, "get string descriptor\n");
  1753. +
  1754. + /* wIndex == language code */
  1755. + value = usb_gadget_get_string(&fsg_stringtab,
  1756. + w_value & 0xff, req->buf);
  1757. + break;
  1758. +
  1759. + case USB_DT_BOS:
  1760. + VDBG(fsg, "get bos descriptor\n");
  1761. +
  1762. + if (gadget_is_superspeed(fsg->gadget))
  1763. + value = populate_bos(fsg, req->buf);
  1764. + break;
  1765. + }
  1766. +
  1767. + break;
  1768. +
  1769. + /* One config, two speeds */
  1770. + case USB_REQ_SET_CONFIGURATION:
  1771. + if (ctrl->bRequestType != (USB_DIR_OUT | USB_TYPE_STANDARD |
  1772. + USB_RECIP_DEVICE))
  1773. + break;
  1774. + VDBG(fsg, "set configuration\n");
  1775. + if (w_value == CONFIG_VALUE || w_value == 0) {
  1776. + fsg->new_config = w_value;
  1777. +
  1778. + /* Raise an exception to wipe out previous transaction
  1779. + * state (queued bufs, etc) and set the new config. */
  1780. + raise_exception(fsg, FSG_STATE_CONFIG_CHANGE);
  1781. + value = DELAYED_STATUS;
  1782. + }
  1783. + break;
  1784. + case USB_REQ_GET_CONFIGURATION:
  1785. + if (ctrl->bRequestType != (USB_DIR_IN | USB_TYPE_STANDARD |
  1786. + USB_RECIP_DEVICE))
  1787. + break;
  1788. + VDBG(fsg, "get configuration\n");
  1789. + *(u8 *) req->buf = fsg->config;
  1790. + value = 1;
  1791. + break;
  1792. +
  1793. + case USB_REQ_SET_INTERFACE:
  1794. + if (ctrl->bRequestType != (USB_DIR_OUT| USB_TYPE_STANDARD |
  1795. + USB_RECIP_INTERFACE))
  1796. + break;
  1797. + if (fsg->config && w_index == 0) {
  1798. +
  1799. + /* Raise an exception to wipe out previous transaction
  1800. + * state (queued bufs, etc) and install the new
  1801. + * interface altsetting. */
  1802. + raise_exception(fsg, FSG_STATE_INTERFACE_CHANGE);
  1803. + value = DELAYED_STATUS;
  1804. + }
  1805. + break;
  1806. + case USB_REQ_GET_INTERFACE:
  1807. + if (ctrl->bRequestType != (USB_DIR_IN | USB_TYPE_STANDARD |
  1808. + USB_RECIP_INTERFACE))
  1809. + break;
  1810. + if (!fsg->config)
  1811. + break;
  1812. + if (w_index != 0) {
  1813. + value = -EDOM;
  1814. + break;
  1815. + }
  1816. + VDBG(fsg, "get interface\n");
  1817. + *(u8 *) req->buf = 0;
  1818. + value = 1;
  1819. + break;
  1820. +
  1821. + default:
  1822. + VDBG(fsg,
  1823. + "unknown control req %02x.%02x v%04x i%04x l%u\n",
  1824. + ctrl->bRequestType, ctrl->bRequest,
  1825. + w_value, w_index, le16_to_cpu(ctrl->wLength));
  1826. + }
  1827. +
  1828. + return value;
  1829. +}
  1830. +
  1831. +
  1832. +static int fsg_setup(struct usb_gadget *gadget,
  1833. + const struct usb_ctrlrequest *ctrl)
  1834. +{
  1835. + struct fsg_dev *fsg = get_gadget_data(gadget);
  1836. + int rc;
  1837. + int w_length = le16_to_cpu(ctrl->wLength);
  1838. +
  1839. + ++fsg->ep0_req_tag; // Record arrival of a new request
  1840. + fsg->ep0req->context = NULL;
  1841. + fsg->ep0req->length = 0;
  1842. + dump_msg(fsg, "ep0-setup", (u8 *) ctrl, sizeof(*ctrl));
  1843. +
  1844. + if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_CLASS)
  1845. + rc = class_setup_req(fsg, ctrl);
  1846. + else
  1847. + rc = standard_setup_req(fsg, ctrl);
  1848. +
  1849. + /* Respond with data/status or defer until later? */
  1850. + if (rc >= 0 && rc != DELAYED_STATUS) {
  1851. + rc = min(rc, w_length);
  1852. + fsg->ep0req->length = rc;
  1853. + fsg->ep0req->zero = rc < w_length;
  1854. + fsg->ep0req_name = (ctrl->bRequestType & USB_DIR_IN ?
  1855. + "ep0-in" : "ep0-out");
  1856. + rc = ep0_queue(fsg);
  1857. + }
  1858. +
  1859. + /* Device either stalls (rc < 0) or reports success */
  1860. + return rc;
  1861. +}
  1862. +
  1863. +
  1864. +/*-------------------------------------------------------------------------*/
  1865. +
  1866. +/* All the following routines run in process context */
  1867. +
  1868. +
  1869. +/* Use this for bulk or interrupt transfers, not ep0 */
  1870. +static void start_transfer(struct fsg_dev *fsg, struct usb_ep *ep,
  1871. + struct usb_request *req, int *pbusy,
  1872. + enum fsg_buffer_state *state)
  1873. +{
  1874. + int rc;
  1875. +
  1876. + if (ep == fsg->bulk_in)
  1877. + dump_msg(fsg, "bulk-in", req->buf, req->length);
  1878. + else if (ep == fsg->intr_in)
  1879. + dump_msg(fsg, "intr-in", req->buf, req->length);
  1880. +
  1881. + spin_lock_irq(&fsg->lock);
  1882. + *pbusy = 1;
  1883. + *state = BUF_STATE_BUSY;
  1884. + spin_unlock_irq(&fsg->lock);
  1885. + rc = usb_ep_queue(ep, req, GFP_KERNEL);
  1886. + if (rc != 0) {
  1887. + *pbusy = 0;
  1888. + *state = BUF_STATE_EMPTY;
  1889. +
  1890. + /* We can't do much more than wait for a reset */
  1891. +
  1892. + /* Note: currently the net2280 driver fails zero-length
  1893. + * submissions if DMA is enabled. */
  1894. + if (rc != -ESHUTDOWN && !(rc == -EOPNOTSUPP &&
  1895. + req->length == 0))
  1896. + WARNING(fsg, "error in submission: %s --> %d\n",
  1897. + ep->name, rc);
  1898. + }
  1899. +}
  1900. +
  1901. +
  1902. +static int sleep_thread(struct fsg_dev *fsg)
  1903. +{
  1904. + int rc = 0;
  1905. +
  1906. + /* Wait until a signal arrives or we are woken up */
  1907. + for (;;) {
  1908. + try_to_freeze();
  1909. + set_current_state(TASK_INTERRUPTIBLE);
  1910. + if (signal_pending(current)) {
  1911. + rc = -EINTR;
  1912. + break;
  1913. + }
  1914. + if (fsg->thread_wakeup_needed)
  1915. + break;
  1916. + schedule();
  1917. + }
  1918. + __set_current_state(TASK_RUNNING);
  1919. + fsg->thread_wakeup_needed = 0;
  1920. + return rc;
  1921. +}
  1922. +
  1923. +
  1924. +/*-------------------------------------------------------------------------*/
  1925. +
  1926. +static int do_read(struct fsg_dev *fsg)
  1927. +{
  1928. + struct fsg_lun *curlun = fsg->curlun;
  1929. + u32 lba;
  1930. + struct fsg_buffhd *bh;
  1931. + int rc;
  1932. + u32 amount_left;
  1933. + loff_t file_offset, file_offset_tmp;
  1934. + unsigned int amount;
  1935. + ssize_t nread;
  1936. +
  1937. + /* Get the starting Logical Block Address and check that it's
  1938. + * not too big */
  1939. + if (fsg->cmnd[0] == READ_6)
  1940. + lba = get_unaligned_be24(&fsg->cmnd[1]);
  1941. + else {
  1942. + lba = get_unaligned_be32(&fsg->cmnd[2]);
  1943. +
  1944. + /* We allow DPO (Disable Page Out = don't save data in the
  1945. + * cache) and FUA (Force Unit Access = don't read from the
  1946. + * cache), but we don't implement them. */
  1947. + if ((fsg->cmnd[1] & ~0x18) != 0) {
  1948. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  1949. + return -EINVAL;
  1950. + }
  1951. + }
  1952. + if (lba >= curlun->num_sectors) {
  1953. + curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  1954. + return -EINVAL;
  1955. + }
  1956. + file_offset = ((loff_t) lba) << curlun->blkbits;
  1957. +
  1958. + /* Carry out the file reads */
  1959. + amount_left = fsg->data_size_from_cmnd;
  1960. + if (unlikely(amount_left == 0))
  1961. + return -EIO; // No default reply
  1962. +
  1963. + for (;;) {
  1964. +
  1965. + /* Figure out how much we need to read:
  1966. + * Try to read the remaining amount.
  1967. + * But don't read more than the buffer size.
  1968. + * And don't try to read past the end of the file.
  1969. + */
  1970. + amount = min((unsigned int) amount_left, mod_data.buflen);
  1971. + amount = min((loff_t) amount,
  1972. + curlun->file_length - file_offset);
  1973. +
  1974. + /* Wait for the next buffer to become available */
  1975. + bh = fsg->next_buffhd_to_fill;
  1976. + while (bh->state != BUF_STATE_EMPTY) {
  1977. + rc = sleep_thread(fsg);
  1978. + if (rc)
  1979. + return rc;
  1980. + }
  1981. +
  1982. + /* If we were asked to read past the end of file,
  1983. + * end with an empty buffer. */
  1984. + if (amount == 0) {
  1985. + curlun->sense_data =
  1986. + SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  1987. + curlun->sense_data_info = file_offset >> curlun->blkbits;
  1988. + curlun->info_valid = 1;
  1989. + bh->inreq->length = 0;
  1990. + bh->state = BUF_STATE_FULL;
  1991. + break;
  1992. + }
  1993. +
  1994. + /* Perform the read */
  1995. + file_offset_tmp = file_offset;
  1996. + nread = vfs_read(curlun->filp,
  1997. + (char __user *) bh->buf,
  1998. + amount, &file_offset_tmp);
  1999. + VLDBG(curlun, "file read %u @ %llu -> %d\n", amount,
  2000. + (unsigned long long) file_offset,
  2001. + (int) nread);
  2002. + if (signal_pending(current))
  2003. + return -EINTR;
  2004. +
  2005. + if (nread < 0) {
  2006. + LDBG(curlun, "error in file read: %d\n",
  2007. + (int) nread);
  2008. + nread = 0;
  2009. + } else if (nread < amount) {
  2010. + LDBG(curlun, "partial file read: %d/%u\n",
  2011. + (int) nread, amount);
  2012. + nread = round_down(nread, curlun->blksize);
  2013. + }
  2014. + file_offset += nread;
  2015. + amount_left -= nread;
  2016. + fsg->residue -= nread;
  2017. +
  2018. + /* Except at the end of the transfer, nread will be
  2019. + * equal to the buffer size, which is divisible by the
  2020. + * bulk-in maxpacket size.
  2021. + */
  2022. + bh->inreq->length = nread;
  2023. + bh->state = BUF_STATE_FULL;
  2024. +
  2025. + /* If an error occurred, report it and its position */
  2026. + if (nread < amount) {
  2027. + curlun->sense_data = SS_UNRECOVERED_READ_ERROR;
  2028. + curlun->sense_data_info = file_offset >> curlun->blkbits;
  2029. + curlun->info_valid = 1;
  2030. + break;
  2031. + }
  2032. +
  2033. + if (amount_left == 0)
  2034. + break; // No more left to read
  2035. +
  2036. + /* Send this buffer and go read some more */
  2037. + bh->inreq->zero = 0;
  2038. + start_transfer(fsg, fsg->bulk_in, bh->inreq,
  2039. + &bh->inreq_busy, &bh->state);
  2040. + fsg->next_buffhd_to_fill = bh->next;
  2041. + }
  2042. +
  2043. + return -EIO; // No default reply
  2044. +}
  2045. +
  2046. +
  2047. +/*-------------------------------------------------------------------------*/
  2048. +
  2049. +static int do_write(struct fsg_dev *fsg)
  2050. +{
  2051. + struct fsg_lun *curlun = fsg->curlun;
  2052. + u32 lba;
  2053. + struct fsg_buffhd *bh;
  2054. + int get_some_more;
  2055. + u32 amount_left_to_req, amount_left_to_write;
  2056. + loff_t usb_offset, file_offset, file_offset_tmp;
  2057. + unsigned int amount;
  2058. + ssize_t nwritten;
  2059. + int rc;
  2060. +
  2061. + if (curlun->ro) {
  2062. + curlun->sense_data = SS_WRITE_PROTECTED;
  2063. + return -EINVAL;
  2064. + }
  2065. + spin_lock(&curlun->filp->f_lock);
  2066. + curlun->filp->f_flags &= ~O_SYNC; // Default is not to wait
  2067. + spin_unlock(&curlun->filp->f_lock);
  2068. +
  2069. + /* Get the starting Logical Block Address and check that it's
  2070. + * not too big */
  2071. + if (fsg->cmnd[0] == WRITE_6)
  2072. + lba = get_unaligned_be24(&fsg->cmnd[1]);
  2073. + else {
  2074. + lba = get_unaligned_be32(&fsg->cmnd[2]);
  2075. +
  2076. + /* We allow DPO (Disable Page Out = don't save data in the
  2077. + * cache) and FUA (Force Unit Access = write directly to the
  2078. + * medium). We don't implement DPO; we implement FUA by
  2079. + * performing synchronous output. */
  2080. + if ((fsg->cmnd[1] & ~0x18) != 0) {
  2081. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  2082. + return -EINVAL;
  2083. + }
  2084. + /* FUA */
  2085. + if (!curlun->nofua && (fsg->cmnd[1] & 0x08)) {
  2086. + spin_lock(&curlun->filp->f_lock);
  2087. + curlun->filp->f_flags |= O_DSYNC;
  2088. + spin_unlock(&curlun->filp->f_lock);
  2089. + }
  2090. + }
  2091. + if (lba >= curlun->num_sectors) {
  2092. + curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  2093. + return -EINVAL;
  2094. + }
  2095. +
  2096. + /* Carry out the file writes */
  2097. + get_some_more = 1;
  2098. + file_offset = usb_offset = ((loff_t) lba) << curlun->blkbits;
  2099. + amount_left_to_req = amount_left_to_write = fsg->data_size_from_cmnd;
  2100. +
  2101. + while (amount_left_to_write > 0) {
  2102. +
  2103. + /* Queue a request for more data from the host */
  2104. + bh = fsg->next_buffhd_to_fill;
  2105. + if (bh->state == BUF_STATE_EMPTY && get_some_more) {
  2106. +
  2107. + /* Figure out how much we want to get:
  2108. + * Try to get the remaining amount,
  2109. + * but not more than the buffer size.
  2110. + */
  2111. + amount = min(amount_left_to_req, mod_data.buflen);
  2112. +
  2113. + /* Beyond the end of the backing file? */
  2114. + if (usb_offset >= curlun->file_length) {
  2115. + get_some_more = 0;
  2116. + curlun->sense_data =
  2117. + SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  2118. + curlun->sense_data_info = usb_offset >> curlun->blkbits;
  2119. + curlun->info_valid = 1;
  2120. + continue;
  2121. + }
  2122. +
  2123. + /* Get the next buffer */
  2124. + usb_offset += amount;
  2125. + fsg->usb_amount_left -= amount;
  2126. + amount_left_to_req -= amount;
  2127. + if (amount_left_to_req == 0)
  2128. + get_some_more = 0;
  2129. +
  2130. + /* Except at the end of the transfer, amount will be
  2131. + * equal to the buffer size, which is divisible by
  2132. + * the bulk-out maxpacket size.
  2133. + */
  2134. + set_bulk_out_req_length(fsg, bh, amount);
  2135. + start_transfer(fsg, fsg->bulk_out, bh->outreq,
  2136. + &bh->outreq_busy, &bh->state);
  2137. + fsg->next_buffhd_to_fill = bh->next;
  2138. + continue;
  2139. + }
  2140. +
  2141. + /* Write the received data to the backing file */
  2142. + bh = fsg->next_buffhd_to_drain;
  2143. + if (bh->state == BUF_STATE_EMPTY && !get_some_more)
  2144. + break; // We stopped early
  2145. + if (bh->state == BUF_STATE_FULL) {
  2146. + smp_rmb();
  2147. + fsg->next_buffhd_to_drain = bh->next;
  2148. + bh->state = BUF_STATE_EMPTY;
  2149. +
  2150. + /* Did something go wrong with the transfer? */
  2151. + if (bh->outreq->status != 0) {
  2152. + curlun->sense_data = SS_COMMUNICATION_FAILURE;
  2153. + curlun->sense_data_info = file_offset >> curlun->blkbits;
  2154. + curlun->info_valid = 1;
  2155. + break;
  2156. + }
  2157. +
  2158. + amount = bh->outreq->actual;
  2159. + if (curlun->file_length - file_offset < amount) {
  2160. + LERROR(curlun,
  2161. + "write %u @ %llu beyond end %llu\n",
  2162. + amount, (unsigned long long) file_offset,
  2163. + (unsigned long long) curlun->file_length);
  2164. + amount = curlun->file_length - file_offset;
  2165. + }
  2166. +
  2167. + /* Don't accept excess data. The spec doesn't say
  2168. + * what to do in this case. We'll ignore the error.
  2169. + */
  2170. + amount = min(amount, bh->bulk_out_intended_length);
  2171. +
  2172. + /* Don't write a partial block */
  2173. + amount = round_down(amount, curlun->blksize);
  2174. + if (amount == 0)
  2175. + goto empty_write;
  2176. +
  2177. + /* Perform the write */
  2178. + file_offset_tmp = file_offset;
  2179. + nwritten = vfs_write(curlun->filp,
  2180. + (char __user *) bh->buf,
  2181. + amount, &file_offset_tmp);
  2182. + VLDBG(curlun, "file write %u @ %llu -> %d\n", amount,
  2183. + (unsigned long long) file_offset,
  2184. + (int) nwritten);
  2185. + if (signal_pending(current))
  2186. + return -EINTR; // Interrupted!
  2187. +
  2188. + if (nwritten < 0) {
  2189. + LDBG(curlun, "error in file write: %d\n",
  2190. + (int) nwritten);
  2191. + nwritten = 0;
  2192. + } else if (nwritten < amount) {
  2193. + LDBG(curlun, "partial file write: %d/%u\n",
  2194. + (int) nwritten, amount);
  2195. + nwritten = round_down(nwritten, curlun->blksize);
  2196. + }
  2197. + file_offset += nwritten;
  2198. + amount_left_to_write -= nwritten;
  2199. + fsg->residue -= nwritten;
  2200. +
  2201. + /* If an error occurred, report it and its position */
  2202. + if (nwritten < amount) {
  2203. + curlun->sense_data = SS_WRITE_ERROR;
  2204. + curlun->sense_data_info = file_offset >> curlun->blkbits;
  2205. + curlun->info_valid = 1;
  2206. + break;
  2207. + }
  2208. +
  2209. + empty_write:
  2210. + /* Did the host decide to stop early? */
  2211. + if (bh->outreq->actual < bh->bulk_out_intended_length) {
  2212. + fsg->short_packet_received = 1;
  2213. + break;
  2214. + }
  2215. + continue;
  2216. + }
  2217. +
  2218. + /* Wait for something to happen */
  2219. + rc = sleep_thread(fsg);
  2220. + if (rc)
  2221. + return rc;
  2222. + }
  2223. +
  2224. + return -EIO; // No default reply
  2225. +}
  2226. +
  2227. +
  2228. +/*-------------------------------------------------------------------------*/
  2229. +
  2230. +static int do_synchronize_cache(struct fsg_dev *fsg)
  2231. +{
  2232. + struct fsg_lun *curlun = fsg->curlun;
  2233. + int rc;
  2234. +
  2235. + /* We ignore the requested LBA and write out all file's
  2236. + * dirty data buffers. */
  2237. + rc = fsg_lun_fsync_sub(curlun);
  2238. + if (rc)
  2239. + curlun->sense_data = SS_WRITE_ERROR;
  2240. + return 0;
  2241. +}
  2242. +
  2243. +
  2244. +/*-------------------------------------------------------------------------*/
  2245. +
  2246. +static void invalidate_sub(struct fsg_lun *curlun)
  2247. +{
  2248. + struct file *filp = curlun->filp;
  2249. + struct inode *inode = filp->f_path.dentry->d_inode;
  2250. + unsigned long rc;
  2251. +
  2252. + rc = invalidate_mapping_pages(inode->i_mapping, 0, -1);
  2253. + VLDBG(curlun, "invalidate_mapping_pages -> %ld\n", rc);
  2254. +}
  2255. +
  2256. +static int do_verify(struct fsg_dev *fsg)
  2257. +{
  2258. + struct fsg_lun *curlun = fsg->curlun;
  2259. + u32 lba;
  2260. + u32 verification_length;
  2261. + struct fsg_buffhd *bh = fsg->next_buffhd_to_fill;
  2262. + loff_t file_offset, file_offset_tmp;
  2263. + u32 amount_left;
  2264. + unsigned int amount;
  2265. + ssize_t nread;
  2266. +
  2267. + /* Get the starting Logical Block Address and check that it's
  2268. + * not too big */
  2269. + lba = get_unaligned_be32(&fsg->cmnd[2]);
  2270. + if (lba >= curlun->num_sectors) {
  2271. + curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  2272. + return -EINVAL;
  2273. + }
  2274. +
  2275. + /* We allow DPO (Disable Page Out = don't save data in the
  2276. + * cache) but we don't implement it. */
  2277. + if ((fsg->cmnd[1] & ~0x10) != 0) {
  2278. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  2279. + return -EINVAL;
  2280. + }
  2281. +
  2282. + verification_length = get_unaligned_be16(&fsg->cmnd[7]);
  2283. + if (unlikely(verification_length == 0))
  2284. + return -EIO; // No default reply
  2285. +
  2286. + /* Prepare to carry out the file verify */
  2287. + amount_left = verification_length << curlun->blkbits;
  2288. + file_offset = ((loff_t) lba) << curlun->blkbits;
  2289. +
  2290. + /* Write out all the dirty buffers before invalidating them */
  2291. + fsg_lun_fsync_sub(curlun);
  2292. + if (signal_pending(current))
  2293. + return -EINTR;
  2294. +
  2295. + invalidate_sub(curlun);
  2296. + if (signal_pending(current))
  2297. + return -EINTR;
  2298. +
  2299. + /* Just try to read the requested blocks */
  2300. + while (amount_left > 0) {
  2301. +
  2302. + /* Figure out how much we need to read:
  2303. + * Try to read the remaining amount, but not more than
  2304. + * the buffer size.
  2305. + * And don't try to read past the end of the file.
  2306. + */
  2307. + amount = min((unsigned int) amount_left, mod_data.buflen);
  2308. + amount = min((loff_t) amount,
  2309. + curlun->file_length - file_offset);
  2310. + if (amount == 0) {
  2311. + curlun->sense_data =
  2312. + SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  2313. + curlun->sense_data_info = file_offset >> curlun->blkbits;
  2314. + curlun->info_valid = 1;
  2315. + break;
  2316. + }
  2317. +
  2318. + /* Perform the read */
  2319. + file_offset_tmp = file_offset;
  2320. + nread = vfs_read(curlun->filp,
  2321. + (char __user *) bh->buf,
  2322. + amount, &file_offset_tmp);
  2323. + VLDBG(curlun, "file read %u @ %llu -> %d\n", amount,
  2324. + (unsigned long long) file_offset,
  2325. + (int) nread);
  2326. + if (signal_pending(current))
  2327. + return -EINTR;
  2328. +
  2329. + if (nread < 0) {
  2330. + LDBG(curlun, "error in file verify: %d\n",
  2331. + (int) nread);
  2332. + nread = 0;
  2333. + } else if (nread < amount) {
  2334. + LDBG(curlun, "partial file verify: %d/%u\n",
  2335. + (int) nread, amount);
  2336. + nread = round_down(nread, curlun->blksize);
  2337. + }
  2338. + if (nread == 0) {
  2339. + curlun->sense_data = SS_UNRECOVERED_READ_ERROR;
  2340. + curlun->sense_data_info = file_offset >> curlun->blkbits;
  2341. + curlun->info_valid = 1;
  2342. + break;
  2343. + }
  2344. + file_offset += nread;
  2345. + amount_left -= nread;
  2346. + }
  2347. + return 0;
  2348. +}
  2349. +
  2350. +
  2351. +/*-------------------------------------------------------------------------*/
  2352. +
  2353. +static int do_inquiry(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  2354. +{
  2355. + u8 *buf = (u8 *) bh->buf;
  2356. +
  2357. + static char vendor_id[] = "Linux ";
  2358. + static char product_disk_id[] = "File-Stor Gadget";
  2359. + static char product_cdrom_id[] = "File-CD Gadget ";
  2360. +
  2361. + if (!fsg->curlun) { // Unsupported LUNs are okay
  2362. + fsg->bad_lun_okay = 1;
  2363. + memset(buf, 0, 36);
  2364. + buf[0] = 0x7f; // Unsupported, no device-type
  2365. + buf[4] = 31; // Additional length
  2366. + return 36;
  2367. + }
  2368. +
  2369. + memset(buf, 0, 8);
  2370. + buf[0] = (mod_data.cdrom ? TYPE_ROM : TYPE_DISK);
  2371. + if (mod_data.removable)
  2372. + buf[1] = 0x80;
  2373. + buf[2] = 2; // ANSI SCSI level 2
  2374. + buf[3] = 2; // SCSI-2 INQUIRY data format
  2375. + buf[4] = 31; // Additional length
  2376. + // No special options
  2377. + sprintf(buf + 8, "%-8s%-16s%04x", vendor_id,
  2378. + (mod_data.cdrom ? product_cdrom_id :
  2379. + product_disk_id),
  2380. + mod_data.release);
  2381. + return 36;
  2382. +}
  2383. +
  2384. +
  2385. +static int do_request_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  2386. +{
  2387. + struct fsg_lun *curlun = fsg->curlun;
  2388. + u8 *buf = (u8 *) bh->buf;
  2389. + u32 sd, sdinfo;
  2390. + int valid;
  2391. +
  2392. + /*
  2393. + * From the SCSI-2 spec., section 7.9 (Unit attention condition):
  2394. + *
  2395. + * If a REQUEST SENSE command is received from an initiator
  2396. + * with a pending unit attention condition (before the target
  2397. + * generates the contingent allegiance condition), then the
  2398. + * target shall either:
  2399. + * a) report any pending sense data and preserve the unit
  2400. + * attention condition on the logical unit, or,
  2401. + * b) report the unit attention condition, may discard any
  2402. + * pending sense data, and clear the unit attention
  2403. + * condition on the logical unit for that initiator.
  2404. + *
  2405. + * FSG normally uses option a); enable this code to use option b).
  2406. + */
  2407. +#if 0
  2408. + if (curlun && curlun->unit_attention_data != SS_NO_SENSE) {
  2409. + curlun->sense_data = curlun->unit_attention_data;
  2410. + curlun->unit_attention_data = SS_NO_SENSE;
  2411. + }
  2412. +#endif
  2413. +
  2414. + if (!curlun) { // Unsupported LUNs are okay
  2415. + fsg->bad_lun_okay = 1;
  2416. + sd = SS_LOGICAL_UNIT_NOT_SUPPORTED;
  2417. + sdinfo = 0;
  2418. + valid = 0;
  2419. + } else {
  2420. + sd = curlun->sense_data;
  2421. + sdinfo = curlun->sense_data_info;
  2422. + valid = curlun->info_valid << 7;
  2423. + curlun->sense_data = SS_NO_SENSE;
  2424. + curlun->sense_data_info = 0;
  2425. + curlun->info_valid = 0;
  2426. + }
  2427. +
  2428. + memset(buf, 0, 18);
  2429. + buf[0] = valid | 0x70; // Valid, current error
  2430. + buf[2] = SK(sd);
  2431. + put_unaligned_be32(sdinfo, &buf[3]); /* Sense information */
  2432. + buf[7] = 18 - 8; // Additional sense length
  2433. + buf[12] = ASC(sd);
  2434. + buf[13] = ASCQ(sd);
  2435. + return 18;
  2436. +}
  2437. +
  2438. +
  2439. +static int do_read_capacity(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  2440. +{
  2441. + struct fsg_lun *curlun = fsg->curlun;
  2442. + u32 lba = get_unaligned_be32(&fsg->cmnd[2]);
  2443. + int pmi = fsg->cmnd[8];
  2444. + u8 *buf = (u8 *) bh->buf;
  2445. +
  2446. + /* Check the PMI and LBA fields */
  2447. + if (pmi > 1 || (pmi == 0 && lba != 0)) {
  2448. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  2449. + return -EINVAL;
  2450. + }
  2451. +
  2452. + put_unaligned_be32(curlun->num_sectors - 1, &buf[0]);
  2453. + /* Max logical block */
  2454. + put_unaligned_be32(curlun->blksize, &buf[4]); /* Block length */
  2455. + return 8;
  2456. +}
  2457. +
  2458. +
  2459. +static int do_read_header(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  2460. +{
  2461. + struct fsg_lun *curlun = fsg->curlun;
  2462. + int msf = fsg->cmnd[1] & 0x02;
  2463. + u32 lba = get_unaligned_be32(&fsg->cmnd[2]);
  2464. + u8 *buf = (u8 *) bh->buf;
  2465. +
  2466. + if ((fsg->cmnd[1] & ~0x02) != 0) { /* Mask away MSF */
  2467. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  2468. + return -EINVAL;
  2469. + }
  2470. + if (lba >= curlun->num_sectors) {
  2471. + curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  2472. + return -EINVAL;
  2473. + }
  2474. +
  2475. + memset(buf, 0, 8);
  2476. + buf[0] = 0x01; /* 2048 bytes of user data, rest is EC */
  2477. + store_cdrom_address(&buf[4], msf, lba);
  2478. + return 8;
  2479. +}
  2480. +
  2481. +
  2482. +static int do_read_toc(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  2483. +{
  2484. + struct fsg_lun *curlun = fsg->curlun;
  2485. + int msf = fsg->cmnd[1] & 0x02;
  2486. + int start_track = fsg->cmnd[6];
  2487. + u8 *buf = (u8 *) bh->buf;
  2488. +
  2489. + if ((fsg->cmnd[1] & ~0x02) != 0 || /* Mask away MSF */
  2490. + start_track > 1) {
  2491. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  2492. + return -EINVAL;
  2493. + }
  2494. +
  2495. + memset(buf, 0, 20);
  2496. + buf[1] = (20-2); /* TOC data length */
  2497. + buf[2] = 1; /* First track number */
  2498. + buf[3] = 1; /* Last track number */
  2499. + buf[5] = 0x16; /* Data track, copying allowed */
  2500. + buf[6] = 0x01; /* Only track is number 1 */
  2501. + store_cdrom_address(&buf[8], msf, 0);
  2502. +
  2503. + buf[13] = 0x16; /* Lead-out track is data */
  2504. + buf[14] = 0xAA; /* Lead-out track number */
  2505. + store_cdrom_address(&buf[16], msf, curlun->num_sectors);
  2506. + return 20;
  2507. +}
  2508. +
  2509. +
  2510. +static int do_mode_sense(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  2511. +{
  2512. + struct fsg_lun *curlun = fsg->curlun;
  2513. + int mscmnd = fsg->cmnd[0];
  2514. + u8 *buf = (u8 *) bh->buf;
  2515. + u8 *buf0 = buf;
  2516. + int pc, page_code;
  2517. + int changeable_values, all_pages;
  2518. + int valid_page = 0;
  2519. + int len, limit;
  2520. +
  2521. + if ((fsg->cmnd[1] & ~0x08) != 0) { // Mask away DBD
  2522. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  2523. + return -EINVAL;
  2524. + }
  2525. + pc = fsg->cmnd[2] >> 6;
  2526. + page_code = fsg->cmnd[2] & 0x3f;
  2527. + if (pc == 3) {
  2528. + curlun->sense_data = SS_SAVING_PARAMETERS_NOT_SUPPORTED;
  2529. + return -EINVAL;
  2530. + }
  2531. + changeable_values = (pc == 1);
  2532. + all_pages = (page_code == 0x3f);
  2533. +
  2534. + /* Write the mode parameter header. Fixed values are: default
  2535. + * medium type, no cache control (DPOFUA), and no block descriptors.
  2536. + * The only variable value is the WriteProtect bit. We will fill in
  2537. + * the mode data length later. */
  2538. + memset(buf, 0, 8);
  2539. + if (mscmnd == MODE_SENSE) {
  2540. + buf[2] = (curlun->ro ? 0x80 : 0x00); // WP, DPOFUA
  2541. + buf += 4;
  2542. + limit = 255;
  2543. + } else { // MODE_SENSE_10
  2544. + buf[3] = (curlun->ro ? 0x80 : 0x00); // WP, DPOFUA
  2545. + buf += 8;
  2546. + limit = 65535; // Should really be mod_data.buflen
  2547. + }
  2548. +
  2549. + /* No block descriptors */
  2550. +
  2551. + /* The mode pages, in numerical order. The only page we support
  2552. + * is the Caching page. */
  2553. + if (page_code == 0x08 || all_pages) {
  2554. + valid_page = 1;
  2555. + buf[0] = 0x08; // Page code
  2556. + buf[1] = 10; // Page length
  2557. + memset(buf+2, 0, 10); // None of the fields are changeable
  2558. +
  2559. + if (!changeable_values) {
  2560. + buf[2] = 0x04; // Write cache enable,
  2561. + // Read cache not disabled
  2562. + // No cache retention priorities
  2563. + put_unaligned_be16(0xffff, &buf[4]);
  2564. + /* Don't disable prefetch */
  2565. + /* Minimum prefetch = 0 */
  2566. + put_unaligned_be16(0xffff, &buf[8]);
  2567. + /* Maximum prefetch */
  2568. + put_unaligned_be16(0xffff, &buf[10]);
  2569. + /* Maximum prefetch ceiling */
  2570. + }
  2571. + buf += 12;
  2572. + }
  2573. +
  2574. + /* Check that a valid page was requested and the mode data length
  2575. + * isn't too long. */
  2576. + len = buf - buf0;
  2577. + if (!valid_page || len > limit) {
  2578. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  2579. + return -EINVAL;
  2580. + }
  2581. +
  2582. + /* Store the mode data length */
  2583. + if (mscmnd == MODE_SENSE)
  2584. + buf0[0] = len - 1;
  2585. + else
  2586. + put_unaligned_be16(len - 2, buf0);
  2587. + return len;
  2588. +}
  2589. +
  2590. +
  2591. +static int do_start_stop(struct fsg_dev *fsg)
  2592. +{
  2593. + struct fsg_lun *curlun = fsg->curlun;
  2594. + int loej, start;
  2595. +
  2596. + if (!mod_data.removable) {
  2597. + curlun->sense_data = SS_INVALID_COMMAND;
  2598. + return -EINVAL;
  2599. + }
  2600. +
  2601. + // int immed = fsg->cmnd[1] & 0x01;
  2602. + loej = fsg->cmnd[4] & 0x02;
  2603. + start = fsg->cmnd[4] & 0x01;
  2604. +
  2605. +#ifdef CONFIG_USB_FILE_STORAGE_TEST
  2606. + if ((fsg->cmnd[1] & ~0x01) != 0 || // Mask away Immed
  2607. + (fsg->cmnd[4] & ~0x03) != 0) { // Mask LoEj, Start
  2608. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  2609. + return -EINVAL;
  2610. + }
  2611. +
  2612. + if (!start) {
  2613. +
  2614. + /* Are we allowed to unload the media? */
  2615. + if (curlun->prevent_medium_removal) {
  2616. + LDBG(curlun, "unload attempt prevented\n");
  2617. + curlun->sense_data = SS_MEDIUM_REMOVAL_PREVENTED;
  2618. + return -EINVAL;
  2619. + }
  2620. + if (loej) { // Simulate an unload/eject
  2621. + up_read(&fsg->filesem);
  2622. + down_write(&fsg->filesem);
  2623. + fsg_lun_close(curlun);
  2624. + up_write(&fsg->filesem);
  2625. + down_read(&fsg->filesem);
  2626. + }
  2627. + } else {
  2628. +
  2629. + /* Our emulation doesn't support mounting; the medium is
  2630. + * available for use as soon as it is loaded. */
  2631. + if (!fsg_lun_is_open(curlun)) {
  2632. + curlun->sense_data = SS_MEDIUM_NOT_PRESENT;
  2633. + return -EINVAL;
  2634. + }
  2635. + }
  2636. +#endif
  2637. + return 0;
  2638. +}
  2639. +
  2640. +
  2641. +static int do_prevent_allow(struct fsg_dev *fsg)
  2642. +{
  2643. + struct fsg_lun *curlun = fsg->curlun;
  2644. + int prevent;
  2645. +
  2646. + if (!mod_data.removable) {
  2647. + curlun->sense_data = SS_INVALID_COMMAND;
  2648. + return -EINVAL;
  2649. + }
  2650. +
  2651. + prevent = fsg->cmnd[4] & 0x01;
  2652. + if ((fsg->cmnd[4] & ~0x01) != 0) { // Mask away Prevent
  2653. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  2654. + return -EINVAL;
  2655. + }
  2656. +
  2657. + if (curlun->prevent_medium_removal && !prevent)
  2658. + fsg_lun_fsync_sub(curlun);
  2659. + curlun->prevent_medium_removal = prevent;
  2660. + return 0;
  2661. +}
  2662. +
  2663. +
  2664. +static int do_read_format_capacities(struct fsg_dev *fsg,
  2665. + struct fsg_buffhd *bh)
  2666. +{
  2667. + struct fsg_lun *curlun = fsg->curlun;
  2668. + u8 *buf = (u8 *) bh->buf;
  2669. +
  2670. + buf[0] = buf[1] = buf[2] = 0;
  2671. + buf[3] = 8; // Only the Current/Maximum Capacity Descriptor
  2672. + buf += 4;
  2673. +
  2674. + put_unaligned_be32(curlun->num_sectors, &buf[0]);
  2675. + /* Number of blocks */
  2676. + put_unaligned_be32(curlun->blksize, &buf[4]); /* Block length */
  2677. + buf[4] = 0x02; /* Current capacity */
  2678. + return 12;
  2679. +}
  2680. +
  2681. +
  2682. +static int do_mode_select(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  2683. +{
  2684. + struct fsg_lun *curlun = fsg->curlun;
  2685. +
  2686. + /* We don't support MODE SELECT */
  2687. + curlun->sense_data = SS_INVALID_COMMAND;
  2688. + return -EINVAL;
  2689. +}
  2690. +
  2691. +
  2692. +/*-------------------------------------------------------------------------*/
  2693. +
  2694. +static int halt_bulk_in_endpoint(struct fsg_dev *fsg)
  2695. +{
  2696. + int rc;
  2697. +
  2698. + rc = fsg_set_halt(fsg, fsg->bulk_in);
  2699. + if (rc == -EAGAIN)
  2700. + VDBG(fsg, "delayed bulk-in endpoint halt\n");
  2701. + while (rc != 0) {
  2702. + if (rc != -EAGAIN) {
  2703. + WARNING(fsg, "usb_ep_set_halt -> %d\n", rc);
  2704. + rc = 0;
  2705. + break;
  2706. + }
  2707. +
  2708. + /* Wait for a short time and then try again */
  2709. + if (msleep_interruptible(100) != 0)
  2710. + return -EINTR;
  2711. + rc = usb_ep_set_halt(fsg->bulk_in);
  2712. + }
  2713. + return rc;
  2714. +}
  2715. +
  2716. +static int wedge_bulk_in_endpoint(struct fsg_dev *fsg)
  2717. +{
  2718. + int rc;
  2719. +
  2720. + DBG(fsg, "bulk-in set wedge\n");
  2721. + rc = usb_ep_set_wedge(fsg->bulk_in);
  2722. + if (rc == -EAGAIN)
  2723. + VDBG(fsg, "delayed bulk-in endpoint wedge\n");
  2724. + while (rc != 0) {
  2725. + if (rc != -EAGAIN) {
  2726. + WARNING(fsg, "usb_ep_set_wedge -> %d\n", rc);
  2727. + rc = 0;
  2728. + break;
  2729. + }
  2730. +
  2731. + /* Wait for a short time and then try again */
  2732. + if (msleep_interruptible(100) != 0)
  2733. + return -EINTR;
  2734. + rc = usb_ep_set_wedge(fsg->bulk_in);
  2735. + }
  2736. + return rc;
  2737. +}
  2738. +
  2739. +static int throw_away_data(struct fsg_dev *fsg)
  2740. +{
  2741. + struct fsg_buffhd *bh;
  2742. + u32 amount;
  2743. + int rc;
  2744. +
  2745. + while ((bh = fsg->next_buffhd_to_drain)->state != BUF_STATE_EMPTY ||
  2746. + fsg->usb_amount_left > 0) {
  2747. +
  2748. + /* Throw away the data in a filled buffer */
  2749. + if (bh->state == BUF_STATE_FULL) {
  2750. + smp_rmb();
  2751. + bh->state = BUF_STATE_EMPTY;
  2752. + fsg->next_buffhd_to_drain = bh->next;
  2753. +
  2754. + /* A short packet or an error ends everything */
  2755. + if (bh->outreq->actual < bh->bulk_out_intended_length ||
  2756. + bh->outreq->status != 0) {
  2757. + raise_exception(fsg, FSG_STATE_ABORT_BULK_OUT);
  2758. + return -EINTR;
  2759. + }
  2760. + continue;
  2761. + }
  2762. +
  2763. + /* Try to submit another request if we need one */
  2764. + bh = fsg->next_buffhd_to_fill;
  2765. + if (bh->state == BUF_STATE_EMPTY && fsg->usb_amount_left > 0) {
  2766. + amount = min(fsg->usb_amount_left,
  2767. + (u32) mod_data.buflen);
  2768. +
  2769. + /* Except at the end of the transfer, amount will be
  2770. + * equal to the buffer size, which is divisible by
  2771. + * the bulk-out maxpacket size.
  2772. + */
  2773. + set_bulk_out_req_length(fsg, bh, amount);
  2774. + start_transfer(fsg, fsg->bulk_out, bh->outreq,
  2775. + &bh->outreq_busy, &bh->state);
  2776. + fsg->next_buffhd_to_fill = bh->next;
  2777. + fsg->usb_amount_left -= amount;
  2778. + continue;
  2779. + }
  2780. +
  2781. + /* Otherwise wait for something to happen */
  2782. + rc = sleep_thread(fsg);
  2783. + if (rc)
  2784. + return rc;
  2785. + }
  2786. + return 0;
  2787. +}
  2788. +
  2789. +
  2790. +static int finish_reply(struct fsg_dev *fsg)
  2791. +{
  2792. + struct fsg_buffhd *bh = fsg->next_buffhd_to_fill;
  2793. + int rc = 0;
  2794. +
  2795. + switch (fsg->data_dir) {
  2796. + case DATA_DIR_NONE:
  2797. + break; // Nothing to send
  2798. +
  2799. + /* If we don't know whether the host wants to read or write,
  2800. + * this must be CB or CBI with an unknown command. We mustn't
  2801. + * try to send or receive any data. So stall both bulk pipes
  2802. + * if we can and wait for a reset. */
  2803. + case DATA_DIR_UNKNOWN:
  2804. + if (mod_data.can_stall) {
  2805. + fsg_set_halt(fsg, fsg->bulk_out);
  2806. + rc = halt_bulk_in_endpoint(fsg);
  2807. + }
  2808. + break;
  2809. +
  2810. + /* All but the last buffer of data must have already been sent */
  2811. + case DATA_DIR_TO_HOST:
  2812. + if (fsg->data_size == 0)
  2813. + ; // Nothing to send
  2814. +
  2815. + /* If there's no residue, simply send the last buffer */
  2816. + else if (fsg->residue == 0) {
  2817. + bh->inreq->zero = 0;
  2818. + start_transfer(fsg, fsg->bulk_in, bh->inreq,
  2819. + &bh->inreq_busy, &bh->state);
  2820. + fsg->next_buffhd_to_fill = bh->next;
  2821. + }
  2822. +
  2823. + /* There is a residue. For CB and CBI, simply mark the end
  2824. + * of the data with a short packet. However, if we are
  2825. + * allowed to stall, there was no data at all (residue ==
  2826. + * data_size), and the command failed (invalid LUN or
  2827. + * sense data is set), then halt the bulk-in endpoint
  2828. + * instead. */
  2829. + else if (!transport_is_bbb()) {
  2830. + if (mod_data.can_stall &&
  2831. + fsg->residue == fsg->data_size &&
  2832. + (!fsg->curlun || fsg->curlun->sense_data != SS_NO_SENSE)) {
  2833. + bh->state = BUF_STATE_EMPTY;
  2834. + rc = halt_bulk_in_endpoint(fsg);
  2835. + } else {
  2836. + bh->inreq->zero = 1;
  2837. + start_transfer(fsg, fsg->bulk_in, bh->inreq,
  2838. + &bh->inreq_busy, &bh->state);
  2839. + fsg->next_buffhd_to_fill = bh->next;
  2840. + }
  2841. + }
  2842. +
  2843. + /*
  2844. + * For Bulk-only, mark the end of the data with a short
  2845. + * packet. If we are allowed to stall, halt the bulk-in
  2846. + * endpoint. (Note: This violates the Bulk-Only Transport
  2847. + * specification, which requires us to pad the data if we
  2848. + * don't halt the endpoint. Presumably nobody will mind.)
  2849. + */
  2850. + else {
  2851. + bh->inreq->zero = 1;
  2852. + start_transfer(fsg, fsg->bulk_in, bh->inreq,
  2853. + &bh->inreq_busy, &bh->state);
  2854. + fsg->next_buffhd_to_fill = bh->next;
  2855. + if (mod_data.can_stall)
  2856. + rc = halt_bulk_in_endpoint(fsg);
  2857. + }
  2858. + break;
  2859. +
  2860. + /* We have processed all we want from the data the host has sent.
  2861. + * There may still be outstanding bulk-out requests. */
  2862. + case DATA_DIR_FROM_HOST:
  2863. + if (fsg->residue == 0)
  2864. + ; // Nothing to receive
  2865. +
  2866. + /* Did the host stop sending unexpectedly early? */
  2867. + else if (fsg->short_packet_received) {
  2868. + raise_exception(fsg, FSG_STATE_ABORT_BULK_OUT);
  2869. + rc = -EINTR;
  2870. + }
  2871. +
  2872. + /* We haven't processed all the incoming data. Even though
  2873. + * we may be allowed to stall, doing so would cause a race.
  2874. + * The controller may already have ACK'ed all the remaining
  2875. + * bulk-out packets, in which case the host wouldn't see a
  2876. + * STALL. Not realizing the endpoint was halted, it wouldn't
  2877. + * clear the halt -- leading to problems later on. */
  2878. +#if 0
  2879. + else if (mod_data.can_stall) {
  2880. + fsg_set_halt(fsg, fsg->bulk_out);
  2881. + raise_exception(fsg, FSG_STATE_ABORT_BULK_OUT);
  2882. + rc = -EINTR;
  2883. + }
  2884. +#endif
  2885. +
  2886. + /* We can't stall. Read in the excess data and throw it
  2887. + * all away. */
  2888. + else
  2889. + rc = throw_away_data(fsg);
  2890. + break;
  2891. + }
  2892. + return rc;
  2893. +}
  2894. +
  2895. +
  2896. +static int send_status(struct fsg_dev *fsg)
  2897. +{
  2898. + struct fsg_lun *curlun = fsg->curlun;
  2899. + struct fsg_buffhd *bh;
  2900. + int rc;
  2901. + u8 status = US_BULK_STAT_OK;
  2902. + u32 sd, sdinfo = 0;
  2903. +
  2904. + /* Wait for the next buffer to become available */
  2905. + bh = fsg->next_buffhd_to_fill;
  2906. + while (bh->state != BUF_STATE_EMPTY) {
  2907. + rc = sleep_thread(fsg);
  2908. + if (rc)
  2909. + return rc;
  2910. + }
  2911. +
  2912. + if (curlun) {
  2913. + sd = curlun->sense_data;
  2914. + sdinfo = curlun->sense_data_info;
  2915. + } else if (fsg->bad_lun_okay)
  2916. + sd = SS_NO_SENSE;
  2917. + else
  2918. + sd = SS_LOGICAL_UNIT_NOT_SUPPORTED;
  2919. +
  2920. + if (fsg->phase_error) {
  2921. + DBG(fsg, "sending phase-error status\n");
  2922. + status = US_BULK_STAT_PHASE;
  2923. + sd = SS_INVALID_COMMAND;
  2924. + } else if (sd != SS_NO_SENSE) {
  2925. + DBG(fsg, "sending command-failure status\n");
  2926. + status = US_BULK_STAT_FAIL;
  2927. + VDBG(fsg, " sense data: SK x%02x, ASC x%02x, ASCQ x%02x;"
  2928. + " info x%x\n",
  2929. + SK(sd), ASC(sd), ASCQ(sd), sdinfo);
  2930. + }
  2931. +
  2932. + if (transport_is_bbb()) {
  2933. + struct bulk_cs_wrap *csw = bh->buf;
  2934. +
  2935. + /* Store and send the Bulk-only CSW */
  2936. + csw->Signature = cpu_to_le32(US_BULK_CS_SIGN);
  2937. + csw->Tag = fsg->tag;
  2938. + csw->Residue = cpu_to_le32(fsg->residue);
  2939. + csw->Status = status;
  2940. +
  2941. + bh->inreq->length = US_BULK_CS_WRAP_LEN;
  2942. + bh->inreq->zero = 0;
  2943. + start_transfer(fsg, fsg->bulk_in, bh->inreq,
  2944. + &bh->inreq_busy, &bh->state);
  2945. +
  2946. + } else if (mod_data.transport_type == USB_PR_CB) {
  2947. +
  2948. + /* Control-Bulk transport has no status phase! */
  2949. + return 0;
  2950. +
  2951. + } else { // USB_PR_CBI
  2952. + struct interrupt_data *buf = bh->buf;
  2953. +
  2954. + /* Store and send the Interrupt data. UFI sends the ASC
  2955. + * and ASCQ bytes. Everything else sends a Type (which
  2956. + * is always 0) and the status Value. */
  2957. + if (mod_data.protocol_type == USB_SC_UFI) {
  2958. + buf->bType = ASC(sd);
  2959. + buf->bValue = ASCQ(sd);
  2960. + } else {
  2961. + buf->bType = 0;
  2962. + buf->bValue = status;
  2963. + }
  2964. + fsg->intreq->length = CBI_INTERRUPT_DATA_LEN;
  2965. +
  2966. + fsg->intr_buffhd = bh; // Point to the right buffhd
  2967. + fsg->intreq->buf = bh->inreq->buf;
  2968. + fsg->intreq->context = bh;
  2969. + start_transfer(fsg, fsg->intr_in, fsg->intreq,
  2970. + &fsg->intreq_busy, &bh->state);
  2971. + }
  2972. +
  2973. + fsg->next_buffhd_to_fill = bh->next;
  2974. + return 0;
  2975. +}
  2976. +
  2977. +
  2978. +/*-------------------------------------------------------------------------*/
  2979. +
  2980. +/* Check whether the command is properly formed and whether its data size
  2981. + * and direction agree with the values we already have. */
  2982. +static int check_command(struct fsg_dev *fsg, int cmnd_size,
  2983. + enum data_direction data_dir, unsigned int mask,
  2984. + int needs_medium, const char *name)
  2985. +{
  2986. + int i;
  2987. + int lun = fsg->cmnd[1] >> 5;
  2988. + static const char dirletter[4] = {'u', 'o', 'i', 'n'};
  2989. + char hdlen[20];
  2990. + struct fsg_lun *curlun;
  2991. +
  2992. + /* Adjust the expected cmnd_size for protocol encapsulation padding.
  2993. + * Transparent SCSI doesn't pad. */
  2994. + if (protocol_is_scsi())
  2995. + ;
  2996. +
  2997. + /* There's some disagreement as to whether RBC pads commands or not.
  2998. + * We'll play it safe and accept either form. */
  2999. + else if (mod_data.protocol_type == USB_SC_RBC) {
  3000. + if (fsg->cmnd_size == 12)
  3001. + cmnd_size = 12;
  3002. +
  3003. + /* All the other protocols pad to 12 bytes */
  3004. + } else
  3005. + cmnd_size = 12;
  3006. +
  3007. + hdlen[0] = 0;
  3008. + if (fsg->data_dir != DATA_DIR_UNKNOWN)
  3009. + sprintf(hdlen, ", H%c=%u", dirletter[(int) fsg->data_dir],
  3010. + fsg->data_size);
  3011. + VDBG(fsg, "SCSI command: %s; Dc=%d, D%c=%u; Hc=%d%s\n",
  3012. + name, cmnd_size, dirletter[(int) data_dir],
  3013. + fsg->data_size_from_cmnd, fsg->cmnd_size, hdlen);
  3014. +
  3015. + /* We can't reply at all until we know the correct data direction
  3016. + * and size. */
  3017. + if (fsg->data_size_from_cmnd == 0)
  3018. + data_dir = DATA_DIR_NONE;
  3019. + if (fsg->data_dir == DATA_DIR_UNKNOWN) { // CB or CBI
  3020. + fsg->data_dir = data_dir;
  3021. + fsg->data_size = fsg->data_size_from_cmnd;
  3022. +
  3023. + } else { // Bulk-only
  3024. + if (fsg->data_size < fsg->data_size_from_cmnd) {
  3025. +
  3026. + /* Host data size < Device data size is a phase error.
  3027. + * Carry out the command, but only transfer as much
  3028. + * as we are allowed. */
  3029. + fsg->data_size_from_cmnd = fsg->data_size;
  3030. + fsg->phase_error = 1;
  3031. + }
  3032. + }
  3033. + fsg->residue = fsg->usb_amount_left = fsg->data_size;
  3034. +
  3035. + /* Conflicting data directions is a phase error */
  3036. + if (fsg->data_dir != data_dir && fsg->data_size_from_cmnd > 0) {
  3037. + fsg->phase_error = 1;
  3038. + return -EINVAL;
  3039. + }
  3040. +
  3041. + /* Verify the length of the command itself */
  3042. + if (cmnd_size != fsg->cmnd_size) {
  3043. +
  3044. + /* Special case workaround: There are plenty of buggy SCSI
  3045. + * implementations. Many have issues with cbw->Length
  3046. + * field passing a wrong command size. For those cases we
  3047. + * always try to work around the problem by using the length
  3048. + * sent by the host side provided it is at least as large
  3049. + * as the correct command length.
  3050. + * Examples of such cases would be MS-Windows, which issues
  3051. + * REQUEST SENSE with cbw->Length == 12 where it should
  3052. + * be 6, and xbox360 issuing INQUIRY, TEST UNIT READY and
  3053. + * REQUEST SENSE with cbw->Length == 10 where it should
  3054. + * be 6 as well.
  3055. + */
  3056. + if (cmnd_size <= fsg->cmnd_size) {
  3057. + DBG(fsg, "%s is buggy! Expected length %d "
  3058. + "but we got %d\n", name,
  3059. + cmnd_size, fsg->cmnd_size);
  3060. + cmnd_size = fsg->cmnd_size;
  3061. + } else {
  3062. + fsg->phase_error = 1;
  3063. + return -EINVAL;
  3064. + }
  3065. + }
  3066. +
  3067. + /* Check that the LUN values are consistent */
  3068. + if (transport_is_bbb()) {
  3069. + if (fsg->lun != lun)
  3070. + DBG(fsg, "using LUN %d from CBW, "
  3071. + "not LUN %d from CDB\n",
  3072. + fsg->lun, lun);
  3073. + }
  3074. +
  3075. + /* Check the LUN */
  3076. + curlun = fsg->curlun;
  3077. + if (curlun) {
  3078. + if (fsg->cmnd[0] != REQUEST_SENSE) {
  3079. + curlun->sense_data = SS_NO_SENSE;
  3080. + curlun->sense_data_info = 0;
  3081. + curlun->info_valid = 0;
  3082. + }
  3083. + } else {
  3084. + fsg->bad_lun_okay = 0;
  3085. +
  3086. + /* INQUIRY and REQUEST SENSE commands are explicitly allowed
  3087. + * to use unsupported LUNs; all others may not. */
  3088. + if (fsg->cmnd[0] != INQUIRY &&
  3089. + fsg->cmnd[0] != REQUEST_SENSE) {
  3090. + DBG(fsg, "unsupported LUN %d\n", fsg->lun);
  3091. + return -EINVAL;
  3092. + }
  3093. + }
  3094. +
  3095. + /* If a unit attention condition exists, only INQUIRY and
  3096. + * REQUEST SENSE commands are allowed; anything else must fail. */
  3097. + if (curlun && curlun->unit_attention_data != SS_NO_SENSE &&
  3098. + fsg->cmnd[0] != INQUIRY &&
  3099. + fsg->cmnd[0] != REQUEST_SENSE) {
  3100. + curlun->sense_data = curlun->unit_attention_data;
  3101. + curlun->unit_attention_data = SS_NO_SENSE;
  3102. + return -EINVAL;
  3103. + }
  3104. +
  3105. + /* Check that only command bytes listed in the mask are non-zero */
  3106. + fsg->cmnd[1] &= 0x1f; // Mask away the LUN
  3107. + for (i = 1; i < cmnd_size; ++i) {
  3108. + if (fsg->cmnd[i] && !(mask & (1 << i))) {
  3109. + if (curlun)
  3110. + curlun->sense_data = SS_INVALID_FIELD_IN_CDB;
  3111. + return -EINVAL;
  3112. + }
  3113. + }
  3114. +
  3115. + /* If the medium isn't mounted and the command needs to access
  3116. + * it, return an error. */
  3117. + if (curlun && !fsg_lun_is_open(curlun) && needs_medium) {
  3118. + curlun->sense_data = SS_MEDIUM_NOT_PRESENT;
  3119. + return -EINVAL;
  3120. + }
  3121. +
  3122. + return 0;
  3123. +}
  3124. +
  3125. +/* wrapper of check_command for data size in blocks handling */
  3126. +static int check_command_size_in_blocks(struct fsg_dev *fsg, int cmnd_size,
  3127. + enum data_direction data_dir, unsigned int mask,
  3128. + int needs_medium, const char *name)
  3129. +{
  3130. + if (fsg->curlun)
  3131. + fsg->data_size_from_cmnd <<= fsg->curlun->blkbits;
  3132. + return check_command(fsg, cmnd_size, data_dir,
  3133. + mask, needs_medium, name);
  3134. +}
  3135. +
  3136. +static int do_scsi_command(struct fsg_dev *fsg)
  3137. +{
  3138. + struct fsg_buffhd *bh;
  3139. + int rc;
  3140. + int reply = -EINVAL;
  3141. + int i;
  3142. + static char unknown[16];
  3143. +
  3144. + dump_cdb(fsg);
  3145. +
  3146. + /* Wait for the next buffer to become available for data or status */
  3147. + bh = fsg->next_buffhd_to_drain = fsg->next_buffhd_to_fill;
  3148. + while (bh->state != BUF_STATE_EMPTY) {
  3149. + rc = sleep_thread(fsg);
  3150. + if (rc)
  3151. + return rc;
  3152. + }
  3153. + fsg->phase_error = 0;
  3154. + fsg->short_packet_received = 0;
  3155. +
  3156. + down_read(&fsg->filesem); // We're using the backing file
  3157. + switch (fsg->cmnd[0]) {
  3158. +
  3159. + case INQUIRY:
  3160. + fsg->data_size_from_cmnd = fsg->cmnd[4];
  3161. + if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
  3162. + (1<<4), 0,
  3163. + "INQUIRY")) == 0)
  3164. + reply = do_inquiry(fsg, bh);
  3165. + break;
  3166. +
  3167. + case MODE_SELECT:
  3168. + fsg->data_size_from_cmnd = fsg->cmnd[4];
  3169. + if ((reply = check_command(fsg, 6, DATA_DIR_FROM_HOST,
  3170. + (1<<1) | (1<<4), 0,
  3171. + "MODE SELECT(6)")) == 0)
  3172. + reply = do_mode_select(fsg, bh);
  3173. + break;
  3174. +
  3175. + case MODE_SELECT_10:
  3176. + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
  3177. + if ((reply = check_command(fsg, 10, DATA_DIR_FROM_HOST,
  3178. + (1<<1) | (3<<7), 0,
  3179. + "MODE SELECT(10)")) == 0)
  3180. + reply = do_mode_select(fsg, bh);
  3181. + break;
  3182. +
  3183. + case MODE_SENSE:
  3184. + fsg->data_size_from_cmnd = fsg->cmnd[4];
  3185. + if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
  3186. + (1<<1) | (1<<2) | (1<<4), 0,
  3187. + "MODE SENSE(6)")) == 0)
  3188. + reply = do_mode_sense(fsg, bh);
  3189. + break;
  3190. +
  3191. + case MODE_SENSE_10:
  3192. + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
  3193. + if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
  3194. + (1<<1) | (1<<2) | (3<<7), 0,
  3195. + "MODE SENSE(10)")) == 0)
  3196. + reply = do_mode_sense(fsg, bh);
  3197. + break;
  3198. +
  3199. + case ALLOW_MEDIUM_REMOVAL:
  3200. + fsg->data_size_from_cmnd = 0;
  3201. + if ((reply = check_command(fsg, 6, DATA_DIR_NONE,
  3202. + (1<<4), 0,
  3203. + "PREVENT-ALLOW MEDIUM REMOVAL")) == 0)
  3204. + reply = do_prevent_allow(fsg);
  3205. + break;
  3206. +
  3207. + case READ_6:
  3208. + i = fsg->cmnd[4];
  3209. + fsg->data_size_from_cmnd = (i == 0) ? 256 : i;
  3210. + if ((reply = check_command_size_in_blocks(fsg, 6,
  3211. + DATA_DIR_TO_HOST,
  3212. + (7<<1) | (1<<4), 1,
  3213. + "READ(6)")) == 0)
  3214. + reply = do_read(fsg);
  3215. + break;
  3216. +
  3217. + case READ_10:
  3218. + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
  3219. + if ((reply = check_command_size_in_blocks(fsg, 10,
  3220. + DATA_DIR_TO_HOST,
  3221. + (1<<1) | (0xf<<2) | (3<<7), 1,
  3222. + "READ(10)")) == 0)
  3223. + reply = do_read(fsg);
  3224. + break;
  3225. +
  3226. + case READ_12:
  3227. + fsg->data_size_from_cmnd = get_unaligned_be32(&fsg->cmnd[6]);
  3228. + if ((reply = check_command_size_in_blocks(fsg, 12,
  3229. + DATA_DIR_TO_HOST,
  3230. + (1<<1) | (0xf<<2) | (0xf<<6), 1,
  3231. + "READ(12)")) == 0)
  3232. + reply = do_read(fsg);
  3233. + break;
  3234. +
  3235. + case READ_CAPACITY:
  3236. + fsg->data_size_from_cmnd = 8;
  3237. + if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
  3238. + (0xf<<2) | (1<<8), 1,
  3239. + "READ CAPACITY")) == 0)
  3240. + reply = do_read_capacity(fsg, bh);
  3241. + break;
  3242. +
  3243. + case READ_HEADER:
  3244. + if (!mod_data.cdrom)
  3245. + goto unknown_cmnd;
  3246. + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
  3247. + if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
  3248. + (3<<7) | (0x1f<<1), 1,
  3249. + "READ HEADER")) == 0)
  3250. + reply = do_read_header(fsg, bh);
  3251. + break;
  3252. +
  3253. + case READ_TOC:
  3254. + if (!mod_data.cdrom)
  3255. + goto unknown_cmnd;
  3256. + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
  3257. + if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
  3258. + (7<<6) | (1<<1), 1,
  3259. + "READ TOC")) == 0)
  3260. + reply = do_read_toc(fsg, bh);
  3261. + break;
  3262. +
  3263. + case READ_FORMAT_CAPACITIES:
  3264. + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
  3265. + if ((reply = check_command(fsg, 10, DATA_DIR_TO_HOST,
  3266. + (3<<7), 1,
  3267. + "READ FORMAT CAPACITIES")) == 0)
  3268. + reply = do_read_format_capacities(fsg, bh);
  3269. + break;
  3270. +
  3271. + case REQUEST_SENSE:
  3272. + fsg->data_size_from_cmnd = fsg->cmnd[4];
  3273. + if ((reply = check_command(fsg, 6, DATA_DIR_TO_HOST,
  3274. + (1<<4), 0,
  3275. + "REQUEST SENSE")) == 0)
  3276. + reply = do_request_sense(fsg, bh);
  3277. + break;
  3278. +
  3279. + case START_STOP:
  3280. + fsg->data_size_from_cmnd = 0;
  3281. + if ((reply = check_command(fsg, 6, DATA_DIR_NONE,
  3282. + (1<<1) | (1<<4), 0,
  3283. + "START-STOP UNIT")) == 0)
  3284. + reply = do_start_stop(fsg);
  3285. + break;
  3286. +
  3287. + case SYNCHRONIZE_CACHE:
  3288. + fsg->data_size_from_cmnd = 0;
  3289. + if ((reply = check_command(fsg, 10, DATA_DIR_NONE,
  3290. + (0xf<<2) | (3<<7), 1,
  3291. + "SYNCHRONIZE CACHE")) == 0)
  3292. + reply = do_synchronize_cache(fsg);
  3293. + break;
  3294. +
  3295. + case TEST_UNIT_READY:
  3296. + fsg->data_size_from_cmnd = 0;
  3297. + reply = check_command(fsg, 6, DATA_DIR_NONE,
  3298. + 0, 1,
  3299. + "TEST UNIT READY");
  3300. + break;
  3301. +
  3302. + /* Although optional, this command is used by MS-Windows. We
  3303. + * support a minimal version: BytChk must be 0. */
  3304. + case VERIFY:
  3305. + fsg->data_size_from_cmnd = 0;
  3306. + if ((reply = check_command(fsg, 10, DATA_DIR_NONE,
  3307. + (1<<1) | (0xf<<2) | (3<<7), 1,
  3308. + "VERIFY")) == 0)
  3309. + reply = do_verify(fsg);
  3310. + break;
  3311. +
  3312. + case WRITE_6:
  3313. + i = fsg->cmnd[4];
  3314. + fsg->data_size_from_cmnd = (i == 0) ? 256 : i;
  3315. + if ((reply = check_command_size_in_blocks(fsg, 6,
  3316. + DATA_DIR_FROM_HOST,
  3317. + (7<<1) | (1<<4), 1,
  3318. + "WRITE(6)")) == 0)
  3319. + reply = do_write(fsg);
  3320. + break;
  3321. +
  3322. + case WRITE_10:
  3323. + fsg->data_size_from_cmnd = get_unaligned_be16(&fsg->cmnd[7]);
  3324. + if ((reply = check_command_size_in_blocks(fsg, 10,
  3325. + DATA_DIR_FROM_HOST,
  3326. + (1<<1) | (0xf<<2) | (3<<7), 1,
  3327. + "WRITE(10)")) == 0)
  3328. + reply = do_write(fsg);
  3329. + break;
  3330. +
  3331. + case WRITE_12:
  3332. + fsg->data_size_from_cmnd = get_unaligned_be32(&fsg->cmnd[6]);
  3333. + if ((reply = check_command_size_in_blocks(fsg, 12,
  3334. + DATA_DIR_FROM_HOST,
  3335. + (1<<1) | (0xf<<2) | (0xf<<6), 1,
  3336. + "WRITE(12)")) == 0)
  3337. + reply = do_write(fsg);
  3338. + break;
  3339. +
  3340. + /* Some mandatory commands that we recognize but don't implement.
  3341. + * They don't mean much in this setting. It's left as an exercise
  3342. + * for anyone interested to implement RESERVE and RELEASE in terms
  3343. + * of Posix locks. */
  3344. + case FORMAT_UNIT:
  3345. + case RELEASE:
  3346. + case RESERVE:
  3347. + case SEND_DIAGNOSTIC:
  3348. + // Fall through
  3349. +
  3350. + default:
  3351. + unknown_cmnd:
  3352. + fsg->data_size_from_cmnd = 0;
  3353. + sprintf(unknown, "Unknown x%02x", fsg->cmnd[0]);
  3354. + if ((reply = check_command(fsg, fsg->cmnd_size,
  3355. + DATA_DIR_UNKNOWN, ~0, 0, unknown)) == 0) {
  3356. + fsg->curlun->sense_data = SS_INVALID_COMMAND;
  3357. + reply = -EINVAL;
  3358. + }
  3359. + break;
  3360. + }
  3361. + up_read(&fsg->filesem);
  3362. +
  3363. + if (reply == -EINTR || signal_pending(current))
  3364. + return -EINTR;
  3365. +
  3366. + /* Set up the single reply buffer for finish_reply() */
  3367. + if (reply == -EINVAL)
  3368. + reply = 0; // Error reply length
  3369. + if (reply >= 0 && fsg->data_dir == DATA_DIR_TO_HOST) {
  3370. + reply = min((u32) reply, fsg->data_size_from_cmnd);
  3371. + bh->inreq->length = reply;
  3372. + bh->state = BUF_STATE_FULL;
  3373. + fsg->residue -= reply;
  3374. + } // Otherwise it's already set
  3375. +
  3376. + return 0;
  3377. +}
  3378. +
  3379. +
  3380. +/*-------------------------------------------------------------------------*/
  3381. +
  3382. +static int received_cbw(struct fsg_dev *fsg, struct fsg_buffhd *bh)
  3383. +{
  3384. + struct usb_request *req = bh->outreq;
  3385. + struct bulk_cb_wrap *cbw = req->buf;
  3386. +
  3387. + /* Was this a real packet? Should it be ignored? */
  3388. + if (req->status || test_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags))
  3389. + return -EINVAL;
  3390. +
  3391. + /* Is the CBW valid? */
  3392. + if (req->actual != US_BULK_CB_WRAP_LEN ||
  3393. + cbw->Signature != cpu_to_le32(
  3394. + US_BULK_CB_SIGN)) {
  3395. + DBG(fsg, "invalid CBW: len %u sig 0x%x\n",
  3396. + req->actual,
  3397. + le32_to_cpu(cbw->Signature));
  3398. +
  3399. + /* The Bulk-only spec says we MUST stall the IN endpoint
  3400. + * (6.6.1), so it's unavoidable. It also says we must
  3401. + * retain this state until the next reset, but there's
  3402. + * no way to tell the controller driver it should ignore
  3403. + * Clear-Feature(HALT) requests.
  3404. + *
  3405. + * We aren't required to halt the OUT endpoint; instead
  3406. + * we can simply accept and discard any data received
  3407. + * until the next reset. */
  3408. + wedge_bulk_in_endpoint(fsg);
  3409. + set_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags);
  3410. + return -EINVAL;
  3411. + }
  3412. +
  3413. + /* Is the CBW meaningful? */
  3414. + if (cbw->Lun >= FSG_MAX_LUNS || cbw->Flags & ~US_BULK_FLAG_IN ||
  3415. + cbw->Length <= 0 || cbw->Length > MAX_COMMAND_SIZE) {
  3416. + DBG(fsg, "non-meaningful CBW: lun = %u, flags = 0x%x, "
  3417. + "cmdlen %u\n",
  3418. + cbw->Lun, cbw->Flags, cbw->Length);
  3419. +
  3420. + /* We can do anything we want here, so let's stall the
  3421. + * bulk pipes if we are allowed to. */
  3422. + if (mod_data.can_stall) {
  3423. + fsg_set_halt(fsg, fsg->bulk_out);
  3424. + halt_bulk_in_endpoint(fsg);
  3425. + }
  3426. + return -EINVAL;
  3427. + }
  3428. +
  3429. + /* Save the command for later */
  3430. + fsg->cmnd_size = cbw->Length;
  3431. + memcpy(fsg->cmnd, cbw->CDB, fsg->cmnd_size);
  3432. + if (cbw->Flags & US_BULK_FLAG_IN)
  3433. + fsg->data_dir = DATA_DIR_TO_HOST;
  3434. + else
  3435. + fsg->data_dir = DATA_DIR_FROM_HOST;
  3436. + fsg->data_size = le32_to_cpu(cbw->DataTransferLength);
  3437. + if (fsg->data_size == 0)
  3438. + fsg->data_dir = DATA_DIR_NONE;
  3439. + fsg->lun = cbw->Lun;
  3440. + fsg->tag = cbw->Tag;
  3441. + return 0;
  3442. +}
  3443. +
  3444. +
  3445. +static int get_next_command(struct fsg_dev *fsg)
  3446. +{
  3447. + struct fsg_buffhd *bh;
  3448. + int rc = 0;
  3449. +
  3450. + if (transport_is_bbb()) {
  3451. +
  3452. + /* Wait for the next buffer to become available */
  3453. + bh = fsg->next_buffhd_to_fill;
  3454. + while (bh->state != BUF_STATE_EMPTY) {
  3455. + rc = sleep_thread(fsg);
  3456. + if (rc)
  3457. + return rc;
  3458. + }
  3459. +
  3460. + /* Queue a request to read a Bulk-only CBW */
  3461. + set_bulk_out_req_length(fsg, bh, US_BULK_CB_WRAP_LEN);
  3462. + start_transfer(fsg, fsg->bulk_out, bh->outreq,
  3463. + &bh->outreq_busy, &bh->state);
  3464. +
  3465. + /* We will drain the buffer in software, which means we
  3466. + * can reuse it for the next filling. No need to advance
  3467. + * next_buffhd_to_fill. */
  3468. +
  3469. + /* Wait for the CBW to arrive */
  3470. + while (bh->state != BUF_STATE_FULL) {
  3471. + rc = sleep_thread(fsg);
  3472. + if (rc)
  3473. + return rc;
  3474. + }
  3475. + smp_rmb();
  3476. + rc = received_cbw(fsg, bh);
  3477. + bh->state = BUF_STATE_EMPTY;
  3478. +
  3479. + } else { // USB_PR_CB or USB_PR_CBI
  3480. +
  3481. + /* Wait for the next command to arrive */
  3482. + while (fsg->cbbuf_cmnd_size == 0) {
  3483. + rc = sleep_thread(fsg);
  3484. + if (rc)
  3485. + return rc;
  3486. + }
  3487. +
  3488. + /* Is the previous status interrupt request still busy?
  3489. + * The host is allowed to skip reading the status,
  3490. + * so we must cancel it. */
  3491. + if (fsg->intreq_busy)
  3492. + usb_ep_dequeue(fsg->intr_in, fsg->intreq);
  3493. +
  3494. + /* Copy the command and mark the buffer empty */
  3495. + fsg->data_dir = DATA_DIR_UNKNOWN;
  3496. + spin_lock_irq(&fsg->lock);
  3497. + fsg->cmnd_size = fsg->cbbuf_cmnd_size;
  3498. + memcpy(fsg->cmnd, fsg->cbbuf_cmnd, fsg->cmnd_size);
  3499. + fsg->cbbuf_cmnd_size = 0;
  3500. + spin_unlock_irq(&fsg->lock);
  3501. +
  3502. + /* Use LUN from the command */
  3503. + fsg->lun = fsg->cmnd[1] >> 5;
  3504. + }
  3505. +
  3506. + /* Update current lun */
  3507. + if (fsg->lun >= 0 && fsg->lun < fsg->nluns)
  3508. + fsg->curlun = &fsg->luns[fsg->lun];
  3509. + else
  3510. + fsg->curlun = NULL;
  3511. +
  3512. + return rc;
  3513. +}
  3514. +
  3515. +
  3516. +/*-------------------------------------------------------------------------*/
  3517. +
  3518. +static int enable_endpoint(struct fsg_dev *fsg, struct usb_ep *ep,
  3519. + const struct usb_endpoint_descriptor *d)
  3520. +{
  3521. + int rc;
  3522. +
  3523. + ep->driver_data = fsg;
  3524. + ep->desc = d;
  3525. + rc = usb_ep_enable(ep);
  3526. + if (rc)
  3527. + ERROR(fsg, "can't enable %s, result %d\n", ep->name, rc);
  3528. + return rc;
  3529. +}
  3530. +
  3531. +static int alloc_request(struct fsg_dev *fsg, struct usb_ep *ep,
  3532. + struct usb_request **preq)
  3533. +{
  3534. + *preq = usb_ep_alloc_request(ep, GFP_ATOMIC);
  3535. + if (*preq)
  3536. + return 0;
  3537. + ERROR(fsg, "can't allocate request for %s\n", ep->name);
  3538. + return -ENOMEM;
  3539. +}
  3540. +
  3541. +/*
  3542. + * Reset interface setting and re-init endpoint state (toggle etc).
  3543. + * Call with altsetting < 0 to disable the interface. The only other
  3544. + * available altsetting is 0, which enables the interface.
  3545. + */
  3546. +static int do_set_interface(struct fsg_dev *fsg, int altsetting)
  3547. +{
  3548. + int rc = 0;
  3549. + int i;
  3550. + const struct usb_endpoint_descriptor *d;
  3551. +
  3552. + if (fsg->running)
  3553. + DBG(fsg, "reset interface\n");
  3554. +
  3555. +reset:
  3556. + /* Deallocate the requests */
  3557. + for (i = 0; i < fsg_num_buffers; ++i) {
  3558. + struct fsg_buffhd *bh = &fsg->buffhds[i];
  3559. +
  3560. + if (bh->inreq) {
  3561. + usb_ep_free_request(fsg->bulk_in, bh->inreq);
  3562. + bh->inreq = NULL;
  3563. + }
  3564. + if (bh->outreq) {
  3565. + usb_ep_free_request(fsg->bulk_out, bh->outreq);
  3566. + bh->outreq = NULL;
  3567. + }
  3568. + }
  3569. + if (fsg->intreq) {
  3570. + usb_ep_free_request(fsg->intr_in, fsg->intreq);
  3571. + fsg->intreq = NULL;
  3572. + }
  3573. +
  3574. + /* Disable the endpoints */
  3575. + if (fsg->bulk_in_enabled) {
  3576. + usb_ep_disable(fsg->bulk_in);
  3577. + fsg->bulk_in_enabled = 0;
  3578. + }
  3579. + if (fsg->bulk_out_enabled) {
  3580. + usb_ep_disable(fsg->bulk_out);
  3581. + fsg->bulk_out_enabled = 0;
  3582. + }
  3583. + if (fsg->intr_in_enabled) {
  3584. + usb_ep_disable(fsg->intr_in);
  3585. + fsg->intr_in_enabled = 0;
  3586. + }
  3587. +
  3588. + fsg->running = 0;
  3589. + if (altsetting < 0 || rc != 0)
  3590. + return rc;
  3591. +
  3592. + DBG(fsg, "set interface %d\n", altsetting);
  3593. +
  3594. + /* Enable the endpoints */
  3595. + d = fsg_ep_desc(fsg->gadget,
  3596. + &fsg_fs_bulk_in_desc, &fsg_hs_bulk_in_desc,
  3597. + &fsg_ss_bulk_in_desc);
  3598. + if ((rc = enable_endpoint(fsg, fsg->bulk_in, d)) != 0)
  3599. + goto reset;
  3600. + fsg->bulk_in_enabled = 1;
  3601. +
  3602. + d = fsg_ep_desc(fsg->gadget,
  3603. + &fsg_fs_bulk_out_desc, &fsg_hs_bulk_out_desc,
  3604. + &fsg_ss_bulk_out_desc);
  3605. + if ((rc = enable_endpoint(fsg, fsg->bulk_out, d)) != 0)
  3606. + goto reset;
  3607. + fsg->bulk_out_enabled = 1;
  3608. + fsg->bulk_out_maxpacket = usb_endpoint_maxp(d);
  3609. + clear_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags);
  3610. +
  3611. + if (transport_is_cbi()) {
  3612. + d = fsg_ep_desc(fsg->gadget,
  3613. + &fsg_fs_intr_in_desc, &fsg_hs_intr_in_desc,
  3614. + &fsg_ss_intr_in_desc);
  3615. + if ((rc = enable_endpoint(fsg, fsg->intr_in, d)) != 0)
  3616. + goto reset;
  3617. + fsg->intr_in_enabled = 1;
  3618. + }
  3619. +
  3620. + /* Allocate the requests */
  3621. + for (i = 0; i < fsg_num_buffers; ++i) {
  3622. + struct fsg_buffhd *bh = &fsg->buffhds[i];
  3623. +
  3624. + if ((rc = alloc_request(fsg, fsg->bulk_in, &bh->inreq)) != 0)
  3625. + goto reset;
  3626. + if ((rc = alloc_request(fsg, fsg->bulk_out, &bh->outreq)) != 0)
  3627. + goto reset;
  3628. + bh->inreq->buf = bh->outreq->buf = bh->buf;
  3629. + bh->inreq->context = bh->outreq->context = bh;
  3630. + bh->inreq->complete = bulk_in_complete;
  3631. + bh->outreq->complete = bulk_out_complete;
  3632. + }
  3633. + if (transport_is_cbi()) {
  3634. + if ((rc = alloc_request(fsg, fsg->intr_in, &fsg->intreq)) != 0)
  3635. + goto reset;
  3636. + fsg->intreq->complete = intr_in_complete;
  3637. + }
  3638. +
  3639. + fsg->running = 1;
  3640. + for (i = 0; i < fsg->nluns; ++i)
  3641. + fsg->luns[i].unit_attention_data = SS_RESET_OCCURRED;
  3642. + return rc;
  3643. +}
  3644. +
  3645. +
  3646. +/*
  3647. + * Change our operational configuration. This code must agree with the code
  3648. + * that returns config descriptors, and with interface altsetting code.
  3649. + *
  3650. + * It's also responsible for power management interactions. Some
  3651. + * configurations might not work with our current power sources.
  3652. + * For now we just assume the gadget is always self-powered.
  3653. + */
  3654. +static int do_set_config(struct fsg_dev *fsg, u8 new_config)
  3655. +{
  3656. + int rc = 0;
  3657. +
  3658. + /* Disable the single interface */
  3659. + if (fsg->config != 0) {
  3660. + DBG(fsg, "reset config\n");
  3661. + fsg->config = 0;
  3662. + rc = do_set_interface(fsg, -1);
  3663. + }
  3664. +
  3665. + /* Enable the interface */
  3666. + if (new_config != 0) {
  3667. + fsg->config = new_config;
  3668. + if ((rc = do_set_interface(fsg, 0)) != 0)
  3669. + fsg->config = 0; // Reset on errors
  3670. + else
  3671. + INFO(fsg, "%s config #%d\n",
  3672. + usb_speed_string(fsg->gadget->speed),
  3673. + fsg->config);
  3674. + }
  3675. + return rc;
  3676. +}
  3677. +
  3678. +
  3679. +/*-------------------------------------------------------------------------*/
  3680. +
  3681. +static void handle_exception(struct fsg_dev *fsg)
  3682. +{
  3683. + siginfo_t info;
  3684. + int sig;
  3685. + int i;
  3686. + int num_active;
  3687. + struct fsg_buffhd *bh;
  3688. + enum fsg_state old_state;
  3689. + u8 new_config;
  3690. + struct fsg_lun *curlun;
  3691. + unsigned int exception_req_tag;
  3692. + int rc;
  3693. +
  3694. + /* Clear the existing signals. Anything but SIGUSR1 is converted
  3695. + * into a high-priority EXIT exception. */
  3696. + for (;;) {
  3697. + sig = dequeue_signal_lock(current, &current->blocked, &info);
  3698. + if (!sig)
  3699. + break;
  3700. + if (sig != SIGUSR1) {
  3701. + if (fsg->state < FSG_STATE_EXIT)
  3702. + DBG(fsg, "Main thread exiting on signal\n");
  3703. + raise_exception(fsg, FSG_STATE_EXIT);
  3704. + }
  3705. + }
  3706. +
  3707. + /* Cancel all the pending transfers */
  3708. + if (fsg->intreq_busy)
  3709. + usb_ep_dequeue(fsg->intr_in, fsg->intreq);
  3710. + for (i = 0; i < fsg_num_buffers; ++i) {
  3711. + bh = &fsg->buffhds[i];
  3712. + if (bh->inreq_busy)
  3713. + usb_ep_dequeue(fsg->bulk_in, bh->inreq);
  3714. + if (bh->outreq_busy)
  3715. + usb_ep_dequeue(fsg->bulk_out, bh->outreq);
  3716. + }
  3717. +
  3718. + /* Wait until everything is idle */
  3719. + for (;;) {
  3720. + num_active = fsg->intreq_busy;
  3721. + for (i = 0; i < fsg_num_buffers; ++i) {
  3722. + bh = &fsg->buffhds[i];
  3723. + num_active += bh->inreq_busy + bh->outreq_busy;
  3724. + }
  3725. + if (num_active == 0)
  3726. + break;
  3727. + if (sleep_thread(fsg))
  3728. + return;
  3729. + }
  3730. +
  3731. + /* Clear out the controller's fifos */
  3732. + if (fsg->bulk_in_enabled)
  3733. + usb_ep_fifo_flush(fsg->bulk_in);
  3734. + if (fsg->bulk_out_enabled)
  3735. + usb_ep_fifo_flush(fsg->bulk_out);
  3736. + if (fsg->intr_in_enabled)
  3737. + usb_ep_fifo_flush(fsg->intr_in);
  3738. +
  3739. + /* Reset the I/O buffer states and pointers, the SCSI
  3740. + * state, and the exception. Then invoke the handler. */
  3741. + spin_lock_irq(&fsg->lock);
  3742. +
  3743. + for (i = 0; i < fsg_num_buffers; ++i) {
  3744. + bh = &fsg->buffhds[i];
  3745. + bh->state = BUF_STATE_EMPTY;
  3746. + }
  3747. + fsg->next_buffhd_to_fill = fsg->next_buffhd_to_drain =
  3748. + &fsg->buffhds[0];
  3749. +
  3750. + exception_req_tag = fsg->exception_req_tag;
  3751. + new_config = fsg->new_config;
  3752. + old_state = fsg->state;
  3753. +
  3754. + if (old_state == FSG_STATE_ABORT_BULK_OUT)
  3755. + fsg->state = FSG_STATE_STATUS_PHASE;
  3756. + else {
  3757. + for (i = 0; i < fsg->nluns; ++i) {
  3758. + curlun = &fsg->luns[i];
  3759. + curlun->prevent_medium_removal = 0;
  3760. + curlun->sense_data = curlun->unit_attention_data =
  3761. + SS_NO_SENSE;
  3762. + curlun->sense_data_info = 0;
  3763. + curlun->info_valid = 0;
  3764. + }
  3765. + fsg->state = FSG_STATE_IDLE;
  3766. + }
  3767. + spin_unlock_irq(&fsg->lock);
  3768. +
  3769. + /* Carry out any extra actions required for the exception */
  3770. + switch (old_state) {
  3771. + default:
  3772. + break;
  3773. +
  3774. + case FSG_STATE_ABORT_BULK_OUT:
  3775. + send_status(fsg);
  3776. + spin_lock_irq(&fsg->lock);
  3777. + if (fsg->state == FSG_STATE_STATUS_PHASE)
  3778. + fsg->state = FSG_STATE_IDLE;
  3779. + spin_unlock_irq(&fsg->lock);
  3780. + break;
  3781. +
  3782. + case FSG_STATE_RESET:
  3783. + /* In case we were forced against our will to halt a
  3784. + * bulk endpoint, clear the halt now. (The SuperH UDC
  3785. + * requires this.) */
  3786. + if (test_and_clear_bit(IGNORE_BULK_OUT, &fsg->atomic_bitflags))
  3787. + usb_ep_clear_halt(fsg->bulk_in);
  3788. +
  3789. + if (transport_is_bbb()) {
  3790. + if (fsg->ep0_req_tag == exception_req_tag)
  3791. + ep0_queue(fsg); // Complete the status stage
  3792. +
  3793. + } else if (transport_is_cbi())
  3794. + send_status(fsg); // Status by interrupt pipe
  3795. +
  3796. + /* Technically this should go here, but it would only be
  3797. + * a waste of time. Ditto for the INTERFACE_CHANGE and
  3798. + * CONFIG_CHANGE cases. */
  3799. + // for (i = 0; i < fsg->nluns; ++i)
  3800. + // fsg->luns[i].unit_attention_data = SS_RESET_OCCURRED;
  3801. + break;
  3802. +
  3803. + case FSG_STATE_INTERFACE_CHANGE:
  3804. + rc = do_set_interface(fsg, 0);
  3805. + if (fsg->ep0_req_tag != exception_req_tag)
  3806. + break;
  3807. + if (rc != 0) // STALL on errors
  3808. + fsg_set_halt(fsg, fsg->ep0);
  3809. + else // Complete the status stage
  3810. + ep0_queue(fsg);
  3811. + break;
  3812. +
  3813. + case FSG_STATE_CONFIG_CHANGE:
  3814. + rc = do_set_config(fsg, new_config);
  3815. + if (fsg->ep0_req_tag != exception_req_tag)
  3816. + break;
  3817. + if (rc != 0) // STALL on errors
  3818. + fsg_set_halt(fsg, fsg->ep0);
  3819. + else // Complete the status stage
  3820. + ep0_queue(fsg);
  3821. + break;
  3822. +
  3823. + case FSG_STATE_DISCONNECT:
  3824. + for (i = 0; i < fsg->nluns; ++i)
  3825. + fsg_lun_fsync_sub(fsg->luns + i);
  3826. + do_set_config(fsg, 0); // Unconfigured state
  3827. + break;
  3828. +
  3829. + case FSG_STATE_EXIT:
  3830. + case FSG_STATE_TERMINATED:
  3831. + do_set_config(fsg, 0); // Free resources
  3832. + spin_lock_irq(&fsg->lock);
  3833. + fsg->state = FSG_STATE_TERMINATED; // Stop the thread
  3834. + spin_unlock_irq(&fsg->lock);
  3835. + break;
  3836. + }
  3837. +}
  3838. +
  3839. +
  3840. +/*-------------------------------------------------------------------------*/
  3841. +
  3842. +static int fsg_main_thread(void *fsg_)
  3843. +{
  3844. + struct fsg_dev *fsg = fsg_;
  3845. +
  3846. + /* Allow the thread to be killed by a signal, but set the signal mask
  3847. + * to block everything but INT, TERM, KILL, and USR1. */
  3848. + allow_signal(SIGINT);
  3849. + allow_signal(SIGTERM);
  3850. + allow_signal(SIGKILL);
  3851. + allow_signal(SIGUSR1);
  3852. +
  3853. + /* Allow the thread to be frozen */
  3854. + set_freezable();
  3855. +
  3856. + /* Arrange for userspace references to be interpreted as kernel
  3857. + * pointers. That way we can pass a kernel pointer to a routine
  3858. + * that expects a __user pointer and it will work okay. */
  3859. + set_fs(get_ds());
  3860. +
  3861. + /* The main loop */
  3862. + while (fsg->state != FSG_STATE_TERMINATED) {
  3863. + if (exception_in_progress(fsg) || signal_pending(current)) {
  3864. + handle_exception(fsg);
  3865. + continue;
  3866. + }
  3867. +
  3868. + if (!fsg->running) {
  3869. + sleep_thread(fsg);
  3870. + continue;
  3871. + }
  3872. +
  3873. + if (get_next_command(fsg))
  3874. + continue;
  3875. +
  3876. + spin_lock_irq(&fsg->lock);
  3877. + if (!exception_in_progress(fsg))
  3878. + fsg->state = FSG_STATE_DATA_PHASE;
  3879. + spin_unlock_irq(&fsg->lock);
  3880. +
  3881. + if (do_scsi_command(fsg) || finish_reply(fsg))
  3882. + continue;
  3883. +
  3884. + spin_lock_irq(&fsg->lock);
  3885. + if (!exception_in_progress(fsg))
  3886. + fsg->state = FSG_STATE_STATUS_PHASE;
  3887. + spin_unlock_irq(&fsg->lock);
  3888. +
  3889. + if (send_status(fsg))
  3890. + continue;
  3891. +
  3892. + spin_lock_irq(&fsg->lock);
  3893. + if (!exception_in_progress(fsg))
  3894. + fsg->state = FSG_STATE_IDLE;
  3895. + spin_unlock_irq(&fsg->lock);
  3896. + }
  3897. +
  3898. + spin_lock_irq(&fsg->lock);
  3899. + fsg->thread_task = NULL;
  3900. + spin_unlock_irq(&fsg->lock);
  3901. +
  3902. + /* If we are exiting because of a signal, unregister the
  3903. + * gadget driver. */
  3904. + if (test_and_clear_bit(REGISTERED, &fsg->atomic_bitflags))
  3905. + usb_gadget_unregister_driver(&fsg_driver);
  3906. +
  3907. + /* Let the unbind and cleanup routines know the thread has exited */
  3908. + complete_and_exit(&fsg->thread_notifier, 0);
  3909. +}
  3910. +
  3911. +
  3912. +/*-------------------------------------------------------------------------*/
  3913. +
  3914. +
  3915. +/* The write permissions and store_xxx pointers are set in fsg_bind() */
  3916. +static DEVICE_ATTR(ro, 0444, fsg_show_ro, NULL);
  3917. +static DEVICE_ATTR(nofua, 0644, fsg_show_nofua, NULL);
  3918. +static DEVICE_ATTR(file, 0444, fsg_show_file, NULL);
  3919. +
  3920. +
  3921. +/*-------------------------------------------------------------------------*/
  3922. +
  3923. +static void fsg_release(struct kref *ref)
  3924. +{
  3925. + struct fsg_dev *fsg = container_of(ref, struct fsg_dev, ref);
  3926. +
  3927. + kfree(fsg->luns);
  3928. + kfree(fsg);
  3929. +}
  3930. +
  3931. +static void lun_release(struct device *dev)
  3932. +{
  3933. + struct rw_semaphore *filesem = dev_get_drvdata(dev);
  3934. + struct fsg_dev *fsg =
  3935. + container_of(filesem, struct fsg_dev, filesem);
  3936. +
  3937. + kref_put(&fsg->ref, fsg_release);
  3938. +}
  3939. +
  3940. +static void /* __init_or_exit */ fsg_unbind(struct usb_gadget *gadget)
  3941. +{
  3942. + struct fsg_dev *fsg = get_gadget_data(gadget);
  3943. + int i;
  3944. + struct fsg_lun *curlun;
  3945. + struct usb_request *req = fsg->ep0req;
  3946. +
  3947. + DBG(fsg, "unbind\n");
  3948. + clear_bit(REGISTERED, &fsg->atomic_bitflags);
  3949. +
  3950. + /* If the thread isn't already dead, tell it to exit now */
  3951. + if (fsg->state != FSG_STATE_TERMINATED) {
  3952. + raise_exception(fsg, FSG_STATE_EXIT);
  3953. + wait_for_completion(&fsg->thread_notifier);
  3954. +
  3955. + /* The cleanup routine waits for this completion also */
  3956. + complete(&fsg->thread_notifier);
  3957. + }
  3958. +
  3959. + /* Unregister the sysfs attribute files and the LUNs */
  3960. + for (i = 0; i < fsg->nluns; ++i) {
  3961. + curlun = &fsg->luns[i];
  3962. + if (curlun->registered) {
  3963. + device_remove_file(&curlun->dev, &dev_attr_nofua);
  3964. + device_remove_file(&curlun->dev, &dev_attr_ro);
  3965. + device_remove_file(&curlun->dev, &dev_attr_file);
  3966. + fsg_lun_close(curlun);
  3967. + device_unregister(&curlun->dev);
  3968. + curlun->registered = 0;
  3969. + }
  3970. + }
  3971. +
  3972. + /* Free the data buffers */
  3973. + for (i = 0; i < fsg_num_buffers; ++i)
  3974. + kfree(fsg->buffhds[i].buf);
  3975. +
  3976. + /* Free the request and buffer for endpoint 0 */
  3977. + if (req) {
  3978. + kfree(req->buf);
  3979. + usb_ep_free_request(fsg->ep0, req);
  3980. + }
  3981. +
  3982. + set_gadget_data(gadget, NULL);
  3983. +}
  3984. +
  3985. +
  3986. +static int __init check_parameters(struct fsg_dev *fsg)
  3987. +{
  3988. + int prot;
  3989. + int gcnum;
  3990. +
  3991. + /* Store the default values */
  3992. + mod_data.transport_type = USB_PR_BULK;
  3993. + mod_data.transport_name = "Bulk-only";
  3994. + mod_data.protocol_type = USB_SC_SCSI;
  3995. + mod_data.protocol_name = "Transparent SCSI";
  3996. +
  3997. + /* Some peripheral controllers are known not to be able to
  3998. + * halt bulk endpoints correctly. If one of them is present,
  3999. + * disable stalls.
  4000. + */
  4001. + if (gadget_is_at91(fsg->gadget))
  4002. + mod_data.can_stall = 0;
  4003. +
  4004. + if (mod_data.release == 0xffff) { // Parameter wasn't set
  4005. + gcnum = usb_gadget_controller_number(fsg->gadget);
  4006. + if (gcnum >= 0)
  4007. + mod_data.release = 0x0300 + gcnum;
  4008. + else {
  4009. + WARNING(fsg, "controller '%s' not recognized\n",
  4010. + fsg->gadget->name);
  4011. + mod_data.release = 0x0399;
  4012. + }
  4013. + }
  4014. +
  4015. + prot = simple_strtol(mod_data.protocol_parm, NULL, 0);
  4016. +
  4017. +#ifdef CONFIG_USB_FILE_STORAGE_TEST
  4018. + if (strnicmp(mod_data.transport_parm, "BBB", 10) == 0) {
  4019. + ; // Use default setting
  4020. + } else if (strnicmp(mod_data.transport_parm, "CB", 10) == 0) {
  4021. + mod_data.transport_type = USB_PR_CB;
  4022. + mod_data.transport_name = "Control-Bulk";
  4023. + } else if (strnicmp(mod_data.transport_parm, "CBI", 10) == 0) {
  4024. + mod_data.transport_type = USB_PR_CBI;
  4025. + mod_data.transport_name = "Control-Bulk-Interrupt";
  4026. + } else {
  4027. + ERROR(fsg, "invalid transport: %s\n", mod_data.transport_parm);
  4028. + return -EINVAL;
  4029. + }
  4030. +
  4031. + if (strnicmp(mod_data.protocol_parm, "SCSI", 10) == 0 ||
  4032. + prot == USB_SC_SCSI) {
  4033. + ; // Use default setting
  4034. + } else if (strnicmp(mod_data.protocol_parm, "RBC", 10) == 0 ||
  4035. + prot == USB_SC_RBC) {
  4036. + mod_data.protocol_type = USB_SC_RBC;
  4037. + mod_data.protocol_name = "RBC";
  4038. + } else if (strnicmp(mod_data.protocol_parm, "8020", 4) == 0 ||
  4039. + strnicmp(mod_data.protocol_parm, "ATAPI", 10) == 0 ||
  4040. + prot == USB_SC_8020) {
  4041. + mod_data.protocol_type = USB_SC_8020;
  4042. + mod_data.protocol_name = "8020i (ATAPI)";
  4043. + } else if (strnicmp(mod_data.protocol_parm, "QIC", 3) == 0 ||
  4044. + prot == USB_SC_QIC) {
  4045. + mod_data.protocol_type = USB_SC_QIC;
  4046. + mod_data.protocol_name = "QIC-157";
  4047. + } else if (strnicmp(mod_data.protocol_parm, "UFI", 10) == 0 ||
  4048. + prot == USB_SC_UFI) {
  4049. + mod_data.protocol_type = USB_SC_UFI;
  4050. + mod_data.protocol_name = "UFI";
  4051. + } else if (strnicmp(mod_data.protocol_parm, "8070", 4) == 0 ||
  4052. + prot == USB_SC_8070) {
  4053. + mod_data.protocol_type = USB_SC_8070;
  4054. + mod_data.protocol_name = "8070i";
  4055. + } else {
  4056. + ERROR(fsg, "invalid protocol: %s\n", mod_data.protocol_parm);
  4057. + return -EINVAL;
  4058. + }
  4059. +
  4060. + mod_data.buflen &= PAGE_CACHE_MASK;
  4061. + if (mod_data.buflen <= 0) {
  4062. + ERROR(fsg, "invalid buflen\n");
  4063. + return -ETOOSMALL;
  4064. + }
  4065. +
  4066. +#endif /* CONFIG_USB_FILE_STORAGE_TEST */
  4067. +
  4068. + /* Serial string handling.
  4069. + * On a real device, the serial string would be loaded
  4070. + * from permanent storage. */
  4071. + if (mod_data.serial) {
  4072. + const char *ch;
  4073. + unsigned len = 0;
  4074. +
  4075. + /* Sanity check :
  4076. + * The CB[I] specification limits the serial string to
  4077. + * 12 uppercase hexadecimal characters.
  4078. + * BBB need at least 12 uppercase hexadecimal characters,
  4079. + * with a maximum of 126. */
  4080. + for (ch = mod_data.serial; *ch; ++ch) {
  4081. + ++len;
  4082. + if ((*ch < '0' || *ch > '9') &&
  4083. + (*ch < 'A' || *ch > 'F')) { /* not uppercase hex */
  4084. + WARNING(fsg,
  4085. + "Invalid serial string character: %c\n",
  4086. + *ch);
  4087. + goto no_serial;
  4088. + }
  4089. + }
  4090. + if (len > 126 ||
  4091. + (mod_data.transport_type == USB_PR_BULK && len < 12) ||
  4092. + (mod_data.transport_type != USB_PR_BULK && len > 12)) {
  4093. + WARNING(fsg, "Invalid serial string length!\n");
  4094. + goto no_serial;
  4095. + }
  4096. + fsg_strings[FSG_STRING_SERIAL - 1].s = mod_data.serial;
  4097. + } else {
  4098. + WARNING(fsg, "No serial-number string provided!\n");
  4099. + no_serial:
  4100. + device_desc.iSerialNumber = 0;
  4101. + }
  4102. +
  4103. + return 0;
  4104. +}
  4105. +
  4106. +
  4107. +static int __init fsg_bind(struct usb_gadget *gadget)
  4108. +{
  4109. + struct fsg_dev *fsg = the_fsg;
  4110. + int rc;
  4111. + int i;
  4112. + struct fsg_lun *curlun;
  4113. + struct usb_ep *ep;
  4114. + struct usb_request *req;
  4115. + char *pathbuf, *p;
  4116. +
  4117. + fsg->gadget = gadget;
  4118. + set_gadget_data(gadget, fsg);
  4119. + fsg->ep0 = gadget->ep0;
  4120. + fsg->ep0->driver_data = fsg;
  4121. +
  4122. + if ((rc = check_parameters(fsg)) != 0)
  4123. + goto out;
  4124. +
  4125. + if (mod_data.removable) { // Enable the store_xxx attributes
  4126. + dev_attr_file.attr.mode = 0644;
  4127. + dev_attr_file.store = fsg_store_file;
  4128. + if (!mod_data.cdrom) {
  4129. + dev_attr_ro.attr.mode = 0644;
  4130. + dev_attr_ro.store = fsg_store_ro;
  4131. + }
  4132. + }
  4133. +
  4134. + /* Only for removable media? */
  4135. + dev_attr_nofua.attr.mode = 0644;
  4136. + dev_attr_nofua.store = fsg_store_nofua;
  4137. +
  4138. + /* Find out how many LUNs there should be */
  4139. + i = mod_data.nluns;
  4140. + if (i == 0)
  4141. + i = max(mod_data.num_filenames, 1u);
  4142. + if (i > FSG_MAX_LUNS) {
  4143. + ERROR(fsg, "invalid number of LUNs: %d\n", i);
  4144. + rc = -EINVAL;
  4145. + goto out;
  4146. + }
  4147. +
  4148. + /* Create the LUNs, open their backing files, and register the
  4149. + * LUN devices in sysfs. */
  4150. + fsg->luns = kzalloc(i * sizeof(struct fsg_lun), GFP_KERNEL);
  4151. + if (!fsg->luns) {
  4152. + rc = -ENOMEM;
  4153. + goto out;
  4154. + }
  4155. + fsg->nluns = i;
  4156. +
  4157. + for (i = 0; i < fsg->nluns; ++i) {
  4158. + curlun = &fsg->luns[i];
  4159. + curlun->cdrom = !!mod_data.cdrom;
  4160. + curlun->ro = mod_data.cdrom || mod_data.ro[i];
  4161. + curlun->initially_ro = curlun->ro;
  4162. + curlun->removable = mod_data.removable;
  4163. + curlun->nofua = mod_data.nofua[i];
  4164. + curlun->dev.release = lun_release;
  4165. + curlun->dev.parent = &gadget->dev;
  4166. + curlun->dev.driver = &fsg_driver.driver;
  4167. + dev_set_drvdata(&curlun->dev, &fsg->filesem);
  4168. + dev_set_name(&curlun->dev,"%s-lun%d",
  4169. + dev_name(&gadget->dev), i);
  4170. +
  4171. + kref_get(&fsg->ref);
  4172. + rc = device_register(&curlun->dev);
  4173. + if (rc) {
  4174. + INFO(fsg, "failed to register LUN%d: %d\n", i, rc);
  4175. + put_device(&curlun->dev);
  4176. + goto out;
  4177. + }
  4178. + curlun->registered = 1;
  4179. +
  4180. + rc = device_create_file(&curlun->dev, &dev_attr_ro);
  4181. + if (rc)
  4182. + goto out;
  4183. + rc = device_create_file(&curlun->dev, &dev_attr_nofua);
  4184. + if (rc)
  4185. + goto out;
  4186. + rc = device_create_file(&curlun->dev, &dev_attr_file);
  4187. + if (rc)
  4188. + goto out;
  4189. +
  4190. + if (mod_data.file[i] && *mod_data.file[i]) {
  4191. + rc = fsg_lun_open(curlun, mod_data.file[i]);
  4192. + if (rc)
  4193. + goto out;
  4194. + } else if (!mod_data.removable) {
  4195. + ERROR(fsg, "no file given for LUN%d\n", i);
  4196. + rc = -EINVAL;
  4197. + goto out;
  4198. + }
  4199. + }
  4200. +
  4201. + /* Find all the endpoints we will use */
  4202. + usb_ep_autoconfig_reset(gadget);
  4203. + ep = usb_ep_autoconfig(gadget, &fsg_fs_bulk_in_desc);
  4204. + if (!ep)
  4205. + goto autoconf_fail;
  4206. + ep->driver_data = fsg; // claim the endpoint
  4207. + fsg->bulk_in = ep;
  4208. +
  4209. + ep = usb_ep_autoconfig(gadget, &fsg_fs_bulk_out_desc);
  4210. + if (!ep)
  4211. + goto autoconf_fail;
  4212. + ep->driver_data = fsg; // claim the endpoint
  4213. + fsg->bulk_out = ep;
  4214. +
  4215. + if (transport_is_cbi()) {
  4216. + ep = usb_ep_autoconfig(gadget, &fsg_fs_intr_in_desc);
  4217. + if (!ep)
  4218. + goto autoconf_fail;
  4219. + ep->driver_data = fsg; // claim the endpoint
  4220. + fsg->intr_in = ep;
  4221. + }
  4222. +
  4223. + /* Fix up the descriptors */
  4224. + device_desc.idVendor = cpu_to_le16(mod_data.vendor);
  4225. + device_desc.idProduct = cpu_to_le16(mod_data.product);
  4226. + device_desc.bcdDevice = cpu_to_le16(mod_data.release);
  4227. +
  4228. + i = (transport_is_cbi() ? 3 : 2); // Number of endpoints
  4229. + fsg_intf_desc.bNumEndpoints = i;
  4230. + fsg_intf_desc.bInterfaceSubClass = mod_data.protocol_type;
  4231. + fsg_intf_desc.bInterfaceProtocol = mod_data.transport_type;
  4232. + fsg_fs_function[i + FSG_FS_FUNCTION_PRE_EP_ENTRIES] = NULL;
  4233. +
  4234. + if (gadget_is_dualspeed(gadget)) {
  4235. + fsg_hs_function[i + FSG_HS_FUNCTION_PRE_EP_ENTRIES] = NULL;
  4236. +
  4237. + /* Assume endpoint addresses are the same for both speeds */
  4238. + fsg_hs_bulk_in_desc.bEndpointAddress =
  4239. + fsg_fs_bulk_in_desc.bEndpointAddress;
  4240. + fsg_hs_bulk_out_desc.bEndpointAddress =
  4241. + fsg_fs_bulk_out_desc.bEndpointAddress;
  4242. + fsg_hs_intr_in_desc.bEndpointAddress =
  4243. + fsg_fs_intr_in_desc.bEndpointAddress;
  4244. + }
  4245. +
  4246. + if (gadget_is_superspeed(gadget)) {
  4247. + unsigned max_burst;
  4248. +
  4249. + fsg_ss_function[i + FSG_SS_FUNCTION_PRE_EP_ENTRIES] = NULL;
  4250. +
  4251. + /* Calculate bMaxBurst, we know packet size is 1024 */
  4252. + max_burst = min_t(unsigned, mod_data.buflen / 1024, 15);
  4253. +
  4254. + /* Assume endpoint addresses are the same for both speeds */
  4255. + fsg_ss_bulk_in_desc.bEndpointAddress =
  4256. + fsg_fs_bulk_in_desc.bEndpointAddress;
  4257. + fsg_ss_bulk_in_comp_desc.bMaxBurst = max_burst;
  4258. +
  4259. + fsg_ss_bulk_out_desc.bEndpointAddress =
  4260. + fsg_fs_bulk_out_desc.bEndpointAddress;
  4261. + fsg_ss_bulk_out_comp_desc.bMaxBurst = max_burst;
  4262. + }
  4263. +
  4264. + if (gadget_is_otg(gadget))
  4265. + fsg_otg_desc.bmAttributes |= USB_OTG_HNP;
  4266. +
  4267. + rc = -ENOMEM;
  4268. +
  4269. + /* Allocate the request and buffer for endpoint 0 */
  4270. + fsg->ep0req = req = usb_ep_alloc_request(fsg->ep0, GFP_KERNEL);
  4271. + if (!req)
  4272. + goto out;
  4273. + req->buf = kmalloc(EP0_BUFSIZE, GFP_KERNEL);
  4274. + if (!req->buf)
  4275. + goto out;
  4276. + req->complete = ep0_complete;
  4277. +
  4278. + /* Allocate the data buffers */
  4279. + for (i = 0; i < fsg_num_buffers; ++i) {
  4280. + struct fsg_buffhd *bh = &fsg->buffhds[i];
  4281. +
  4282. + /* Allocate for the bulk-in endpoint. We assume that
  4283. + * the buffer will also work with the bulk-out (and
  4284. + * interrupt-in) endpoint. */
  4285. + bh->buf = kmalloc(mod_data.buflen, GFP_KERNEL);
  4286. + if (!bh->buf)
  4287. + goto out;
  4288. + bh->next = bh + 1;
  4289. + }
  4290. + fsg->buffhds[fsg_num_buffers - 1].next = &fsg->buffhds[0];
  4291. +
  4292. + /* This should reflect the actual gadget power source */
  4293. + usb_gadget_set_selfpowered(gadget);
  4294. +
  4295. + snprintf(fsg_string_manufacturer, sizeof fsg_string_manufacturer,
  4296. + "%s %s with %s",
  4297. + init_utsname()->sysname, init_utsname()->release,
  4298. + gadget->name);
  4299. +
  4300. + fsg->thread_task = kthread_create(fsg_main_thread, fsg,
  4301. + "file-storage-gadget");
  4302. + if (IS_ERR(fsg->thread_task)) {
  4303. + rc = PTR_ERR(fsg->thread_task);
  4304. + goto out;
  4305. + }
  4306. +
  4307. + INFO(fsg, DRIVER_DESC ", version: " DRIVER_VERSION "\n");
  4308. + INFO(fsg, "NOTE: This driver is deprecated. "
  4309. + "Consider using g_mass_storage instead.\n");
  4310. + INFO(fsg, "Number of LUNs=%d\n", fsg->nluns);
  4311. +
  4312. + pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
  4313. + for (i = 0; i < fsg->nluns; ++i) {
  4314. + curlun = &fsg->luns[i];
  4315. + if (fsg_lun_is_open(curlun)) {
  4316. + p = NULL;
  4317. + if (pathbuf) {
  4318. + p = d_path(&curlun->filp->f_path,
  4319. + pathbuf, PATH_MAX);
  4320. + if (IS_ERR(p))
  4321. + p = NULL;
  4322. + }
  4323. + LINFO(curlun, "ro=%d, nofua=%d, file: %s\n",
  4324. + curlun->ro, curlun->nofua, (p ? p : "(error)"));
  4325. + }
  4326. + }
  4327. + kfree(pathbuf);
  4328. +
  4329. + DBG(fsg, "transport=%s (x%02x)\n",
  4330. + mod_data.transport_name, mod_data.transport_type);
  4331. + DBG(fsg, "protocol=%s (x%02x)\n",
  4332. + mod_data.protocol_name, mod_data.protocol_type);
  4333. + DBG(fsg, "VendorID=x%04x, ProductID=x%04x, Release=x%04x\n",
  4334. + mod_data.vendor, mod_data.product, mod_data.release);
  4335. + DBG(fsg, "removable=%d, stall=%d, cdrom=%d, buflen=%u\n",
  4336. + mod_data.removable, mod_data.can_stall,
  4337. + mod_data.cdrom, mod_data.buflen);
  4338. + DBG(fsg, "I/O thread pid: %d\n", task_pid_nr(fsg->thread_task));
  4339. +
  4340. + set_bit(REGISTERED, &fsg->atomic_bitflags);
  4341. +
  4342. + /* Tell the thread to start working */
  4343. + wake_up_process(fsg->thread_task);
  4344. + return 0;
  4345. +
  4346. +autoconf_fail:
  4347. + ERROR(fsg, "unable to autoconfigure all endpoints\n");
  4348. + rc = -ENOTSUPP;
  4349. +
  4350. +out:
  4351. + fsg->state = FSG_STATE_TERMINATED; // The thread is dead
  4352. + fsg_unbind(gadget);
  4353. + complete(&fsg->thread_notifier);
  4354. + return rc;
  4355. +}
  4356. +
  4357. +
  4358. +/*-------------------------------------------------------------------------*/
  4359. +
  4360. +static void fsg_suspend(struct usb_gadget *gadget)
  4361. +{
  4362. + struct fsg_dev *fsg = get_gadget_data(gadget);
  4363. +
  4364. + DBG(fsg, "suspend\n");
  4365. + set_bit(SUSPENDED, &fsg->atomic_bitflags);
  4366. +}
  4367. +
  4368. +static void fsg_resume(struct usb_gadget *gadget)
  4369. +{
  4370. + struct fsg_dev *fsg = get_gadget_data(gadget);
  4371. +
  4372. + DBG(fsg, "resume\n");
  4373. + clear_bit(SUSPENDED, &fsg->atomic_bitflags);
  4374. +}
  4375. +
  4376. +
  4377. +/*-------------------------------------------------------------------------*/
  4378. +
  4379. +static struct usb_gadget_driver fsg_driver = {
  4380. + .max_speed = USB_SPEED_SUPER,
  4381. + .function = (char *) fsg_string_product,
  4382. + .unbind = fsg_unbind,
  4383. + .disconnect = fsg_disconnect,
  4384. + .setup = fsg_setup,
  4385. + .suspend = fsg_suspend,
  4386. + .resume = fsg_resume,
  4387. +
  4388. + .driver = {
  4389. + .name = DRIVER_NAME,
  4390. + .owner = THIS_MODULE,
  4391. + // .release = ...
  4392. + // .suspend = ...
  4393. + // .resume = ...
  4394. + },
  4395. +};
  4396. +
  4397. +
  4398. +static int __init fsg_alloc(void)
  4399. +{
  4400. + struct fsg_dev *fsg;
  4401. +
  4402. + fsg = kzalloc(sizeof *fsg +
  4403. + fsg_num_buffers * sizeof *(fsg->buffhds), GFP_KERNEL);
  4404. +
  4405. + if (!fsg)
  4406. + return -ENOMEM;
  4407. + spin_lock_init(&fsg->lock);
  4408. + init_rwsem(&fsg->filesem);
  4409. + kref_init(&fsg->ref);
  4410. + init_completion(&fsg->thread_notifier);
  4411. +
  4412. + the_fsg = fsg;
  4413. + return 0;
  4414. +}
  4415. +
  4416. +
  4417. +static int __init fsg_init(void)
  4418. +{
  4419. + int rc;
  4420. + struct fsg_dev *fsg;
  4421. +
  4422. + rc = fsg_num_buffers_validate();
  4423. + if (rc != 0)
  4424. + return rc;
  4425. +
  4426. + if ((rc = fsg_alloc()) != 0)
  4427. + return rc;
  4428. + fsg = the_fsg;
  4429. + if ((rc = usb_gadget_probe_driver(&fsg_driver, fsg_bind)) != 0)
  4430. + kref_put(&fsg->ref, fsg_release);
  4431. + return rc;
  4432. +}
  4433. +module_init(fsg_init);
  4434. +
  4435. +
  4436. +static void __exit fsg_cleanup(void)
  4437. +{
  4438. + struct fsg_dev *fsg = the_fsg;
  4439. +
  4440. + /* Unregister the driver iff the thread hasn't already done so */
  4441. + if (test_and_clear_bit(REGISTERED, &fsg->atomic_bitflags))
  4442. + usb_gadget_unregister_driver(&fsg_driver);
  4443. +
  4444. + /* Wait for the thread to finish up */
  4445. + wait_for_completion(&fsg->thread_notifier);
  4446. +
  4447. + kref_put(&fsg->ref, fsg_release);
  4448. +}
  4449. +module_exit(fsg_cleanup);
  4450. --- a/drivers/usb/host/Kconfig
  4451. +++ b/drivers/usb/host/Kconfig
  4452. @@ -754,6 +754,19 @@ config USB_HWA_HCD
  4453. To compile this driver a module, choose M here: the module
  4454. will be called "hwa-hc".
  4455. +config USB_DWCOTG
  4456. + tristate "Synopsis DWC host support"
  4457. + depends on USB
  4458. + help
  4459. + The Synopsis DWC controller is a dual-role
  4460. + host/peripheral/OTG ("On The Go") USB controllers.
  4461. +
  4462. + Enable this option to support this IP in host controller mode.
  4463. + If unsure, say N.
  4464. +
  4465. + To compile this driver as a module, choose M here: the
  4466. + modules built will be called dwc_otg and dwc_common_port.
  4467. +
  4468. config USB_IMX21_HCD
  4469. tristate "i.MX21 HCD support"
  4470. depends on ARM && ARCH_MXC
  4471. --- a/drivers/usb/host/Makefile
  4472. +++ b/drivers/usb/host/Makefile
  4473. @@ -74,6 +74,8 @@ obj-$(CONFIG_USB_SL811_CS) += sl811_cs.o
  4474. obj-$(CONFIG_USB_U132_HCD) += u132-hcd.o
  4475. obj-$(CONFIG_USB_R8A66597_HCD) += r8a66597-hcd.o
  4476. obj-$(CONFIG_USB_HWA_HCD) += hwa-hc.o
  4477. +
  4478. +obj-$(CONFIG_USB_DWCOTG) += dwc_otg/ dwc_common_port/
  4479. obj-$(CONFIG_USB_IMX21_HCD) += imx21-hcd.o
  4480. obj-$(CONFIG_USB_FSL_USB2) += fsl-mph-dr-of.o
  4481. obj-$(CONFIG_USB_EHCI_FSL) += fsl-mph-dr-of.o
  4482. --- /dev/null
  4483. +++ b/drivers/usb/host/dwc_common_port/Makefile
  4484. @@ -0,0 +1,58 @@
  4485. +#
  4486. +# Makefile for DWC_common library
  4487. +#
  4488. +
  4489. +ifneq ($(KERNELRELEASE),)
  4490. +
  4491. +ccflags-y += -DDWC_LINUX
  4492. +#ccflags-y += -DDEBUG
  4493. +#ccflags-y += -DDWC_DEBUG_REGS
  4494. +#ccflags-y += -DDWC_DEBUG_MEMORY
  4495. +
  4496. +ccflags-y += -DDWC_LIBMODULE
  4497. +ccflags-y += -DDWC_CCLIB
  4498. +#ccflags-y += -DDWC_CRYPTOLIB
  4499. +ccflags-y += -DDWC_NOTIFYLIB
  4500. +ccflags-y += -DDWC_UTFLIB
  4501. +
  4502. +obj-$(CONFIG_USB_DWCOTG) += dwc_common_port_lib.o
  4503. +dwc_common_port_lib-objs := dwc_cc.o dwc_modpow.o dwc_dh.o \
  4504. + dwc_crypto.o dwc_notifier.o \
  4505. + dwc_common_linux.o dwc_mem.o
  4506. +
  4507. +kernrelwd := $(subst ., ,$(KERNELRELEASE))
  4508. +kernrel3 := $(word 1,$(kernrelwd)).$(word 2,$(kernrelwd)).$(word 3,$(kernrelwd))
  4509. +
  4510. +ifneq ($(kernrel3),2.6.20)
  4511. +# grayg - I only know that we use ccflags-y in 2.6.31 actually
  4512. +ccflags-y += $(CPPFLAGS)
  4513. +endif
  4514. +
  4515. +else
  4516. +
  4517. +#ifeq ($(KDIR),)
  4518. +#$(error Must give "KDIR=/path/to/kernel/source" on command line or in environment)
  4519. +#endif
  4520. +
  4521. +ifeq ($(ARCH),)
  4522. +$(error Must give "ARCH=<arch>" on command line or in environment. Also, if \
  4523. + cross-compiling, must give "CROSS_COMPILE=/path/to/compiler/plus/tool-prefix-")
  4524. +endif
  4525. +
  4526. +ifeq ($(DOXYGEN),)
  4527. +DOXYGEN := doxygen
  4528. +endif
  4529. +
  4530. +default:
  4531. + $(MAKE) -C$(KDIR) M=$(PWD) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) modules
  4532. +
  4533. +docs: $(wildcard *.[hc]) doc/doxygen.cfg
  4534. + $(DOXYGEN) doc/doxygen.cfg
  4535. +
  4536. +tags: $(wildcard *.[hc])
  4537. + $(CTAGS) -e $(wildcard *.[hc]) $(wildcard linux/*.[hc]) $(wildcard $(KDIR)/include/linux/usb*.h)
  4538. +
  4539. +endif
  4540. +
  4541. +clean:
  4542. + rm -rf *.o *.ko .*.cmd *.mod.c .*.o.d .*.o.tmp modules.order Module.markers Module.symvers .tmp_versions/
  4543. --- /dev/null
  4544. +++ b/drivers/usb/host/dwc_common_port/Makefile.fbsd
  4545. @@ -0,0 +1,17 @@
  4546. +CFLAGS += -I/sys/i386/compile/GENERIC -I/sys/i386/include -I/usr/include
  4547. +CFLAGS += -DDWC_FREEBSD
  4548. +CFLAGS += -DDEBUG
  4549. +#CFLAGS += -DDWC_DEBUG_REGS
  4550. +#CFLAGS += -DDWC_DEBUG_MEMORY
  4551. +
  4552. +#CFLAGS += -DDWC_LIBMODULE
  4553. +#CFLAGS += -DDWC_CCLIB
  4554. +#CFLAGS += -DDWC_CRYPTOLIB
  4555. +#CFLAGS += -DDWC_NOTIFYLIB
  4556. +#CFLAGS += -DDWC_UTFLIB
  4557. +
  4558. +KMOD = dwc_common_port_lib
  4559. +SRCS = dwc_cc.c dwc_modpow.c dwc_dh.c dwc_crypto.c dwc_notifier.c \
  4560. + dwc_common_fbsd.c dwc_mem.c
  4561. +
  4562. +.include <bsd.kmod.mk>
  4563. --- /dev/null
  4564. +++ b/drivers/usb/host/dwc_common_port/Makefile.linux
  4565. @@ -0,0 +1,49 @@
  4566. +#
  4567. +# Makefile for DWC_common library
  4568. +#
  4569. +ifneq ($(KERNELRELEASE),)
  4570. +
  4571. +ccflags-y += -DDWC_LINUX
  4572. +#ccflags-y += -DDEBUG
  4573. +#ccflags-y += -DDWC_DEBUG_REGS
  4574. +#ccflags-y += -DDWC_DEBUG_MEMORY
  4575. +
  4576. +ccflags-y += -DDWC_LIBMODULE
  4577. +ccflags-y += -DDWC_CCLIB
  4578. +ccflags-y += -DDWC_CRYPTOLIB
  4579. +ccflags-y += -DDWC_NOTIFYLIB
  4580. +ccflags-y += -DDWC_UTFLIB
  4581. +
  4582. +obj-m := dwc_common_port_lib.o
  4583. +dwc_common_port_lib-objs := dwc_cc.o dwc_modpow.o dwc_dh.o \
  4584. + dwc_crypto.o dwc_notifier.o \
  4585. + dwc_common_linux.o dwc_mem.o
  4586. +
  4587. +else
  4588. +
  4589. +ifeq ($(KDIR),)
  4590. +$(error Must give "KDIR=/path/to/kernel/source" on command line or in environment)
  4591. +endif
  4592. +
  4593. +ifeq ($(ARCH),)
  4594. +$(error Must give "ARCH=<arch>" on command line or in environment. Also, if \
  4595. + cross-compiling, must give "CROSS_COMPILE=/path/to/compiler/plus/tool-prefix-")
  4596. +endif
  4597. +
  4598. +ifeq ($(DOXYGEN),)
  4599. +DOXYGEN := doxygen
  4600. +endif
  4601. +
  4602. +default:
  4603. + $(MAKE) -C$(KDIR) M=$(PWD) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) modules
  4604. +
  4605. +docs: $(wildcard *.[hc]) doc/doxygen.cfg
  4606. + $(DOXYGEN) doc/doxygen.cfg
  4607. +
  4608. +tags: $(wildcard *.[hc])
  4609. + $(CTAGS) -e $(wildcard *.[hc]) $(wildcard linux/*.[hc]) $(wildcard $(KDIR)/include/linux/usb*.h)
  4610. +
  4611. +endif
  4612. +
  4613. +clean:
  4614. + rm -rf *.o *.ko .*.cmd *.mod.c .*.o.d .*.o.tmp modules.order Module.markers Module.symvers .tmp_versions/
  4615. --- /dev/null
  4616. +++ b/drivers/usb/host/dwc_common_port/changes.txt
  4617. @@ -0,0 +1,174 @@
  4618. +
  4619. +dwc_read_reg32() and friends now take an additional parameter, a pointer to an
  4620. +IO context struct. The IO context struct should live in an os-dependent struct
  4621. +in your driver. As an example, the dwc_usb3 driver has an os-dependent struct
  4622. +named 'os_dep' embedded in the main device struct. So there these calls look
  4623. +like this:
  4624. +
  4625. + dwc_read_reg32(&usb3_dev->os_dep.ioctx, &pcd->dev_global_regs->dcfg);
  4626. +
  4627. + dwc_write_reg32(&usb3_dev->os_dep.ioctx,
  4628. + &pcd->dev_global_regs->dcfg, 0);
  4629. +
  4630. +Note that for the existing Linux driver ports, it is not necessary to actually
  4631. +define the 'ioctx' member in the os-dependent struct. Since Linux does not
  4632. +require an IO context, its macros for dwc_read_reg32() and friends do not
  4633. +use the context pointer, so it is optimized away by the compiler. But it is
  4634. +necessary to add the pointer parameter to all of the call sites, to be ready
  4635. +for any future ports (such as FreeBSD) which do require an IO context.
  4636. +
  4637. +
  4638. +Similarly, dwc_alloc(), dwc_alloc_atomic(), dwc_strdup(), and dwc_free() now
  4639. +take an additional parameter, a pointer to a memory context. Examples:
  4640. +
  4641. + addr = dwc_alloc(&usb3_dev->os_dep.memctx, size);
  4642. +
  4643. + dwc_free(&usb3_dev->os_dep.memctx, addr);
  4644. +
  4645. +Again, for the Linux ports, it is not necessary to actually define the memctx
  4646. +member, but it is necessary to add the pointer parameter to all of the call
  4647. +sites.
  4648. +
  4649. +
  4650. +Same for dwc_dma_alloc() and dwc_dma_free(). Examples:
  4651. +
  4652. + virt_addr = dwc_dma_alloc(&usb3_dev->os_dep.dmactx, size, &phys_addr);
  4653. +
  4654. + dwc_dma_free(&usb3_dev->os_dep.dmactx, size, virt_addr, phys_addr);
  4655. +
  4656. +
  4657. +Same for dwc_mutex_alloc() and dwc_mutex_free(). Examples:
  4658. +
  4659. + mutex = dwc_mutex_alloc(&usb3_dev->os_dep.mtxctx);
  4660. +
  4661. + dwc_mutex_free(&usb3_dev->os_dep.mtxctx, mutex);
  4662. +
  4663. +
  4664. +Same for dwc_spinlock_alloc() and dwc_spinlock_free(). Examples:
  4665. +
  4666. + lock = dwc_spinlock_alloc(&usb3_dev->osdep.splctx);
  4667. +
  4668. + dwc_spinlock_free(&usb3_dev->osdep.splctx, lock);
  4669. +
  4670. +
  4671. +Same for dwc_timer_alloc(). Example:
  4672. +
  4673. + timer = dwc_timer_alloc(&usb3_dev->os_dep.tmrctx, "dwc_usb3_tmr1",
  4674. + cb_func, cb_data);
  4675. +
  4676. +
  4677. +Same for dwc_waitq_alloc(). Example:
  4678. +
  4679. + waitq = dwc_waitq_alloc(&usb3_dev->os_dep.wtqctx);
  4680. +
  4681. +
  4682. +Same for dwc_thread_run(). Example:
  4683. +
  4684. + thread = dwc_thread_run(&usb3_dev->os_dep.thdctx, func,
  4685. + "dwc_usb3_thd1", data);
  4686. +
  4687. +
  4688. +Same for dwc_workq_alloc(). Example:
  4689. +
  4690. + workq = dwc_workq_alloc(&usb3_dev->osdep.wkqctx, "dwc_usb3_wkq1");
  4691. +
  4692. +
  4693. +Same for dwc_task_alloc(). Example:
  4694. +
  4695. + task = dwc_task_alloc(&usb3_dev->os_dep.tskctx, "dwc_usb3_tsk1",
  4696. + cb_func, cb_data);
  4697. +
  4698. +
  4699. +In addition to the context pointer additions, a few core functions have had
  4700. +other changes made to their parameters:
  4701. +
  4702. +The 'flags' parameter to dwc_spinlock_irqsave() and dwc_spinunlock_irqrestore()
  4703. +has been changed from a uint64_t to a dwc_irqflags_t.
  4704. +
  4705. +dwc_thread_should_stop() now takes a 'dwc_thread_t *' parameter, because the
  4706. +FreeBSD equivalent of that function requires it.
  4707. +
  4708. +And, in addition to the context pointer, dwc_task_alloc() also adds a
  4709. +'char *name' parameter, to be consistent with dwc_thread_run() and
  4710. +dwc_workq_alloc(), and because the FreeBSD equivalent of that function
  4711. +requires a unique name.
  4712. +
  4713. +
  4714. +Here is a complete list of the core functions that now take a pointer to a
  4715. +context as their first parameter:
  4716. +
  4717. + dwc_read_reg32
  4718. + dwc_read_reg64
  4719. + dwc_write_reg32
  4720. + dwc_write_reg64
  4721. + dwc_modify_reg32
  4722. + dwc_modify_reg64
  4723. + dwc_alloc
  4724. + dwc_alloc_atomic
  4725. + dwc_strdup
  4726. + dwc_free
  4727. + dwc_dma_alloc
  4728. + dwc_dma_free
  4729. + dwc_mutex_alloc
  4730. + dwc_mutex_free
  4731. + dwc_spinlock_alloc
  4732. + dwc_spinlock_free
  4733. + dwc_timer_alloc
  4734. + dwc_waitq_alloc
  4735. + dwc_thread_run
  4736. + dwc_workq_alloc
  4737. + dwc_task_alloc Also adds a 'char *name' as its 2nd parameter
  4738. +
  4739. +And here are the core functions that have other changes to their parameters:
  4740. +
  4741. + dwc_spinlock_irqsave 'flags' param is now a 'dwc_irqflags_t *'
  4742. + dwc_spinunlock_irqrestore 'flags' param is now a 'dwc_irqflags_t'
  4743. + dwc_thread_should_stop Adds a 'dwc_thread_t *' parameter
  4744. +
  4745. +
  4746. +
  4747. +The changes to the core functions also require some of the other library
  4748. +functions to change:
  4749. +
  4750. + dwc_cc_if_alloc() and dwc_cc_if_free() now take a 'void *memctx'
  4751. + (for memory allocation) as the 1st param and a 'void *mtxctx'
  4752. + (for mutex allocation) as the 2nd param.
  4753. +
  4754. + dwc_cc_clear(), dwc_cc_add(), dwc_cc_change(), dwc_cc_remove(),
  4755. + dwc_cc_data_for_save(), and dwc_cc_restore_from_data() now take a
  4756. + 'void *memctx' as the 1st param.
  4757. +
  4758. + dwc_dh_modpow(), dwc_dh_pk(), and dwc_dh_derive_keys() now take a
  4759. + 'void *memctx' as the 1st param.
  4760. +
  4761. + dwc_modpow() now takes a 'void *memctx' as the 1st param.
  4762. +
  4763. + dwc_alloc_notification_manager() now takes a 'void *memctx' as the
  4764. + 1st param and a 'void *wkqctx' (for work queue allocation) as the 2nd
  4765. + param, and also now returns an integer value that is non-zero if
  4766. + allocation of its data structures or work queue fails.
  4767. +
  4768. + dwc_register_notifier() now takes a 'void *memctx' as the 1st param.
  4769. +
  4770. + dwc_memory_debug_start() now takes a 'void *mem_ctx' as the first
  4771. + param, and also now returns an integer value that is non-zero if
  4772. + allocation of its data structures fails.
  4773. +
  4774. +
  4775. +
  4776. +Other miscellaneous changes:
  4777. +
  4778. +The DEBUG_MEMORY and DEBUG_REGS #define's have been renamed to
  4779. +DWC_DEBUG_MEMORY and DWC_DEBUG_REGS.
  4780. +
  4781. +The following #define's have been added to allow selectively compiling library
  4782. +features:
  4783. +
  4784. + DWC_CCLIB
  4785. + DWC_CRYPTOLIB
  4786. + DWC_NOTIFYLIB
  4787. + DWC_UTFLIB
  4788. +
  4789. +A DWC_LIBMODULE #define has also been added. If this is not defined, then the
  4790. +module code in dwc_common_linux.c is not compiled in. This allows linking the
  4791. +library code directly into a driver module, instead of as a standalone module.
  4792. --- /dev/null
  4793. +++ b/drivers/usb/host/dwc_common_port/doc/doxygen.cfg
  4794. @@ -0,0 +1,270 @@
  4795. +# Doxyfile 1.4.5
  4796. +
  4797. +#---------------------------------------------------------------------------
  4798. +# Project related configuration options
  4799. +#---------------------------------------------------------------------------
  4800. +PROJECT_NAME = "Synopsys DWC Portability and Common Library for UWB"
  4801. +PROJECT_NUMBER =
  4802. +OUTPUT_DIRECTORY = doc
  4803. +CREATE_SUBDIRS = NO
  4804. +OUTPUT_LANGUAGE = English
  4805. +BRIEF_MEMBER_DESC = YES
  4806. +REPEAT_BRIEF = YES
  4807. +ABBREVIATE_BRIEF = "The $name class" \
  4808. + "The $name widget" \
  4809. + "The $name file" \
  4810. + is \
  4811. + provides \
  4812. + specifies \
  4813. + contains \
  4814. + represents \
  4815. + a \
  4816. + an \
  4817. + the
  4818. +ALWAYS_DETAILED_SEC = YES
  4819. +INLINE_INHERITED_MEMB = NO
  4820. +FULL_PATH_NAMES = NO
  4821. +STRIP_FROM_PATH = ..
  4822. +STRIP_FROM_INC_PATH =
  4823. +SHORT_NAMES = NO
  4824. +JAVADOC_AUTOBRIEF = YES
  4825. +MULTILINE_CPP_IS_BRIEF = NO
  4826. +DETAILS_AT_TOP = YES
  4827. +INHERIT_DOCS = YES
  4828. +SEPARATE_MEMBER_PAGES = NO
  4829. +TAB_SIZE = 8
  4830. +ALIASES =
  4831. +OPTIMIZE_OUTPUT_FOR_C = YES
  4832. +OPTIMIZE_OUTPUT_JAVA = NO
  4833. +BUILTIN_STL_SUPPORT = NO
  4834. +DISTRIBUTE_GROUP_DOC = NO
  4835. +SUBGROUPING = NO
  4836. +#---------------------------------------------------------------------------
  4837. +# Build related configuration options
  4838. +#---------------------------------------------------------------------------
  4839. +EXTRACT_ALL = NO
  4840. +EXTRACT_PRIVATE = NO
  4841. +EXTRACT_STATIC = YES
  4842. +EXTRACT_LOCAL_CLASSES = NO
  4843. +EXTRACT_LOCAL_METHODS = NO
  4844. +HIDE_UNDOC_MEMBERS = NO
  4845. +HIDE_UNDOC_CLASSES = NO
  4846. +HIDE_FRIEND_COMPOUNDS = NO
  4847. +HIDE_IN_BODY_DOCS = NO
  4848. +INTERNAL_DOCS = NO
  4849. +CASE_SENSE_NAMES = YES
  4850. +HIDE_SCOPE_NAMES = NO
  4851. +SHOW_INCLUDE_FILES = NO
  4852. +INLINE_INFO = YES
  4853. +SORT_MEMBER_DOCS = NO
  4854. +SORT_BRIEF_DOCS = NO
  4855. +SORT_BY_SCOPE_NAME = NO
  4856. +GENERATE_TODOLIST = YES
  4857. +GENERATE_TESTLIST = YES
  4858. +GENERATE_BUGLIST = YES
  4859. +GENERATE_DEPRECATEDLIST= YES
  4860. +ENABLED_SECTIONS =
  4861. +MAX_INITIALIZER_LINES = 30
  4862. +SHOW_USED_FILES = YES
  4863. +SHOW_DIRECTORIES = YES
  4864. +FILE_VERSION_FILTER =
  4865. +#---------------------------------------------------------------------------
  4866. +# configuration options related to warning and progress messages
  4867. +#---------------------------------------------------------------------------
  4868. +QUIET = YES
  4869. +WARNINGS = YES
  4870. +WARN_IF_UNDOCUMENTED = NO
  4871. +WARN_IF_DOC_ERROR = YES
  4872. +WARN_NO_PARAMDOC = YES
  4873. +WARN_FORMAT = "$file:$line: $text"
  4874. +WARN_LOGFILE =
  4875. +#---------------------------------------------------------------------------
  4876. +# configuration options related to the input files
  4877. +#---------------------------------------------------------------------------
  4878. +INPUT = .
  4879. +FILE_PATTERNS = *.c \
  4880. + *.cc \
  4881. + *.cxx \
  4882. + *.cpp \
  4883. + *.c++ \
  4884. + *.d \
  4885. + *.java \
  4886. + *.ii \
  4887. + *.ixx \
  4888. + *.ipp \
  4889. + *.i++ \
  4890. + *.inl \
  4891. + *.h \
  4892. + *.hh \
  4893. + *.hxx \
  4894. + *.hpp \
  4895. + *.h++ \
  4896. + *.idl \
  4897. + *.odl \
  4898. + *.cs \
  4899. + *.php \
  4900. + *.php3 \
  4901. + *.inc \
  4902. + *.m \
  4903. + *.mm \
  4904. + *.dox \
  4905. + *.py \
  4906. + *.C \
  4907. + *.CC \
  4908. + *.C++ \
  4909. + *.II \
  4910. + *.I++ \
  4911. + *.H \
  4912. + *.HH \
  4913. + *.H++ \
  4914. + *.CS \
  4915. + *.PHP \
  4916. + *.PHP3 \
  4917. + *.M \
  4918. + *.MM \
  4919. + *.PY
  4920. +RECURSIVE = NO
  4921. +EXCLUDE =
  4922. +EXCLUDE_SYMLINKS = NO
  4923. +EXCLUDE_PATTERNS =
  4924. +EXAMPLE_PATH =
  4925. +EXAMPLE_PATTERNS = *
  4926. +EXAMPLE_RECURSIVE = NO
  4927. +IMAGE_PATH =
  4928. +INPUT_FILTER =
  4929. +FILTER_PATTERNS =
  4930. +FILTER_SOURCE_FILES = NO
  4931. +#---------------------------------------------------------------------------
  4932. +# configuration options related to source browsing
  4933. +#---------------------------------------------------------------------------
  4934. +SOURCE_BROWSER = NO
  4935. +INLINE_SOURCES = NO
  4936. +STRIP_CODE_COMMENTS = YES
  4937. +REFERENCED_BY_RELATION = YES
  4938. +REFERENCES_RELATION = YES
  4939. +USE_HTAGS = NO
  4940. +VERBATIM_HEADERS = NO
  4941. +#---------------------------------------------------------------------------
  4942. +# configuration options related to the alphabetical class index
  4943. +#---------------------------------------------------------------------------
  4944. +ALPHABETICAL_INDEX = NO
  4945. +COLS_IN_ALPHA_INDEX = 5
  4946. +IGNORE_PREFIX =
  4947. +#---------------------------------------------------------------------------
  4948. +# configuration options related to the HTML output
  4949. +#---------------------------------------------------------------------------
  4950. +GENERATE_HTML = YES
  4951. +HTML_OUTPUT = html
  4952. +HTML_FILE_EXTENSION = .html
  4953. +HTML_HEADER =
  4954. +HTML_FOOTER =
  4955. +HTML_STYLESHEET =
  4956. +HTML_ALIGN_MEMBERS = YES
  4957. +GENERATE_HTMLHELP = NO
  4958. +CHM_FILE =
  4959. +HHC_LOCATION =
  4960. +GENERATE_CHI = NO
  4961. +BINARY_TOC = NO
  4962. +TOC_EXPAND = NO
  4963. +DISABLE_INDEX = NO
  4964. +ENUM_VALUES_PER_LINE = 4
  4965. +GENERATE_TREEVIEW = YES
  4966. +TREEVIEW_WIDTH = 250
  4967. +#---------------------------------------------------------------------------
  4968. +# configuration options related to the LaTeX output
  4969. +#---------------------------------------------------------------------------
  4970. +GENERATE_LATEX = NO
  4971. +LATEX_OUTPUT = latex
  4972. +LATEX_CMD_NAME = latex
  4973. +MAKEINDEX_CMD_NAME = makeindex
  4974. +COMPACT_LATEX = NO
  4975. +PAPER_TYPE = a4wide
  4976. +EXTRA_PACKAGES =
  4977. +LATEX_HEADER =
  4978. +PDF_HYPERLINKS = NO
  4979. +USE_PDFLATEX = NO
  4980. +LATEX_BATCHMODE = NO
  4981. +LATEX_HIDE_INDICES = NO
  4982. +#---------------------------------------------------------------------------
  4983. +# configuration options related to the RTF output
  4984. +#---------------------------------------------------------------------------
  4985. +GENERATE_RTF = NO
  4986. +RTF_OUTPUT = rtf
  4987. +COMPACT_RTF = NO
  4988. +RTF_HYPERLINKS = NO
  4989. +RTF_STYLESHEET_FILE =
  4990. +RTF_EXTENSIONS_FILE =
  4991. +#---------------------------------------------------------------------------
  4992. +# configuration options related to the man page output
  4993. +#---------------------------------------------------------------------------
  4994. +GENERATE_MAN = NO
  4995. +MAN_OUTPUT = man
  4996. +MAN_EXTENSION = .3
  4997. +MAN_LINKS = NO
  4998. +#---------------------------------------------------------------------------
  4999. +# configuration options related to the XML output
  5000. +#---------------------------------------------------------------------------
  5001. +GENERATE_XML = NO
  5002. +XML_OUTPUT = xml
  5003. +XML_SCHEMA =
  5004. +XML_DTD =
  5005. +XML_PROGRAMLISTING = YES
  5006. +#---------------------------------------------------------------------------
  5007. +# configuration options for the AutoGen Definitions output
  5008. +#---------------------------------------------------------------------------
  5009. +GENERATE_AUTOGEN_DEF = NO
  5010. +#---------------------------------------------------------------------------
  5011. +# configuration options related to the Perl module output
  5012. +#---------------------------------------------------------------------------
  5013. +GENERATE_PERLMOD = NO
  5014. +PERLMOD_LATEX = NO
  5015. +PERLMOD_PRETTY = YES
  5016. +PERLMOD_MAKEVAR_PREFIX =
  5017. +#---------------------------------------------------------------------------
  5018. +# Configuration options related to the preprocessor
  5019. +#---------------------------------------------------------------------------
  5020. +ENABLE_PREPROCESSING = YES
  5021. +MACRO_EXPANSION = NO
  5022. +EXPAND_ONLY_PREDEF = NO
  5023. +SEARCH_INCLUDES = YES
  5024. +INCLUDE_PATH =
  5025. +INCLUDE_FILE_PATTERNS =
  5026. +PREDEFINED = DEBUG DEBUG_MEMORY
  5027. +EXPAND_AS_DEFINED =
  5028. +SKIP_FUNCTION_MACROS = YES
  5029. +#---------------------------------------------------------------------------
  5030. +# Configuration::additions related to external references
  5031. +#---------------------------------------------------------------------------
  5032. +TAGFILES =
  5033. +GENERATE_TAGFILE =
  5034. +ALLEXTERNALS = NO
  5035. +EXTERNAL_GROUPS = YES
  5036. +PERL_PATH = /usr/bin/perl
  5037. +#---------------------------------------------------------------------------
  5038. +# Configuration options related to the dot tool
  5039. +#---------------------------------------------------------------------------
  5040. +CLASS_DIAGRAMS = YES
  5041. +HIDE_UNDOC_RELATIONS = YES
  5042. +HAVE_DOT = NO
  5043. +CLASS_GRAPH = YES
  5044. +COLLABORATION_GRAPH = YES
  5045. +GROUP_GRAPHS = YES
  5046. +UML_LOOK = NO
  5047. +TEMPLATE_RELATIONS = NO
  5048. +INCLUDE_GRAPH = NO
  5049. +INCLUDED_BY_GRAPH = YES
  5050. +CALL_GRAPH = NO
  5051. +GRAPHICAL_HIERARCHY = YES
  5052. +DIRECTORY_GRAPH = YES
  5053. +DOT_IMAGE_FORMAT = png
  5054. +DOT_PATH =
  5055. +DOTFILE_DIRS =
  5056. +MAX_DOT_GRAPH_DEPTH = 1000
  5057. +DOT_TRANSPARENT = NO
  5058. +DOT_MULTI_TARGETS = NO
  5059. +GENERATE_LEGEND = YES
  5060. +DOT_CLEANUP = YES
  5061. +#---------------------------------------------------------------------------
  5062. +# Configuration::additions related to the search engine
  5063. +#---------------------------------------------------------------------------
  5064. +SEARCHENGINE = NO
  5065. --- /dev/null
  5066. +++ b/drivers/usb/host/dwc_common_port/dwc_cc.c
  5067. @@ -0,0 +1,532 @@
  5068. +/* =========================================================================
  5069. + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_cc.c $
  5070. + * $Revision: #4 $
  5071. + * $Date: 2010/11/04 $
  5072. + * $Change: 1621692 $
  5073. + *
  5074. + * Synopsys Portability Library Software and documentation
  5075. + * (hereinafter, "Software") is an Unsupported proprietary work of
  5076. + * Synopsys, Inc. unless otherwise expressly agreed to in writing
  5077. + * between Synopsys and you.
  5078. + *
  5079. + * The Software IS NOT an item of Licensed Software or Licensed Product
  5080. + * under any End User Software License Agreement or Agreement for
  5081. + * Licensed Product with Synopsys or any supplement thereto. You are
  5082. + * permitted to use and redistribute this Software in source and binary
  5083. + * forms, with or without modification, provided that redistributions
  5084. + * of source code must retain this notice. You may not view, use,
  5085. + * disclose, copy or distribute this file or any information contained
  5086. + * herein except pursuant to this license grant from Synopsys. If you
  5087. + * do not agree with this notice, including the disclaimer below, then
  5088. + * you are not authorized to use the Software.
  5089. + *
  5090. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  5091. + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5092. + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  5093. + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
  5094. + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  5095. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  5096. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  5097. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  5098. + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  5099. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  5100. + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  5101. + * DAMAGE.
  5102. + * ========================================================================= */
  5103. +#ifdef DWC_CCLIB
  5104. +
  5105. +#include "dwc_cc.h"
  5106. +
  5107. +typedef struct dwc_cc
  5108. +{
  5109. + uint32_t uid;
  5110. + uint8_t chid[16];
  5111. + uint8_t cdid[16];
  5112. + uint8_t ck[16];
  5113. + uint8_t *name;
  5114. + uint8_t length;
  5115. + DWC_CIRCLEQ_ENTRY(dwc_cc) list_entry;
  5116. +} dwc_cc_t;
  5117. +
  5118. +DWC_CIRCLEQ_HEAD(context_list, dwc_cc);
  5119. +
  5120. +/** The main structure for CC management. */
  5121. +struct dwc_cc_if
  5122. +{
  5123. + dwc_mutex_t *mutex;
  5124. + char *filename;
  5125. +
  5126. + unsigned is_host:1;
  5127. +
  5128. + dwc_notifier_t *notifier;
  5129. +
  5130. + struct context_list list;
  5131. +};
  5132. +
  5133. +#ifdef DEBUG
  5134. +static inline void dump_bytes(char *name, uint8_t *bytes, int len)
  5135. +{
  5136. + int i;
  5137. + DWC_PRINTF("%s: ", name);
  5138. + for (i=0; i<len; i++) {
  5139. + DWC_PRINTF("%02x ", bytes[i]);
  5140. + }
  5141. + DWC_PRINTF("\n");
  5142. +}
  5143. +#else
  5144. +#define dump_bytes(x...)
  5145. +#endif
  5146. +
  5147. +static dwc_cc_t *alloc_cc(void *mem_ctx, uint8_t *name, uint32_t length)
  5148. +{
  5149. + dwc_cc_t *cc = dwc_alloc(mem_ctx, sizeof(dwc_cc_t));
  5150. + if (!cc) {
  5151. + return NULL;
  5152. + }
  5153. + DWC_MEMSET(cc, 0, sizeof(dwc_cc_t));
  5154. +
  5155. + if (name) {
  5156. + cc->length = length;
  5157. + cc->name = dwc_alloc(mem_ctx, length);
  5158. + if (!cc->name) {
  5159. + dwc_free(mem_ctx, cc);
  5160. + return NULL;
  5161. + }
  5162. +
  5163. + DWC_MEMCPY(cc->name, name, length);
  5164. + }
  5165. +
  5166. + return cc;
  5167. +}
  5168. +
  5169. +static void free_cc(void *mem_ctx, dwc_cc_t *cc)
  5170. +{
  5171. + if (cc->name) {
  5172. + dwc_free(mem_ctx, cc->name);
  5173. + }
  5174. + dwc_free(mem_ctx, cc);
  5175. +}
  5176. +
  5177. +static uint32_t next_uid(dwc_cc_if_t *cc_if)
  5178. +{
  5179. + uint32_t uid = 0;
  5180. + dwc_cc_t *cc;
  5181. + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
  5182. + if (cc->uid > uid) {
  5183. + uid = cc->uid;
  5184. + }
  5185. + }
  5186. +
  5187. + if (uid == 0) {
  5188. + uid = 255;
  5189. + }
  5190. +
  5191. + return uid + 1;
  5192. +}
  5193. +
  5194. +static dwc_cc_t *cc_find(dwc_cc_if_t *cc_if, uint32_t uid)
  5195. +{
  5196. + dwc_cc_t *cc;
  5197. + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
  5198. + if (cc->uid == uid) {
  5199. + return cc;
  5200. + }
  5201. + }
  5202. + return NULL;
  5203. +}
  5204. +
  5205. +static unsigned int cc_data_size(dwc_cc_if_t *cc_if)
  5206. +{
  5207. + unsigned int size = 0;
  5208. + dwc_cc_t *cc;
  5209. + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
  5210. + size += (48 + 1);
  5211. + if (cc->name) {
  5212. + size += cc->length;
  5213. + }
  5214. + }
  5215. + return size;
  5216. +}
  5217. +
  5218. +static uint32_t cc_match_chid(dwc_cc_if_t *cc_if, uint8_t *chid)
  5219. +{
  5220. + uint32_t uid = 0;
  5221. + dwc_cc_t *cc;
  5222. +
  5223. + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
  5224. + if (DWC_MEMCMP(cc->chid, chid, 16) == 0) {
  5225. + uid = cc->uid;
  5226. + break;
  5227. + }
  5228. + }
  5229. + return uid;
  5230. +}
  5231. +static uint32_t cc_match_cdid(dwc_cc_if_t *cc_if, uint8_t *cdid)
  5232. +{
  5233. + uint32_t uid = 0;
  5234. + dwc_cc_t *cc;
  5235. +
  5236. + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
  5237. + if (DWC_MEMCMP(cc->cdid, cdid, 16) == 0) {
  5238. + uid = cc->uid;
  5239. + break;
  5240. + }
  5241. + }
  5242. + return uid;
  5243. +}
  5244. +
  5245. +/* Internal cc_add */
  5246. +static int32_t cc_add(void *mem_ctx, dwc_cc_if_t *cc_if, uint8_t *chid,
  5247. + uint8_t *cdid, uint8_t *ck, uint8_t *name, uint8_t length)
  5248. +{
  5249. + dwc_cc_t *cc;
  5250. + uint32_t uid;
  5251. +
  5252. + if (cc_if->is_host) {
  5253. + uid = cc_match_cdid(cc_if, cdid);
  5254. + }
  5255. + else {
  5256. + uid = cc_match_chid(cc_if, chid);
  5257. + }
  5258. +
  5259. + if (uid) {
  5260. + DWC_DEBUGC("Replacing previous connection context id=%d name=%p name_len=%d", uid, name, length);
  5261. + cc = cc_find(cc_if, uid);
  5262. + }
  5263. + else {
  5264. + cc = alloc_cc(mem_ctx, name, length);
  5265. + cc->uid = next_uid(cc_if);
  5266. + DWC_CIRCLEQ_INSERT_TAIL(&cc_if->list, cc, list_entry);
  5267. + }
  5268. +
  5269. + DWC_MEMCPY(&(cc->chid[0]), chid, 16);
  5270. + DWC_MEMCPY(&(cc->cdid[0]), cdid, 16);
  5271. + DWC_MEMCPY(&(cc->ck[0]), ck, 16);
  5272. +
  5273. + DWC_DEBUGC("Added connection context id=%d name=%p name_len=%d", cc->uid, name, length);
  5274. + dump_bytes("CHID", cc->chid, 16);
  5275. + dump_bytes("CDID", cc->cdid, 16);
  5276. + dump_bytes("CK", cc->ck, 16);
  5277. + return cc->uid;
  5278. +}
  5279. +
  5280. +/* Internal cc_clear */
  5281. +static void cc_clear(void *mem_ctx, dwc_cc_if_t *cc_if)
  5282. +{
  5283. + while (!DWC_CIRCLEQ_EMPTY(&cc_if->list)) {
  5284. + dwc_cc_t *cc = DWC_CIRCLEQ_FIRST(&cc_if->list);
  5285. + DWC_CIRCLEQ_REMOVE_INIT(&cc_if->list, cc, list_entry);
  5286. + free_cc(mem_ctx, cc);
  5287. + }
  5288. +}
  5289. +
  5290. +dwc_cc_if_t *dwc_cc_if_alloc(void *mem_ctx, void *mtx_ctx,
  5291. + dwc_notifier_t *notifier, unsigned is_host)
  5292. +{
  5293. + dwc_cc_if_t *cc_if = NULL;
  5294. +
  5295. + /* Allocate a common_cc_if structure */
  5296. + cc_if = dwc_alloc(mem_ctx, sizeof(dwc_cc_if_t));
  5297. +
  5298. + if (!cc_if)
  5299. + return NULL;
  5300. +
  5301. +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
  5302. + DWC_MUTEX_ALLOC_LINUX_DEBUG(cc_if->mutex);
  5303. +#else
  5304. + cc_if->mutex = dwc_mutex_alloc(mtx_ctx);
  5305. +#endif
  5306. + if (!cc_if->mutex) {
  5307. + dwc_free(mem_ctx, cc_if);
  5308. + return NULL;
  5309. + }
  5310. +
  5311. + DWC_CIRCLEQ_INIT(&cc_if->list);
  5312. + cc_if->is_host = is_host;
  5313. + cc_if->notifier = notifier;
  5314. + return cc_if;
  5315. +}
  5316. +
  5317. +void dwc_cc_if_free(void *mem_ctx, void *mtx_ctx, dwc_cc_if_t *cc_if)
  5318. +{
  5319. +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
  5320. + DWC_MUTEX_FREE(cc_if->mutex);
  5321. +#else
  5322. + dwc_mutex_free(mtx_ctx, cc_if->mutex);
  5323. +#endif
  5324. + cc_clear(mem_ctx, cc_if);
  5325. + dwc_free(mem_ctx, cc_if);
  5326. +}
  5327. +
  5328. +static void cc_changed(dwc_cc_if_t *cc_if)
  5329. +{
  5330. + if (cc_if->notifier) {
  5331. + dwc_notify(cc_if->notifier, DWC_CC_LIST_CHANGED_NOTIFICATION, cc_if);
  5332. + }
  5333. +}
  5334. +
  5335. +void dwc_cc_clear(void *mem_ctx, dwc_cc_if_t *cc_if)
  5336. +{
  5337. + DWC_MUTEX_LOCK(cc_if->mutex);
  5338. + cc_clear(mem_ctx, cc_if);
  5339. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  5340. + cc_changed(cc_if);
  5341. +}
  5342. +
  5343. +int32_t dwc_cc_add(void *mem_ctx, dwc_cc_if_t *cc_if, uint8_t *chid,
  5344. + uint8_t *cdid, uint8_t *ck, uint8_t *name, uint8_t length)
  5345. +{
  5346. + uint32_t uid;
  5347. +
  5348. + DWC_MUTEX_LOCK(cc_if->mutex);
  5349. + uid = cc_add(mem_ctx, cc_if, chid, cdid, ck, name, length);
  5350. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  5351. + cc_changed(cc_if);
  5352. +
  5353. + return uid;
  5354. +}
  5355. +
  5356. +void dwc_cc_change(void *mem_ctx, dwc_cc_if_t *cc_if, int32_t id, uint8_t *chid,
  5357. + uint8_t *cdid, uint8_t *ck, uint8_t *name, uint8_t length)
  5358. +{
  5359. + dwc_cc_t* cc;
  5360. +
  5361. + DWC_DEBUGC("Change connection context %d", id);
  5362. +
  5363. + DWC_MUTEX_LOCK(cc_if->mutex);
  5364. + cc = cc_find(cc_if, id);
  5365. + if (!cc) {
  5366. + DWC_ERROR("Uid %d not found in cc list\n", id);
  5367. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  5368. + return;
  5369. + }
  5370. +
  5371. + if (chid) {
  5372. + DWC_MEMCPY(&(cc->chid[0]), chid, 16);
  5373. + }
  5374. + if (cdid) {
  5375. + DWC_MEMCPY(&(cc->cdid[0]), cdid, 16);
  5376. + }
  5377. + if (ck) {
  5378. + DWC_MEMCPY(&(cc->ck[0]), ck, 16);
  5379. + }
  5380. +
  5381. + if (name) {
  5382. + if (cc->name) {
  5383. + dwc_free(mem_ctx, cc->name);
  5384. + }
  5385. + cc->name = dwc_alloc(mem_ctx, length);
  5386. + if (!cc->name) {
  5387. + DWC_ERROR("Out of memory in dwc_cc_change()\n");
  5388. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  5389. + return;
  5390. + }
  5391. + cc->length = length;
  5392. + DWC_MEMCPY(cc->name, name, length);
  5393. + }
  5394. +
  5395. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  5396. +
  5397. + cc_changed(cc_if);
  5398. +
  5399. + DWC_DEBUGC("Changed connection context id=%d\n", id);
  5400. + dump_bytes("New CHID", cc->chid, 16);
  5401. + dump_bytes("New CDID", cc->cdid, 16);
  5402. + dump_bytes("New CK", cc->ck, 16);
  5403. +}
  5404. +
  5405. +void dwc_cc_remove(void *mem_ctx, dwc_cc_if_t *cc_if, int32_t id)
  5406. +{
  5407. + dwc_cc_t *cc;
  5408. +
  5409. + DWC_DEBUGC("Removing connection context %d", id);
  5410. +
  5411. + DWC_MUTEX_LOCK(cc_if->mutex);
  5412. + cc = cc_find(cc_if, id);
  5413. + if (!cc) {
  5414. + DWC_ERROR("Uid %d not found in cc list\n", id);
  5415. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  5416. + return;
  5417. + }
  5418. +
  5419. + DWC_CIRCLEQ_REMOVE_INIT(&cc_if->list, cc, list_entry);
  5420. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  5421. + free_cc(mem_ctx, cc);
  5422. +
  5423. + cc_changed(cc_if);
  5424. +}
  5425. +
  5426. +uint8_t *dwc_cc_data_for_save(void *mem_ctx, dwc_cc_if_t *cc_if, unsigned int *length)
  5427. +{
  5428. + uint8_t *buf, *x;
  5429. + uint8_t zero = 0;
  5430. + dwc_cc_t *cc;
  5431. +
  5432. + DWC_MUTEX_LOCK(cc_if->mutex);
  5433. + *length = cc_data_size(cc_if);
  5434. + if (!(*length)) {
  5435. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  5436. + return NULL;
  5437. + }
  5438. +
  5439. + DWC_DEBUGC("Creating data for saving (length=%d)", *length);
  5440. +
  5441. + buf = dwc_alloc(mem_ctx, *length);
  5442. + if (!buf) {
  5443. + *length = 0;
  5444. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  5445. + return NULL;
  5446. + }
  5447. +
  5448. + x = buf;
  5449. + DWC_CIRCLEQ_FOREACH(cc, &cc_if->list, list_entry) {
  5450. + DWC_MEMCPY(x, cc->chid, 16);
  5451. + x += 16;
  5452. + DWC_MEMCPY(x, cc->cdid, 16);
  5453. + x += 16;
  5454. + DWC_MEMCPY(x, cc->ck, 16);
  5455. + x += 16;
  5456. + if (cc->name) {
  5457. + DWC_MEMCPY(x, &cc->length, 1);
  5458. + x += 1;
  5459. + DWC_MEMCPY(x, cc->name, cc->length);
  5460. + x += cc->length;
  5461. + }
  5462. + else {
  5463. + DWC_MEMCPY(x, &zero, 1);
  5464. + x += 1;
  5465. + }
  5466. + }
  5467. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  5468. +
  5469. + return buf;
  5470. +}
  5471. +
  5472. +void dwc_cc_restore_from_data(void *mem_ctx, dwc_cc_if_t *cc_if, uint8_t *data, uint32_t length)
  5473. +{
  5474. + uint8_t name_length;
  5475. + uint8_t *name;
  5476. + uint8_t *chid;
  5477. + uint8_t *cdid;
  5478. + uint8_t *ck;
  5479. + uint32_t i = 0;
  5480. +
  5481. + DWC_MUTEX_LOCK(cc_if->mutex);
  5482. + cc_clear(mem_ctx, cc_if);
  5483. +
  5484. + while (i < length) {
  5485. + chid = &data[i];
  5486. + i += 16;
  5487. + cdid = &data[i];
  5488. + i += 16;
  5489. + ck = &data[i];
  5490. + i += 16;
  5491. +
  5492. + name_length = data[i];
  5493. + i ++;
  5494. +
  5495. + if (name_length) {
  5496. + name = &data[i];
  5497. + i += name_length;
  5498. + }
  5499. + else {
  5500. + name = NULL;
  5501. + }
  5502. +
  5503. + /* check to see if we haven't overflown the buffer */
  5504. + if (i > length) {
  5505. + DWC_ERROR("Data format error while attempting to load CCs "
  5506. + "(nlen=%d, iter=%d, buflen=%d).\n", name_length, i, length);
  5507. + break;
  5508. + }
  5509. +
  5510. + cc_add(mem_ctx, cc_if, chid, cdid, ck, name, name_length);
  5511. + }
  5512. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  5513. +
  5514. + cc_changed(cc_if);
  5515. +}
  5516. +
  5517. +uint32_t dwc_cc_match_chid(dwc_cc_if_t *cc_if, uint8_t *chid)
  5518. +{
  5519. + uint32_t uid = 0;
  5520. +
  5521. + DWC_MUTEX_LOCK(cc_if->mutex);
  5522. + uid = cc_match_chid(cc_if, chid);
  5523. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  5524. + return uid;
  5525. +}
  5526. +uint32_t dwc_cc_match_cdid(dwc_cc_if_t *cc_if, uint8_t *cdid)
  5527. +{
  5528. + uint32_t uid = 0;
  5529. +
  5530. + DWC_MUTEX_LOCK(cc_if->mutex);
  5531. + uid = cc_match_cdid(cc_if, cdid);
  5532. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  5533. + return uid;
  5534. +}
  5535. +
  5536. +uint8_t *dwc_cc_ck(dwc_cc_if_t *cc_if, int32_t id)
  5537. +{
  5538. + uint8_t *ck = NULL;
  5539. + dwc_cc_t *cc;
  5540. +
  5541. + DWC_MUTEX_LOCK(cc_if->mutex);
  5542. + cc = cc_find(cc_if, id);
  5543. + if (cc) {
  5544. + ck = cc->ck;
  5545. + }
  5546. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  5547. +
  5548. + return ck;
  5549. +
  5550. +}
  5551. +
  5552. +uint8_t *dwc_cc_chid(dwc_cc_if_t *cc_if, int32_t id)
  5553. +{
  5554. + uint8_t *retval = NULL;
  5555. + dwc_cc_t *cc;
  5556. +
  5557. + DWC_MUTEX_LOCK(cc_if->mutex);
  5558. + cc = cc_find(cc_if, id);
  5559. + if (cc) {
  5560. + retval = cc->chid;
  5561. + }
  5562. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  5563. +
  5564. + return retval;
  5565. +}
  5566. +
  5567. +uint8_t *dwc_cc_cdid(dwc_cc_if_t *cc_if, int32_t id)
  5568. +{
  5569. + uint8_t *retval = NULL;
  5570. + dwc_cc_t *cc;
  5571. +
  5572. + DWC_MUTEX_LOCK(cc_if->mutex);
  5573. + cc = cc_find(cc_if, id);
  5574. + if (cc) {
  5575. + retval = cc->cdid;
  5576. + }
  5577. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  5578. +
  5579. + return retval;
  5580. +}
  5581. +
  5582. +uint8_t *dwc_cc_name(dwc_cc_if_t *cc_if, int32_t id, uint8_t *length)
  5583. +{
  5584. + uint8_t *retval = NULL;
  5585. + dwc_cc_t *cc;
  5586. +
  5587. + DWC_MUTEX_LOCK(cc_if->mutex);
  5588. + *length = 0;
  5589. + cc = cc_find(cc_if, id);
  5590. + if (cc) {
  5591. + *length = cc->length;
  5592. + retval = cc->name;
  5593. + }
  5594. + DWC_MUTEX_UNLOCK(cc_if->mutex);
  5595. +
  5596. + return retval;
  5597. +}
  5598. +
  5599. +#endif /* DWC_CCLIB */
  5600. --- /dev/null
  5601. +++ b/drivers/usb/host/dwc_common_port/dwc_cc.h
  5602. @@ -0,0 +1,224 @@
  5603. +/* =========================================================================
  5604. + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_cc.h $
  5605. + * $Revision: #4 $
  5606. + * $Date: 2010/09/28 $
  5607. + * $Change: 1596182 $
  5608. + *
  5609. + * Synopsys Portability Library Software and documentation
  5610. + * (hereinafter, "Software") is an Unsupported proprietary work of
  5611. + * Synopsys, Inc. unless otherwise expressly agreed to in writing
  5612. + * between Synopsys and you.
  5613. + *
  5614. + * The Software IS NOT an item of Licensed Software or Licensed Product
  5615. + * under any End User Software License Agreement or Agreement for
  5616. + * Licensed Product with Synopsys or any supplement thereto. You are
  5617. + * permitted to use and redistribute this Software in source and binary
  5618. + * forms, with or without modification, provided that redistributions
  5619. + * of source code must retain this notice. You may not view, use,
  5620. + * disclose, copy or distribute this file or any information contained
  5621. + * herein except pursuant to this license grant from Synopsys. If you
  5622. + * do not agree with this notice, including the disclaimer below, then
  5623. + * you are not authorized to use the Software.
  5624. + *
  5625. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  5626. + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5627. + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  5628. + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
  5629. + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  5630. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  5631. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  5632. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  5633. + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  5634. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  5635. + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  5636. + * DAMAGE.
  5637. + * ========================================================================= */
  5638. +#ifndef _DWC_CC_H_
  5639. +#define _DWC_CC_H_
  5640. +
  5641. +#ifdef __cplusplus
  5642. +extern "C" {
  5643. +#endif
  5644. +
  5645. +/** @file
  5646. + *
  5647. + * This file defines the Context Context library.
  5648. + *
  5649. + * The main data structure is dwc_cc_if_t which is returned by either the
  5650. + * dwc_cc_if_alloc function or returned by the module to the user via a provided
  5651. + * function. The data structure is opaque and should only be manipulated via the
  5652. + * functions provied in this API.
  5653. + *
  5654. + * It manages a list of connection contexts and operations can be performed to
  5655. + * add, remove, query, search, and change, those contexts. Additionally,
  5656. + * a dwc_notifier_t object can be requested from the manager so that
  5657. + * the user can be notified whenever the context list has changed.
  5658. + */
  5659. +
  5660. +#include "dwc_os.h"
  5661. +#include "dwc_list.h"
  5662. +#include "dwc_notifier.h"
  5663. +
  5664. +
  5665. +/* Notifications */
  5666. +#define DWC_CC_LIST_CHANGED_NOTIFICATION "DWC_CC_LIST_CHANGED_NOTIFICATION"
  5667. +
  5668. +struct dwc_cc_if;
  5669. +typedef struct dwc_cc_if dwc_cc_if_t;
  5670. +
  5671. +
  5672. +/** @name Connection Context Operations */
  5673. +/** @{ */
  5674. +
  5675. +/** This function allocates memory for a dwc_cc_if_t structure, initializes
  5676. + * fields to default values, and returns a pointer to the structure or NULL on
  5677. + * error. */
  5678. +extern dwc_cc_if_t *dwc_cc_if_alloc(void *mem_ctx, void *mtx_ctx,
  5679. + dwc_notifier_t *notifier, unsigned is_host);
  5680. +
  5681. +/** Frees the memory for the specified CC structure allocated from
  5682. + * dwc_cc_if_alloc(). */
  5683. +extern void dwc_cc_if_free(void *mem_ctx, void *mtx_ctx, dwc_cc_if_t *cc_if);
  5684. +
  5685. +/** Removes all contexts from the connection context list */
  5686. +extern void dwc_cc_clear(void *mem_ctx, dwc_cc_if_t *cc_if);
  5687. +
  5688. +/** Adds a connection context (CHID, CK, CDID, Name) to the connection context list.
  5689. + * If a CHID already exists, the CK and name are overwritten. Statistics are
  5690. + * not overwritten.
  5691. + *
  5692. + * @param cc_if The cc_if structure.
  5693. + * @param chid A pointer to the 16-byte CHID. This value will be copied.
  5694. + * @param ck A pointer to the 16-byte CK. This value will be copied.
  5695. + * @param cdid A pointer to the 16-byte CDID. This value will be copied.
  5696. + * @param name An optional host friendly name as defined in the association model
  5697. + * spec. Must be a UTF16-LE unicode string. Can be NULL to indicated no name.
  5698. + * @param length The length othe unicode string.
  5699. + * @return A unique identifier used to refer to this context that is valid for
  5700. + * as long as this context is still in the list. */
  5701. +extern int32_t dwc_cc_add(void *mem_ctx, dwc_cc_if_t *cc_if, uint8_t *chid,
  5702. + uint8_t *cdid, uint8_t *ck, uint8_t *name,
  5703. + uint8_t length);
  5704. +
  5705. +/** Changes the CHID, CK, CDID, or Name values of a connection context in the
  5706. + * list, preserving any accumulated statistics. This would typically be called
  5707. + * if the host decideds to change the context with a SET_CONNECTION request.
  5708. + *
  5709. + * @param cc_if The cc_if structure.
  5710. + * @param id The identifier of the connection context.
  5711. + * @param chid A pointer to the 16-byte CHID. This value will be copied. NULL
  5712. + * indicates no change.
  5713. + * @param cdid A pointer to the 16-byte CDID. This value will be copied. NULL
  5714. + * indicates no change.
  5715. + * @param ck A pointer to the 16-byte CK. This value will be copied. NULL
  5716. + * indicates no change.
  5717. + * @param name Host friendly name UTF16-LE. NULL indicates no change.
  5718. + * @param length Length of name. */
  5719. +extern void dwc_cc_change(void *mem_ctx, dwc_cc_if_t *cc_if, int32_t id,
  5720. + uint8_t *chid, uint8_t *cdid, uint8_t *ck,
  5721. + uint8_t *name, uint8_t length);
  5722. +
  5723. +/** Remove the specified connection context.
  5724. + * @param cc_if The cc_if structure.
  5725. + * @param id The identifier of the connection context to remove. */
  5726. +extern void dwc_cc_remove(void *mem_ctx, dwc_cc_if_t *cc_if, int32_t id);
  5727. +
  5728. +/** Get a binary block of data for the connection context list and attributes.
  5729. + * This data can be used by the OS specific driver to save the connection
  5730. + * context list into non-volatile memory.
  5731. + *
  5732. + * @param cc_if The cc_if structure.
  5733. + * @param length Return the length of the data buffer.
  5734. + * @return A pointer to the data buffer. The memory for this buffer should be
  5735. + * freed with DWC_FREE() after use. */
  5736. +extern uint8_t *dwc_cc_data_for_save(void *mem_ctx, dwc_cc_if_t *cc_if,
  5737. + unsigned int *length);
  5738. +
  5739. +/** Restore the connection context list from the binary data that was previously
  5740. + * returned from a call to dwc_cc_data_for_save. This can be used by the OS specific
  5741. + * driver to load a connection context list from non-volatile memory.
  5742. + *
  5743. + * @param cc_if The cc_if structure.
  5744. + * @param data The data bytes as returned from dwc_cc_data_for_save.
  5745. + * @param length The length of the data. */
  5746. +extern void dwc_cc_restore_from_data(void *mem_ctx, dwc_cc_if_t *cc_if,
  5747. + uint8_t *data, unsigned int length);
  5748. +
  5749. +/** Find the connection context from the specified CHID.
  5750. + *
  5751. + * @param cc_if The cc_if structure.
  5752. + * @param chid A pointer to the CHID data.
  5753. + * @return A non-zero identifier of the connection context if the CHID matches.
  5754. + * Otherwise returns 0. */
  5755. +extern uint32_t dwc_cc_match_chid(dwc_cc_if_t *cc_if, uint8_t *chid);
  5756. +
  5757. +/** Find the connection context from the specified CDID.
  5758. + *
  5759. + * @param cc_if The cc_if structure.
  5760. + * @param cdid A pointer to the CDID data.
  5761. + * @return A non-zero identifier of the connection context if the CHID matches.
  5762. + * Otherwise returns 0. */
  5763. +extern uint32_t dwc_cc_match_cdid(dwc_cc_if_t *cc_if, uint8_t *cdid);
  5764. +
  5765. +/** Retrieve the CK from the specified connection context.
  5766. + *
  5767. + * @param cc_if The cc_if structure.
  5768. + * @param id The identifier of the connection context.
  5769. + * @return A pointer to the CK data. The memory does not need to be freed. */
  5770. +extern uint8_t *dwc_cc_ck(dwc_cc_if_t *cc_if, int32_t id);
  5771. +
  5772. +/** Retrieve the CHID from the specified connection context.
  5773. + *
  5774. + * @param cc_if The cc_if structure.
  5775. + * @param id The identifier of the connection context.
  5776. + * @return A pointer to the CHID data. The memory does not need to be freed. */
  5777. +extern uint8_t *dwc_cc_chid(dwc_cc_if_t *cc_if, int32_t id);
  5778. +
  5779. +/** Retrieve the CDID from the specified connection context.
  5780. + *
  5781. + * @param cc_if The cc_if structure.
  5782. + * @param id The identifier of the connection context.
  5783. + * @return A pointer to the CDID data. The memory does not need to be freed. */
  5784. +extern uint8_t *dwc_cc_cdid(dwc_cc_if_t *cc_if, int32_t id);
  5785. +
  5786. +extern uint8_t *dwc_cc_name(dwc_cc_if_t *cc_if, int32_t id, uint8_t *length);
  5787. +
  5788. +/** Checks a buffer for non-zero.
  5789. + * @param id A pointer to a 16 byte buffer.
  5790. + * @return true if the 16 byte value is non-zero. */
  5791. +static inline unsigned dwc_assoc_is_not_zero_id(uint8_t *id) {
  5792. + int i;
  5793. + for (i=0; i<16; i++) {
  5794. + if (id[i]) return 1;
  5795. + }
  5796. + return 0;
  5797. +}
  5798. +
  5799. +/** Checks a buffer for zero.
  5800. + * @param id A pointer to a 16 byte buffer.
  5801. + * @return true if the 16 byte value is zero. */
  5802. +static inline unsigned dwc_assoc_is_zero_id(uint8_t *id) {
  5803. + return !dwc_assoc_is_not_zero_id(id);
  5804. +}
  5805. +
  5806. +/** Prints an ASCII representation for the 16-byte chid, cdid, or ck, into
  5807. + * buffer. */
  5808. +static inline int dwc_print_id_string(char *buffer, uint8_t *id) {
  5809. + char *ptr = buffer;
  5810. + int i;
  5811. + for (i=0; i<16; i++) {
  5812. + ptr += DWC_SPRINTF(ptr, "%02x", id[i]);
  5813. + if (i < 15) {
  5814. + ptr += DWC_SPRINTF(ptr, " ");
  5815. + }
  5816. + }
  5817. + return ptr - buffer;
  5818. +}
  5819. +
  5820. +/** @} */
  5821. +
  5822. +#ifdef __cplusplus
  5823. +}
  5824. +#endif
  5825. +
  5826. +#endif /* _DWC_CC_H_ */
  5827. --- /dev/null
  5828. +++ b/drivers/usb/host/dwc_common_port/dwc_common_fbsd.c
  5829. @@ -0,0 +1,1308 @@
  5830. +#include "dwc_os.h"
  5831. +#include "dwc_list.h"
  5832. +
  5833. +#ifdef DWC_CCLIB
  5834. +# include "dwc_cc.h"
  5835. +#endif
  5836. +
  5837. +#ifdef DWC_CRYPTOLIB
  5838. +# include "dwc_modpow.h"
  5839. +# include "dwc_dh.h"
  5840. +# include "dwc_crypto.h"
  5841. +#endif
  5842. +
  5843. +#ifdef DWC_NOTIFYLIB
  5844. +# include "dwc_notifier.h"
  5845. +#endif
  5846. +
  5847. +/* OS-Level Implementations */
  5848. +
  5849. +/* This is the FreeBSD 7.0 kernel implementation of the DWC platform library. */
  5850. +
  5851. +
  5852. +/* MISC */
  5853. +
  5854. +void *DWC_MEMSET(void *dest, uint8_t byte, uint32_t size)
  5855. +{
  5856. + return memset(dest, byte, size);
  5857. +}
  5858. +
  5859. +void *DWC_MEMCPY(void *dest, void const *src, uint32_t size)
  5860. +{
  5861. + return memcpy(dest, src, size);
  5862. +}
  5863. +
  5864. +void *DWC_MEMMOVE(void *dest, void *src, uint32_t size)
  5865. +{
  5866. + bcopy(src, dest, size);
  5867. + return dest;
  5868. +}
  5869. +
  5870. +int DWC_MEMCMP(void *m1, void *m2, uint32_t size)
  5871. +{
  5872. + return memcmp(m1, m2, size);
  5873. +}
  5874. +
  5875. +int DWC_STRNCMP(void *s1, void *s2, uint32_t size)
  5876. +{
  5877. + return strncmp(s1, s2, size);
  5878. +}
  5879. +
  5880. +int DWC_STRCMP(void *s1, void *s2)
  5881. +{
  5882. + return strcmp(s1, s2);
  5883. +}
  5884. +
  5885. +int DWC_STRLEN(char const *str)
  5886. +{
  5887. + return strlen(str);
  5888. +}
  5889. +
  5890. +char *DWC_STRCPY(char *to, char const *from)
  5891. +{
  5892. + return strcpy(to, from);
  5893. +}
  5894. +
  5895. +char *DWC_STRDUP(char const *str)
  5896. +{
  5897. + int len = DWC_STRLEN(str) + 1;
  5898. + char *new = DWC_ALLOC_ATOMIC(len);
  5899. +
  5900. + if (!new) {
  5901. + return NULL;
  5902. + }
  5903. +
  5904. + DWC_MEMCPY(new, str, len);
  5905. + return new;
  5906. +}
  5907. +
  5908. +int DWC_ATOI(char *str, int32_t *value)
  5909. +{
  5910. + char *end = NULL;
  5911. +
  5912. + *value = strtol(str, &end, 0);
  5913. + if (*end == '\0') {
  5914. + return 0;
  5915. + }
  5916. +
  5917. + return -1;
  5918. +}
  5919. +
  5920. +int DWC_ATOUI(char *str, uint32_t *value)
  5921. +{
  5922. + char *end = NULL;
  5923. +
  5924. + *value = strtoul(str, &end, 0);
  5925. + if (*end == '\0') {
  5926. + return 0;
  5927. + }
  5928. +
  5929. + return -1;
  5930. +}
  5931. +
  5932. +
  5933. +#ifdef DWC_UTFLIB
  5934. +/* From usbstring.c */
  5935. +
  5936. +int DWC_UTF8_TO_UTF16LE(uint8_t const *s, uint16_t *cp, unsigned len)
  5937. +{
  5938. + int count = 0;
  5939. + u8 c;
  5940. + u16 uchar;
  5941. +
  5942. + /* this insists on correct encodings, though not minimal ones.
  5943. + * BUT it currently rejects legit 4-byte UTF-8 code points,
  5944. + * which need surrogate pairs. (Unicode 3.1 can use them.)
  5945. + */
  5946. + while (len != 0 && (c = (u8) *s++) != 0) {
  5947. + if (unlikely(c & 0x80)) {
  5948. + // 2-byte sequence:
  5949. + // 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx
  5950. + if ((c & 0xe0) == 0xc0) {
  5951. + uchar = (c & 0x1f) << 6;
  5952. +
  5953. + c = (u8) *s++;
  5954. + if ((c & 0xc0) != 0xc0)
  5955. + goto fail;
  5956. + c &= 0x3f;
  5957. + uchar |= c;
  5958. +
  5959. + // 3-byte sequence (most CJKV characters):
  5960. + // zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx
  5961. + } else if ((c & 0xf0) == 0xe0) {
  5962. + uchar = (c & 0x0f) << 12;
  5963. +
  5964. + c = (u8) *s++;
  5965. + if ((c & 0xc0) != 0xc0)
  5966. + goto fail;
  5967. + c &= 0x3f;
  5968. + uchar |= c << 6;
  5969. +
  5970. + c = (u8) *s++;
  5971. + if ((c & 0xc0) != 0xc0)
  5972. + goto fail;
  5973. + c &= 0x3f;
  5974. + uchar |= c;
  5975. +
  5976. + /* no bogus surrogates */
  5977. + if (0xd800 <= uchar && uchar <= 0xdfff)
  5978. + goto fail;
  5979. +
  5980. + // 4-byte sequence (surrogate pairs, currently rare):
  5981. + // 11101110wwwwzzzzyy + 110111yyyyxxxxxx
  5982. + // = 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx
  5983. + // (uuuuu = wwww + 1)
  5984. + // FIXME accept the surrogate code points (only)
  5985. + } else
  5986. + goto fail;
  5987. + } else
  5988. + uchar = c;
  5989. + put_unaligned (cpu_to_le16 (uchar), cp++);
  5990. + count++;
  5991. + len--;
  5992. + }
  5993. + return count;
  5994. +fail:
  5995. + return -1;
  5996. +}
  5997. +
  5998. +#endif /* DWC_UTFLIB */
  5999. +
  6000. +
  6001. +/* dwc_debug.h */
  6002. +
  6003. +dwc_bool_t DWC_IN_IRQ(void)
  6004. +{
  6005. +// return in_irq();
  6006. + return 0;
  6007. +}
  6008. +
  6009. +dwc_bool_t DWC_IN_BH(void)
  6010. +{
  6011. +// return in_softirq();
  6012. + return 0;
  6013. +}
  6014. +
  6015. +void DWC_VPRINTF(char *format, va_list args)
  6016. +{
  6017. + vprintf(format, args);
  6018. +}
  6019. +
  6020. +int DWC_VSNPRINTF(char *str, int size, char *format, va_list args)
  6021. +{
  6022. + return vsnprintf(str, size, format, args);
  6023. +}
  6024. +
  6025. +void DWC_PRINTF(char *format, ...)
  6026. +{
  6027. + va_list args;
  6028. +
  6029. + va_start(args, format);
  6030. + DWC_VPRINTF(format, args);
  6031. + va_end(args);
  6032. +}
  6033. +
  6034. +int DWC_SPRINTF(char *buffer, char *format, ...)
  6035. +{
  6036. + int retval;
  6037. + va_list args;
  6038. +
  6039. + va_start(args, format);
  6040. + retval = vsprintf(buffer, format, args);
  6041. + va_end(args);
  6042. + return retval;
  6043. +}
  6044. +
  6045. +int DWC_SNPRINTF(char *buffer, int size, char *format, ...)
  6046. +{
  6047. + int retval;
  6048. + va_list args;
  6049. +
  6050. + va_start(args, format);
  6051. + retval = vsnprintf(buffer, size, format, args);
  6052. + va_end(args);
  6053. + return retval;
  6054. +}
  6055. +
  6056. +void __DWC_WARN(char *format, ...)
  6057. +{
  6058. + va_list args;
  6059. +
  6060. + va_start(args, format);
  6061. + DWC_VPRINTF(format, args);
  6062. + va_end(args);
  6063. +}
  6064. +
  6065. +void __DWC_ERROR(char *format, ...)
  6066. +{
  6067. + va_list args;
  6068. +
  6069. + va_start(args, format);
  6070. + DWC_VPRINTF(format, args);
  6071. + va_end(args);
  6072. +}
  6073. +
  6074. +void DWC_EXCEPTION(char *format, ...)
  6075. +{
  6076. + va_list args;
  6077. +
  6078. + va_start(args, format);
  6079. + DWC_VPRINTF(format, args);
  6080. + va_end(args);
  6081. +// BUG_ON(1); ???
  6082. +}
  6083. +
  6084. +#ifdef DEBUG
  6085. +void __DWC_DEBUG(char *format, ...)
  6086. +{
  6087. + va_list args;
  6088. +
  6089. + va_start(args, format);
  6090. + DWC_VPRINTF(format, args);
  6091. + va_end(args);
  6092. +}
  6093. +#endif
  6094. +
  6095. +
  6096. +/* dwc_mem.h */
  6097. +
  6098. +#if 0
  6099. +dwc_pool_t *DWC_DMA_POOL_CREATE(uint32_t size,
  6100. + uint32_t align,
  6101. + uint32_t alloc)
  6102. +{
  6103. + struct dma_pool *pool = dma_pool_create("Pool", NULL,
  6104. + size, align, alloc);
  6105. + return (dwc_pool_t *)pool;
  6106. +}
  6107. +
  6108. +void DWC_DMA_POOL_DESTROY(dwc_pool_t *pool)
  6109. +{
  6110. + dma_pool_destroy((struct dma_pool *)pool);
  6111. +}
  6112. +
  6113. +void *DWC_DMA_POOL_ALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
  6114. +{
  6115. +// return dma_pool_alloc((struct dma_pool *)pool, GFP_KERNEL, dma_addr);
  6116. + return dma_pool_alloc((struct dma_pool *)pool, M_WAITOK, dma_addr);
  6117. +}
  6118. +
  6119. +void *DWC_DMA_POOL_ZALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
  6120. +{
  6121. + void *vaddr = DWC_DMA_POOL_ALLOC(pool, dma_addr);
  6122. + memset(..);
  6123. +}
  6124. +
  6125. +void DWC_DMA_POOL_FREE(dwc_pool_t *pool, void *vaddr, void *daddr)
  6126. +{
  6127. + dma_pool_free(pool, vaddr, daddr);
  6128. +}
  6129. +#endif
  6130. +
  6131. +static void dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
  6132. +{
  6133. + if (error)
  6134. + return;
  6135. + *(bus_addr_t *)arg = segs[0].ds_addr;
  6136. +}
  6137. +
  6138. +void *__DWC_DMA_ALLOC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr)
  6139. +{
  6140. + dwc_dmactx_t *dma = (dwc_dmactx_t *)dma_ctx;
  6141. + int error;
  6142. +
  6143. + error = bus_dma_tag_create(
  6144. +#if __FreeBSD_version >= 700000
  6145. + bus_get_dma_tag(dma->dev), /* parent */
  6146. +#else
  6147. + NULL, /* parent */
  6148. +#endif
  6149. + 4, 0, /* alignment, bounds */
  6150. + BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
  6151. + BUS_SPACE_MAXADDR, /* highaddr */
  6152. + NULL, NULL, /* filter, filterarg */
  6153. + size, /* maxsize */
  6154. + 1, /* nsegments */
  6155. + size, /* maxsegsize */
  6156. + 0, /* flags */
  6157. + NULL, /* lockfunc */
  6158. + NULL, /* lockarg */
  6159. + &dma->dma_tag);
  6160. + if (error) {
  6161. + device_printf(dma->dev, "%s: bus_dma_tag_create failed: %d\n",
  6162. + __func__, error);
  6163. + goto fail_0;
  6164. + }
  6165. +
  6166. + error = bus_dmamem_alloc(dma->dma_tag, &dma->dma_vaddr,
  6167. + BUS_DMA_NOWAIT | BUS_DMA_COHERENT, &dma->dma_map);
  6168. + if (error) {
  6169. + device_printf(dma->dev, "%s: bus_dmamem_alloc(%ju) failed: %d\n",
  6170. + __func__, (uintmax_t)size, error);
  6171. + goto fail_1;
  6172. + }
  6173. +
  6174. + dma->dma_paddr = 0;
  6175. + error = bus_dmamap_load(dma->dma_tag, dma->dma_map, dma->dma_vaddr, size,
  6176. + dmamap_cb, &dma->dma_paddr, BUS_DMA_NOWAIT);
  6177. + if (error || dma->dma_paddr == 0) {
  6178. + device_printf(dma->dev, "%s: bus_dmamap_load failed: %d\n",
  6179. + __func__, error);
  6180. + goto fail_2;
  6181. + }
  6182. +
  6183. + *dma_addr = dma->dma_paddr;
  6184. + return dma->dma_vaddr;
  6185. +
  6186. +fail_2:
  6187. + bus_dmamap_unload(dma->dma_tag, dma->dma_map);
  6188. +fail_1:
  6189. + bus_dmamem_free(dma->dma_tag, dma->dma_vaddr, dma->dma_map);
  6190. + bus_dma_tag_destroy(dma->dma_tag);
  6191. +fail_0:
  6192. + dma->dma_map = NULL;
  6193. + dma->dma_tag = NULL;
  6194. +
  6195. + return NULL;
  6196. +}
  6197. +
  6198. +void __DWC_DMA_FREE(void *dma_ctx, uint32_t size, void *virt_addr, dwc_dma_t dma_addr)
  6199. +{
  6200. + dwc_dmactx_t *dma = (dwc_dmactx_t *)dma_ctx;
  6201. +
  6202. + if (dma->dma_tag == NULL)
  6203. + return;
  6204. + if (dma->dma_map != NULL) {
  6205. + bus_dmamap_sync(dma->dma_tag, dma->dma_map,
  6206. + BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
  6207. + bus_dmamap_unload(dma->dma_tag, dma->dma_map);
  6208. + bus_dmamem_free(dma->dma_tag, dma->dma_vaddr, dma->dma_map);
  6209. + dma->dma_map = NULL;
  6210. + }
  6211. +
  6212. + bus_dma_tag_destroy(dma->dma_tag);
  6213. + dma->dma_tag = NULL;
  6214. +}
  6215. +
  6216. +void *__DWC_ALLOC(void *mem_ctx, uint32_t size)
  6217. +{
  6218. + return malloc(size, M_DEVBUF, M_WAITOK | M_ZERO);
  6219. +}
  6220. +
  6221. +void *__DWC_ALLOC_ATOMIC(void *mem_ctx, uint32_t size)
  6222. +{
  6223. + return malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO);
  6224. +}
  6225. +
  6226. +void __DWC_FREE(void *mem_ctx, void *addr)
  6227. +{
  6228. + free(addr, M_DEVBUF);
  6229. +}
  6230. +
  6231. +
  6232. +#ifdef DWC_CRYPTOLIB
  6233. +/* dwc_crypto.h */
  6234. +
  6235. +void DWC_RANDOM_BYTES(uint8_t *buffer, uint32_t length)
  6236. +{
  6237. + get_random_bytes(buffer, length);
  6238. +}
  6239. +
  6240. +int DWC_AES_CBC(uint8_t *message, uint32_t messagelen, uint8_t *key, uint32_t keylen, uint8_t iv[16], uint8_t *out)
  6241. +{
  6242. + struct crypto_blkcipher *tfm;
  6243. + struct blkcipher_desc desc;
  6244. + struct scatterlist sgd;
  6245. + struct scatterlist sgs;
  6246. +
  6247. + tfm = crypto_alloc_blkcipher("cbc(aes)", 0, CRYPTO_ALG_ASYNC);
  6248. + if (tfm == NULL) {
  6249. + printk("failed to load transform for aes CBC\n");
  6250. + return -1;
  6251. + }
  6252. +
  6253. + crypto_blkcipher_setkey(tfm, key, keylen);
  6254. + crypto_blkcipher_set_iv(tfm, iv, 16);
  6255. +
  6256. + sg_init_one(&sgd, out, messagelen);
  6257. + sg_init_one(&sgs, message, messagelen);
  6258. +
  6259. + desc.tfm = tfm;
  6260. + desc.flags = 0;
  6261. +
  6262. + if (crypto_blkcipher_encrypt(&desc, &sgd, &sgs, messagelen)) {
  6263. + crypto_free_blkcipher(tfm);
  6264. + DWC_ERROR("AES CBC encryption failed");
  6265. + return -1;
  6266. + }
  6267. +
  6268. + crypto_free_blkcipher(tfm);
  6269. + return 0;
  6270. +}
  6271. +
  6272. +int DWC_SHA256(uint8_t *message, uint32_t len, uint8_t *out)
  6273. +{
  6274. + struct crypto_hash *tfm;
  6275. + struct hash_desc desc;
  6276. + struct scatterlist sg;
  6277. +
  6278. + tfm = crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC);
  6279. + if (IS_ERR(tfm)) {
  6280. + DWC_ERROR("Failed to load transform for sha256: %ld", PTR_ERR(tfm));
  6281. + return 0;
  6282. + }
  6283. + desc.tfm = tfm;
  6284. + desc.flags = 0;
  6285. +
  6286. + sg_init_one(&sg, message, len);
  6287. + crypto_hash_digest(&desc, &sg, len, out);
  6288. + crypto_free_hash(tfm);
  6289. +
  6290. + return 1;
  6291. +}
  6292. +
  6293. +int DWC_HMAC_SHA256(uint8_t *message, uint32_t messagelen,
  6294. + uint8_t *key, uint32_t keylen, uint8_t *out)
  6295. +{
  6296. + struct crypto_hash *tfm;
  6297. + struct hash_desc desc;
  6298. + struct scatterlist sg;
  6299. +
  6300. + tfm = crypto_alloc_hash("hmac(sha256)", 0, CRYPTO_ALG_ASYNC);
  6301. + if (IS_ERR(tfm)) {
  6302. + DWC_ERROR("Failed to load transform for hmac(sha256): %ld", PTR_ERR(tfm));
  6303. + return 0;
  6304. + }
  6305. + desc.tfm = tfm;
  6306. + desc.flags = 0;
  6307. +
  6308. + sg_init_one(&sg, message, messagelen);
  6309. + crypto_hash_setkey(tfm, key, keylen);
  6310. + crypto_hash_digest(&desc, &sg, messagelen, out);
  6311. + crypto_free_hash(tfm);
  6312. +
  6313. + return 1;
  6314. +}
  6315. +
  6316. +#endif /* DWC_CRYPTOLIB */
  6317. +
  6318. +
  6319. +/* Byte Ordering Conversions */
  6320. +
  6321. +uint32_t DWC_CPU_TO_LE32(uint32_t *p)
  6322. +{
  6323. +#ifdef __LITTLE_ENDIAN
  6324. + return *p;
  6325. +#else
  6326. + uint8_t *u_p = (uint8_t *)p;
  6327. +
  6328. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  6329. +#endif
  6330. +}
  6331. +
  6332. +uint32_t DWC_CPU_TO_BE32(uint32_t *p)
  6333. +{
  6334. +#ifdef __BIG_ENDIAN
  6335. + return *p;
  6336. +#else
  6337. + uint8_t *u_p = (uint8_t *)p;
  6338. +
  6339. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  6340. +#endif
  6341. +}
  6342. +
  6343. +uint32_t DWC_LE32_TO_CPU(uint32_t *p)
  6344. +{
  6345. +#ifdef __LITTLE_ENDIAN
  6346. + return *p;
  6347. +#else
  6348. + uint8_t *u_p = (uint8_t *)p;
  6349. +
  6350. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  6351. +#endif
  6352. +}
  6353. +
  6354. +uint32_t DWC_BE32_TO_CPU(uint32_t *p)
  6355. +{
  6356. +#ifdef __BIG_ENDIAN
  6357. + return *p;
  6358. +#else
  6359. + uint8_t *u_p = (uint8_t *)p;
  6360. +
  6361. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  6362. +#endif
  6363. +}
  6364. +
  6365. +uint16_t DWC_CPU_TO_LE16(uint16_t *p)
  6366. +{
  6367. +#ifdef __LITTLE_ENDIAN
  6368. + return *p;
  6369. +#else
  6370. + uint8_t *u_p = (uint8_t *)p;
  6371. + return (u_p[1] | (u_p[0] << 8));
  6372. +#endif
  6373. +}
  6374. +
  6375. +uint16_t DWC_CPU_TO_BE16(uint16_t *p)
  6376. +{
  6377. +#ifdef __BIG_ENDIAN
  6378. + return *p;
  6379. +#else
  6380. + uint8_t *u_p = (uint8_t *)p;
  6381. + return (u_p[1] | (u_p[0] << 8));
  6382. +#endif
  6383. +}
  6384. +
  6385. +uint16_t DWC_LE16_TO_CPU(uint16_t *p)
  6386. +{
  6387. +#ifdef __LITTLE_ENDIAN
  6388. + return *p;
  6389. +#else
  6390. + uint8_t *u_p = (uint8_t *)p;
  6391. + return (u_p[1] | (u_p[0] << 8));
  6392. +#endif
  6393. +}
  6394. +
  6395. +uint16_t DWC_BE16_TO_CPU(uint16_t *p)
  6396. +{
  6397. +#ifdef __BIG_ENDIAN
  6398. + return *p;
  6399. +#else
  6400. + uint8_t *u_p = (uint8_t *)p;
  6401. + return (u_p[1] | (u_p[0] << 8));
  6402. +#endif
  6403. +}
  6404. +
  6405. +
  6406. +/* Registers */
  6407. +
  6408. +uint32_t DWC_READ_REG32(void *io_ctx, uint32_t volatile *reg)
  6409. +{
  6410. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  6411. + bus_size_t ior = (bus_size_t)reg;
  6412. +
  6413. + return bus_space_read_4(io->iot, io->ioh, ior);
  6414. +}
  6415. +
  6416. +#if 0
  6417. +uint64_t DWC_READ_REG64(void *io_ctx, uint64_t volatile *reg)
  6418. +{
  6419. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  6420. + bus_size_t ior = (bus_size_t)reg;
  6421. +
  6422. + return bus_space_read_8(io->iot, io->ioh, ior);
  6423. +}
  6424. +#endif
  6425. +
  6426. +void DWC_WRITE_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t value)
  6427. +{
  6428. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  6429. + bus_size_t ior = (bus_size_t)reg;
  6430. +
  6431. + bus_space_write_4(io->iot, io->ioh, ior, value);
  6432. +}
  6433. +
  6434. +#if 0
  6435. +void DWC_WRITE_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t value)
  6436. +{
  6437. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  6438. + bus_size_t ior = (bus_size_t)reg;
  6439. +
  6440. + bus_space_write_8(io->iot, io->ioh, ior, value);
  6441. +}
  6442. +#endif
  6443. +
  6444. +void DWC_MODIFY_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t clear_mask,
  6445. + uint32_t set_mask)
  6446. +{
  6447. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  6448. + bus_size_t ior = (bus_size_t)reg;
  6449. +
  6450. + bus_space_write_4(io->iot, io->ioh, ior,
  6451. + (bus_space_read_4(io->iot, io->ioh, ior) &
  6452. + ~clear_mask) | set_mask);
  6453. +}
  6454. +
  6455. +#if 0
  6456. +void DWC_MODIFY_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t clear_mask,
  6457. + uint64_t set_mask)
  6458. +{
  6459. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  6460. + bus_size_t ior = (bus_size_t)reg;
  6461. +
  6462. + bus_space_write_8(io->iot, io->ioh, ior,
  6463. + (bus_space_read_8(io->iot, io->ioh, ior) &
  6464. + ~clear_mask) | set_mask);
  6465. +}
  6466. +#endif
  6467. +
  6468. +
  6469. +/* Locking */
  6470. +
  6471. +dwc_spinlock_t *DWC_SPINLOCK_ALLOC(void)
  6472. +{
  6473. + struct mtx *sl = DWC_ALLOC(sizeof(*sl));
  6474. +
  6475. + if (!sl) {
  6476. + DWC_ERROR("Cannot allocate memory for spinlock");
  6477. + return NULL;
  6478. + }
  6479. +
  6480. + mtx_init(sl, "dw3spn", NULL, MTX_SPIN);
  6481. + return (dwc_spinlock_t *)sl;
  6482. +}
  6483. +
  6484. +void DWC_SPINLOCK_FREE(dwc_spinlock_t *lock)
  6485. +{
  6486. + struct mtx *sl = (struct mtx *)lock;
  6487. +
  6488. + mtx_destroy(sl);
  6489. + DWC_FREE(sl);
  6490. +}
  6491. +
  6492. +void DWC_SPINLOCK(dwc_spinlock_t *lock)
  6493. +{
  6494. + mtx_lock_spin((struct mtx *)lock); // ???
  6495. +}
  6496. +
  6497. +void DWC_SPINUNLOCK(dwc_spinlock_t *lock)
  6498. +{
  6499. + mtx_unlock_spin((struct mtx *)lock); // ???
  6500. +}
  6501. +
  6502. +void DWC_SPINLOCK_IRQSAVE(dwc_spinlock_t *lock, dwc_irqflags_t *flags)
  6503. +{
  6504. + mtx_lock_spin((struct mtx *)lock);
  6505. +}
  6506. +
  6507. +void DWC_SPINUNLOCK_IRQRESTORE(dwc_spinlock_t *lock, dwc_irqflags_t flags)
  6508. +{
  6509. + mtx_unlock_spin((struct mtx *)lock);
  6510. +}
  6511. +
  6512. +dwc_mutex_t *DWC_MUTEX_ALLOC(void)
  6513. +{
  6514. + struct mtx *m;
  6515. + dwc_mutex_t *mutex = (dwc_mutex_t *)DWC_ALLOC(sizeof(struct mtx));
  6516. +
  6517. + if (!mutex) {
  6518. + DWC_ERROR("Cannot allocate memory for mutex");
  6519. + return NULL;
  6520. + }
  6521. +
  6522. + m = (struct mtx *)mutex;
  6523. + mtx_init(m, "dw3mtx", NULL, MTX_DEF);
  6524. + return mutex;
  6525. +}
  6526. +
  6527. +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
  6528. +#else
  6529. +void DWC_MUTEX_FREE(dwc_mutex_t *mutex)
  6530. +{
  6531. + mtx_destroy((struct mtx *)mutex);
  6532. + DWC_FREE(mutex);
  6533. +}
  6534. +#endif
  6535. +
  6536. +void DWC_MUTEX_LOCK(dwc_mutex_t *mutex)
  6537. +{
  6538. + struct mtx *m = (struct mtx *)mutex;
  6539. +
  6540. + mtx_lock(m);
  6541. +}
  6542. +
  6543. +int DWC_MUTEX_TRYLOCK(dwc_mutex_t *mutex)
  6544. +{
  6545. + struct mtx *m = (struct mtx *)mutex;
  6546. +
  6547. + return mtx_trylock(m);
  6548. +}
  6549. +
  6550. +void DWC_MUTEX_UNLOCK(dwc_mutex_t *mutex)
  6551. +{
  6552. + struct mtx *m = (struct mtx *)mutex;
  6553. +
  6554. + mtx_unlock(m);
  6555. +}
  6556. +
  6557. +
  6558. +/* Timing */
  6559. +
  6560. +void DWC_UDELAY(uint32_t usecs)
  6561. +{
  6562. + DELAY(usecs);
  6563. +}
  6564. +
  6565. +void DWC_MDELAY(uint32_t msecs)
  6566. +{
  6567. + do {
  6568. + DELAY(1000);
  6569. + } while (--msecs);
  6570. +}
  6571. +
  6572. +void DWC_MSLEEP(uint32_t msecs)
  6573. +{
  6574. + struct timeval tv;
  6575. +
  6576. + tv.tv_sec = msecs / 1000;
  6577. + tv.tv_usec = (msecs - tv.tv_sec * 1000) * 1000;
  6578. + pause("dw3slp", tvtohz(&tv));
  6579. +}
  6580. +
  6581. +uint32_t DWC_TIME(void)
  6582. +{
  6583. + struct timeval tv;
  6584. +
  6585. + microuptime(&tv); // or getmicrouptime? (less precise, but faster)
  6586. + return tv.tv_sec * 1000 + tv.tv_usec / 1000;
  6587. +}
  6588. +
  6589. +
  6590. +/* Timers */
  6591. +
  6592. +struct dwc_timer {
  6593. + struct callout t;
  6594. + char *name;
  6595. + dwc_spinlock_t *lock;
  6596. + dwc_timer_callback_t cb;
  6597. + void *data;
  6598. +};
  6599. +
  6600. +dwc_timer_t *DWC_TIMER_ALLOC(char *name, dwc_timer_callback_t cb, void *data)
  6601. +{
  6602. + dwc_timer_t *t = DWC_ALLOC(sizeof(*t));
  6603. +
  6604. + if (!t) {
  6605. + DWC_ERROR("Cannot allocate memory for timer");
  6606. + return NULL;
  6607. + }
  6608. +
  6609. + callout_init(&t->t, 1);
  6610. +
  6611. + t->name = DWC_STRDUP(name);
  6612. + if (!t->name) {
  6613. + DWC_ERROR("Cannot allocate memory for timer->name");
  6614. + goto no_name;
  6615. + }
  6616. +
  6617. + t->lock = DWC_SPINLOCK_ALLOC();
  6618. + if (!t->lock) {
  6619. + DWC_ERROR("Cannot allocate memory for lock");
  6620. + goto no_lock;
  6621. + }
  6622. +
  6623. + t->cb = cb;
  6624. + t->data = data;
  6625. +
  6626. + return t;
  6627. +
  6628. + no_lock:
  6629. + DWC_FREE(t->name);
  6630. + no_name:
  6631. + DWC_FREE(t);
  6632. +
  6633. + return NULL;
  6634. +}
  6635. +
  6636. +void DWC_TIMER_FREE(dwc_timer_t *timer)
  6637. +{
  6638. + callout_stop(&timer->t);
  6639. + DWC_SPINLOCK_FREE(timer->lock);
  6640. + DWC_FREE(timer->name);
  6641. + DWC_FREE(timer);
  6642. +}
  6643. +
  6644. +void DWC_TIMER_SCHEDULE(dwc_timer_t *timer, uint32_t time)
  6645. +{
  6646. + struct timeval tv;
  6647. +
  6648. + tv.tv_sec = time / 1000;
  6649. + tv.tv_usec = (time - tv.tv_sec * 1000) * 1000;
  6650. + callout_reset(&timer->t, tvtohz(&tv), timer->cb, timer->data);
  6651. +}
  6652. +
  6653. +void DWC_TIMER_CANCEL(dwc_timer_t *timer)
  6654. +{
  6655. + callout_stop(&timer->t);
  6656. +}
  6657. +
  6658. +
  6659. +/* Wait Queues */
  6660. +
  6661. +struct dwc_waitq {
  6662. + struct mtx lock;
  6663. + int abort;
  6664. +};
  6665. +
  6666. +dwc_waitq_t *DWC_WAITQ_ALLOC(void)
  6667. +{
  6668. + dwc_waitq_t *wq = DWC_ALLOC(sizeof(*wq));
  6669. +
  6670. + if (!wq) {
  6671. + DWC_ERROR("Cannot allocate memory for waitqueue");
  6672. + return NULL;
  6673. + }
  6674. +
  6675. + mtx_init(&wq->lock, "dw3wtq", NULL, MTX_DEF);
  6676. + wq->abort = 0;
  6677. +
  6678. + return wq;
  6679. +}
  6680. +
  6681. +void DWC_WAITQ_FREE(dwc_waitq_t *wq)
  6682. +{
  6683. + mtx_destroy(&wq->lock);
  6684. + DWC_FREE(wq);
  6685. +}
  6686. +
  6687. +int32_t DWC_WAITQ_WAIT(dwc_waitq_t *wq, dwc_waitq_condition_t cond, void *data)
  6688. +{
  6689. +// intrmask_t ipl;
  6690. + int result = 0;
  6691. +
  6692. + mtx_lock(&wq->lock);
  6693. +// ipl = splbio();
  6694. +
  6695. + /* Skip the sleep if already aborted or triggered */
  6696. + if (!wq->abort && !cond(data)) {
  6697. +// splx(ipl);
  6698. + result = msleep(wq, &wq->lock, PCATCH, "dw3wat", 0); // infinite timeout
  6699. +// ipl = splbio();
  6700. + }
  6701. +
  6702. + if (result == ERESTART) { // signaled - restart
  6703. + result = -DWC_E_RESTART;
  6704. +
  6705. + } else if (result == EINTR) { // signaled - interrupt
  6706. + result = -DWC_E_ABORT;
  6707. +
  6708. + } else if (wq->abort) {
  6709. + result = -DWC_E_ABORT;
  6710. +
  6711. + } else {
  6712. + result = 0;
  6713. + }
  6714. +
  6715. + wq->abort = 0;
  6716. +// splx(ipl);
  6717. + mtx_unlock(&wq->lock);
  6718. + return result;
  6719. +}
  6720. +
  6721. +int32_t DWC_WAITQ_WAIT_TIMEOUT(dwc_waitq_t *wq, dwc_waitq_condition_t cond,
  6722. + void *data, int32_t msecs)
  6723. +{
  6724. + struct timeval tv, tv1, tv2;
  6725. +// intrmask_t ipl;
  6726. + int result = 0;
  6727. +
  6728. + tv.tv_sec = msecs / 1000;
  6729. + tv.tv_usec = (msecs - tv.tv_sec * 1000) * 1000;
  6730. +
  6731. + mtx_lock(&wq->lock);
  6732. +// ipl = splbio();
  6733. +
  6734. + /* Skip the sleep if already aborted or triggered */
  6735. + if (!wq->abort && !cond(data)) {
  6736. +// splx(ipl);
  6737. + getmicrouptime(&tv1);
  6738. + result = msleep(wq, &wq->lock, PCATCH, "dw3wto", tvtohz(&tv));
  6739. + getmicrouptime(&tv2);
  6740. +// ipl = splbio();
  6741. + }
  6742. +
  6743. + if (result == 0) { // awoken
  6744. + if (wq->abort) {
  6745. + result = -DWC_E_ABORT;
  6746. + } else {
  6747. + tv2.tv_usec -= tv1.tv_usec;
  6748. + if (tv2.tv_usec < 0) {
  6749. + tv2.tv_usec += 1000000;
  6750. + tv2.tv_sec--;
  6751. + }
  6752. +
  6753. + tv2.tv_sec -= tv1.tv_sec;
  6754. + result = tv2.tv_sec * 1000 + tv2.tv_usec / 1000;
  6755. + result = msecs - result;
  6756. + if (result <= 0)
  6757. + result = 1;
  6758. + }
  6759. + } else if (result == ERESTART) { // signaled - restart
  6760. + result = -DWC_E_RESTART;
  6761. +
  6762. + } else if (result == EINTR) { // signaled - interrupt
  6763. + result = -DWC_E_ABORT;
  6764. +
  6765. + } else { // timed out
  6766. + result = -DWC_E_TIMEOUT;
  6767. + }
  6768. +
  6769. + wq->abort = 0;
  6770. +// splx(ipl);
  6771. + mtx_unlock(&wq->lock);
  6772. + return result;
  6773. +}
  6774. +
  6775. +void DWC_WAITQ_TRIGGER(dwc_waitq_t *wq)
  6776. +{
  6777. + wakeup(wq);
  6778. +}
  6779. +
  6780. +void DWC_WAITQ_ABORT(dwc_waitq_t *wq)
  6781. +{
  6782. +// intrmask_t ipl;
  6783. +
  6784. + mtx_lock(&wq->lock);
  6785. +// ipl = splbio();
  6786. + wq->abort = 1;
  6787. + wakeup(wq);
  6788. +// splx(ipl);
  6789. + mtx_unlock(&wq->lock);
  6790. +}
  6791. +
  6792. +
  6793. +/* Threading */
  6794. +
  6795. +struct dwc_thread {
  6796. + struct proc *proc;
  6797. + int abort;
  6798. +};
  6799. +
  6800. +dwc_thread_t *DWC_THREAD_RUN(dwc_thread_function_t func, char *name, void *data)
  6801. +{
  6802. + int retval;
  6803. + dwc_thread_t *thread = DWC_ALLOC(sizeof(*thread));
  6804. +
  6805. + if (!thread) {
  6806. + return NULL;
  6807. + }
  6808. +
  6809. + thread->abort = 0;
  6810. + retval = kthread_create((void (*)(void *))func, data, &thread->proc,
  6811. + RFPROC | RFNOWAIT, 0, "%s", name);
  6812. + if (retval) {
  6813. + DWC_FREE(thread);
  6814. + return NULL;
  6815. + }
  6816. +
  6817. + return thread;
  6818. +}
  6819. +
  6820. +int DWC_THREAD_STOP(dwc_thread_t *thread)
  6821. +{
  6822. + int retval;
  6823. +
  6824. + thread->abort = 1;
  6825. + retval = tsleep(&thread->abort, 0, "dw3stp", 60 * hz);
  6826. +
  6827. + if (retval == 0) {
  6828. + /* DWC_THREAD_EXIT() will free the thread struct */
  6829. + return 0;
  6830. + }
  6831. +
  6832. + /* NOTE: We leak the thread struct if thread doesn't die */
  6833. +
  6834. + if (retval == EWOULDBLOCK) {
  6835. + return -DWC_E_TIMEOUT;
  6836. + }
  6837. +
  6838. + return -DWC_E_UNKNOWN;
  6839. +}
  6840. +
  6841. +dwc_bool_t DWC_THREAD_SHOULD_STOP(dwc_thread_t *thread)
  6842. +{
  6843. + return thread->abort;
  6844. +}
  6845. +
  6846. +void DWC_THREAD_EXIT(dwc_thread_t *thread)
  6847. +{
  6848. + wakeup(&thread->abort);
  6849. + DWC_FREE(thread);
  6850. + kthread_exit(0);
  6851. +}
  6852. +
  6853. +
  6854. +/* tasklets
  6855. + - Runs in interrupt context (cannot sleep)
  6856. + - Each tasklet runs on a single CPU [ How can we ensure this on FreeBSD? Does it matter? ]
  6857. + - Different tasklets can be running simultaneously on different CPUs [ shouldn't matter ]
  6858. + */
  6859. +struct dwc_tasklet {
  6860. + struct task t;
  6861. + dwc_tasklet_callback_t cb;
  6862. + void *data;
  6863. +};
  6864. +
  6865. +static void tasklet_callback(void *data, int pending) // what to do with pending ???
  6866. +{
  6867. + dwc_tasklet_t *task = (dwc_tasklet_t *)data;
  6868. +
  6869. + task->cb(task->data);
  6870. +}
  6871. +
  6872. +dwc_tasklet_t *DWC_TASK_ALLOC(char *name, dwc_tasklet_callback_t cb, void *data)
  6873. +{
  6874. + dwc_tasklet_t *task = DWC_ALLOC(sizeof(*task));
  6875. +
  6876. + if (task) {
  6877. + task->cb = cb;
  6878. + task->data = data;
  6879. + TASK_INIT(&task->t, 0, tasklet_callback, task);
  6880. + } else {
  6881. + DWC_ERROR("Cannot allocate memory for tasklet");
  6882. + }
  6883. +
  6884. + return task;
  6885. +}
  6886. +
  6887. +void DWC_TASK_FREE(dwc_tasklet_t *task)
  6888. +{
  6889. + taskqueue_drain(taskqueue_fast, &task->t); // ???
  6890. + DWC_FREE(task);
  6891. +}
  6892. +
  6893. +void DWC_TASK_SCHEDULE(dwc_tasklet_t *task)
  6894. +{
  6895. + /* Uses predefined system queue */
  6896. + taskqueue_enqueue_fast(taskqueue_fast, &task->t);
  6897. +}
  6898. +
  6899. +
  6900. +/* workqueues
  6901. + - Runs in process context (can sleep)
  6902. + */
  6903. +typedef struct work_container {
  6904. + dwc_work_callback_t cb;
  6905. + void *data;
  6906. + dwc_workq_t *wq;
  6907. + char *name;
  6908. + int hz;
  6909. +
  6910. +#ifdef DEBUG
  6911. + DWC_CIRCLEQ_ENTRY(work_container) entry;
  6912. +#endif
  6913. + struct task task;
  6914. +} work_container_t;
  6915. +
  6916. +#ifdef DEBUG
  6917. +DWC_CIRCLEQ_HEAD(work_container_queue, work_container);
  6918. +#endif
  6919. +
  6920. +struct dwc_workq {
  6921. + struct taskqueue *taskq;
  6922. + dwc_spinlock_t *lock;
  6923. + dwc_waitq_t *waitq;
  6924. + int pending;
  6925. +
  6926. +#ifdef DEBUG
  6927. + struct work_container_queue entries;
  6928. +#endif
  6929. +};
  6930. +
  6931. +static void do_work(void *data, int pending) // what to do with pending ???
  6932. +{
  6933. + work_container_t *container = (work_container_t *)data;
  6934. + dwc_workq_t *wq = container->wq;
  6935. + dwc_irqflags_t flags;
  6936. +
  6937. + if (container->hz) {
  6938. + pause("dw3wrk", container->hz);
  6939. + }
  6940. +
  6941. + container->cb(container->data);
  6942. + DWC_DEBUG("Work done: %s, container=%p", container->name, container);
  6943. +
  6944. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  6945. +
  6946. +#ifdef DEBUG
  6947. + DWC_CIRCLEQ_REMOVE(&wq->entries, container, entry);
  6948. +#endif
  6949. + if (container->name)
  6950. + DWC_FREE(container->name);
  6951. + DWC_FREE(container);
  6952. + wq->pending--;
  6953. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  6954. + DWC_WAITQ_TRIGGER(wq->waitq);
  6955. +}
  6956. +
  6957. +static int work_done(void *data)
  6958. +{
  6959. + dwc_workq_t *workq = (dwc_workq_t *)data;
  6960. +
  6961. + return workq->pending == 0;
  6962. +}
  6963. +
  6964. +int DWC_WORKQ_WAIT_WORK_DONE(dwc_workq_t *workq, int timeout)
  6965. +{
  6966. + return DWC_WAITQ_WAIT_TIMEOUT(workq->waitq, work_done, workq, timeout);
  6967. +}
  6968. +
  6969. +dwc_workq_t *DWC_WORKQ_ALLOC(char *name)
  6970. +{
  6971. + dwc_workq_t *wq = DWC_ALLOC(sizeof(*wq));
  6972. +
  6973. + if (!wq) {
  6974. + DWC_ERROR("Cannot allocate memory for workqueue");
  6975. + return NULL;
  6976. + }
  6977. +
  6978. + wq->taskq = taskqueue_create(name, M_NOWAIT, taskqueue_thread_enqueue, &wq->taskq);
  6979. + if (!wq->taskq) {
  6980. + DWC_ERROR("Cannot allocate memory for taskqueue");
  6981. + goto no_taskq;
  6982. + }
  6983. +
  6984. + wq->pending = 0;
  6985. +
  6986. + wq->lock = DWC_SPINLOCK_ALLOC();
  6987. + if (!wq->lock) {
  6988. + DWC_ERROR("Cannot allocate memory for spinlock");
  6989. + goto no_lock;
  6990. + }
  6991. +
  6992. + wq->waitq = DWC_WAITQ_ALLOC();
  6993. + if (!wq->waitq) {
  6994. + DWC_ERROR("Cannot allocate memory for waitqueue");
  6995. + goto no_waitq;
  6996. + }
  6997. +
  6998. + taskqueue_start_threads(&wq->taskq, 1, PWAIT, "%s taskq", "dw3tsk");
  6999. +
  7000. +#ifdef DEBUG
  7001. + DWC_CIRCLEQ_INIT(&wq->entries);
  7002. +#endif
  7003. + return wq;
  7004. +
  7005. + no_waitq:
  7006. + DWC_SPINLOCK_FREE(wq->lock);
  7007. + no_lock:
  7008. + taskqueue_free(wq->taskq);
  7009. + no_taskq:
  7010. + DWC_FREE(wq);
  7011. +
  7012. + return NULL;
  7013. +}
  7014. +
  7015. +void DWC_WORKQ_FREE(dwc_workq_t *wq)
  7016. +{
  7017. +#ifdef DEBUG
  7018. + dwc_irqflags_t flags;
  7019. +
  7020. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  7021. +
  7022. + if (wq->pending != 0) {
  7023. + struct work_container *container;
  7024. +
  7025. + DWC_ERROR("Destroying work queue with pending work");
  7026. +
  7027. + DWC_CIRCLEQ_FOREACH(container, &wq->entries, entry) {
  7028. + DWC_ERROR("Work %s still pending", container->name);
  7029. + }
  7030. + }
  7031. +
  7032. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  7033. +#endif
  7034. + DWC_WAITQ_FREE(wq->waitq);
  7035. + DWC_SPINLOCK_FREE(wq->lock);
  7036. + taskqueue_free(wq->taskq);
  7037. + DWC_FREE(wq);
  7038. +}
  7039. +
  7040. +void DWC_WORKQ_SCHEDULE(dwc_workq_t *wq, dwc_work_callback_t cb, void *data,
  7041. + char *format, ...)
  7042. +{
  7043. + dwc_irqflags_t flags;
  7044. + work_container_t *container;
  7045. + static char name[128];
  7046. + va_list args;
  7047. +
  7048. + va_start(args, format);
  7049. + DWC_VSNPRINTF(name, 128, format, args);
  7050. + va_end(args);
  7051. +
  7052. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  7053. + wq->pending++;
  7054. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  7055. + DWC_WAITQ_TRIGGER(wq->waitq);
  7056. +
  7057. + container = DWC_ALLOC_ATOMIC(sizeof(*container));
  7058. + if (!container) {
  7059. + DWC_ERROR("Cannot allocate memory for container");
  7060. + return;
  7061. + }
  7062. +
  7063. + container->name = DWC_STRDUP(name);
  7064. + if (!container->name) {
  7065. + DWC_ERROR("Cannot allocate memory for container->name");
  7066. + DWC_FREE(container);
  7067. + return;
  7068. + }
  7069. +
  7070. + container->cb = cb;
  7071. + container->data = data;
  7072. + container->wq = wq;
  7073. + container->hz = 0;
  7074. +
  7075. + DWC_DEBUG("Queueing work: %s, container=%p", container->name, container);
  7076. +
  7077. + TASK_INIT(&container->task, 0, do_work, container);
  7078. +
  7079. +#ifdef DEBUG
  7080. + DWC_CIRCLEQ_INSERT_TAIL(&wq->entries, container, entry);
  7081. +#endif
  7082. + taskqueue_enqueue_fast(wq->taskq, &container->task);
  7083. +}
  7084. +
  7085. +void DWC_WORKQ_SCHEDULE_DELAYED(dwc_workq_t *wq, dwc_work_callback_t cb,
  7086. + void *data, uint32_t time, char *format, ...)
  7087. +{
  7088. + dwc_irqflags_t flags;
  7089. + work_container_t *container;
  7090. + static char name[128];
  7091. + struct timeval tv;
  7092. + va_list args;
  7093. +
  7094. + va_start(args, format);
  7095. + DWC_VSNPRINTF(name, 128, format, args);
  7096. + va_end(args);
  7097. +
  7098. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  7099. + wq->pending++;
  7100. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  7101. + DWC_WAITQ_TRIGGER(wq->waitq);
  7102. +
  7103. + container = DWC_ALLOC_ATOMIC(sizeof(*container));
  7104. + if (!container) {
  7105. + DWC_ERROR("Cannot allocate memory for container");
  7106. + return;
  7107. + }
  7108. +
  7109. + container->name = DWC_STRDUP(name);
  7110. + if (!container->name) {
  7111. + DWC_ERROR("Cannot allocate memory for container->name");
  7112. + DWC_FREE(container);
  7113. + return;
  7114. + }
  7115. +
  7116. + container->cb = cb;
  7117. + container->data = data;
  7118. + container->wq = wq;
  7119. +
  7120. + tv.tv_sec = time / 1000;
  7121. + tv.tv_usec = (time - tv.tv_sec * 1000) * 1000;
  7122. + container->hz = tvtohz(&tv);
  7123. +
  7124. + DWC_DEBUG("Queueing work: %s, container=%p", container->name, container);
  7125. +
  7126. + TASK_INIT(&container->task, 0, do_work, container);
  7127. +
  7128. +#ifdef DEBUG
  7129. + DWC_CIRCLEQ_INSERT_TAIL(&wq->entries, container, entry);
  7130. +#endif
  7131. + taskqueue_enqueue_fast(wq->taskq, &container->task);
  7132. +}
  7133. +
  7134. +int DWC_WORKQ_PENDING(dwc_workq_t *wq)
  7135. +{
  7136. + return wq->pending;
  7137. +}
  7138. --- /dev/null
  7139. +++ b/drivers/usb/host/dwc_common_port/dwc_common_linux.c
  7140. @@ -0,0 +1,1418 @@
  7141. +#include <linux/kernel.h>
  7142. +#include <linux/init.h>
  7143. +#include <linux/module.h>
  7144. +#include <linux/kthread.h>
  7145. +
  7146. +#ifdef DWC_CCLIB
  7147. +# include "dwc_cc.h"
  7148. +#endif
  7149. +
  7150. +#ifdef DWC_CRYPTOLIB
  7151. +# include "dwc_modpow.h"
  7152. +# include "dwc_dh.h"
  7153. +# include "dwc_crypto.h"
  7154. +#endif
  7155. +
  7156. +#ifdef DWC_NOTIFYLIB
  7157. +# include "dwc_notifier.h"
  7158. +#endif
  7159. +
  7160. +/* OS-Level Implementations */
  7161. +
  7162. +/* This is the Linux kernel implementation of the DWC platform library. */
  7163. +#include <linux/moduleparam.h>
  7164. +#include <linux/ctype.h>
  7165. +#include <linux/crypto.h>
  7166. +#include <linux/delay.h>
  7167. +#include <linux/device.h>
  7168. +#include <linux/dma-mapping.h>
  7169. +#include <linux/cdev.h>
  7170. +#include <linux/errno.h>
  7171. +#include <linux/interrupt.h>
  7172. +#include <linux/jiffies.h>
  7173. +#include <linux/list.h>
  7174. +#include <linux/pci.h>
  7175. +#include <linux/random.h>
  7176. +#include <linux/scatterlist.h>
  7177. +#include <linux/slab.h>
  7178. +#include <linux/stat.h>
  7179. +#include <linux/string.h>
  7180. +#include <linux/timer.h>
  7181. +#include <linux/usb.h>
  7182. +
  7183. +#include <linux/version.h>
  7184. +
  7185. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
  7186. +# include <linux/usb/gadget.h>
  7187. +#else
  7188. +# include <linux/usb_gadget.h>
  7189. +#endif
  7190. +
  7191. +#include <asm/io.h>
  7192. +#include <asm/page.h>
  7193. +#include <asm/uaccess.h>
  7194. +#include <asm/unaligned.h>
  7195. +
  7196. +#include "dwc_os.h"
  7197. +#include "dwc_list.h"
  7198. +
  7199. +
  7200. +/* MISC */
  7201. +
  7202. +void *DWC_MEMSET(void *dest, uint8_t byte, uint32_t size)
  7203. +{
  7204. + return memset(dest, byte, size);
  7205. +}
  7206. +
  7207. +void *DWC_MEMCPY(void *dest, void const *src, uint32_t size)
  7208. +{
  7209. + return memcpy(dest, src, size);
  7210. +}
  7211. +
  7212. +void *DWC_MEMMOVE(void *dest, void *src, uint32_t size)
  7213. +{
  7214. + return memmove(dest, src, size);
  7215. +}
  7216. +
  7217. +int DWC_MEMCMP(void *m1, void *m2, uint32_t size)
  7218. +{
  7219. + return memcmp(m1, m2, size);
  7220. +}
  7221. +
  7222. +int DWC_STRNCMP(void *s1, void *s2, uint32_t size)
  7223. +{
  7224. + return strncmp(s1, s2, size);
  7225. +}
  7226. +
  7227. +int DWC_STRCMP(void *s1, void *s2)
  7228. +{
  7229. + return strcmp(s1, s2);
  7230. +}
  7231. +
  7232. +int DWC_STRLEN(char const *str)
  7233. +{
  7234. + return strlen(str);
  7235. +}
  7236. +
  7237. +char *DWC_STRCPY(char *to, char const *from)
  7238. +{
  7239. + return strcpy(to, from);
  7240. +}
  7241. +
  7242. +char *DWC_STRDUP(char const *str)
  7243. +{
  7244. + int len = DWC_STRLEN(str) + 1;
  7245. + char *new = DWC_ALLOC_ATOMIC(len);
  7246. +
  7247. + if (!new) {
  7248. + return NULL;
  7249. + }
  7250. +
  7251. + DWC_MEMCPY(new, str, len);
  7252. + return new;
  7253. +}
  7254. +
  7255. +int DWC_ATOI(const char *str, int32_t *value)
  7256. +{
  7257. + char *end = NULL;
  7258. +
  7259. + *value = simple_strtol(str, &end, 0);
  7260. + if (*end == '\0') {
  7261. + return 0;
  7262. + }
  7263. +
  7264. + return -1;
  7265. +}
  7266. +
  7267. +int DWC_ATOUI(const char *str, uint32_t *value)
  7268. +{
  7269. + char *end = NULL;
  7270. +
  7271. + *value = simple_strtoul(str, &end, 0);
  7272. + if (*end == '\0') {
  7273. + return 0;
  7274. + }
  7275. +
  7276. + return -1;
  7277. +}
  7278. +
  7279. +
  7280. +#ifdef DWC_UTFLIB
  7281. +/* From usbstring.c */
  7282. +
  7283. +int DWC_UTF8_TO_UTF16LE(uint8_t const *s, uint16_t *cp, unsigned len)
  7284. +{
  7285. + int count = 0;
  7286. + u8 c;
  7287. + u16 uchar;
  7288. +
  7289. + /* this insists on correct encodings, though not minimal ones.
  7290. + * BUT it currently rejects legit 4-byte UTF-8 code points,
  7291. + * which need surrogate pairs. (Unicode 3.1 can use them.)
  7292. + */
  7293. + while (len != 0 && (c = (u8) *s++) != 0) {
  7294. + if (unlikely(c & 0x80)) {
  7295. + // 2-byte sequence:
  7296. + // 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx
  7297. + if ((c & 0xe0) == 0xc0) {
  7298. + uchar = (c & 0x1f) << 6;
  7299. +
  7300. + c = (u8) *s++;
  7301. + if ((c & 0xc0) != 0xc0)
  7302. + goto fail;
  7303. + c &= 0x3f;
  7304. + uchar |= c;
  7305. +
  7306. + // 3-byte sequence (most CJKV characters):
  7307. + // zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx
  7308. + } else if ((c & 0xf0) == 0xe0) {
  7309. + uchar = (c & 0x0f) << 12;
  7310. +
  7311. + c = (u8) *s++;
  7312. + if ((c & 0xc0) != 0xc0)
  7313. + goto fail;
  7314. + c &= 0x3f;
  7315. + uchar |= c << 6;
  7316. +
  7317. + c = (u8) *s++;
  7318. + if ((c & 0xc0) != 0xc0)
  7319. + goto fail;
  7320. + c &= 0x3f;
  7321. + uchar |= c;
  7322. +
  7323. + /* no bogus surrogates */
  7324. + if (0xd800 <= uchar && uchar <= 0xdfff)
  7325. + goto fail;
  7326. +
  7327. + // 4-byte sequence (surrogate pairs, currently rare):
  7328. + // 11101110wwwwzzzzyy + 110111yyyyxxxxxx
  7329. + // = 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx
  7330. + // (uuuuu = wwww + 1)
  7331. + // FIXME accept the surrogate code points (only)
  7332. + } else
  7333. + goto fail;
  7334. + } else
  7335. + uchar = c;
  7336. + put_unaligned (cpu_to_le16 (uchar), cp++);
  7337. + count++;
  7338. + len--;
  7339. + }
  7340. + return count;
  7341. +fail:
  7342. + return -1;
  7343. +}
  7344. +#endif /* DWC_UTFLIB */
  7345. +
  7346. +
  7347. +/* dwc_debug.h */
  7348. +
  7349. +dwc_bool_t DWC_IN_IRQ(void)
  7350. +{
  7351. + return in_irq();
  7352. +}
  7353. +
  7354. +dwc_bool_t DWC_IN_BH(void)
  7355. +{
  7356. + return in_softirq();
  7357. +}
  7358. +
  7359. +void DWC_VPRINTF(char *format, va_list args)
  7360. +{
  7361. + vprintk(format, args);
  7362. +}
  7363. +
  7364. +int DWC_VSNPRINTF(char *str, int size, char *format, va_list args)
  7365. +{
  7366. + return vsnprintf(str, size, format, args);
  7367. +}
  7368. +
  7369. +void DWC_PRINTF(char *format, ...)
  7370. +{
  7371. + va_list args;
  7372. +
  7373. + va_start(args, format);
  7374. + DWC_VPRINTF(format, args);
  7375. + va_end(args);
  7376. +}
  7377. +
  7378. +int DWC_SPRINTF(char *buffer, char *format, ...)
  7379. +{
  7380. + int retval;
  7381. + va_list args;
  7382. +
  7383. + va_start(args, format);
  7384. + retval = vsprintf(buffer, format, args);
  7385. + va_end(args);
  7386. + return retval;
  7387. +}
  7388. +
  7389. +int DWC_SNPRINTF(char *buffer, int size, char *format, ...)
  7390. +{
  7391. + int retval;
  7392. + va_list args;
  7393. +
  7394. + va_start(args, format);
  7395. + retval = vsnprintf(buffer, size, format, args);
  7396. + va_end(args);
  7397. + return retval;
  7398. +}
  7399. +
  7400. +void __DWC_WARN(char *format, ...)
  7401. +{
  7402. + va_list args;
  7403. +
  7404. + va_start(args, format);
  7405. + DWC_PRINTF(KERN_WARNING);
  7406. + DWC_VPRINTF(format, args);
  7407. + va_end(args);
  7408. +}
  7409. +
  7410. +void __DWC_ERROR(char *format, ...)
  7411. +{
  7412. + va_list args;
  7413. +
  7414. + va_start(args, format);
  7415. + DWC_PRINTF(KERN_ERR);
  7416. + DWC_VPRINTF(format, args);
  7417. + va_end(args);
  7418. +}
  7419. +
  7420. +void DWC_EXCEPTION(char *format, ...)
  7421. +{
  7422. + va_list args;
  7423. +
  7424. + va_start(args, format);
  7425. + DWC_PRINTF(KERN_ERR);
  7426. + DWC_VPRINTF(format, args);
  7427. + va_end(args);
  7428. + BUG_ON(1);
  7429. +}
  7430. +
  7431. +#ifdef DEBUG
  7432. +void __DWC_DEBUG(char *format, ...)
  7433. +{
  7434. + va_list args;
  7435. +
  7436. + va_start(args, format);
  7437. + DWC_PRINTF(KERN_DEBUG);
  7438. + DWC_VPRINTF(format, args);
  7439. + va_end(args);
  7440. +}
  7441. +#endif
  7442. +
  7443. +
  7444. +/* dwc_mem.h */
  7445. +
  7446. +#if 0
  7447. +dwc_pool_t *DWC_DMA_POOL_CREATE(uint32_t size,
  7448. + uint32_t align,
  7449. + uint32_t alloc)
  7450. +{
  7451. + struct dma_pool *pool = dma_pool_create("Pool", NULL,
  7452. + size, align, alloc);
  7453. + return (dwc_pool_t *)pool;
  7454. +}
  7455. +
  7456. +void DWC_DMA_POOL_DESTROY(dwc_pool_t *pool)
  7457. +{
  7458. + dma_pool_destroy((struct dma_pool *)pool);
  7459. +}
  7460. +
  7461. +void *DWC_DMA_POOL_ALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
  7462. +{
  7463. + return dma_pool_alloc((struct dma_pool *)pool, GFP_KERNEL, dma_addr);
  7464. +}
  7465. +
  7466. +void *DWC_DMA_POOL_ZALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
  7467. +{
  7468. + void *vaddr = DWC_DMA_POOL_ALLOC(pool, dma_addr);
  7469. + memset(..);
  7470. +}
  7471. +
  7472. +void DWC_DMA_POOL_FREE(dwc_pool_t *pool, void *vaddr, void *daddr)
  7473. +{
  7474. + dma_pool_free(pool, vaddr, daddr);
  7475. +}
  7476. +#endif
  7477. +
  7478. +void *__DWC_DMA_ALLOC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr)
  7479. +{
  7480. + return dma_zalloc_coherent(dma_ctx, size, dma_addr, GFP_KERNEL | GFP_DMA32);
  7481. +}
  7482. +
  7483. +void *__DWC_DMA_ALLOC_ATOMIC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr)
  7484. +{
  7485. + return dma_zalloc_coherent(dma_ctx, size, dma_addr, GFP_ATOMIC);
  7486. +}
  7487. +
  7488. +void __DWC_DMA_FREE(void *dma_ctx, uint32_t size, void *virt_addr, dwc_dma_t dma_addr)
  7489. +{
  7490. + dma_free_coherent(dma_ctx, size, virt_addr, dma_addr);
  7491. +}
  7492. +
  7493. +void *__DWC_ALLOC(void *mem_ctx, uint32_t size)
  7494. +{
  7495. + return kzalloc(size, GFP_KERNEL);
  7496. +}
  7497. +
  7498. +void *__DWC_ALLOC_ATOMIC(void *mem_ctx, uint32_t size)
  7499. +{
  7500. + return kzalloc(size, GFP_ATOMIC);
  7501. +}
  7502. +
  7503. +void __DWC_FREE(void *mem_ctx, void *addr)
  7504. +{
  7505. + kfree(addr);
  7506. +}
  7507. +
  7508. +
  7509. +#ifdef DWC_CRYPTOLIB
  7510. +/* dwc_crypto.h */
  7511. +
  7512. +void DWC_RANDOM_BYTES(uint8_t *buffer, uint32_t length)
  7513. +{
  7514. + get_random_bytes(buffer, length);
  7515. +}
  7516. +
  7517. +int DWC_AES_CBC(uint8_t *message, uint32_t messagelen, uint8_t *key, uint32_t keylen, uint8_t iv[16], uint8_t *out)
  7518. +{
  7519. + struct crypto_blkcipher *tfm;
  7520. + struct blkcipher_desc desc;
  7521. + struct scatterlist sgd;
  7522. + struct scatterlist sgs;
  7523. +
  7524. + tfm = crypto_alloc_blkcipher("cbc(aes)", 0, CRYPTO_ALG_ASYNC);
  7525. + if (tfm == NULL) {
  7526. + printk("failed to load transform for aes CBC\n");
  7527. + return -1;
  7528. + }
  7529. +
  7530. + crypto_blkcipher_setkey(tfm, key, keylen);
  7531. + crypto_blkcipher_set_iv(tfm, iv, 16);
  7532. +
  7533. + sg_init_one(&sgd, out, messagelen);
  7534. + sg_init_one(&sgs, message, messagelen);
  7535. +
  7536. + desc.tfm = tfm;
  7537. + desc.flags = 0;
  7538. +
  7539. + if (crypto_blkcipher_encrypt(&desc, &sgd, &sgs, messagelen)) {
  7540. + crypto_free_blkcipher(tfm);
  7541. + DWC_ERROR("AES CBC encryption failed");
  7542. + return -1;
  7543. + }
  7544. +
  7545. + crypto_free_blkcipher(tfm);
  7546. + return 0;
  7547. +}
  7548. +
  7549. +int DWC_SHA256(uint8_t *message, uint32_t len, uint8_t *out)
  7550. +{
  7551. + struct crypto_hash *tfm;
  7552. + struct hash_desc desc;
  7553. + struct scatterlist sg;
  7554. +
  7555. + tfm = crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC);
  7556. + if (IS_ERR(tfm)) {
  7557. + DWC_ERROR("Failed to load transform for sha256: %ld\n", PTR_ERR(tfm));
  7558. + return 0;
  7559. + }
  7560. + desc.tfm = tfm;
  7561. + desc.flags = 0;
  7562. +
  7563. + sg_init_one(&sg, message, len);
  7564. + crypto_hash_digest(&desc, &sg, len, out);
  7565. + crypto_free_hash(tfm);
  7566. +
  7567. + return 1;
  7568. +}
  7569. +
  7570. +int DWC_HMAC_SHA256(uint8_t *message, uint32_t messagelen,
  7571. + uint8_t *key, uint32_t keylen, uint8_t *out)
  7572. +{
  7573. + struct crypto_hash *tfm;
  7574. + struct hash_desc desc;
  7575. + struct scatterlist sg;
  7576. +
  7577. + tfm = crypto_alloc_hash("hmac(sha256)", 0, CRYPTO_ALG_ASYNC);
  7578. + if (IS_ERR(tfm)) {
  7579. + DWC_ERROR("Failed to load transform for hmac(sha256): %ld\n", PTR_ERR(tfm));
  7580. + return 0;
  7581. + }
  7582. + desc.tfm = tfm;
  7583. + desc.flags = 0;
  7584. +
  7585. + sg_init_one(&sg, message, messagelen);
  7586. + crypto_hash_setkey(tfm, key, keylen);
  7587. + crypto_hash_digest(&desc, &sg, messagelen, out);
  7588. + crypto_free_hash(tfm);
  7589. +
  7590. + return 1;
  7591. +}
  7592. +#endif /* DWC_CRYPTOLIB */
  7593. +
  7594. +
  7595. +/* Byte Ordering Conversions */
  7596. +
  7597. +uint32_t DWC_CPU_TO_LE32(uint32_t *p)
  7598. +{
  7599. +#ifdef __LITTLE_ENDIAN
  7600. + return *p;
  7601. +#else
  7602. + uint8_t *u_p = (uint8_t *)p;
  7603. +
  7604. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  7605. +#endif
  7606. +}
  7607. +
  7608. +uint32_t DWC_CPU_TO_BE32(uint32_t *p)
  7609. +{
  7610. +#ifdef __BIG_ENDIAN
  7611. + return *p;
  7612. +#else
  7613. + uint8_t *u_p = (uint8_t *)p;
  7614. +
  7615. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  7616. +#endif
  7617. +}
  7618. +
  7619. +uint32_t DWC_LE32_TO_CPU(uint32_t *p)
  7620. +{
  7621. +#ifdef __LITTLE_ENDIAN
  7622. + return *p;
  7623. +#else
  7624. + uint8_t *u_p = (uint8_t *)p;
  7625. +
  7626. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  7627. +#endif
  7628. +}
  7629. +
  7630. +uint32_t DWC_BE32_TO_CPU(uint32_t *p)
  7631. +{
  7632. +#ifdef __BIG_ENDIAN
  7633. + return *p;
  7634. +#else
  7635. + uint8_t *u_p = (uint8_t *)p;
  7636. +
  7637. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  7638. +#endif
  7639. +}
  7640. +
  7641. +uint16_t DWC_CPU_TO_LE16(uint16_t *p)
  7642. +{
  7643. +#ifdef __LITTLE_ENDIAN
  7644. + return *p;
  7645. +#else
  7646. + uint8_t *u_p = (uint8_t *)p;
  7647. + return (u_p[1] | (u_p[0] << 8));
  7648. +#endif
  7649. +}
  7650. +
  7651. +uint16_t DWC_CPU_TO_BE16(uint16_t *p)
  7652. +{
  7653. +#ifdef __BIG_ENDIAN
  7654. + return *p;
  7655. +#else
  7656. + uint8_t *u_p = (uint8_t *)p;
  7657. + return (u_p[1] | (u_p[0] << 8));
  7658. +#endif
  7659. +}
  7660. +
  7661. +uint16_t DWC_LE16_TO_CPU(uint16_t *p)
  7662. +{
  7663. +#ifdef __LITTLE_ENDIAN
  7664. + return *p;
  7665. +#else
  7666. + uint8_t *u_p = (uint8_t *)p;
  7667. + return (u_p[1] | (u_p[0] << 8));
  7668. +#endif
  7669. +}
  7670. +
  7671. +uint16_t DWC_BE16_TO_CPU(uint16_t *p)
  7672. +{
  7673. +#ifdef __BIG_ENDIAN
  7674. + return *p;
  7675. +#else
  7676. + uint8_t *u_p = (uint8_t *)p;
  7677. + return (u_p[1] | (u_p[0] << 8));
  7678. +#endif
  7679. +}
  7680. +
  7681. +
  7682. +/* Registers */
  7683. +
  7684. +uint32_t DWC_READ_REG32(uint32_t volatile *reg)
  7685. +{
  7686. + return readl(reg);
  7687. +}
  7688. +
  7689. +#if 0
  7690. +uint64_t DWC_READ_REG64(uint64_t volatile *reg)
  7691. +{
  7692. +}
  7693. +#endif
  7694. +
  7695. +void DWC_WRITE_REG32(uint32_t volatile *reg, uint32_t value)
  7696. +{
  7697. + writel(value, reg);
  7698. +}
  7699. +
  7700. +#if 0
  7701. +void DWC_WRITE_REG64(uint64_t volatile *reg, uint64_t value)
  7702. +{
  7703. +}
  7704. +#endif
  7705. +
  7706. +void DWC_MODIFY_REG32(uint32_t volatile *reg, uint32_t clear_mask, uint32_t set_mask)
  7707. +{
  7708. + writel((readl(reg) & ~clear_mask) | set_mask, reg);
  7709. +}
  7710. +
  7711. +#if 0
  7712. +void DWC_MODIFY_REG64(uint64_t volatile *reg, uint64_t clear_mask, uint64_t set_mask)
  7713. +{
  7714. +}
  7715. +#endif
  7716. +
  7717. +
  7718. +/* Locking */
  7719. +
  7720. +dwc_spinlock_t *DWC_SPINLOCK_ALLOC(void)
  7721. +{
  7722. + spinlock_t *sl = (spinlock_t *)1;
  7723. +
  7724. +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
  7725. + sl = DWC_ALLOC(sizeof(*sl));
  7726. + if (!sl) {
  7727. + DWC_ERROR("Cannot allocate memory for spinlock\n");
  7728. + return NULL;
  7729. + }
  7730. +
  7731. + spin_lock_init(sl);
  7732. +#endif
  7733. + return (dwc_spinlock_t *)sl;
  7734. +}
  7735. +
  7736. +void DWC_SPINLOCK_FREE(dwc_spinlock_t *lock)
  7737. +{
  7738. +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
  7739. + DWC_FREE(lock);
  7740. +#endif
  7741. +}
  7742. +
  7743. +void DWC_SPINLOCK(dwc_spinlock_t *lock)
  7744. +{
  7745. +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
  7746. + spin_lock((spinlock_t *)lock);
  7747. +#endif
  7748. +}
  7749. +
  7750. +void DWC_SPINUNLOCK(dwc_spinlock_t *lock)
  7751. +{
  7752. +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
  7753. + spin_unlock((spinlock_t *)lock);
  7754. +#endif
  7755. +}
  7756. +
  7757. +void DWC_SPINLOCK_IRQSAVE(dwc_spinlock_t *lock, dwc_irqflags_t *flags)
  7758. +{
  7759. + dwc_irqflags_t f;
  7760. +
  7761. +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
  7762. + spin_lock_irqsave((spinlock_t *)lock, f);
  7763. +#else
  7764. + local_irq_save(f);
  7765. +#endif
  7766. + *flags = f;
  7767. +}
  7768. +
  7769. +void DWC_SPINUNLOCK_IRQRESTORE(dwc_spinlock_t *lock, dwc_irqflags_t flags)
  7770. +{
  7771. +#if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
  7772. + spin_unlock_irqrestore((spinlock_t *)lock, flags);
  7773. +#else
  7774. + local_irq_restore(flags);
  7775. +#endif
  7776. +}
  7777. +
  7778. +dwc_mutex_t *DWC_MUTEX_ALLOC(void)
  7779. +{
  7780. + struct mutex *m;
  7781. + dwc_mutex_t *mutex = (dwc_mutex_t *)DWC_ALLOC(sizeof(struct mutex));
  7782. +
  7783. + if (!mutex) {
  7784. + DWC_ERROR("Cannot allocate memory for mutex\n");
  7785. + return NULL;
  7786. + }
  7787. +
  7788. + m = (struct mutex *)mutex;
  7789. + mutex_init(m);
  7790. + return mutex;
  7791. +}
  7792. +
  7793. +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
  7794. +#else
  7795. +void DWC_MUTEX_FREE(dwc_mutex_t *mutex)
  7796. +{
  7797. + mutex_destroy((struct mutex *)mutex);
  7798. + DWC_FREE(mutex);
  7799. +}
  7800. +#endif
  7801. +
  7802. +void DWC_MUTEX_LOCK(dwc_mutex_t *mutex)
  7803. +{
  7804. + struct mutex *m = (struct mutex *)mutex;
  7805. + mutex_lock(m);
  7806. +}
  7807. +
  7808. +int DWC_MUTEX_TRYLOCK(dwc_mutex_t *mutex)
  7809. +{
  7810. + struct mutex *m = (struct mutex *)mutex;
  7811. + return mutex_trylock(m);
  7812. +}
  7813. +
  7814. +void DWC_MUTEX_UNLOCK(dwc_mutex_t *mutex)
  7815. +{
  7816. + struct mutex *m = (struct mutex *)mutex;
  7817. + mutex_unlock(m);
  7818. +}
  7819. +
  7820. +
  7821. +/* Timing */
  7822. +
  7823. +void DWC_UDELAY(uint32_t usecs)
  7824. +{
  7825. + udelay(usecs);
  7826. +}
  7827. +
  7828. +void DWC_MDELAY(uint32_t msecs)
  7829. +{
  7830. + mdelay(msecs);
  7831. +}
  7832. +
  7833. +void DWC_MSLEEP(uint32_t msecs)
  7834. +{
  7835. + msleep(msecs);
  7836. +}
  7837. +
  7838. +uint32_t DWC_TIME(void)
  7839. +{
  7840. + return jiffies_to_msecs(jiffies);
  7841. +}
  7842. +
  7843. +
  7844. +/* Timers */
  7845. +
  7846. +struct dwc_timer {
  7847. + struct timer_list *t;
  7848. + char *name;
  7849. + dwc_timer_callback_t cb;
  7850. + void *data;
  7851. + uint8_t scheduled;
  7852. + dwc_spinlock_t *lock;
  7853. +};
  7854. +
  7855. +static void timer_callback(unsigned long data)
  7856. +{
  7857. + dwc_timer_t *timer = (dwc_timer_t *)data;
  7858. + dwc_irqflags_t flags;
  7859. +
  7860. + DWC_SPINLOCK_IRQSAVE(timer->lock, &flags);
  7861. + timer->scheduled = 0;
  7862. + DWC_SPINUNLOCK_IRQRESTORE(timer->lock, flags);
  7863. + DWC_DEBUGC("Timer %s callback", timer->name);
  7864. + timer->cb(timer->data);
  7865. +}
  7866. +
  7867. +dwc_timer_t *DWC_TIMER_ALLOC(char *name, dwc_timer_callback_t cb, void *data)
  7868. +{
  7869. + dwc_timer_t *t = DWC_ALLOC(sizeof(*t));
  7870. +
  7871. + if (!t) {
  7872. + DWC_ERROR("Cannot allocate memory for timer");
  7873. + return NULL;
  7874. + }
  7875. +
  7876. + t->t = DWC_ALLOC(sizeof(*t->t));
  7877. + if (!t->t) {
  7878. + DWC_ERROR("Cannot allocate memory for timer->t");
  7879. + goto no_timer;
  7880. + }
  7881. +
  7882. + t->name = DWC_STRDUP(name);
  7883. + if (!t->name) {
  7884. + DWC_ERROR("Cannot allocate memory for timer->name");
  7885. + goto no_name;
  7886. + }
  7887. +
  7888. +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_SPINLOCK))
  7889. + DWC_SPINLOCK_ALLOC_LINUX_DEBUG(t->lock);
  7890. +#else
  7891. + t->lock = DWC_SPINLOCK_ALLOC();
  7892. +#endif
  7893. + if (!t->lock) {
  7894. + DWC_ERROR("Cannot allocate memory for lock");
  7895. + goto no_lock;
  7896. + }
  7897. +
  7898. + t->scheduled = 0;
  7899. + t->t->expires = jiffies;
  7900. + setup_timer(t->t, timer_callback, (unsigned long)t);
  7901. +
  7902. + t->cb = cb;
  7903. + t->data = data;
  7904. +
  7905. + return t;
  7906. +
  7907. + no_lock:
  7908. + DWC_FREE(t->name);
  7909. + no_name:
  7910. + DWC_FREE(t->t);
  7911. + no_timer:
  7912. + DWC_FREE(t);
  7913. + return NULL;
  7914. +}
  7915. +
  7916. +void DWC_TIMER_FREE(dwc_timer_t *timer)
  7917. +{
  7918. + dwc_irqflags_t flags;
  7919. +
  7920. + DWC_SPINLOCK_IRQSAVE(timer->lock, &flags);
  7921. +
  7922. + if (timer->scheduled) {
  7923. + del_timer(timer->t);
  7924. + timer->scheduled = 0;
  7925. + }
  7926. +
  7927. + DWC_SPINUNLOCK_IRQRESTORE(timer->lock, flags);
  7928. + DWC_SPINLOCK_FREE(timer->lock);
  7929. + DWC_FREE(timer->t);
  7930. + DWC_FREE(timer->name);
  7931. + DWC_FREE(timer);
  7932. +}
  7933. +
  7934. +void DWC_TIMER_SCHEDULE(dwc_timer_t *timer, uint32_t time)
  7935. +{
  7936. + dwc_irqflags_t flags;
  7937. +
  7938. + DWC_SPINLOCK_IRQSAVE(timer->lock, &flags);
  7939. +
  7940. + if (!timer->scheduled) {
  7941. + timer->scheduled = 1;
  7942. + DWC_DEBUGC("Scheduling timer %s to expire in +%d msec", timer->name, time);
  7943. + timer->t->expires = jiffies + msecs_to_jiffies(time);
  7944. + add_timer(timer->t);
  7945. + } else {
  7946. + DWC_DEBUGC("Modifying timer %s to expire in +%d msec", timer->name, time);
  7947. + mod_timer(timer->t, jiffies + msecs_to_jiffies(time));
  7948. + }
  7949. +
  7950. + DWC_SPINUNLOCK_IRQRESTORE(timer->lock, flags);
  7951. +}
  7952. +
  7953. +void DWC_TIMER_CANCEL(dwc_timer_t *timer)
  7954. +{
  7955. + del_timer(timer->t);
  7956. +}
  7957. +
  7958. +
  7959. +/* Wait Queues */
  7960. +
  7961. +struct dwc_waitq {
  7962. + wait_queue_head_t queue;
  7963. + int abort;
  7964. +};
  7965. +
  7966. +dwc_waitq_t *DWC_WAITQ_ALLOC(void)
  7967. +{
  7968. + dwc_waitq_t *wq = DWC_ALLOC(sizeof(*wq));
  7969. +
  7970. + if (!wq) {
  7971. + DWC_ERROR("Cannot allocate memory for waitqueue\n");
  7972. + return NULL;
  7973. + }
  7974. +
  7975. + init_waitqueue_head(&wq->queue);
  7976. + wq->abort = 0;
  7977. + return wq;
  7978. +}
  7979. +
  7980. +void DWC_WAITQ_FREE(dwc_waitq_t *wq)
  7981. +{
  7982. + DWC_FREE(wq);
  7983. +}
  7984. +
  7985. +int32_t DWC_WAITQ_WAIT(dwc_waitq_t *wq, dwc_waitq_condition_t cond, void *data)
  7986. +{
  7987. + int result = wait_event_interruptible(wq->queue,
  7988. + cond(data) || wq->abort);
  7989. + if (result == -ERESTARTSYS) {
  7990. + wq->abort = 0;
  7991. + return -DWC_E_RESTART;
  7992. + }
  7993. +
  7994. + if (wq->abort == 1) {
  7995. + wq->abort = 0;
  7996. + return -DWC_E_ABORT;
  7997. + }
  7998. +
  7999. + wq->abort = 0;
  8000. +
  8001. + if (result == 0) {
  8002. + return 0;
  8003. + }
  8004. +
  8005. + return -DWC_E_UNKNOWN;
  8006. +}
  8007. +
  8008. +int32_t DWC_WAITQ_WAIT_TIMEOUT(dwc_waitq_t *wq, dwc_waitq_condition_t cond,
  8009. + void *data, int32_t msecs)
  8010. +{
  8011. + int32_t tmsecs;
  8012. + int result = wait_event_interruptible_timeout(wq->queue,
  8013. + cond(data) || wq->abort,
  8014. + msecs_to_jiffies(msecs));
  8015. + if (result == -ERESTARTSYS) {
  8016. + wq->abort = 0;
  8017. + return -DWC_E_RESTART;
  8018. + }
  8019. +
  8020. + if (wq->abort == 1) {
  8021. + wq->abort = 0;
  8022. + return -DWC_E_ABORT;
  8023. + }
  8024. +
  8025. + wq->abort = 0;
  8026. +
  8027. + if (result > 0) {
  8028. + tmsecs = jiffies_to_msecs(result);
  8029. + if (!tmsecs) {
  8030. + return 1;
  8031. + }
  8032. +
  8033. + return tmsecs;
  8034. + }
  8035. +
  8036. + if (result == 0) {
  8037. + return -DWC_E_TIMEOUT;
  8038. + }
  8039. +
  8040. + return -DWC_E_UNKNOWN;
  8041. +}
  8042. +
  8043. +void DWC_WAITQ_TRIGGER(dwc_waitq_t *wq)
  8044. +{
  8045. + wq->abort = 0;
  8046. + wake_up_interruptible(&wq->queue);
  8047. +}
  8048. +
  8049. +void DWC_WAITQ_ABORT(dwc_waitq_t *wq)
  8050. +{
  8051. + wq->abort = 1;
  8052. + wake_up_interruptible(&wq->queue);
  8053. +}
  8054. +
  8055. +
  8056. +/* Threading */
  8057. +
  8058. +dwc_thread_t *DWC_THREAD_RUN(dwc_thread_function_t func, char *name, void *data)
  8059. +{
  8060. + struct task_struct *thread = kthread_run(func, data, name);
  8061. +
  8062. + if (thread == ERR_PTR(-ENOMEM)) {
  8063. + return NULL;
  8064. + }
  8065. +
  8066. + return (dwc_thread_t *)thread;
  8067. +}
  8068. +
  8069. +int DWC_THREAD_STOP(dwc_thread_t *thread)
  8070. +{
  8071. + return kthread_stop((struct task_struct *)thread);
  8072. +}
  8073. +
  8074. +dwc_bool_t DWC_THREAD_SHOULD_STOP(void)
  8075. +{
  8076. + return kthread_should_stop();
  8077. +}
  8078. +
  8079. +
  8080. +/* tasklets
  8081. + - run in interrupt context (cannot sleep)
  8082. + - each tasklet runs on a single CPU
  8083. + - different tasklets can be running simultaneously on different CPUs
  8084. + */
  8085. +struct dwc_tasklet {
  8086. + struct tasklet_struct t;
  8087. + dwc_tasklet_callback_t cb;
  8088. + void *data;
  8089. +};
  8090. +
  8091. +static void tasklet_callback(unsigned long data)
  8092. +{
  8093. + dwc_tasklet_t *t = (dwc_tasklet_t *)data;
  8094. + t->cb(t->data);
  8095. +}
  8096. +
  8097. +dwc_tasklet_t *DWC_TASK_ALLOC(char *name, dwc_tasklet_callback_t cb, void *data)
  8098. +{
  8099. + dwc_tasklet_t *t = DWC_ALLOC(sizeof(*t));
  8100. +
  8101. + if (t) {
  8102. + t->cb = cb;
  8103. + t->data = data;
  8104. + tasklet_init(&t->t, tasklet_callback, (unsigned long)t);
  8105. + } else {
  8106. + DWC_ERROR("Cannot allocate memory for tasklet\n");
  8107. + }
  8108. +
  8109. + return t;
  8110. +}
  8111. +
  8112. +void DWC_TASK_FREE(dwc_tasklet_t *task)
  8113. +{
  8114. + DWC_FREE(task);
  8115. +}
  8116. +
  8117. +void DWC_TASK_SCHEDULE(dwc_tasklet_t *task)
  8118. +{
  8119. + tasklet_schedule(&task->t);
  8120. +}
  8121. +
  8122. +void DWC_TASK_HI_SCHEDULE(dwc_tasklet_t *task)
  8123. +{
  8124. + tasklet_hi_schedule(&task->t);
  8125. +}
  8126. +
  8127. +
  8128. +/* workqueues
  8129. + - run in process context (can sleep)
  8130. + */
  8131. +typedef struct work_container {
  8132. + dwc_work_callback_t cb;
  8133. + void *data;
  8134. + dwc_workq_t *wq;
  8135. + char *name;
  8136. +
  8137. +#ifdef DEBUG
  8138. + DWC_CIRCLEQ_ENTRY(work_container) entry;
  8139. +#endif
  8140. + struct delayed_work work;
  8141. +} work_container_t;
  8142. +
  8143. +#ifdef DEBUG
  8144. +DWC_CIRCLEQ_HEAD(work_container_queue, work_container);
  8145. +#endif
  8146. +
  8147. +struct dwc_workq {
  8148. + struct workqueue_struct *wq;
  8149. + dwc_spinlock_t *lock;
  8150. + dwc_waitq_t *waitq;
  8151. + int pending;
  8152. +
  8153. +#ifdef DEBUG
  8154. + struct work_container_queue entries;
  8155. +#endif
  8156. +};
  8157. +
  8158. +static void do_work(struct work_struct *work)
  8159. +{
  8160. + dwc_irqflags_t flags;
  8161. + struct delayed_work *dw = container_of(work, struct delayed_work, work);
  8162. + work_container_t *container = container_of(dw, struct work_container, work);
  8163. + dwc_workq_t *wq = container->wq;
  8164. +
  8165. + container->cb(container->data);
  8166. +
  8167. +#ifdef DEBUG
  8168. + DWC_CIRCLEQ_REMOVE(&wq->entries, container, entry);
  8169. +#endif
  8170. + DWC_DEBUGC("Work done: %s, container=%p", container->name, container);
  8171. + if (container->name) {
  8172. + DWC_FREE(container->name);
  8173. + }
  8174. + DWC_FREE(container);
  8175. +
  8176. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  8177. + wq->pending--;
  8178. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  8179. + DWC_WAITQ_TRIGGER(wq->waitq);
  8180. +}
  8181. +
  8182. +static int work_done(void *data)
  8183. +{
  8184. + dwc_workq_t *workq = (dwc_workq_t *)data;
  8185. + return workq->pending == 0;
  8186. +}
  8187. +
  8188. +int DWC_WORKQ_WAIT_WORK_DONE(dwc_workq_t *workq, int timeout)
  8189. +{
  8190. + return DWC_WAITQ_WAIT_TIMEOUT(workq->waitq, work_done, workq, timeout);
  8191. +}
  8192. +
  8193. +dwc_workq_t *DWC_WORKQ_ALLOC(char *name)
  8194. +{
  8195. + dwc_workq_t *wq = DWC_ALLOC(sizeof(*wq));
  8196. +
  8197. + if (!wq) {
  8198. + return NULL;
  8199. + }
  8200. +
  8201. + wq->wq = create_singlethread_workqueue(name);
  8202. + if (!wq->wq) {
  8203. + goto no_wq;
  8204. + }
  8205. +
  8206. + wq->pending = 0;
  8207. +
  8208. +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_SPINLOCK))
  8209. + DWC_SPINLOCK_ALLOC_LINUX_DEBUG(wq->lock);
  8210. +#else
  8211. + wq->lock = DWC_SPINLOCK_ALLOC();
  8212. +#endif
  8213. + if (!wq->lock) {
  8214. + goto no_lock;
  8215. + }
  8216. +
  8217. + wq->waitq = DWC_WAITQ_ALLOC();
  8218. + if (!wq->waitq) {
  8219. + goto no_waitq;
  8220. + }
  8221. +
  8222. +#ifdef DEBUG
  8223. + DWC_CIRCLEQ_INIT(&wq->entries);
  8224. +#endif
  8225. + return wq;
  8226. +
  8227. + no_waitq:
  8228. + DWC_SPINLOCK_FREE(wq->lock);
  8229. + no_lock:
  8230. + destroy_workqueue(wq->wq);
  8231. + no_wq:
  8232. + DWC_FREE(wq);
  8233. +
  8234. + return NULL;
  8235. +}
  8236. +
  8237. +void DWC_WORKQ_FREE(dwc_workq_t *wq)
  8238. +{
  8239. +#ifdef DEBUG
  8240. + if (wq->pending != 0) {
  8241. + struct work_container *wc;
  8242. + DWC_ERROR("Destroying work queue with pending work");
  8243. + DWC_CIRCLEQ_FOREACH(wc, &wq->entries, entry) {
  8244. + DWC_ERROR("Work %s still pending", wc->name);
  8245. + }
  8246. + }
  8247. +#endif
  8248. + destroy_workqueue(wq->wq);
  8249. + DWC_SPINLOCK_FREE(wq->lock);
  8250. + DWC_WAITQ_FREE(wq->waitq);
  8251. + DWC_FREE(wq);
  8252. +}
  8253. +
  8254. +void DWC_WORKQ_SCHEDULE(dwc_workq_t *wq, dwc_work_callback_t cb, void *data,
  8255. + char *format, ...)
  8256. +{
  8257. + dwc_irqflags_t flags;
  8258. + work_container_t *container;
  8259. + static char name[128];
  8260. + va_list args;
  8261. +
  8262. + va_start(args, format);
  8263. + DWC_VSNPRINTF(name, 128, format, args);
  8264. + va_end(args);
  8265. +
  8266. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  8267. + wq->pending++;
  8268. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  8269. + DWC_WAITQ_TRIGGER(wq->waitq);
  8270. +
  8271. + container = DWC_ALLOC_ATOMIC(sizeof(*container));
  8272. + if (!container) {
  8273. + DWC_ERROR("Cannot allocate memory for container\n");
  8274. + return;
  8275. + }
  8276. +
  8277. + container->name = DWC_STRDUP(name);
  8278. + if (!container->name) {
  8279. + DWC_ERROR("Cannot allocate memory for container->name\n");
  8280. + DWC_FREE(container);
  8281. + return;
  8282. + }
  8283. +
  8284. + container->cb = cb;
  8285. + container->data = data;
  8286. + container->wq = wq;
  8287. + DWC_DEBUGC("Queueing work: %s, container=%p", container->name, container);
  8288. + INIT_WORK(&container->work.work, do_work);
  8289. +
  8290. +#ifdef DEBUG
  8291. + DWC_CIRCLEQ_INSERT_TAIL(&wq->entries, container, entry);
  8292. +#endif
  8293. + queue_work(wq->wq, &container->work.work);
  8294. +}
  8295. +
  8296. +void DWC_WORKQ_SCHEDULE_DELAYED(dwc_workq_t *wq, dwc_work_callback_t cb,
  8297. + void *data, uint32_t time, char *format, ...)
  8298. +{
  8299. + dwc_irqflags_t flags;
  8300. + work_container_t *container;
  8301. + static char name[128];
  8302. + va_list args;
  8303. +
  8304. + va_start(args, format);
  8305. + DWC_VSNPRINTF(name, 128, format, args);
  8306. + va_end(args);
  8307. +
  8308. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  8309. + wq->pending++;
  8310. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  8311. + DWC_WAITQ_TRIGGER(wq->waitq);
  8312. +
  8313. + container = DWC_ALLOC_ATOMIC(sizeof(*container));
  8314. + if (!container) {
  8315. + DWC_ERROR("Cannot allocate memory for container\n");
  8316. + return;
  8317. + }
  8318. +
  8319. + container->name = DWC_STRDUP(name);
  8320. + if (!container->name) {
  8321. + DWC_ERROR("Cannot allocate memory for container->name\n");
  8322. + DWC_FREE(container);
  8323. + return;
  8324. + }
  8325. +
  8326. + container->cb = cb;
  8327. + container->data = data;
  8328. + container->wq = wq;
  8329. + DWC_DEBUGC("Queueing work: %s, container=%p", container->name, container);
  8330. + INIT_DELAYED_WORK(&container->work, do_work);
  8331. +
  8332. +#ifdef DEBUG
  8333. + DWC_CIRCLEQ_INSERT_TAIL(&wq->entries, container, entry);
  8334. +#endif
  8335. + queue_delayed_work(wq->wq, &container->work, msecs_to_jiffies(time));
  8336. +}
  8337. +
  8338. +int DWC_WORKQ_PENDING(dwc_workq_t *wq)
  8339. +{
  8340. + return wq->pending;
  8341. +}
  8342. +
  8343. +
  8344. +#ifdef DWC_LIBMODULE
  8345. +
  8346. +#ifdef DWC_CCLIB
  8347. +/* CC */
  8348. +EXPORT_SYMBOL(dwc_cc_if_alloc);
  8349. +EXPORT_SYMBOL(dwc_cc_if_free);
  8350. +EXPORT_SYMBOL(dwc_cc_clear);
  8351. +EXPORT_SYMBOL(dwc_cc_add);
  8352. +EXPORT_SYMBOL(dwc_cc_remove);
  8353. +EXPORT_SYMBOL(dwc_cc_change);
  8354. +EXPORT_SYMBOL(dwc_cc_data_for_save);
  8355. +EXPORT_SYMBOL(dwc_cc_restore_from_data);
  8356. +EXPORT_SYMBOL(dwc_cc_match_chid);
  8357. +EXPORT_SYMBOL(dwc_cc_match_cdid);
  8358. +EXPORT_SYMBOL(dwc_cc_ck);
  8359. +EXPORT_SYMBOL(dwc_cc_chid);
  8360. +EXPORT_SYMBOL(dwc_cc_cdid);
  8361. +EXPORT_SYMBOL(dwc_cc_name);
  8362. +#endif /* DWC_CCLIB */
  8363. +
  8364. +#ifdef DWC_CRYPTOLIB
  8365. +# ifndef CONFIG_MACH_IPMATE
  8366. +/* Modpow */
  8367. +EXPORT_SYMBOL(dwc_modpow);
  8368. +
  8369. +/* DH */
  8370. +EXPORT_SYMBOL(dwc_dh_modpow);
  8371. +EXPORT_SYMBOL(dwc_dh_derive_keys);
  8372. +EXPORT_SYMBOL(dwc_dh_pk);
  8373. +# endif /* CONFIG_MACH_IPMATE */
  8374. +
  8375. +/* Crypto */
  8376. +EXPORT_SYMBOL(dwc_wusb_aes_encrypt);
  8377. +EXPORT_SYMBOL(dwc_wusb_cmf);
  8378. +EXPORT_SYMBOL(dwc_wusb_prf);
  8379. +EXPORT_SYMBOL(dwc_wusb_fill_ccm_nonce);
  8380. +EXPORT_SYMBOL(dwc_wusb_gen_nonce);
  8381. +EXPORT_SYMBOL(dwc_wusb_gen_key);
  8382. +EXPORT_SYMBOL(dwc_wusb_gen_mic);
  8383. +#endif /* DWC_CRYPTOLIB */
  8384. +
  8385. +/* Notification */
  8386. +#ifdef DWC_NOTIFYLIB
  8387. +EXPORT_SYMBOL(dwc_alloc_notification_manager);
  8388. +EXPORT_SYMBOL(dwc_free_notification_manager);
  8389. +EXPORT_SYMBOL(dwc_register_notifier);
  8390. +EXPORT_SYMBOL(dwc_unregister_notifier);
  8391. +EXPORT_SYMBOL(dwc_add_observer);
  8392. +EXPORT_SYMBOL(dwc_remove_observer);
  8393. +EXPORT_SYMBOL(dwc_notify);
  8394. +#endif
  8395. +
  8396. +/* Memory Debugging Routines */
  8397. +#ifdef DWC_DEBUG_MEMORY
  8398. +EXPORT_SYMBOL(dwc_alloc_debug);
  8399. +EXPORT_SYMBOL(dwc_alloc_atomic_debug);
  8400. +EXPORT_SYMBOL(dwc_free_debug);
  8401. +EXPORT_SYMBOL(dwc_dma_alloc_debug);
  8402. +EXPORT_SYMBOL(dwc_dma_free_debug);
  8403. +#endif
  8404. +
  8405. +EXPORT_SYMBOL(DWC_MEMSET);
  8406. +EXPORT_SYMBOL(DWC_MEMCPY);
  8407. +EXPORT_SYMBOL(DWC_MEMMOVE);
  8408. +EXPORT_SYMBOL(DWC_MEMCMP);
  8409. +EXPORT_SYMBOL(DWC_STRNCMP);
  8410. +EXPORT_SYMBOL(DWC_STRCMP);
  8411. +EXPORT_SYMBOL(DWC_STRLEN);
  8412. +EXPORT_SYMBOL(DWC_STRCPY);
  8413. +EXPORT_SYMBOL(DWC_STRDUP);
  8414. +EXPORT_SYMBOL(DWC_ATOI);
  8415. +EXPORT_SYMBOL(DWC_ATOUI);
  8416. +
  8417. +#ifdef DWC_UTFLIB
  8418. +EXPORT_SYMBOL(DWC_UTF8_TO_UTF16LE);
  8419. +#endif /* DWC_UTFLIB */
  8420. +
  8421. +EXPORT_SYMBOL(DWC_IN_IRQ);
  8422. +EXPORT_SYMBOL(DWC_IN_BH);
  8423. +EXPORT_SYMBOL(DWC_VPRINTF);
  8424. +EXPORT_SYMBOL(DWC_VSNPRINTF);
  8425. +EXPORT_SYMBOL(DWC_PRINTF);
  8426. +EXPORT_SYMBOL(DWC_SPRINTF);
  8427. +EXPORT_SYMBOL(DWC_SNPRINTF);
  8428. +EXPORT_SYMBOL(__DWC_WARN);
  8429. +EXPORT_SYMBOL(__DWC_ERROR);
  8430. +EXPORT_SYMBOL(DWC_EXCEPTION);
  8431. +
  8432. +#ifdef DEBUG
  8433. +EXPORT_SYMBOL(__DWC_DEBUG);
  8434. +#endif
  8435. +
  8436. +EXPORT_SYMBOL(__DWC_DMA_ALLOC);
  8437. +EXPORT_SYMBOL(__DWC_DMA_ALLOC_ATOMIC);
  8438. +EXPORT_SYMBOL(__DWC_DMA_FREE);
  8439. +EXPORT_SYMBOL(__DWC_ALLOC);
  8440. +EXPORT_SYMBOL(__DWC_ALLOC_ATOMIC);
  8441. +EXPORT_SYMBOL(__DWC_FREE);
  8442. +
  8443. +#ifdef DWC_CRYPTOLIB
  8444. +EXPORT_SYMBOL(DWC_RANDOM_BYTES);
  8445. +EXPORT_SYMBOL(DWC_AES_CBC);
  8446. +EXPORT_SYMBOL(DWC_SHA256);
  8447. +EXPORT_SYMBOL(DWC_HMAC_SHA256);
  8448. +#endif
  8449. +
  8450. +EXPORT_SYMBOL(DWC_CPU_TO_LE32);
  8451. +EXPORT_SYMBOL(DWC_CPU_TO_BE32);
  8452. +EXPORT_SYMBOL(DWC_LE32_TO_CPU);
  8453. +EXPORT_SYMBOL(DWC_BE32_TO_CPU);
  8454. +EXPORT_SYMBOL(DWC_CPU_TO_LE16);
  8455. +EXPORT_SYMBOL(DWC_CPU_TO_BE16);
  8456. +EXPORT_SYMBOL(DWC_LE16_TO_CPU);
  8457. +EXPORT_SYMBOL(DWC_BE16_TO_CPU);
  8458. +EXPORT_SYMBOL(DWC_READ_REG32);
  8459. +EXPORT_SYMBOL(DWC_WRITE_REG32);
  8460. +EXPORT_SYMBOL(DWC_MODIFY_REG32);
  8461. +
  8462. +#if 0
  8463. +EXPORT_SYMBOL(DWC_READ_REG64);
  8464. +EXPORT_SYMBOL(DWC_WRITE_REG64);
  8465. +EXPORT_SYMBOL(DWC_MODIFY_REG64);
  8466. +#endif
  8467. +
  8468. +EXPORT_SYMBOL(DWC_SPINLOCK_ALLOC);
  8469. +EXPORT_SYMBOL(DWC_SPINLOCK_FREE);
  8470. +EXPORT_SYMBOL(DWC_SPINLOCK);
  8471. +EXPORT_SYMBOL(DWC_SPINUNLOCK);
  8472. +EXPORT_SYMBOL(DWC_SPINLOCK_IRQSAVE);
  8473. +EXPORT_SYMBOL(DWC_SPINUNLOCK_IRQRESTORE);
  8474. +EXPORT_SYMBOL(DWC_MUTEX_ALLOC);
  8475. +
  8476. +#if (!defined(DWC_LINUX) || !defined(CONFIG_DEBUG_MUTEXES))
  8477. +EXPORT_SYMBOL(DWC_MUTEX_FREE);
  8478. +#endif
  8479. +
  8480. +EXPORT_SYMBOL(DWC_MUTEX_LOCK);
  8481. +EXPORT_SYMBOL(DWC_MUTEX_TRYLOCK);
  8482. +EXPORT_SYMBOL(DWC_MUTEX_UNLOCK);
  8483. +EXPORT_SYMBOL(DWC_UDELAY);
  8484. +EXPORT_SYMBOL(DWC_MDELAY);
  8485. +EXPORT_SYMBOL(DWC_MSLEEP);
  8486. +EXPORT_SYMBOL(DWC_TIME);
  8487. +EXPORT_SYMBOL(DWC_TIMER_ALLOC);
  8488. +EXPORT_SYMBOL(DWC_TIMER_FREE);
  8489. +EXPORT_SYMBOL(DWC_TIMER_SCHEDULE);
  8490. +EXPORT_SYMBOL(DWC_TIMER_CANCEL);
  8491. +EXPORT_SYMBOL(DWC_WAITQ_ALLOC);
  8492. +EXPORT_SYMBOL(DWC_WAITQ_FREE);
  8493. +EXPORT_SYMBOL(DWC_WAITQ_WAIT);
  8494. +EXPORT_SYMBOL(DWC_WAITQ_WAIT_TIMEOUT);
  8495. +EXPORT_SYMBOL(DWC_WAITQ_TRIGGER);
  8496. +EXPORT_SYMBOL(DWC_WAITQ_ABORT);
  8497. +EXPORT_SYMBOL(DWC_THREAD_RUN);
  8498. +EXPORT_SYMBOL(DWC_THREAD_STOP);
  8499. +EXPORT_SYMBOL(DWC_THREAD_SHOULD_STOP);
  8500. +EXPORT_SYMBOL(DWC_TASK_ALLOC);
  8501. +EXPORT_SYMBOL(DWC_TASK_FREE);
  8502. +EXPORT_SYMBOL(DWC_TASK_SCHEDULE);
  8503. +EXPORT_SYMBOL(DWC_WORKQ_WAIT_WORK_DONE);
  8504. +EXPORT_SYMBOL(DWC_WORKQ_ALLOC);
  8505. +EXPORT_SYMBOL(DWC_WORKQ_FREE);
  8506. +EXPORT_SYMBOL(DWC_WORKQ_SCHEDULE);
  8507. +EXPORT_SYMBOL(DWC_WORKQ_SCHEDULE_DELAYED);
  8508. +EXPORT_SYMBOL(DWC_WORKQ_PENDING);
  8509. +
  8510. +static int dwc_common_port_init_module(void)
  8511. +{
  8512. + int result = 0;
  8513. +
  8514. + printk(KERN_DEBUG "Module dwc_common_port init\n" );
  8515. +
  8516. +#ifdef DWC_DEBUG_MEMORY
  8517. + result = dwc_memory_debug_start(NULL);
  8518. + if (result) {
  8519. + printk(KERN_ERR
  8520. + "dwc_memory_debug_start() failed with error %d\n",
  8521. + result);
  8522. + return result;
  8523. + }
  8524. +#endif
  8525. +
  8526. +#ifdef DWC_NOTIFYLIB
  8527. + result = dwc_alloc_notification_manager(NULL, NULL);
  8528. + if (result) {
  8529. + printk(KERN_ERR
  8530. + "dwc_alloc_notification_manager() failed with error %d\n",
  8531. + result);
  8532. + return result;
  8533. + }
  8534. +#endif
  8535. + return result;
  8536. +}
  8537. +
  8538. +static void dwc_common_port_exit_module(void)
  8539. +{
  8540. + printk(KERN_DEBUG "Module dwc_common_port exit\n" );
  8541. +
  8542. +#ifdef DWC_NOTIFYLIB
  8543. + dwc_free_notification_manager();
  8544. +#endif
  8545. +
  8546. +#ifdef DWC_DEBUG_MEMORY
  8547. + dwc_memory_debug_stop();
  8548. +#endif
  8549. +}
  8550. +
  8551. +module_init(dwc_common_port_init_module);
  8552. +module_exit(dwc_common_port_exit_module);
  8553. +
  8554. +MODULE_DESCRIPTION("DWC Common Library - Portable version");
  8555. +MODULE_AUTHOR("Synopsys Inc.");
  8556. +MODULE_LICENSE ("GPL");
  8557. +
  8558. +#endif /* DWC_LIBMODULE */
  8559. --- /dev/null
  8560. +++ b/drivers/usb/host/dwc_common_port/dwc_common_nbsd.c
  8561. @@ -0,0 +1,1275 @@
  8562. +#include "dwc_os.h"
  8563. +#include "dwc_list.h"
  8564. +
  8565. +#ifdef DWC_CCLIB
  8566. +# include "dwc_cc.h"
  8567. +#endif
  8568. +
  8569. +#ifdef DWC_CRYPTOLIB
  8570. +# include "dwc_modpow.h"
  8571. +# include "dwc_dh.h"
  8572. +# include "dwc_crypto.h"
  8573. +#endif
  8574. +
  8575. +#ifdef DWC_NOTIFYLIB
  8576. +# include "dwc_notifier.h"
  8577. +#endif
  8578. +
  8579. +/* OS-Level Implementations */
  8580. +
  8581. +/* This is the NetBSD 4.0.1 kernel implementation of the DWC platform library. */
  8582. +
  8583. +
  8584. +/* MISC */
  8585. +
  8586. +void *DWC_MEMSET(void *dest, uint8_t byte, uint32_t size)
  8587. +{
  8588. + return memset(dest, byte, size);
  8589. +}
  8590. +
  8591. +void *DWC_MEMCPY(void *dest, void const *src, uint32_t size)
  8592. +{
  8593. + return memcpy(dest, src, size);
  8594. +}
  8595. +
  8596. +void *DWC_MEMMOVE(void *dest, void *src, uint32_t size)
  8597. +{
  8598. + bcopy(src, dest, size);
  8599. + return dest;
  8600. +}
  8601. +
  8602. +int DWC_MEMCMP(void *m1, void *m2, uint32_t size)
  8603. +{
  8604. + return memcmp(m1, m2, size);
  8605. +}
  8606. +
  8607. +int DWC_STRNCMP(void *s1, void *s2, uint32_t size)
  8608. +{
  8609. + return strncmp(s1, s2, size);
  8610. +}
  8611. +
  8612. +int DWC_STRCMP(void *s1, void *s2)
  8613. +{
  8614. + return strcmp(s1, s2);
  8615. +}
  8616. +
  8617. +int DWC_STRLEN(char const *str)
  8618. +{
  8619. + return strlen(str);
  8620. +}
  8621. +
  8622. +char *DWC_STRCPY(char *to, char const *from)
  8623. +{
  8624. + return strcpy(to, from);
  8625. +}
  8626. +
  8627. +char *DWC_STRDUP(char const *str)
  8628. +{
  8629. + int len = DWC_STRLEN(str) + 1;
  8630. + char *new = DWC_ALLOC_ATOMIC(len);
  8631. +
  8632. + if (!new) {
  8633. + return NULL;
  8634. + }
  8635. +
  8636. + DWC_MEMCPY(new, str, len);
  8637. + return new;
  8638. +}
  8639. +
  8640. +int DWC_ATOI(char *str, int32_t *value)
  8641. +{
  8642. + char *end = NULL;
  8643. +
  8644. + /* NetBSD doesn't have 'strtol' in the kernel, but 'strtoul'
  8645. + * should be equivalent on 2's complement machines
  8646. + */
  8647. + *value = strtoul(str, &end, 0);
  8648. + if (*end == '\0') {
  8649. + return 0;
  8650. + }
  8651. +
  8652. + return -1;
  8653. +}
  8654. +
  8655. +int DWC_ATOUI(char *str, uint32_t *value)
  8656. +{
  8657. + char *end = NULL;
  8658. +
  8659. + *value = strtoul(str, &end, 0);
  8660. + if (*end == '\0') {
  8661. + return 0;
  8662. + }
  8663. +
  8664. + return -1;
  8665. +}
  8666. +
  8667. +
  8668. +#ifdef DWC_UTFLIB
  8669. +/* From usbstring.c */
  8670. +
  8671. +int DWC_UTF8_TO_UTF16LE(uint8_t const *s, uint16_t *cp, unsigned len)
  8672. +{
  8673. + int count = 0;
  8674. + u8 c;
  8675. + u16 uchar;
  8676. +
  8677. + /* this insists on correct encodings, though not minimal ones.
  8678. + * BUT it currently rejects legit 4-byte UTF-8 code points,
  8679. + * which need surrogate pairs. (Unicode 3.1 can use them.)
  8680. + */
  8681. + while (len != 0 && (c = (u8) *s++) != 0) {
  8682. + if (unlikely(c & 0x80)) {
  8683. + // 2-byte sequence:
  8684. + // 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx
  8685. + if ((c & 0xe0) == 0xc0) {
  8686. + uchar = (c & 0x1f) << 6;
  8687. +
  8688. + c = (u8) *s++;
  8689. + if ((c & 0xc0) != 0xc0)
  8690. + goto fail;
  8691. + c &= 0x3f;
  8692. + uchar |= c;
  8693. +
  8694. + // 3-byte sequence (most CJKV characters):
  8695. + // zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx
  8696. + } else if ((c & 0xf0) == 0xe0) {
  8697. + uchar = (c & 0x0f) << 12;
  8698. +
  8699. + c = (u8) *s++;
  8700. + if ((c & 0xc0) != 0xc0)
  8701. + goto fail;
  8702. + c &= 0x3f;
  8703. + uchar |= c << 6;
  8704. +
  8705. + c = (u8) *s++;
  8706. + if ((c & 0xc0) != 0xc0)
  8707. + goto fail;
  8708. + c &= 0x3f;
  8709. + uchar |= c;
  8710. +
  8711. + /* no bogus surrogates */
  8712. + if (0xd800 <= uchar && uchar <= 0xdfff)
  8713. + goto fail;
  8714. +
  8715. + // 4-byte sequence (surrogate pairs, currently rare):
  8716. + // 11101110wwwwzzzzyy + 110111yyyyxxxxxx
  8717. + // = 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx
  8718. + // (uuuuu = wwww + 1)
  8719. + // FIXME accept the surrogate code points (only)
  8720. + } else
  8721. + goto fail;
  8722. + } else
  8723. + uchar = c;
  8724. + put_unaligned (cpu_to_le16 (uchar), cp++);
  8725. + count++;
  8726. + len--;
  8727. + }
  8728. + return count;
  8729. +fail:
  8730. + return -1;
  8731. +}
  8732. +
  8733. +#endif /* DWC_UTFLIB */
  8734. +
  8735. +
  8736. +/* dwc_debug.h */
  8737. +
  8738. +dwc_bool_t DWC_IN_IRQ(void)
  8739. +{
  8740. +// return in_irq();
  8741. + return 0;
  8742. +}
  8743. +
  8744. +dwc_bool_t DWC_IN_BH(void)
  8745. +{
  8746. +// return in_softirq();
  8747. + return 0;
  8748. +}
  8749. +
  8750. +void DWC_VPRINTF(char *format, va_list args)
  8751. +{
  8752. + vprintf(format, args);
  8753. +}
  8754. +
  8755. +int DWC_VSNPRINTF(char *str, int size, char *format, va_list args)
  8756. +{
  8757. + return vsnprintf(str, size, format, args);
  8758. +}
  8759. +
  8760. +void DWC_PRINTF(char *format, ...)
  8761. +{
  8762. + va_list args;
  8763. +
  8764. + va_start(args, format);
  8765. + DWC_VPRINTF(format, args);
  8766. + va_end(args);
  8767. +}
  8768. +
  8769. +int DWC_SPRINTF(char *buffer, char *format, ...)
  8770. +{
  8771. + int retval;
  8772. + va_list args;
  8773. +
  8774. + va_start(args, format);
  8775. + retval = vsprintf(buffer, format, args);
  8776. + va_end(args);
  8777. + return retval;
  8778. +}
  8779. +
  8780. +int DWC_SNPRINTF(char *buffer, int size, char *format, ...)
  8781. +{
  8782. + int retval;
  8783. + va_list args;
  8784. +
  8785. + va_start(args, format);
  8786. + retval = vsnprintf(buffer, size, format, args);
  8787. + va_end(args);
  8788. + return retval;
  8789. +}
  8790. +
  8791. +void __DWC_WARN(char *format, ...)
  8792. +{
  8793. + va_list args;
  8794. +
  8795. + va_start(args, format);
  8796. + DWC_VPRINTF(format, args);
  8797. + va_end(args);
  8798. +}
  8799. +
  8800. +void __DWC_ERROR(char *format, ...)
  8801. +{
  8802. + va_list args;
  8803. +
  8804. + va_start(args, format);
  8805. + DWC_VPRINTF(format, args);
  8806. + va_end(args);
  8807. +}
  8808. +
  8809. +void DWC_EXCEPTION(char *format, ...)
  8810. +{
  8811. + va_list args;
  8812. +
  8813. + va_start(args, format);
  8814. + DWC_VPRINTF(format, args);
  8815. + va_end(args);
  8816. +// BUG_ON(1); ???
  8817. +}
  8818. +
  8819. +#ifdef DEBUG
  8820. +void __DWC_DEBUG(char *format, ...)
  8821. +{
  8822. + va_list args;
  8823. +
  8824. + va_start(args, format);
  8825. + DWC_VPRINTF(format, args);
  8826. + va_end(args);
  8827. +}
  8828. +#endif
  8829. +
  8830. +
  8831. +/* dwc_mem.h */
  8832. +
  8833. +#if 0
  8834. +dwc_pool_t *DWC_DMA_POOL_CREATE(uint32_t size,
  8835. + uint32_t align,
  8836. + uint32_t alloc)
  8837. +{
  8838. + struct dma_pool *pool = dma_pool_create("Pool", NULL,
  8839. + size, align, alloc);
  8840. + return (dwc_pool_t *)pool;
  8841. +}
  8842. +
  8843. +void DWC_DMA_POOL_DESTROY(dwc_pool_t *pool)
  8844. +{
  8845. + dma_pool_destroy((struct dma_pool *)pool);
  8846. +}
  8847. +
  8848. +void *DWC_DMA_POOL_ALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
  8849. +{
  8850. +// return dma_pool_alloc((struct dma_pool *)pool, GFP_KERNEL, dma_addr);
  8851. + return dma_pool_alloc((struct dma_pool *)pool, M_WAITOK, dma_addr);
  8852. +}
  8853. +
  8854. +void *DWC_DMA_POOL_ZALLOC(dwc_pool_t *pool, uint64_t *dma_addr)
  8855. +{
  8856. + void *vaddr = DWC_DMA_POOL_ALLOC(pool, dma_addr);
  8857. + memset(..);
  8858. +}
  8859. +
  8860. +void DWC_DMA_POOL_FREE(dwc_pool_t *pool, void *vaddr, void *daddr)
  8861. +{
  8862. + dma_pool_free(pool, vaddr, daddr);
  8863. +}
  8864. +#endif
  8865. +
  8866. +void *__DWC_DMA_ALLOC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr)
  8867. +{
  8868. + dwc_dmactx_t *dma = (dwc_dmactx_t *)dma_ctx;
  8869. + int error;
  8870. +
  8871. + error = bus_dmamem_alloc(dma->dma_tag, size, 1, size, dma->segs,
  8872. + sizeof(dma->segs) / sizeof(dma->segs[0]),
  8873. + &dma->nsegs, BUS_DMA_NOWAIT);
  8874. + if (error) {
  8875. + printf("%s: bus_dmamem_alloc(%ju) failed: %d\n", __func__,
  8876. + (uintmax_t)size, error);
  8877. + goto fail_0;
  8878. + }
  8879. +
  8880. + error = bus_dmamem_map(dma->dma_tag, dma->segs, dma->nsegs, size,
  8881. + (caddr_t *)&dma->dma_vaddr,
  8882. + BUS_DMA_NOWAIT | BUS_DMA_COHERENT);
  8883. + if (error) {
  8884. + printf("%s: bus_dmamem_map failed: %d\n", __func__, error);
  8885. + goto fail_1;
  8886. + }
  8887. +
  8888. + error = bus_dmamap_create(dma->dma_tag, size, 1, size, 0,
  8889. + BUS_DMA_NOWAIT, &dma->dma_map);
  8890. + if (error) {
  8891. + printf("%s: bus_dmamap_create failed: %d\n", __func__, error);
  8892. + goto fail_2;
  8893. + }
  8894. +
  8895. + error = bus_dmamap_load(dma->dma_tag, dma->dma_map, dma->dma_vaddr,
  8896. + size, NULL, BUS_DMA_NOWAIT);
  8897. + if (error) {
  8898. + printf("%s: bus_dmamap_load failed: %d\n", __func__, error);
  8899. + goto fail_3;
  8900. + }
  8901. +
  8902. + dma->dma_paddr = (bus_addr_t)dma->segs[0].ds_addr;
  8903. + *dma_addr = dma->dma_paddr;
  8904. + return dma->dma_vaddr;
  8905. +
  8906. +fail_3:
  8907. + bus_dmamap_destroy(dma->dma_tag, dma->dma_map);
  8908. +fail_2:
  8909. + bus_dmamem_unmap(dma->dma_tag, dma->dma_vaddr, size);
  8910. +fail_1:
  8911. + bus_dmamem_free(dma->dma_tag, dma->segs, dma->nsegs);
  8912. +fail_0:
  8913. + dma->dma_map = NULL;
  8914. + dma->dma_vaddr = NULL;
  8915. + dma->nsegs = 0;
  8916. +
  8917. + return NULL;
  8918. +}
  8919. +
  8920. +void __DWC_DMA_FREE(void *dma_ctx, uint32_t size, void *virt_addr, dwc_dma_t dma_addr)
  8921. +{
  8922. + dwc_dmactx_t *dma = (dwc_dmactx_t *)dma_ctx;
  8923. +
  8924. + if (dma->dma_map != NULL) {
  8925. + bus_dmamap_sync(dma->dma_tag, dma->dma_map, 0, size,
  8926. + BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
  8927. + bus_dmamap_unload(dma->dma_tag, dma->dma_map);
  8928. + bus_dmamap_destroy(dma->dma_tag, dma->dma_map);
  8929. + bus_dmamem_unmap(dma->dma_tag, dma->dma_vaddr, size);
  8930. + bus_dmamem_free(dma->dma_tag, dma->segs, dma->nsegs);
  8931. + dma->dma_paddr = 0;
  8932. + dma->dma_map = NULL;
  8933. + dma->dma_vaddr = NULL;
  8934. + dma->nsegs = 0;
  8935. + }
  8936. +}
  8937. +
  8938. +void *__DWC_ALLOC(void *mem_ctx, uint32_t size)
  8939. +{
  8940. + return malloc(size, M_DEVBUF, M_WAITOK | M_ZERO);
  8941. +}
  8942. +
  8943. +void *__DWC_ALLOC_ATOMIC(void *mem_ctx, uint32_t size)
  8944. +{
  8945. + return malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO);
  8946. +}
  8947. +
  8948. +void __DWC_FREE(void *mem_ctx, void *addr)
  8949. +{
  8950. + free(addr, M_DEVBUF);
  8951. +}
  8952. +
  8953. +
  8954. +#ifdef DWC_CRYPTOLIB
  8955. +/* dwc_crypto.h */
  8956. +
  8957. +void DWC_RANDOM_BYTES(uint8_t *buffer, uint32_t length)
  8958. +{
  8959. + get_random_bytes(buffer, length);
  8960. +}
  8961. +
  8962. +int DWC_AES_CBC(uint8_t *message, uint32_t messagelen, uint8_t *key, uint32_t keylen, uint8_t iv[16], uint8_t *out)
  8963. +{
  8964. + struct crypto_blkcipher *tfm;
  8965. + struct blkcipher_desc desc;
  8966. + struct scatterlist sgd;
  8967. + struct scatterlist sgs;
  8968. +
  8969. + tfm = crypto_alloc_blkcipher("cbc(aes)", 0, CRYPTO_ALG_ASYNC);
  8970. + if (tfm == NULL) {
  8971. + printk("failed to load transform for aes CBC\n");
  8972. + return -1;
  8973. + }
  8974. +
  8975. + crypto_blkcipher_setkey(tfm, key, keylen);
  8976. + crypto_blkcipher_set_iv(tfm, iv, 16);
  8977. +
  8978. + sg_init_one(&sgd, out, messagelen);
  8979. + sg_init_one(&sgs, message, messagelen);
  8980. +
  8981. + desc.tfm = tfm;
  8982. + desc.flags = 0;
  8983. +
  8984. + if (crypto_blkcipher_encrypt(&desc, &sgd, &sgs, messagelen)) {
  8985. + crypto_free_blkcipher(tfm);
  8986. + DWC_ERROR("AES CBC encryption failed");
  8987. + return -1;
  8988. + }
  8989. +
  8990. + crypto_free_blkcipher(tfm);
  8991. + return 0;
  8992. +}
  8993. +
  8994. +int DWC_SHA256(uint8_t *message, uint32_t len, uint8_t *out)
  8995. +{
  8996. + struct crypto_hash *tfm;
  8997. + struct hash_desc desc;
  8998. + struct scatterlist sg;
  8999. +
  9000. + tfm = crypto_alloc_hash("sha256", 0, CRYPTO_ALG_ASYNC);
  9001. + if (IS_ERR(tfm)) {
  9002. + DWC_ERROR("Failed to load transform for sha256: %ld", PTR_ERR(tfm));
  9003. + return 0;
  9004. + }
  9005. + desc.tfm = tfm;
  9006. + desc.flags = 0;
  9007. +
  9008. + sg_init_one(&sg, message, len);
  9009. + crypto_hash_digest(&desc, &sg, len, out);
  9010. + crypto_free_hash(tfm);
  9011. +
  9012. + return 1;
  9013. +}
  9014. +
  9015. +int DWC_HMAC_SHA256(uint8_t *message, uint32_t messagelen,
  9016. + uint8_t *key, uint32_t keylen, uint8_t *out)
  9017. +{
  9018. + struct crypto_hash *tfm;
  9019. + struct hash_desc desc;
  9020. + struct scatterlist sg;
  9021. +
  9022. + tfm = crypto_alloc_hash("hmac(sha256)", 0, CRYPTO_ALG_ASYNC);
  9023. + if (IS_ERR(tfm)) {
  9024. + DWC_ERROR("Failed to load transform for hmac(sha256): %ld", PTR_ERR(tfm));
  9025. + return 0;
  9026. + }
  9027. + desc.tfm = tfm;
  9028. + desc.flags = 0;
  9029. +
  9030. + sg_init_one(&sg, message, messagelen);
  9031. + crypto_hash_setkey(tfm, key, keylen);
  9032. + crypto_hash_digest(&desc, &sg, messagelen, out);
  9033. + crypto_free_hash(tfm);
  9034. +
  9035. + return 1;
  9036. +}
  9037. +
  9038. +#endif /* DWC_CRYPTOLIB */
  9039. +
  9040. +
  9041. +/* Byte Ordering Conversions */
  9042. +
  9043. +uint32_t DWC_CPU_TO_LE32(uint32_t *p)
  9044. +{
  9045. +#ifdef __LITTLE_ENDIAN
  9046. + return *p;
  9047. +#else
  9048. + uint8_t *u_p = (uint8_t *)p;
  9049. +
  9050. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  9051. +#endif
  9052. +}
  9053. +
  9054. +uint32_t DWC_CPU_TO_BE32(uint32_t *p)
  9055. +{
  9056. +#ifdef __BIG_ENDIAN
  9057. + return *p;
  9058. +#else
  9059. + uint8_t *u_p = (uint8_t *)p;
  9060. +
  9061. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  9062. +#endif
  9063. +}
  9064. +
  9065. +uint32_t DWC_LE32_TO_CPU(uint32_t *p)
  9066. +{
  9067. +#ifdef __LITTLE_ENDIAN
  9068. + return *p;
  9069. +#else
  9070. + uint8_t *u_p = (uint8_t *)p;
  9071. +
  9072. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  9073. +#endif
  9074. +}
  9075. +
  9076. +uint32_t DWC_BE32_TO_CPU(uint32_t *p)
  9077. +{
  9078. +#ifdef __BIG_ENDIAN
  9079. + return *p;
  9080. +#else
  9081. + uint8_t *u_p = (uint8_t *)p;
  9082. +
  9083. + return (u_p[3] | (u_p[2] << 8) | (u_p[1] << 16) | (u_p[0] << 24));
  9084. +#endif
  9085. +}
  9086. +
  9087. +uint16_t DWC_CPU_TO_LE16(uint16_t *p)
  9088. +{
  9089. +#ifdef __LITTLE_ENDIAN
  9090. + return *p;
  9091. +#else
  9092. + uint8_t *u_p = (uint8_t *)p;
  9093. + return (u_p[1] | (u_p[0] << 8));
  9094. +#endif
  9095. +}
  9096. +
  9097. +uint16_t DWC_CPU_TO_BE16(uint16_t *p)
  9098. +{
  9099. +#ifdef __BIG_ENDIAN
  9100. + return *p;
  9101. +#else
  9102. + uint8_t *u_p = (uint8_t *)p;
  9103. + return (u_p[1] | (u_p[0] << 8));
  9104. +#endif
  9105. +}
  9106. +
  9107. +uint16_t DWC_LE16_TO_CPU(uint16_t *p)
  9108. +{
  9109. +#ifdef __LITTLE_ENDIAN
  9110. + return *p;
  9111. +#else
  9112. + uint8_t *u_p = (uint8_t *)p;
  9113. + return (u_p[1] | (u_p[0] << 8));
  9114. +#endif
  9115. +}
  9116. +
  9117. +uint16_t DWC_BE16_TO_CPU(uint16_t *p)
  9118. +{
  9119. +#ifdef __BIG_ENDIAN
  9120. + return *p;
  9121. +#else
  9122. + uint8_t *u_p = (uint8_t *)p;
  9123. + return (u_p[1] | (u_p[0] << 8));
  9124. +#endif
  9125. +}
  9126. +
  9127. +
  9128. +/* Registers */
  9129. +
  9130. +uint32_t DWC_READ_REG32(void *io_ctx, uint32_t volatile *reg)
  9131. +{
  9132. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  9133. + bus_size_t ior = (bus_size_t)reg;
  9134. +
  9135. + return bus_space_read_4(io->iot, io->ioh, ior);
  9136. +}
  9137. +
  9138. +#if 0
  9139. +uint64_t DWC_READ_REG64(void *io_ctx, uint64_t volatile *reg)
  9140. +{
  9141. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  9142. + bus_size_t ior = (bus_size_t)reg;
  9143. +
  9144. + return bus_space_read_8(io->iot, io->ioh, ior);
  9145. +}
  9146. +#endif
  9147. +
  9148. +void DWC_WRITE_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t value)
  9149. +{
  9150. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  9151. + bus_size_t ior = (bus_size_t)reg;
  9152. +
  9153. + bus_space_write_4(io->iot, io->ioh, ior, value);
  9154. +}
  9155. +
  9156. +#if 0
  9157. +void DWC_WRITE_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t value)
  9158. +{
  9159. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  9160. + bus_size_t ior = (bus_size_t)reg;
  9161. +
  9162. + bus_space_write_8(io->iot, io->ioh, ior, value);
  9163. +}
  9164. +#endif
  9165. +
  9166. +void DWC_MODIFY_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t clear_mask,
  9167. + uint32_t set_mask)
  9168. +{
  9169. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  9170. + bus_size_t ior = (bus_size_t)reg;
  9171. +
  9172. + bus_space_write_4(io->iot, io->ioh, ior,
  9173. + (bus_space_read_4(io->iot, io->ioh, ior) &
  9174. + ~clear_mask) | set_mask);
  9175. +}
  9176. +
  9177. +#if 0
  9178. +void DWC_MODIFY_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t clear_mask,
  9179. + uint64_t set_mask)
  9180. +{
  9181. + dwc_ioctx_t *io = (dwc_ioctx_t *)io_ctx;
  9182. + bus_size_t ior = (bus_size_t)reg;
  9183. +
  9184. + bus_space_write_8(io->iot, io->ioh, ior,
  9185. + (bus_space_read_8(io->iot, io->ioh, ior) &
  9186. + ~clear_mask) | set_mask);
  9187. +}
  9188. +#endif
  9189. +
  9190. +
  9191. +/* Locking */
  9192. +
  9193. +dwc_spinlock_t *DWC_SPINLOCK_ALLOC(void)
  9194. +{
  9195. + struct simplelock *sl = DWC_ALLOC(sizeof(*sl));
  9196. +
  9197. + if (!sl) {
  9198. + DWC_ERROR("Cannot allocate memory for spinlock");
  9199. + return NULL;
  9200. + }
  9201. +
  9202. + simple_lock_init(sl);
  9203. + return (dwc_spinlock_t *)sl;
  9204. +}
  9205. +
  9206. +void DWC_SPINLOCK_FREE(dwc_spinlock_t *lock)
  9207. +{
  9208. + struct simplelock *sl = (struct simplelock *)lock;
  9209. +
  9210. + DWC_FREE(sl);
  9211. +}
  9212. +
  9213. +void DWC_SPINLOCK(dwc_spinlock_t *lock)
  9214. +{
  9215. + simple_lock((struct simplelock *)lock);
  9216. +}
  9217. +
  9218. +void DWC_SPINUNLOCK(dwc_spinlock_t *lock)
  9219. +{
  9220. + simple_unlock((struct simplelock *)lock);
  9221. +}
  9222. +
  9223. +void DWC_SPINLOCK_IRQSAVE(dwc_spinlock_t *lock, dwc_irqflags_t *flags)
  9224. +{
  9225. + simple_lock((struct simplelock *)lock);
  9226. + *flags = splbio();
  9227. +}
  9228. +
  9229. +void DWC_SPINUNLOCK_IRQRESTORE(dwc_spinlock_t *lock, dwc_irqflags_t flags)
  9230. +{
  9231. + splx(flags);
  9232. + simple_unlock((struct simplelock *)lock);
  9233. +}
  9234. +
  9235. +dwc_mutex_t *DWC_MUTEX_ALLOC(void)
  9236. +{
  9237. + dwc_mutex_t *mutex = DWC_ALLOC(sizeof(struct lock));
  9238. +
  9239. + if (!mutex) {
  9240. + DWC_ERROR("Cannot allocate memory for mutex");
  9241. + return NULL;
  9242. + }
  9243. +
  9244. + lockinit((struct lock *)mutex, 0, "dw3mtx", 0, 0);
  9245. + return mutex;
  9246. +}
  9247. +
  9248. +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
  9249. +#else
  9250. +void DWC_MUTEX_FREE(dwc_mutex_t *mutex)
  9251. +{
  9252. + DWC_FREE(mutex);
  9253. +}
  9254. +#endif
  9255. +
  9256. +void DWC_MUTEX_LOCK(dwc_mutex_t *mutex)
  9257. +{
  9258. + lockmgr((struct lock *)mutex, LK_EXCLUSIVE, NULL);
  9259. +}
  9260. +
  9261. +int DWC_MUTEX_TRYLOCK(dwc_mutex_t *mutex)
  9262. +{
  9263. + int status;
  9264. +
  9265. + status = lockmgr((struct lock *)mutex, LK_EXCLUSIVE | LK_NOWAIT, NULL);
  9266. + return status == 0;
  9267. +}
  9268. +
  9269. +void DWC_MUTEX_UNLOCK(dwc_mutex_t *mutex)
  9270. +{
  9271. + lockmgr((struct lock *)mutex, LK_RELEASE, NULL);
  9272. +}
  9273. +
  9274. +
  9275. +/* Timing */
  9276. +
  9277. +void DWC_UDELAY(uint32_t usecs)
  9278. +{
  9279. + DELAY(usecs);
  9280. +}
  9281. +
  9282. +void DWC_MDELAY(uint32_t msecs)
  9283. +{
  9284. + do {
  9285. + DELAY(1000);
  9286. + } while (--msecs);
  9287. +}
  9288. +
  9289. +void DWC_MSLEEP(uint32_t msecs)
  9290. +{
  9291. + struct timeval tv;
  9292. +
  9293. + tv.tv_sec = msecs / 1000;
  9294. + tv.tv_usec = (msecs - tv.tv_sec * 1000) * 1000;
  9295. + tsleep(&tv, 0, "dw3slp", tvtohz(&tv));
  9296. +}
  9297. +
  9298. +uint32_t DWC_TIME(void)
  9299. +{
  9300. + struct timeval tv;
  9301. +
  9302. + microuptime(&tv); // or getmicrouptime? (less precise, but faster)
  9303. + return tv.tv_sec * 1000 + tv.tv_usec / 1000;
  9304. +}
  9305. +
  9306. +
  9307. +/* Timers */
  9308. +
  9309. +struct dwc_timer {
  9310. + struct callout t;
  9311. + char *name;
  9312. + dwc_spinlock_t *lock;
  9313. + dwc_timer_callback_t cb;
  9314. + void *data;
  9315. +};
  9316. +
  9317. +dwc_timer_t *DWC_TIMER_ALLOC(char *name, dwc_timer_callback_t cb, void *data)
  9318. +{
  9319. + dwc_timer_t *t = DWC_ALLOC(sizeof(*t));
  9320. +
  9321. + if (!t) {
  9322. + DWC_ERROR("Cannot allocate memory for timer");
  9323. + return NULL;
  9324. + }
  9325. +
  9326. + callout_init(&t->t);
  9327. +
  9328. + t->name = DWC_STRDUP(name);
  9329. + if (!t->name) {
  9330. + DWC_ERROR("Cannot allocate memory for timer->name");
  9331. + goto no_name;
  9332. + }
  9333. +
  9334. + t->lock = DWC_SPINLOCK_ALLOC();
  9335. + if (!t->lock) {
  9336. + DWC_ERROR("Cannot allocate memory for timer->lock");
  9337. + goto no_lock;
  9338. + }
  9339. +
  9340. + t->cb = cb;
  9341. + t->data = data;
  9342. +
  9343. + return t;
  9344. +
  9345. + no_lock:
  9346. + DWC_FREE(t->name);
  9347. + no_name:
  9348. + DWC_FREE(t);
  9349. +
  9350. + return NULL;
  9351. +}
  9352. +
  9353. +void DWC_TIMER_FREE(dwc_timer_t *timer)
  9354. +{
  9355. + callout_stop(&timer->t);
  9356. + DWC_SPINLOCK_FREE(timer->lock);
  9357. + DWC_FREE(timer->name);
  9358. + DWC_FREE(timer);
  9359. +}
  9360. +
  9361. +void DWC_TIMER_SCHEDULE(dwc_timer_t *timer, uint32_t time)
  9362. +{
  9363. + struct timeval tv;
  9364. +
  9365. + tv.tv_sec = time / 1000;
  9366. + tv.tv_usec = (time - tv.tv_sec * 1000) * 1000;
  9367. + callout_reset(&timer->t, tvtohz(&tv), timer->cb, timer->data);
  9368. +}
  9369. +
  9370. +void DWC_TIMER_CANCEL(dwc_timer_t *timer)
  9371. +{
  9372. + callout_stop(&timer->t);
  9373. +}
  9374. +
  9375. +
  9376. +/* Wait Queues */
  9377. +
  9378. +struct dwc_waitq {
  9379. + struct simplelock lock;
  9380. + int abort;
  9381. +};
  9382. +
  9383. +dwc_waitq_t *DWC_WAITQ_ALLOC(void)
  9384. +{
  9385. + dwc_waitq_t *wq = DWC_ALLOC(sizeof(*wq));
  9386. +
  9387. + if (!wq) {
  9388. + DWC_ERROR("Cannot allocate memory for waitqueue");
  9389. + return NULL;
  9390. + }
  9391. +
  9392. + simple_lock_init(&wq->lock);
  9393. + wq->abort = 0;
  9394. +
  9395. + return wq;
  9396. +}
  9397. +
  9398. +void DWC_WAITQ_FREE(dwc_waitq_t *wq)
  9399. +{
  9400. + DWC_FREE(wq);
  9401. +}
  9402. +
  9403. +int32_t DWC_WAITQ_WAIT(dwc_waitq_t *wq, dwc_waitq_condition_t cond, void *data)
  9404. +{
  9405. + int ipl;
  9406. + int result = 0;
  9407. +
  9408. + simple_lock(&wq->lock);
  9409. + ipl = splbio();
  9410. +
  9411. + /* Skip the sleep if already aborted or triggered */
  9412. + if (!wq->abort && !cond(data)) {
  9413. + splx(ipl);
  9414. + result = ltsleep(wq, PCATCH, "dw3wat", 0, &wq->lock); // infinite timeout
  9415. + ipl = splbio();
  9416. + }
  9417. +
  9418. + if (result == 0) { // awoken
  9419. + if (wq->abort) {
  9420. + wq->abort = 0;
  9421. + result = -DWC_E_ABORT;
  9422. + } else {
  9423. + result = 0;
  9424. + }
  9425. +
  9426. + splx(ipl);
  9427. + simple_unlock(&wq->lock);
  9428. + } else {
  9429. + wq->abort = 0;
  9430. + splx(ipl);
  9431. + simple_unlock(&wq->lock);
  9432. +
  9433. + if (result == ERESTART) { // signaled - restart
  9434. + result = -DWC_E_RESTART;
  9435. + } else { // signaled - must be EINTR
  9436. + result = -DWC_E_ABORT;
  9437. + }
  9438. + }
  9439. +
  9440. + return result;
  9441. +}
  9442. +
  9443. +int32_t DWC_WAITQ_WAIT_TIMEOUT(dwc_waitq_t *wq, dwc_waitq_condition_t cond,
  9444. + void *data, int32_t msecs)
  9445. +{
  9446. + struct timeval tv, tv1, tv2;
  9447. + int ipl;
  9448. + int result = 0;
  9449. +
  9450. + tv.tv_sec = msecs / 1000;
  9451. + tv.tv_usec = (msecs - tv.tv_sec * 1000) * 1000;
  9452. +
  9453. + simple_lock(&wq->lock);
  9454. + ipl = splbio();
  9455. +
  9456. + /* Skip the sleep if already aborted or triggered */
  9457. + if (!wq->abort && !cond(data)) {
  9458. + splx(ipl);
  9459. + getmicrouptime(&tv1);
  9460. + result = ltsleep(wq, PCATCH, "dw3wto", tvtohz(&tv), &wq->lock);
  9461. + getmicrouptime(&tv2);
  9462. + ipl = splbio();
  9463. + }
  9464. +
  9465. + if (result == 0) { // awoken
  9466. + if (wq->abort) {
  9467. + wq->abort = 0;
  9468. + splx(ipl);
  9469. + simple_unlock(&wq->lock);
  9470. + result = -DWC_E_ABORT;
  9471. + } else {
  9472. + splx(ipl);
  9473. + simple_unlock(&wq->lock);
  9474. +
  9475. + tv2.tv_usec -= tv1.tv_usec;
  9476. + if (tv2.tv_usec < 0) {
  9477. + tv2.tv_usec += 1000000;
  9478. + tv2.tv_sec--;
  9479. + }
  9480. +
  9481. + tv2.tv_sec -= tv1.tv_sec;
  9482. + result = tv2.tv_sec * 1000 + tv2.tv_usec / 1000;
  9483. + result = msecs - result;
  9484. + if (result <= 0)
  9485. + result = 1;
  9486. + }
  9487. + } else {
  9488. + wq->abort = 0;
  9489. + splx(ipl);
  9490. + simple_unlock(&wq->lock);
  9491. +
  9492. + if (result == ERESTART) { // signaled - restart
  9493. + result = -DWC_E_RESTART;
  9494. +
  9495. + } else if (result == EINTR) { // signaled - interrupt
  9496. + result = -DWC_E_ABORT;
  9497. +
  9498. + } else { // timed out
  9499. + result = -DWC_E_TIMEOUT;
  9500. + }
  9501. + }
  9502. +
  9503. + return result;
  9504. +}
  9505. +
  9506. +void DWC_WAITQ_TRIGGER(dwc_waitq_t *wq)
  9507. +{
  9508. + wakeup(wq);
  9509. +}
  9510. +
  9511. +void DWC_WAITQ_ABORT(dwc_waitq_t *wq)
  9512. +{
  9513. + int ipl;
  9514. +
  9515. + simple_lock(&wq->lock);
  9516. + ipl = splbio();
  9517. + wq->abort = 1;
  9518. + wakeup(wq);
  9519. + splx(ipl);
  9520. + simple_unlock(&wq->lock);
  9521. +}
  9522. +
  9523. +
  9524. +/* Threading */
  9525. +
  9526. +struct dwc_thread {
  9527. + struct proc *proc;
  9528. + int abort;
  9529. +};
  9530. +
  9531. +dwc_thread_t *DWC_THREAD_RUN(dwc_thread_function_t func, char *name, void *data)
  9532. +{
  9533. + int retval;
  9534. + dwc_thread_t *thread = DWC_ALLOC(sizeof(*thread));
  9535. +
  9536. + if (!thread) {
  9537. + return NULL;
  9538. + }
  9539. +
  9540. + thread->abort = 0;
  9541. + retval = kthread_create1((void (*)(void *))func, data, &thread->proc,
  9542. + "%s", name);
  9543. + if (retval) {
  9544. + DWC_FREE(thread);
  9545. + return NULL;
  9546. + }
  9547. +
  9548. + return thread;
  9549. +}
  9550. +
  9551. +int DWC_THREAD_STOP(dwc_thread_t *thread)
  9552. +{
  9553. + int retval;
  9554. +
  9555. + thread->abort = 1;
  9556. + retval = tsleep(&thread->abort, 0, "dw3stp", 60 * hz);
  9557. +
  9558. + if (retval == 0) {
  9559. + /* DWC_THREAD_EXIT() will free the thread struct */
  9560. + return 0;
  9561. + }
  9562. +
  9563. + /* NOTE: We leak the thread struct if thread doesn't die */
  9564. +
  9565. + if (retval == EWOULDBLOCK) {
  9566. + return -DWC_E_TIMEOUT;
  9567. + }
  9568. +
  9569. + return -DWC_E_UNKNOWN;
  9570. +}
  9571. +
  9572. +dwc_bool_t DWC_THREAD_SHOULD_STOP(dwc_thread_t *thread)
  9573. +{
  9574. + return thread->abort;
  9575. +}
  9576. +
  9577. +void DWC_THREAD_EXIT(dwc_thread_t *thread)
  9578. +{
  9579. + wakeup(&thread->abort);
  9580. + DWC_FREE(thread);
  9581. + kthread_exit(0);
  9582. +}
  9583. +
  9584. +/* tasklets
  9585. + - Runs in interrupt context (cannot sleep)
  9586. + - Each tasklet runs on a single CPU
  9587. + - Different tasklets can be running simultaneously on different CPUs
  9588. + [ On NetBSD there is no corresponding mechanism, drivers don't have bottom-
  9589. + halves. So we just call the callback directly from DWC_TASK_SCHEDULE() ]
  9590. + */
  9591. +struct dwc_tasklet {
  9592. + dwc_tasklet_callback_t cb;
  9593. + void *data;
  9594. +};
  9595. +
  9596. +static void tasklet_callback(void *data)
  9597. +{
  9598. + dwc_tasklet_t *task = (dwc_tasklet_t *)data;
  9599. +
  9600. + task->cb(task->data);
  9601. +}
  9602. +
  9603. +dwc_tasklet_t *DWC_TASK_ALLOC(char *name, dwc_tasklet_callback_t cb, void *data)
  9604. +{
  9605. + dwc_tasklet_t *task = DWC_ALLOC(sizeof(*task));
  9606. +
  9607. + if (task) {
  9608. + task->cb = cb;
  9609. + task->data = data;
  9610. + } else {
  9611. + DWC_ERROR("Cannot allocate memory for tasklet");
  9612. + }
  9613. +
  9614. + return task;
  9615. +}
  9616. +
  9617. +void DWC_TASK_FREE(dwc_tasklet_t *task)
  9618. +{
  9619. + DWC_FREE(task);
  9620. +}
  9621. +
  9622. +void DWC_TASK_SCHEDULE(dwc_tasklet_t *task)
  9623. +{
  9624. + tasklet_callback(task);
  9625. +}
  9626. +
  9627. +
  9628. +/* workqueues
  9629. + - Runs in process context (can sleep)
  9630. + */
  9631. +typedef struct work_container {
  9632. + dwc_work_callback_t cb;
  9633. + void *data;
  9634. + dwc_workq_t *wq;
  9635. + char *name;
  9636. + int hz;
  9637. + struct work task;
  9638. +} work_container_t;
  9639. +
  9640. +struct dwc_workq {
  9641. + struct workqueue *taskq;
  9642. + dwc_spinlock_t *lock;
  9643. + dwc_waitq_t *waitq;
  9644. + int pending;
  9645. + struct work_container *container;
  9646. +};
  9647. +
  9648. +static void do_work(struct work *task, void *data)
  9649. +{
  9650. + dwc_workq_t *wq = (dwc_workq_t *)data;
  9651. + work_container_t *container = wq->container;
  9652. + dwc_irqflags_t flags;
  9653. +
  9654. + if (container->hz) {
  9655. + tsleep(container, 0, "dw3wrk", container->hz);
  9656. + }
  9657. +
  9658. + container->cb(container->data);
  9659. + DWC_DEBUG("Work done: %s, container=%p", container->name, container);
  9660. +
  9661. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  9662. + if (container->name)
  9663. + DWC_FREE(container->name);
  9664. + DWC_FREE(container);
  9665. + wq->pending--;
  9666. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  9667. + DWC_WAITQ_TRIGGER(wq->waitq);
  9668. +}
  9669. +
  9670. +static int work_done(void *data)
  9671. +{
  9672. + dwc_workq_t *workq = (dwc_workq_t *)data;
  9673. +
  9674. + return workq->pending == 0;
  9675. +}
  9676. +
  9677. +int DWC_WORKQ_WAIT_WORK_DONE(dwc_workq_t *workq, int timeout)
  9678. +{
  9679. + return DWC_WAITQ_WAIT_TIMEOUT(workq->waitq, work_done, workq, timeout);
  9680. +}
  9681. +
  9682. +dwc_workq_t *DWC_WORKQ_ALLOC(char *name)
  9683. +{
  9684. + int result;
  9685. + dwc_workq_t *wq = DWC_ALLOC(sizeof(*wq));
  9686. +
  9687. + if (!wq) {
  9688. + DWC_ERROR("Cannot allocate memory for workqueue");
  9689. + return NULL;
  9690. + }
  9691. +
  9692. + result = workqueue_create(&wq->taskq, name, do_work, wq, 0 /*PWAIT*/,
  9693. + IPL_BIO, 0);
  9694. + if (result) {
  9695. + DWC_ERROR("Cannot create workqueue");
  9696. + goto no_taskq;
  9697. + }
  9698. +
  9699. + wq->pending = 0;
  9700. +
  9701. + wq->lock = DWC_SPINLOCK_ALLOC();
  9702. + if (!wq->lock) {
  9703. + DWC_ERROR("Cannot allocate memory for spinlock");
  9704. + goto no_lock;
  9705. + }
  9706. +
  9707. + wq->waitq = DWC_WAITQ_ALLOC();
  9708. + if (!wq->waitq) {
  9709. + DWC_ERROR("Cannot allocate memory for waitqueue");
  9710. + goto no_waitq;
  9711. + }
  9712. +
  9713. + return wq;
  9714. +
  9715. + no_waitq:
  9716. + DWC_SPINLOCK_FREE(wq->lock);
  9717. + no_lock:
  9718. + workqueue_destroy(wq->taskq);
  9719. + no_taskq:
  9720. + DWC_FREE(wq);
  9721. +
  9722. + return NULL;
  9723. +}
  9724. +
  9725. +void DWC_WORKQ_FREE(dwc_workq_t *wq)
  9726. +{
  9727. +#ifdef DEBUG
  9728. + dwc_irqflags_t flags;
  9729. +
  9730. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  9731. +
  9732. + if (wq->pending != 0) {
  9733. + struct work_container *container = wq->container;
  9734. +
  9735. + DWC_ERROR("Destroying work queue with pending work");
  9736. +
  9737. + if (container && container->name) {
  9738. + DWC_ERROR("Work %s still pending", container->name);
  9739. + }
  9740. + }
  9741. +
  9742. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  9743. +#endif
  9744. + DWC_WAITQ_FREE(wq->waitq);
  9745. + DWC_SPINLOCK_FREE(wq->lock);
  9746. + workqueue_destroy(wq->taskq);
  9747. + DWC_FREE(wq);
  9748. +}
  9749. +
  9750. +void DWC_WORKQ_SCHEDULE(dwc_workq_t *wq, dwc_work_callback_t cb, void *data,
  9751. + char *format, ...)
  9752. +{
  9753. + dwc_irqflags_t flags;
  9754. + work_container_t *container;
  9755. + static char name[128];
  9756. + va_list args;
  9757. +
  9758. + va_start(args, format);
  9759. + DWC_VSNPRINTF(name, 128, format, args);
  9760. + va_end(args);
  9761. +
  9762. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  9763. + wq->pending++;
  9764. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  9765. + DWC_WAITQ_TRIGGER(wq->waitq);
  9766. +
  9767. + container = DWC_ALLOC_ATOMIC(sizeof(*container));
  9768. + if (!container) {
  9769. + DWC_ERROR("Cannot allocate memory for container");
  9770. + return;
  9771. + }
  9772. +
  9773. + container->name = DWC_STRDUP(name);
  9774. + if (!container->name) {
  9775. + DWC_ERROR("Cannot allocate memory for container->name");
  9776. + DWC_FREE(container);
  9777. + return;
  9778. + }
  9779. +
  9780. + container->cb = cb;
  9781. + container->data = data;
  9782. + container->wq = wq;
  9783. + container->hz = 0;
  9784. + wq->container = container;
  9785. +
  9786. + DWC_DEBUG("Queueing work: %s, container=%p", container->name, container);
  9787. + workqueue_enqueue(wq->taskq, &container->task);
  9788. +}
  9789. +
  9790. +void DWC_WORKQ_SCHEDULE_DELAYED(dwc_workq_t *wq, dwc_work_callback_t cb,
  9791. + void *data, uint32_t time, char *format, ...)
  9792. +{
  9793. + dwc_irqflags_t flags;
  9794. + work_container_t *container;
  9795. + static char name[128];
  9796. + struct timeval tv;
  9797. + va_list args;
  9798. +
  9799. + va_start(args, format);
  9800. + DWC_VSNPRINTF(name, 128, format, args);
  9801. + va_end(args);
  9802. +
  9803. + DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
  9804. + wq->pending++;
  9805. + DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
  9806. + DWC_WAITQ_TRIGGER(wq->waitq);
  9807. +
  9808. + container = DWC_ALLOC_ATOMIC(sizeof(*container));
  9809. + if (!container) {
  9810. + DWC_ERROR("Cannot allocate memory for container");
  9811. + return;
  9812. + }
  9813. +
  9814. + container->name = DWC_STRDUP(name);
  9815. + if (!container->name) {
  9816. + DWC_ERROR("Cannot allocate memory for container->name");
  9817. + DWC_FREE(container);
  9818. + return;
  9819. + }
  9820. +
  9821. + container->cb = cb;
  9822. + container->data = data;
  9823. + container->wq = wq;
  9824. + tv.tv_sec = time / 1000;
  9825. + tv.tv_usec = (time - tv.tv_sec * 1000) * 1000;
  9826. + container->hz = tvtohz(&tv);
  9827. + wq->container = container;
  9828. +
  9829. + DWC_DEBUG("Queueing work: %s, container=%p", container->name, container);
  9830. + workqueue_enqueue(wq->taskq, &container->task);
  9831. +}
  9832. +
  9833. +int DWC_WORKQ_PENDING(dwc_workq_t *wq)
  9834. +{
  9835. + return wq->pending;
  9836. +}
  9837. --- /dev/null
  9838. +++ b/drivers/usb/host/dwc_common_port/dwc_crypto.c
  9839. @@ -0,0 +1,308 @@
  9840. +/* =========================================================================
  9841. + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_crypto.c $
  9842. + * $Revision: #5 $
  9843. + * $Date: 2010/09/28 $
  9844. + * $Change: 1596182 $
  9845. + *
  9846. + * Synopsys Portability Library Software and documentation
  9847. + * (hereinafter, "Software") is an Unsupported proprietary work of
  9848. + * Synopsys, Inc. unless otherwise expressly agreed to in writing
  9849. + * between Synopsys and you.
  9850. + *
  9851. + * The Software IS NOT an item of Licensed Software or Licensed Product
  9852. + * under any End User Software License Agreement or Agreement for
  9853. + * Licensed Product with Synopsys or any supplement thereto. You are
  9854. + * permitted to use and redistribute this Software in source and binary
  9855. + * forms, with or without modification, provided that redistributions
  9856. + * of source code must retain this notice. You may not view, use,
  9857. + * disclose, copy or distribute this file or any information contained
  9858. + * herein except pursuant to this license grant from Synopsys. If you
  9859. + * do not agree with this notice, including the disclaimer below, then
  9860. + * you are not authorized to use the Software.
  9861. + *
  9862. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  9863. + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  9864. + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  9865. + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
  9866. + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  9867. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  9868. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  9869. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  9870. + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  9871. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  9872. + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  9873. + * DAMAGE.
  9874. + * ========================================================================= */
  9875. +
  9876. +/** @file
  9877. + * This file contains the WUSB cryptographic routines.
  9878. + */
  9879. +
  9880. +#ifdef DWC_CRYPTOLIB
  9881. +
  9882. +#include "dwc_crypto.h"
  9883. +#include "usb.h"
  9884. +
  9885. +#ifdef DEBUG
  9886. +static inline void dump_bytes(char *name, uint8_t *bytes, int len)
  9887. +{
  9888. + int i;
  9889. + DWC_PRINTF("%s: ", name);
  9890. + for (i=0; i<len; i++) {
  9891. + DWC_PRINTF("%02x ", bytes[i]);
  9892. + }
  9893. + DWC_PRINTF("\n");
  9894. +}
  9895. +#else
  9896. +#define dump_bytes(x...)
  9897. +#endif
  9898. +
  9899. +/* Display a block */
  9900. +void show_block(const u8 *blk, const char *prefix, const char *suffix, int a)
  9901. +{
  9902. +#ifdef DWC_DEBUG_CRYPTO
  9903. + int i, blksize = 16;
  9904. +
  9905. + DWC_DEBUG("%s", prefix);
  9906. +
  9907. + if (suffix == NULL) {
  9908. + suffix = "\n";
  9909. + blksize = a;
  9910. + }
  9911. +
  9912. + for (i = 0; i < blksize; i++)
  9913. + DWC_PRINT("%02x%s", *blk++, ((i & 3) == 3) ? " " : " ");
  9914. + DWC_PRINT(suffix);
  9915. +#endif
  9916. +}
  9917. +
  9918. +/**
  9919. + * Encrypts an array of bytes using the AES encryption engine.
  9920. + * If <code>dst</code> == <code>src</code>, then the bytes will be encrypted
  9921. + * in-place.
  9922. + *
  9923. + * @return 0 on success, negative error code on error.
  9924. + */
  9925. +int dwc_wusb_aes_encrypt(u8 *src, u8 *key, u8 *dst)
  9926. +{
  9927. + u8 block_t[16];
  9928. + DWC_MEMSET(block_t, 0, 16);
  9929. +
  9930. + return DWC_AES_CBC(src, 16, key, 16, block_t, dst);
  9931. +}
  9932. +
  9933. +/**
  9934. + * The CCM-MAC-FUNCTION described in section 6.5 of the WUSB spec.
  9935. + * This function takes a data string and returns the encrypted CBC
  9936. + * Counter-mode MIC.
  9937. + *
  9938. + * @param key The 128-bit symmetric key.
  9939. + * @param nonce The CCM nonce.
  9940. + * @param label The unique 14-byte ASCII text label.
  9941. + * @param bytes The byte array to be encrypted.
  9942. + * @param len Length of the byte array.
  9943. + * @param result Byte array to receive the 8-byte encrypted MIC.
  9944. + */
  9945. +void dwc_wusb_cmf(u8 *key, u8 *nonce,
  9946. + char *label, u8 *bytes, int len, u8 *result)
  9947. +{
  9948. + u8 block_m[16];
  9949. + u8 block_x[16];
  9950. + u8 block_t[8];
  9951. + int idx, blkNum;
  9952. + u16 la = (u16)(len + 14);
  9953. +
  9954. + /* Set the AES-128 key */
  9955. + //dwc_aes_setkey(tfm, key, 16);
  9956. +
  9957. + /* Fill block B0 from flags = 0x59, N, and l(m) = 0 */
  9958. + block_m[0] = 0x59;
  9959. + for (idx = 0; idx < 13; idx++)
  9960. + block_m[idx + 1] = nonce[idx];
  9961. + block_m[14] = 0;
  9962. + block_m[15] = 0;
  9963. +
  9964. + /* Produce the CBC IV */
  9965. + dwc_wusb_aes_encrypt(block_m, key, block_x);
  9966. + show_block(block_m, "CBC IV in: ", "\n", 0);
  9967. + show_block(block_x, "CBC IV out:", "\n", 0);
  9968. +
  9969. + /* Fill block B1 from l(a) = Blen + 14, and A */
  9970. + block_x[0] ^= (u8)(la >> 8);
  9971. + block_x[1] ^= (u8)la;
  9972. + for (idx = 0; idx < 14; idx++)
  9973. + block_x[idx + 2] ^= label[idx];
  9974. + show_block(block_x, "After xor: ", "b1\n", 16);
  9975. +
  9976. + dwc_wusb_aes_encrypt(block_x, key, block_x);
  9977. + show_block(block_x, "After AES: ", "b1\n", 16);
  9978. +
  9979. + idx = 0;
  9980. + blkNum = 0;
  9981. +
  9982. + /* Fill remaining blocks with B */
  9983. + while (len-- > 0) {
  9984. + block_x[idx] ^= *bytes++;
  9985. + if (++idx >= 16) {
  9986. + idx = 0;
  9987. + show_block(block_x, "After xor: ", "\n", blkNum);
  9988. + dwc_wusb_aes_encrypt(block_x, key, block_x);
  9989. + show_block(block_x, "After AES: ", "\n", blkNum);
  9990. + blkNum++;
  9991. + }
  9992. + }
  9993. +
  9994. + /* Handle partial last block */
  9995. + if (idx > 0) {
  9996. + show_block(block_x, "After xor: ", "\n", blkNum);
  9997. + dwc_wusb_aes_encrypt(block_x, key, block_x);
  9998. + show_block(block_x, "After AES: ", "\n", blkNum);
  9999. + }
  10000. +
  10001. + /* Save the MIC tag */
  10002. + DWC_MEMCPY(block_t, block_x, 8);
  10003. + show_block(block_t, "MIC tag : ", NULL, 8);
  10004. +
  10005. + /* Fill block A0 from flags = 0x01, N, and counter = 0 */
  10006. + block_m[0] = 0x01;
  10007. + block_m[14] = 0;
  10008. + block_m[15] = 0;
  10009. +
  10010. + /* Encrypt the counter */
  10011. + dwc_wusb_aes_encrypt(block_m, key, block_x);
  10012. + show_block(block_x, "CTR[MIC] : ", NULL, 8);
  10013. +
  10014. + /* XOR with MIC tag */
  10015. + for (idx = 0; idx < 8; idx++) {
  10016. + block_t[idx] ^= block_x[idx];
  10017. + }
  10018. +
  10019. + /* Return result to caller */
  10020. + DWC_MEMCPY(result, block_t, 8);
  10021. + show_block(result, "CCM-MIC : ", NULL, 8);
  10022. +
  10023. +}
  10024. +
  10025. +/**
  10026. + * The PRF function described in section 6.5 of the WUSB spec. This function
  10027. + * concatenates MIC values returned from dwc_cmf() to create a value of
  10028. + * the requested length.
  10029. + *
  10030. + * @param prf_len Length of the PRF function in bits (64, 128, or 256).
  10031. + * @param key, nonce, label, bytes, len Same as for dwc_cmf().
  10032. + * @param result Byte array to receive the result.
  10033. + */
  10034. +void dwc_wusb_prf(int prf_len, u8 *key,
  10035. + u8 *nonce, char *label, u8 *bytes, int len, u8 *result)
  10036. +{
  10037. + int i;
  10038. +
  10039. + nonce[0] = 0;
  10040. + for (i = 0; i < prf_len >> 6; i++, nonce[0]++) {
  10041. + dwc_wusb_cmf(key, nonce, label, bytes, len, result);
  10042. + result += 8;
  10043. + }
  10044. +}
  10045. +
  10046. +/**
  10047. + * Fills in CCM Nonce per the WUSB spec.
  10048. + *
  10049. + * @param[in] haddr Host address.
  10050. + * @param[in] daddr Device address.
  10051. + * @param[in] tkid Session Key(PTK) identifier.
  10052. + * @param[out] nonce Pointer to where the CCM Nonce output is to be written.
  10053. + */
  10054. +void dwc_wusb_fill_ccm_nonce(uint16_t haddr, uint16_t daddr, uint8_t *tkid,
  10055. + uint8_t *nonce)
  10056. +{
  10057. +
  10058. + DWC_DEBUG("%s %x %x\n", __func__, daddr, haddr);
  10059. +
  10060. + DWC_MEMSET(&nonce[0], 0, 16);
  10061. +
  10062. + DWC_MEMCPY(&nonce[6], tkid, 3);
  10063. + nonce[9] = daddr & 0xFF;
  10064. + nonce[10] = (daddr >> 8) & 0xFF;
  10065. + nonce[11] = haddr & 0xFF;
  10066. + nonce[12] = (haddr >> 8) & 0xFF;
  10067. +
  10068. + dump_bytes("CCM nonce", nonce, 16);
  10069. +}
  10070. +
  10071. +/**
  10072. + * Generates a 16-byte cryptographic-grade random number for the Host/Device
  10073. + * Nonce.
  10074. + */
  10075. +void dwc_wusb_gen_nonce(uint16_t addr, uint8_t *nonce)
  10076. +{
  10077. + uint8_t inonce[16];
  10078. + uint32_t temp[4];
  10079. +
  10080. + /* Fill in the Nonce */
  10081. + DWC_MEMSET(&inonce[0], 0, sizeof(inonce));
  10082. + inonce[9] = addr & 0xFF;
  10083. + inonce[10] = (addr >> 8) & 0xFF;
  10084. + inonce[11] = inonce[9];
  10085. + inonce[12] = inonce[10];
  10086. +
  10087. + /* Collect "randomness samples" */
  10088. + DWC_RANDOM_BYTES((uint8_t *)temp, 16);
  10089. +
  10090. + dwc_wusb_prf_128((uint8_t *)temp, nonce,
  10091. + "Random Numbers", (uint8_t *)temp, sizeof(temp),
  10092. + nonce);
  10093. +}
  10094. +
  10095. +/**
  10096. + * Generates the Session Key (PTK) and Key Confirmation Key (KCK) per the
  10097. + * WUSB spec.
  10098. + *
  10099. + * @param[in] ccm_nonce Pointer to CCM Nonce.
  10100. + * @param[in] mk Master Key to derive the session from
  10101. + * @param[in] hnonce Pointer to Host Nonce.
  10102. + * @param[in] dnonce Pointer to Device Nonce.
  10103. + * @param[out] kck Pointer to where the KCK output is to be written.
  10104. + * @param[out] ptk Pointer to where the PTK output is to be written.
  10105. + */
  10106. +void dwc_wusb_gen_key(uint8_t *ccm_nonce, uint8_t *mk, uint8_t *hnonce,
  10107. + uint8_t *dnonce, uint8_t *kck, uint8_t *ptk)
  10108. +{
  10109. + uint8_t idata[32];
  10110. + uint8_t odata[32];
  10111. +
  10112. + dump_bytes("ck", mk, 16);
  10113. + dump_bytes("hnonce", hnonce, 16);
  10114. + dump_bytes("dnonce", dnonce, 16);
  10115. +
  10116. + /* The data is the HNonce and DNonce concatenated */
  10117. + DWC_MEMCPY(&idata[0], hnonce, 16);
  10118. + DWC_MEMCPY(&idata[16], dnonce, 16);
  10119. +
  10120. + dwc_wusb_prf_256(mk, ccm_nonce, "Pair-wise keys", idata, 32, odata);
  10121. +
  10122. + /* Low 16 bytes of the result is the KCK, high 16 is the PTK */
  10123. + DWC_MEMCPY(kck, &odata[0], 16);
  10124. + DWC_MEMCPY(ptk, &odata[16], 16);
  10125. +
  10126. + dump_bytes("kck", kck, 16);
  10127. + dump_bytes("ptk", ptk, 16);
  10128. +}
  10129. +
  10130. +/**
  10131. + * Generates the Message Integrity Code over the Handshake data per the
  10132. + * WUSB spec.
  10133. + *
  10134. + * @param ccm_nonce Pointer to CCM Nonce.
  10135. + * @param kck Pointer to Key Confirmation Key.
  10136. + * @param data Pointer to Handshake data to be checked.
  10137. + * @param mic Pointer to where the MIC output is to be written.
  10138. + */
  10139. +void dwc_wusb_gen_mic(uint8_t *ccm_nonce, uint8_t *kck,
  10140. + uint8_t *data, uint8_t *mic)
  10141. +{
  10142. +
  10143. + dwc_wusb_prf_64(kck, ccm_nonce, "out-of-bandMIC",
  10144. + data, WUSB_HANDSHAKE_LEN_FOR_MIC, mic);
  10145. +}
  10146. +
  10147. +#endif /* DWC_CRYPTOLIB */
  10148. --- /dev/null
  10149. +++ b/drivers/usb/host/dwc_common_port/dwc_crypto.h
  10150. @@ -0,0 +1,111 @@
  10151. +/* =========================================================================
  10152. + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_crypto.h $
  10153. + * $Revision: #3 $
  10154. + * $Date: 2010/09/28 $
  10155. + * $Change: 1596182 $
  10156. + *
  10157. + * Synopsys Portability Library Software and documentation
  10158. + * (hereinafter, "Software") is an Unsupported proprietary work of
  10159. + * Synopsys, Inc. unless otherwise expressly agreed to in writing
  10160. + * between Synopsys and you.
  10161. + *
  10162. + * The Software IS NOT an item of Licensed Software or Licensed Product
  10163. + * under any End User Software License Agreement or Agreement for
  10164. + * Licensed Product with Synopsys or any supplement thereto. You are
  10165. + * permitted to use and redistribute this Software in source and binary
  10166. + * forms, with or without modification, provided that redistributions
  10167. + * of source code must retain this notice. You may not view, use,
  10168. + * disclose, copy or distribute this file or any information contained
  10169. + * herein except pursuant to this license grant from Synopsys. If you
  10170. + * do not agree with this notice, including the disclaimer below, then
  10171. + * you are not authorized to use the Software.
  10172. + *
  10173. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  10174. + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  10175. + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  10176. + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
  10177. + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  10178. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  10179. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  10180. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  10181. + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  10182. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  10183. + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  10184. + * DAMAGE.
  10185. + * ========================================================================= */
  10186. +
  10187. +#ifndef _DWC_CRYPTO_H_
  10188. +#define _DWC_CRYPTO_H_
  10189. +
  10190. +#ifdef __cplusplus
  10191. +extern "C" {
  10192. +#endif
  10193. +
  10194. +/** @file
  10195. + *
  10196. + * This file contains declarations for the WUSB Cryptographic routines as
  10197. + * defined in the WUSB spec. They are only to be used internally by the DWC UWB
  10198. + * modules.
  10199. + */
  10200. +
  10201. +#include "dwc_os.h"
  10202. +
  10203. +int dwc_wusb_aes_encrypt(u8 *src, u8 *key, u8 *dst);
  10204. +
  10205. +void dwc_wusb_cmf(u8 *key, u8 *nonce,
  10206. + char *label, u8 *bytes, int len, u8 *result);
  10207. +void dwc_wusb_prf(int prf_len, u8 *key,
  10208. + u8 *nonce, char *label, u8 *bytes, int len, u8 *result);
  10209. +
  10210. +/**
  10211. + * The PRF-64 function described in section 6.5 of the WUSB spec.
  10212. + *
  10213. + * @param key, nonce, label, bytes, len, result Same as for dwc_prf().
  10214. + */
  10215. +static inline void dwc_wusb_prf_64(u8 *key, u8 *nonce,
  10216. + char *label, u8 *bytes, int len, u8 *result)
  10217. +{
  10218. + dwc_wusb_prf(64, key, nonce, label, bytes, len, result);
  10219. +}
  10220. +
  10221. +/**
  10222. + * The PRF-128 function described in section 6.5 of the WUSB spec.
  10223. + *
  10224. + * @param key, nonce, label, bytes, len, result Same as for dwc_prf().
  10225. + */
  10226. +static inline void dwc_wusb_prf_128(u8 *key, u8 *nonce,
  10227. + char *label, u8 *bytes, int len, u8 *result)
  10228. +{
  10229. + dwc_wusb_prf(128, key, nonce, label, bytes, len, result);
  10230. +}
  10231. +
  10232. +/**
  10233. + * The PRF-256 function described in section 6.5 of the WUSB spec.
  10234. + *
  10235. + * @param key, nonce, label, bytes, len, result Same as for dwc_prf().
  10236. + */
  10237. +static inline void dwc_wusb_prf_256(u8 *key, u8 *nonce,
  10238. + char *label, u8 *bytes, int len, u8 *result)
  10239. +{
  10240. + dwc_wusb_prf(256, key, nonce, label, bytes, len, result);
  10241. +}
  10242. +
  10243. +
  10244. +void dwc_wusb_fill_ccm_nonce(uint16_t haddr, uint16_t daddr, uint8_t *tkid,
  10245. + uint8_t *nonce);
  10246. +void dwc_wusb_gen_nonce(uint16_t addr,
  10247. + uint8_t *nonce);
  10248. +
  10249. +void dwc_wusb_gen_key(uint8_t *ccm_nonce, uint8_t *mk,
  10250. + uint8_t *hnonce, uint8_t *dnonce,
  10251. + uint8_t *kck, uint8_t *ptk);
  10252. +
  10253. +
  10254. +void dwc_wusb_gen_mic(uint8_t *ccm_nonce, uint8_t
  10255. + *kck, uint8_t *data, uint8_t *mic);
  10256. +
  10257. +#ifdef __cplusplus
  10258. +}
  10259. +#endif
  10260. +
  10261. +#endif /* _DWC_CRYPTO_H_ */
  10262. --- /dev/null
  10263. +++ b/drivers/usb/host/dwc_common_port/dwc_dh.c
  10264. @@ -0,0 +1,291 @@
  10265. +/* =========================================================================
  10266. + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_dh.c $
  10267. + * $Revision: #3 $
  10268. + * $Date: 2010/09/28 $
  10269. + * $Change: 1596182 $
  10270. + *
  10271. + * Synopsys Portability Library Software and documentation
  10272. + * (hereinafter, "Software") is an Unsupported proprietary work of
  10273. + * Synopsys, Inc. unless otherwise expressly agreed to in writing
  10274. + * between Synopsys and you.
  10275. + *
  10276. + * The Software IS NOT an item of Licensed Software or Licensed Product
  10277. + * under any End User Software License Agreement or Agreement for
  10278. + * Licensed Product with Synopsys or any supplement thereto. You are
  10279. + * permitted to use and redistribute this Software in source and binary
  10280. + * forms, with or without modification, provided that redistributions
  10281. + * of source code must retain this notice. You may not view, use,
  10282. + * disclose, copy or distribute this file or any information contained
  10283. + * herein except pursuant to this license grant from Synopsys. If you
  10284. + * do not agree with this notice, including the disclaimer below, then
  10285. + * you are not authorized to use the Software.
  10286. + *
  10287. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  10288. + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  10289. + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  10290. + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
  10291. + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  10292. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  10293. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  10294. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  10295. + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  10296. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  10297. + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  10298. + * DAMAGE.
  10299. + * ========================================================================= */
  10300. +#ifdef DWC_CRYPTOLIB
  10301. +
  10302. +#ifndef CONFIG_MACH_IPMATE
  10303. +
  10304. +#include "dwc_dh.h"
  10305. +#include "dwc_modpow.h"
  10306. +
  10307. +#ifdef DEBUG
  10308. +/* This function prints out a buffer in the format described in the Association
  10309. + * Model specification. */
  10310. +static void dh_dump(char *str, void *_num, int len)
  10311. +{
  10312. + uint8_t *num = _num;
  10313. + int i;
  10314. + DWC_PRINTF("%s\n", str);
  10315. + for (i = 0; i < len; i ++) {
  10316. + DWC_PRINTF("%02x", num[i]);
  10317. + if (((i + 1) % 2) == 0) DWC_PRINTF(" ");
  10318. + if (((i + 1) % 26) == 0) DWC_PRINTF("\n");
  10319. + }
  10320. +
  10321. + DWC_PRINTF("\n");
  10322. +}
  10323. +#else
  10324. +#define dh_dump(_x...) do {; } while(0)
  10325. +#endif
  10326. +
  10327. +/* Constant g value */
  10328. +static __u32 dh_g[] = {
  10329. + 0x02000000,
  10330. +};
  10331. +
  10332. +/* Constant p value */
  10333. +static __u32 dh_p[] = {
  10334. + 0xFFFFFFFF, 0xFFFFFFFF, 0xA2DA0FC9, 0x34C26821, 0x8B62C6C4, 0xD11CDC80, 0x084E0229, 0x74CC678A,
  10335. + 0xA6BE0B02, 0x229B133B, 0x79084A51, 0xDD04348E, 0xB31995EF, 0x1B433ACD, 0x6D0A2B30, 0x37145FF2,
  10336. + 0x6D35E14F, 0x45C2516D, 0x76B585E4, 0xC67E5E62, 0xE9424CF4, 0x6BED37A6, 0xB65CFF0B, 0xEDB706F4,
  10337. + 0xFB6B38EE, 0xA59F895A, 0x11249FAE, 0xE61F4B7C, 0x51662849, 0x3D5BE4EC, 0xB87C00C2, 0x05BF63A1,
  10338. + 0x3648DA98, 0x9AD3551C, 0xA83F1669, 0x5FCF24FD, 0x235D6583, 0x96ADA3DC, 0x56F3621C, 0xBB528520,
  10339. + 0x0729D59E, 0x6D969670, 0x4E350C67, 0x0498BC4A, 0x086C74F1, 0x7C2118CA, 0x465E9032, 0x3BCE362E,
  10340. + 0x2C779EE3, 0x03860E18, 0xA283279B, 0x8FA207EC, 0xF05DC5B5, 0xC9524C6F, 0xF6CB2BDE, 0x18175895,
  10341. + 0x7C499539, 0xE56A95EA, 0x1826D215, 0x1005FA98, 0x5A8E7215, 0x2DC4AA8A, 0x0D1733AD, 0x337A5004,
  10342. + 0xAB2155A8, 0x64BA1CDF, 0x0485FBEC, 0x0AEFDB58, 0x5771EA8A, 0x7D0C065D, 0x850F97B3, 0xC7E4E1A6,
  10343. + 0x8CAEF5AB, 0xD73309DB, 0xE0948C1E, 0x9D61254A, 0x26D2E3CE, 0x6BEED21A, 0x06FA2FF1, 0x64088AD9,
  10344. + 0x730276D8, 0x646AC83E, 0x182B1F52, 0x0C207B17, 0x5717E1BB, 0x6C5D617A, 0xC0880977, 0xE246D9BA,
  10345. + 0xA04FE208, 0x31ABE574, 0xFC5BDB43, 0x8E10FDE0, 0x20D1824B, 0xCAD23AA9, 0xFFFFFFFF, 0xFFFFFFFF,
  10346. +};
  10347. +
  10348. +static void dh_swap_bytes(void *_in, void *_out, uint32_t len)
  10349. +{
  10350. + uint8_t *in = _in;
  10351. + uint8_t *out = _out;
  10352. + int i;
  10353. + for (i=0; i<len; i++) {
  10354. + out[i] = in[len-1-i];
  10355. + }
  10356. +}
  10357. +
  10358. +/* Computes the modular exponentiation (num^exp % mod). num, exp, and mod are
  10359. + * big endian numbers of size len, in bytes. Each len value must be a multiple
  10360. + * of 4. */
  10361. +int dwc_dh_modpow(void *mem_ctx, void *num, uint32_t num_len,
  10362. + void *exp, uint32_t exp_len,
  10363. + void *mod, uint32_t mod_len,
  10364. + void *out)
  10365. +{
  10366. + /* modpow() takes little endian numbers. AM uses big-endian. This
  10367. + * function swaps bytes of numbers before passing onto modpow. */
  10368. +
  10369. + int retval = 0;
  10370. + uint32_t *result;
  10371. +
  10372. + uint32_t *bignum_num = dwc_alloc(mem_ctx, num_len + 4);
  10373. + uint32_t *bignum_exp = dwc_alloc(mem_ctx, exp_len + 4);
  10374. + uint32_t *bignum_mod = dwc_alloc(mem_ctx, mod_len + 4);
  10375. +
  10376. + dh_swap_bytes(num, &bignum_num[1], num_len);
  10377. + bignum_num[0] = num_len / 4;
  10378. +
  10379. + dh_swap_bytes(exp, &bignum_exp[1], exp_len);
  10380. + bignum_exp[0] = exp_len / 4;
  10381. +
  10382. + dh_swap_bytes(mod, &bignum_mod[1], mod_len);
  10383. + bignum_mod[0] = mod_len / 4;
  10384. +
  10385. + result = dwc_modpow(mem_ctx, bignum_num, bignum_exp, bignum_mod);
  10386. + if (!result) {
  10387. + retval = -1;
  10388. + goto dh_modpow_nomem;
  10389. + }
  10390. +
  10391. + dh_swap_bytes(&result[1], out, result[0] * 4);
  10392. + dwc_free(mem_ctx, result);
  10393. +
  10394. + dh_modpow_nomem:
  10395. + dwc_free(mem_ctx, bignum_num);
  10396. + dwc_free(mem_ctx, bignum_exp);
  10397. + dwc_free(mem_ctx, bignum_mod);
  10398. + return retval;
  10399. +}
  10400. +
  10401. +
  10402. +int dwc_dh_pk(void *mem_ctx, uint8_t nd, uint8_t *exp, uint8_t *pk, uint8_t *hash)
  10403. +{
  10404. + int retval;
  10405. + uint8_t m3[385];
  10406. +
  10407. +#ifndef DH_TEST_VECTORS
  10408. + DWC_RANDOM_BYTES(exp, 32);
  10409. +#endif
  10410. +
  10411. + /* Compute the pkd */
  10412. + if ((retval = dwc_dh_modpow(mem_ctx, dh_g, 4,
  10413. + exp, 32,
  10414. + dh_p, 384, pk))) {
  10415. + return retval;
  10416. + }
  10417. +
  10418. + m3[384] = nd;
  10419. + DWC_MEMCPY(&m3[0], pk, 384);
  10420. + DWC_SHA256(m3, 385, hash);
  10421. +
  10422. + dh_dump("PK", pk, 384);
  10423. + dh_dump("SHA-256(M3)", hash, 32);
  10424. + return 0;
  10425. +}
  10426. +
  10427. +int dwc_dh_derive_keys(void *mem_ctx, uint8_t nd, uint8_t *pkh, uint8_t *pkd,
  10428. + uint8_t *exp, int is_host,
  10429. + char *dd, uint8_t *ck, uint8_t *kdk)
  10430. +{
  10431. + int retval;
  10432. + uint8_t mv[784];
  10433. + uint8_t sha_result[32];
  10434. + uint8_t dhkey[384];
  10435. + uint8_t shared_secret[384];
  10436. + char *message;
  10437. + uint32_t vd;
  10438. +
  10439. + uint8_t *pk;
  10440. +
  10441. + if (is_host) {
  10442. + pk = pkd;
  10443. + }
  10444. + else {
  10445. + pk = pkh;
  10446. + }
  10447. +
  10448. + if ((retval = dwc_dh_modpow(mem_ctx, pk, 384,
  10449. + exp, 32,
  10450. + dh_p, 384, shared_secret))) {
  10451. + return retval;
  10452. + }
  10453. + dh_dump("Shared Secret", shared_secret, 384);
  10454. +
  10455. + DWC_SHA256(shared_secret, 384, dhkey);
  10456. + dh_dump("DHKEY", dhkey, 384);
  10457. +
  10458. + DWC_MEMCPY(&mv[0], pkd, 384);
  10459. + DWC_MEMCPY(&mv[384], pkh, 384);
  10460. + DWC_MEMCPY(&mv[768], "displayed digest", 16);
  10461. + dh_dump("MV", mv, 784);
  10462. +
  10463. + DWC_SHA256(mv, 784, sha_result);
  10464. + dh_dump("SHA-256(MV)", sha_result, 32);
  10465. + dh_dump("First 32-bits of SHA-256(MV)", sha_result, 4);
  10466. +
  10467. + dh_swap_bytes(sha_result, &vd, 4);
  10468. +#ifdef DEBUG
  10469. + DWC_PRINTF("Vd (decimal) = %d\n", vd);
  10470. +#endif
  10471. +
  10472. + switch (nd) {
  10473. + case 2:
  10474. + vd = vd % 100;
  10475. + DWC_SPRINTF(dd, "%02d", vd);
  10476. + break;
  10477. + case 3:
  10478. + vd = vd % 1000;
  10479. + DWC_SPRINTF(dd, "%03d", vd);
  10480. + break;
  10481. + case 4:
  10482. + vd = vd % 10000;
  10483. + DWC_SPRINTF(dd, "%04d", vd);
  10484. + break;
  10485. + }
  10486. +#ifdef DEBUG
  10487. + DWC_PRINTF("Display Digits: %s\n", dd);
  10488. +#endif
  10489. +
  10490. + message = "connection key";
  10491. + DWC_HMAC_SHA256(message, DWC_STRLEN(message), dhkey, 32, sha_result);
  10492. + dh_dump("HMAC(SHA-256, DHKey, connection key)", sha_result, 32);
  10493. + DWC_MEMCPY(ck, sha_result, 16);
  10494. +
  10495. + message = "key derivation key";
  10496. + DWC_HMAC_SHA256(message, DWC_STRLEN(message), dhkey, 32, sha_result);
  10497. + dh_dump("HMAC(SHA-256, DHKey, key derivation key)", sha_result, 32);
  10498. + DWC_MEMCPY(kdk, sha_result, 32);
  10499. +
  10500. + return 0;
  10501. +}
  10502. +
  10503. +
  10504. +#ifdef DH_TEST_VECTORS
  10505. +
  10506. +static __u8 dh_a[] = {
  10507. + 0x44, 0x00, 0x51, 0xd6,
  10508. + 0xf0, 0xb5, 0x5e, 0xa9,
  10509. + 0x67, 0xab, 0x31, 0xc6,
  10510. + 0x8a, 0x8b, 0x5e, 0x37,
  10511. + 0xd9, 0x10, 0xda, 0xe0,
  10512. + 0xe2, 0xd4, 0x59, 0xa4,
  10513. + 0x86, 0x45, 0x9c, 0xaa,
  10514. + 0xdf, 0x36, 0x75, 0x16,
  10515. +};
  10516. +
  10517. +static __u8 dh_b[] = {
  10518. + 0x5d, 0xae, 0xc7, 0x86,
  10519. + 0x79, 0x80, 0xa3, 0x24,
  10520. + 0x8c, 0xe3, 0x57, 0x8f,
  10521. + 0xc7, 0x5f, 0x1b, 0x0f,
  10522. + 0x2d, 0xf8, 0x9d, 0x30,
  10523. + 0x6f, 0xa4, 0x52, 0xcd,
  10524. + 0xe0, 0x7a, 0x04, 0x8a,
  10525. + 0xde, 0xd9, 0x26, 0x56,
  10526. +};
  10527. +
  10528. +void dwc_run_dh_test_vectors(void *mem_ctx)
  10529. +{
  10530. + uint8_t pkd[384];
  10531. + uint8_t pkh[384];
  10532. + uint8_t hashd[32];
  10533. + uint8_t hashh[32];
  10534. + uint8_t ck[16];
  10535. + uint8_t kdk[32];
  10536. + char dd[5];
  10537. +
  10538. + DWC_PRINTF("\n\n\nDH_TEST_VECTORS\n\n");
  10539. +
  10540. + /* compute the PKd and SHA-256(PKd || Nd) */
  10541. + DWC_PRINTF("Computing PKd\n");
  10542. + dwc_dh_pk(mem_ctx, 2, dh_a, pkd, hashd);
  10543. +
  10544. + /* compute the PKd and SHA-256(PKh || Nd) */
  10545. + DWC_PRINTF("Computing PKh\n");
  10546. + dwc_dh_pk(mem_ctx, 2, dh_b, pkh, hashh);
  10547. +
  10548. + /* compute the dhkey */
  10549. + dwc_dh_derive_keys(mem_ctx, 2, pkh, pkd, dh_a, 0, dd, ck, kdk);
  10550. +}
  10551. +#endif /* DH_TEST_VECTORS */
  10552. +
  10553. +#endif /* !CONFIG_MACH_IPMATE */
  10554. +
  10555. +#endif /* DWC_CRYPTOLIB */
  10556. --- /dev/null
  10557. +++ b/drivers/usb/host/dwc_common_port/dwc_dh.h
  10558. @@ -0,0 +1,106 @@
  10559. +/* =========================================================================
  10560. + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_dh.h $
  10561. + * $Revision: #4 $
  10562. + * $Date: 2010/09/28 $
  10563. + * $Change: 1596182 $
  10564. + *
  10565. + * Synopsys Portability Library Software and documentation
  10566. + * (hereinafter, "Software") is an Unsupported proprietary work of
  10567. + * Synopsys, Inc. unless otherwise expressly agreed to in writing
  10568. + * between Synopsys and you.
  10569. + *
  10570. + * The Software IS NOT an item of Licensed Software or Licensed Product
  10571. + * under any End User Software License Agreement or Agreement for
  10572. + * Licensed Product with Synopsys or any supplement thereto. You are
  10573. + * permitted to use and redistribute this Software in source and binary
  10574. + * forms, with or without modification, provided that redistributions
  10575. + * of source code must retain this notice. You may not view, use,
  10576. + * disclose, copy or distribute this file or any information contained
  10577. + * herein except pursuant to this license grant from Synopsys. If you
  10578. + * do not agree with this notice, including the disclaimer below, then
  10579. + * you are not authorized to use the Software.
  10580. + *
  10581. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  10582. + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  10583. + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  10584. + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
  10585. + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  10586. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  10587. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  10588. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  10589. + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  10590. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  10591. + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  10592. + * DAMAGE.
  10593. + * ========================================================================= */
  10594. +#ifndef _DWC_DH_H_
  10595. +#define _DWC_DH_H_
  10596. +
  10597. +#ifdef __cplusplus
  10598. +extern "C" {
  10599. +#endif
  10600. +
  10601. +#include "dwc_os.h"
  10602. +
  10603. +/** @file
  10604. + *
  10605. + * This file defines the common functions on device and host for performing
  10606. + * numeric association as defined in the WUSB spec. They are only to be
  10607. + * used internally by the DWC UWB modules. */
  10608. +
  10609. +extern int dwc_dh_sha256(uint8_t *message, uint32_t len, uint8_t *out);
  10610. +extern int dwc_dh_hmac_sha256(uint8_t *message, uint32_t messagelen,
  10611. + uint8_t *key, uint32_t keylen,
  10612. + uint8_t *out);
  10613. +extern int dwc_dh_modpow(void *mem_ctx, void *num, uint32_t num_len,
  10614. + void *exp, uint32_t exp_len,
  10615. + void *mod, uint32_t mod_len,
  10616. + void *out);
  10617. +
  10618. +/** Computes PKD or PKH, and SHA-256(PKd || Nd)
  10619. + *
  10620. + * PK = g^exp mod p.
  10621. + *
  10622. + * Input:
  10623. + * Nd = Number of digits on the device.
  10624. + *
  10625. + * Output:
  10626. + * exp = A 32-byte buffer to be filled with a randomly generated number.
  10627. + * used as either A or B.
  10628. + * pk = A 384-byte buffer to be filled with the PKH or PKD.
  10629. + * hash = A 32-byte buffer to be filled with SHA-256(PK || ND).
  10630. + */
  10631. +extern int dwc_dh_pk(void *mem_ctx, uint8_t nd, uint8_t *exp, uint8_t *pkd, uint8_t *hash);
  10632. +
  10633. +/** Computes the DHKEY, and VD.
  10634. + *
  10635. + * If called from host, then it will comput DHKEY=PKD^exp % p.
  10636. + * If called from device, then it will comput DHKEY=PKH^exp % p.
  10637. + *
  10638. + * Input:
  10639. + * pkd = The PKD value.
  10640. + * pkh = The PKH value.
  10641. + * exp = The A value (if device) or B value (if host) generated in dwc_wudev_dh_pk.
  10642. + * is_host = Set to non zero if a WUSB host is calling this function.
  10643. + *
  10644. + * Output:
  10645. +
  10646. + * dd = A pointer to an buffer to be set to the displayed digits string to be shown
  10647. + * to the user. This buffer should be at 5 bytes long to hold 4 digits plus a
  10648. + * null termination character. This buffer can be used directly for display.
  10649. + * ck = A 16-byte buffer to be filled with the CK.
  10650. + * kdk = A 32-byte buffer to be filled with the KDK.
  10651. + */
  10652. +extern int dwc_dh_derive_keys(void *mem_ctx, uint8_t nd, uint8_t *pkh, uint8_t *pkd,
  10653. + uint8_t *exp, int is_host,
  10654. + char *dd, uint8_t *ck, uint8_t *kdk);
  10655. +
  10656. +#ifdef DH_TEST_VECTORS
  10657. +extern void dwc_run_dh_test_vectors(void);
  10658. +#endif
  10659. +
  10660. +#ifdef __cplusplus
  10661. +}
  10662. +#endif
  10663. +
  10664. +#endif /* _DWC_DH_H_ */
  10665. --- /dev/null
  10666. +++ b/drivers/usb/host/dwc_common_port/dwc_list.h
  10667. @@ -0,0 +1,594 @@
  10668. +/* $OpenBSD: queue.h,v 1.26 2004/05/04 16:59:32 grange Exp $ */
  10669. +/* $NetBSD: queue.h,v 1.11 1996/05/16 05:17:14 mycroft Exp $ */
  10670. +
  10671. +/*
  10672. + * Copyright (c) 1991, 1993
  10673. + * The Regents of the University of California. All rights reserved.
  10674. + *
  10675. + * Redistribution and use in source and binary forms, with or without
  10676. + * modification, are permitted provided that the following conditions
  10677. + * are met:
  10678. + * 1. Redistributions of source code must retain the above copyright
  10679. + * notice, this list of conditions and the following disclaimer.
  10680. + * 2. Redistributions in binary form must reproduce the above copyright
  10681. + * notice, this list of conditions and the following disclaimer in the
  10682. + * documentation and/or other materials provided with the distribution.
  10683. + * 3. Neither the name of the University nor the names of its contributors
  10684. + * may be used to endorse or promote products derived from this software
  10685. + * without specific prior written permission.
  10686. + *
  10687. + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  10688. + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  10689. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  10690. + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  10691. + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  10692. + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  10693. + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  10694. + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  10695. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  10696. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  10697. + * SUCH DAMAGE.
  10698. + *
  10699. + * @(#)queue.h 8.5 (Berkeley) 8/20/94
  10700. + */
  10701. +
  10702. +#ifndef _DWC_LIST_H_
  10703. +#define _DWC_LIST_H_
  10704. +
  10705. +#ifdef __cplusplus
  10706. +extern "C" {
  10707. +#endif
  10708. +
  10709. +/** @file
  10710. + *
  10711. + * This file defines linked list operations. It is derived from BSD with
  10712. + * only the MACRO names being prefixed with DWC_. This is because a few of
  10713. + * these names conflict with those on Linux. For documentation on use, see the
  10714. + * inline comments in the source code. The original license for this source
  10715. + * code applies and is preserved in the dwc_list.h source file.
  10716. + */
  10717. +
  10718. +/*
  10719. + * This file defines five types of data structures: singly-linked lists,
  10720. + * lists, simple queues, tail queues, and circular queues.
  10721. + *
  10722. + *
  10723. + * A singly-linked list is headed by a single forward pointer. The elements
  10724. + * are singly linked for minimum space and pointer manipulation overhead at
  10725. + * the expense of O(n) removal for arbitrary elements. New elements can be
  10726. + * added to the list after an existing element or at the head of the list.
  10727. + * Elements being removed from the head of the list should use the explicit
  10728. + * macro for this purpose for optimum efficiency. A singly-linked list may
  10729. + * only be traversed in the forward direction. Singly-linked lists are ideal
  10730. + * for applications with large datasets and few or no removals or for
  10731. + * implementing a LIFO queue.
  10732. + *
  10733. + * A list is headed by a single forward pointer (or an array of forward
  10734. + * pointers for a hash table header). The elements are doubly linked
  10735. + * so that an arbitrary element can be removed without a need to
  10736. + * traverse the list. New elements can be added to the list before
  10737. + * or after an existing element or at the head of the list. A list
  10738. + * may only be traversed in the forward direction.
  10739. + *
  10740. + * A simple queue is headed by a pair of pointers, one the head of the
  10741. + * list and the other to the tail of the list. The elements are singly
  10742. + * linked to save space, so elements can only be removed from the
  10743. + * head of the list. New elements can be added to the list before or after
  10744. + * an existing element, at the head of the list, or at the end of the
  10745. + * list. A simple queue may only be traversed in the forward direction.
  10746. + *
  10747. + * A tail queue is headed by a pair of pointers, one to the head of the
  10748. + * list and the other to the tail of the list. The elements are doubly
  10749. + * linked so that an arbitrary element can be removed without a need to
  10750. + * traverse the list. New elements can be added to the list before or
  10751. + * after an existing element, at the head of the list, or at the end of
  10752. + * the list. A tail queue may be traversed in either direction.
  10753. + *
  10754. + * A circle queue is headed by a pair of pointers, one to the head of the
  10755. + * list and the other to the tail of the list. The elements are doubly
  10756. + * linked so that an arbitrary element can be removed without a need to
  10757. + * traverse the list. New elements can be added to the list before or after
  10758. + * an existing element, at the head of the list, or at the end of the list.
  10759. + * A circle queue may be traversed in either direction, but has a more
  10760. + * complex end of list detection.
  10761. + *
  10762. + * For details on the use of these macros, see the queue(3) manual page.
  10763. + */
  10764. +
  10765. +/*
  10766. + * Double-linked List.
  10767. + */
  10768. +
  10769. +typedef struct dwc_list_link {
  10770. + struct dwc_list_link *next;
  10771. + struct dwc_list_link *prev;
  10772. +} dwc_list_link_t;
  10773. +
  10774. +#define DWC_LIST_INIT(link) do { \
  10775. + (link)->next = (link); \
  10776. + (link)->prev = (link); \
  10777. +} while (0)
  10778. +
  10779. +#define DWC_LIST_FIRST(link) ((link)->next)
  10780. +#define DWC_LIST_LAST(link) ((link)->prev)
  10781. +#define DWC_LIST_END(link) (link)
  10782. +#define DWC_LIST_NEXT(link) ((link)->next)
  10783. +#define DWC_LIST_PREV(link) ((link)->prev)
  10784. +#define DWC_LIST_EMPTY(link) \
  10785. + (DWC_LIST_FIRST(link) == DWC_LIST_END(link))
  10786. +#define DWC_LIST_ENTRY(link, type, field) \
  10787. + (type *)((uint8_t *)(link) - (size_t)(&((type *)0)->field))
  10788. +
  10789. +#if 0
  10790. +#define DWC_LIST_INSERT_HEAD(list, link) do { \
  10791. + (link)->next = (list)->next; \
  10792. + (link)->prev = (list); \
  10793. + (list)->next->prev = (link); \
  10794. + (list)->next = (link); \
  10795. +} while (0)
  10796. +
  10797. +#define DWC_LIST_INSERT_TAIL(list, link) do { \
  10798. + (link)->next = (list); \
  10799. + (link)->prev = (list)->prev; \
  10800. + (list)->prev->next = (link); \
  10801. + (list)->prev = (link); \
  10802. +} while (0)
  10803. +#else
  10804. +#define DWC_LIST_INSERT_HEAD(list, link) do { \
  10805. + dwc_list_link_t *__next__ = (list)->next; \
  10806. + __next__->prev = (link); \
  10807. + (link)->next = __next__; \
  10808. + (link)->prev = (list); \
  10809. + (list)->next = (link); \
  10810. +} while (0)
  10811. +
  10812. +#define DWC_LIST_INSERT_TAIL(list, link) do { \
  10813. + dwc_list_link_t *__prev__ = (list)->prev; \
  10814. + (list)->prev = (link); \
  10815. + (link)->next = (list); \
  10816. + (link)->prev = __prev__; \
  10817. + __prev__->next = (link); \
  10818. +} while (0)
  10819. +#endif
  10820. +
  10821. +#if 0
  10822. +static inline void __list_add(struct list_head *new,
  10823. + struct list_head *prev,
  10824. + struct list_head *next)
  10825. +{
  10826. + next->prev = new;
  10827. + new->next = next;
  10828. + new->prev = prev;
  10829. + prev->next = new;
  10830. +}
  10831. +
  10832. +static inline void list_add(struct list_head *new, struct list_head *head)
  10833. +{
  10834. + __list_add(new, head, head->next);
  10835. +}
  10836. +
  10837. +static inline void list_add_tail(struct list_head *new, struct list_head *head)
  10838. +{
  10839. + __list_add(new, head->prev, head);
  10840. +}
  10841. +
  10842. +static inline void __list_del(struct list_head * prev, struct list_head * next)
  10843. +{
  10844. + next->prev = prev;
  10845. + prev->next = next;
  10846. +}
  10847. +
  10848. +static inline void list_del(struct list_head *entry)
  10849. +{
  10850. + __list_del(entry->prev, entry->next);
  10851. + entry->next = LIST_POISON1;
  10852. + entry->prev = LIST_POISON2;
  10853. +}
  10854. +#endif
  10855. +
  10856. +#define DWC_LIST_REMOVE(link) do { \
  10857. + (link)->next->prev = (link)->prev; \
  10858. + (link)->prev->next = (link)->next; \
  10859. +} while (0)
  10860. +
  10861. +#define DWC_LIST_REMOVE_INIT(link) do { \
  10862. + DWC_LIST_REMOVE(link); \
  10863. + DWC_LIST_INIT(link); \
  10864. +} while (0)
  10865. +
  10866. +#define DWC_LIST_MOVE_HEAD(list, link) do { \
  10867. + DWC_LIST_REMOVE(link); \
  10868. + DWC_LIST_INSERT_HEAD(list, link); \
  10869. +} while (0)
  10870. +
  10871. +#define DWC_LIST_MOVE_TAIL(list, link) do { \
  10872. + DWC_LIST_REMOVE(link); \
  10873. + DWC_LIST_INSERT_TAIL(list, link); \
  10874. +} while (0)
  10875. +
  10876. +#define DWC_LIST_FOREACH(var, list) \
  10877. + for((var) = DWC_LIST_FIRST(list); \
  10878. + (var) != DWC_LIST_END(list); \
  10879. + (var) = DWC_LIST_NEXT(var))
  10880. +
  10881. +#define DWC_LIST_FOREACH_SAFE(var, var2, list) \
  10882. + for((var) = DWC_LIST_FIRST(list), (var2) = DWC_LIST_NEXT(var); \
  10883. + (var) != DWC_LIST_END(list); \
  10884. + (var) = (var2), (var2) = DWC_LIST_NEXT(var2))
  10885. +
  10886. +#define DWC_LIST_FOREACH_REVERSE(var, list) \
  10887. + for((var) = DWC_LIST_LAST(list); \
  10888. + (var) != DWC_LIST_END(list); \
  10889. + (var) = DWC_LIST_PREV(var))
  10890. +
  10891. +/*
  10892. + * Singly-linked List definitions.
  10893. + */
  10894. +#define DWC_SLIST_HEAD(name, type) \
  10895. +struct name { \
  10896. + struct type *slh_first; /* first element */ \
  10897. +}
  10898. +
  10899. +#define DWC_SLIST_HEAD_INITIALIZER(head) \
  10900. + { NULL }
  10901. +
  10902. +#define DWC_SLIST_ENTRY(type) \
  10903. +struct { \
  10904. + struct type *sle_next; /* next element */ \
  10905. +}
  10906. +
  10907. +/*
  10908. + * Singly-linked List access methods.
  10909. + */
  10910. +#define DWC_SLIST_FIRST(head) ((head)->slh_first)
  10911. +#define DWC_SLIST_END(head) NULL
  10912. +#define DWC_SLIST_EMPTY(head) (SLIST_FIRST(head) == SLIST_END(head))
  10913. +#define DWC_SLIST_NEXT(elm, field) ((elm)->field.sle_next)
  10914. +
  10915. +#define DWC_SLIST_FOREACH(var, head, field) \
  10916. + for((var) = SLIST_FIRST(head); \
  10917. + (var) != SLIST_END(head); \
  10918. + (var) = SLIST_NEXT(var, field))
  10919. +
  10920. +#define DWC_SLIST_FOREACH_PREVPTR(var, varp, head, field) \
  10921. + for((varp) = &SLIST_FIRST((head)); \
  10922. + ((var) = *(varp)) != SLIST_END(head); \
  10923. + (varp) = &SLIST_NEXT((var), field))
  10924. +
  10925. +/*
  10926. + * Singly-linked List functions.
  10927. + */
  10928. +#define DWC_SLIST_INIT(head) { \
  10929. + SLIST_FIRST(head) = SLIST_END(head); \
  10930. +}
  10931. +
  10932. +#define DWC_SLIST_INSERT_AFTER(slistelm, elm, field) do { \
  10933. + (elm)->field.sle_next = (slistelm)->field.sle_next; \
  10934. + (slistelm)->field.sle_next = (elm); \
  10935. +} while (0)
  10936. +
  10937. +#define DWC_SLIST_INSERT_HEAD(head, elm, field) do { \
  10938. + (elm)->field.sle_next = (head)->slh_first; \
  10939. + (head)->slh_first = (elm); \
  10940. +} while (0)
  10941. +
  10942. +#define DWC_SLIST_REMOVE_NEXT(head, elm, field) do { \
  10943. + (elm)->field.sle_next = (elm)->field.sle_next->field.sle_next; \
  10944. +} while (0)
  10945. +
  10946. +#define DWC_SLIST_REMOVE_HEAD(head, field) do { \
  10947. + (head)->slh_first = (head)->slh_first->field.sle_next; \
  10948. +} while (0)
  10949. +
  10950. +#define DWC_SLIST_REMOVE(head, elm, type, field) do { \
  10951. + if ((head)->slh_first == (elm)) { \
  10952. + SLIST_REMOVE_HEAD((head), field); \
  10953. + } \
  10954. + else { \
  10955. + struct type *curelm = (head)->slh_first; \
  10956. + while( curelm->field.sle_next != (elm) ) \
  10957. + curelm = curelm->field.sle_next; \
  10958. + curelm->field.sle_next = \
  10959. + curelm->field.sle_next->field.sle_next; \
  10960. + } \
  10961. +} while (0)
  10962. +
  10963. +/*
  10964. + * Simple queue definitions.
  10965. + */
  10966. +#define DWC_SIMPLEQ_HEAD(name, type) \
  10967. +struct name { \
  10968. + struct type *sqh_first; /* first element */ \
  10969. + struct type **sqh_last; /* addr of last next element */ \
  10970. +}
  10971. +
  10972. +#define DWC_SIMPLEQ_HEAD_INITIALIZER(head) \
  10973. + { NULL, &(head).sqh_first }
  10974. +
  10975. +#define DWC_SIMPLEQ_ENTRY(type) \
  10976. +struct { \
  10977. + struct type *sqe_next; /* next element */ \
  10978. +}
  10979. +
  10980. +/*
  10981. + * Simple queue access methods.
  10982. + */
  10983. +#define DWC_SIMPLEQ_FIRST(head) ((head)->sqh_first)
  10984. +#define DWC_SIMPLEQ_END(head) NULL
  10985. +#define DWC_SIMPLEQ_EMPTY(head) (SIMPLEQ_FIRST(head) == SIMPLEQ_END(head))
  10986. +#define DWC_SIMPLEQ_NEXT(elm, field) ((elm)->field.sqe_next)
  10987. +
  10988. +#define DWC_SIMPLEQ_FOREACH(var, head, field) \
  10989. + for((var) = SIMPLEQ_FIRST(head); \
  10990. + (var) != SIMPLEQ_END(head); \
  10991. + (var) = SIMPLEQ_NEXT(var, field))
  10992. +
  10993. +/*
  10994. + * Simple queue functions.
  10995. + */
  10996. +#define DWC_SIMPLEQ_INIT(head) do { \
  10997. + (head)->sqh_first = NULL; \
  10998. + (head)->sqh_last = &(head)->sqh_first; \
  10999. +} while (0)
  11000. +
  11001. +#define DWC_SIMPLEQ_INSERT_HEAD(head, elm, field) do { \
  11002. + if (((elm)->field.sqe_next = (head)->sqh_first) == NULL) \
  11003. + (head)->sqh_last = &(elm)->field.sqe_next; \
  11004. + (head)->sqh_first = (elm); \
  11005. +} while (0)
  11006. +
  11007. +#define DWC_SIMPLEQ_INSERT_TAIL(head, elm, field) do { \
  11008. + (elm)->field.sqe_next = NULL; \
  11009. + *(head)->sqh_last = (elm); \
  11010. + (head)->sqh_last = &(elm)->field.sqe_next; \
  11011. +} while (0)
  11012. +
  11013. +#define DWC_SIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
  11014. + if (((elm)->field.sqe_next = (listelm)->field.sqe_next) == NULL)\
  11015. + (head)->sqh_last = &(elm)->field.sqe_next; \
  11016. + (listelm)->field.sqe_next = (elm); \
  11017. +} while (0)
  11018. +
  11019. +#define DWC_SIMPLEQ_REMOVE_HEAD(head, field) do { \
  11020. + if (((head)->sqh_first = (head)->sqh_first->field.sqe_next) == NULL) \
  11021. + (head)->sqh_last = &(head)->sqh_first; \
  11022. +} while (0)
  11023. +
  11024. +/*
  11025. + * Tail queue definitions.
  11026. + */
  11027. +#define DWC_TAILQ_HEAD(name, type) \
  11028. +struct name { \
  11029. + struct type *tqh_first; /* first element */ \
  11030. + struct type **tqh_last; /* addr of last next element */ \
  11031. +}
  11032. +
  11033. +#define DWC_TAILQ_HEAD_INITIALIZER(head) \
  11034. + { NULL, &(head).tqh_first }
  11035. +
  11036. +#define DWC_TAILQ_ENTRY(type) \
  11037. +struct { \
  11038. + struct type *tqe_next; /* next element */ \
  11039. + struct type **tqe_prev; /* address of previous next element */ \
  11040. +}
  11041. +
  11042. +/*
  11043. + * tail queue access methods
  11044. + */
  11045. +#define DWC_TAILQ_FIRST(head) ((head)->tqh_first)
  11046. +#define DWC_TAILQ_END(head) NULL
  11047. +#define DWC_TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
  11048. +#define DWC_TAILQ_LAST(head, headname) \
  11049. + (*(((struct headname *)((head)->tqh_last))->tqh_last))
  11050. +/* XXX */
  11051. +#define DWC_TAILQ_PREV(elm, headname, field) \
  11052. + (*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
  11053. +#define DWC_TAILQ_EMPTY(head) \
  11054. + (DWC_TAILQ_FIRST(head) == DWC_TAILQ_END(head))
  11055. +
  11056. +#define DWC_TAILQ_FOREACH(var, head, field) \
  11057. + for ((var) = DWC_TAILQ_FIRST(head); \
  11058. + (var) != DWC_TAILQ_END(head); \
  11059. + (var) = DWC_TAILQ_NEXT(var, field))
  11060. +
  11061. +#define DWC_TAILQ_FOREACH_REVERSE(var, head, headname, field) \
  11062. + for ((var) = DWC_TAILQ_LAST(head, headname); \
  11063. + (var) != DWC_TAILQ_END(head); \
  11064. + (var) = DWC_TAILQ_PREV(var, headname, field))
  11065. +
  11066. +/*
  11067. + * Tail queue functions.
  11068. + */
  11069. +#define DWC_TAILQ_INIT(head) do { \
  11070. + (head)->tqh_first = NULL; \
  11071. + (head)->tqh_last = &(head)->tqh_first; \
  11072. +} while (0)
  11073. +
  11074. +#define DWC_TAILQ_INSERT_HEAD(head, elm, field) do { \
  11075. + if (((elm)->field.tqe_next = (head)->tqh_first) != NULL) \
  11076. + (head)->tqh_first->field.tqe_prev = \
  11077. + &(elm)->field.tqe_next; \
  11078. + else \
  11079. + (head)->tqh_last = &(elm)->field.tqe_next; \
  11080. + (head)->tqh_first = (elm); \
  11081. + (elm)->field.tqe_prev = &(head)->tqh_first; \
  11082. +} while (0)
  11083. +
  11084. +#define DWC_TAILQ_INSERT_TAIL(head, elm, field) do { \
  11085. + (elm)->field.tqe_next = NULL; \
  11086. + (elm)->field.tqe_prev = (head)->tqh_last; \
  11087. + *(head)->tqh_last = (elm); \
  11088. + (head)->tqh_last = &(elm)->field.tqe_next; \
  11089. +} while (0)
  11090. +
  11091. +#define DWC_TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \
  11092. + if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\
  11093. + (elm)->field.tqe_next->field.tqe_prev = \
  11094. + &(elm)->field.tqe_next; \
  11095. + else \
  11096. + (head)->tqh_last = &(elm)->field.tqe_next; \
  11097. + (listelm)->field.tqe_next = (elm); \
  11098. + (elm)->field.tqe_prev = &(listelm)->field.tqe_next; \
  11099. +} while (0)
  11100. +
  11101. +#define DWC_TAILQ_INSERT_BEFORE(listelm, elm, field) do { \
  11102. + (elm)->field.tqe_prev = (listelm)->field.tqe_prev; \
  11103. + (elm)->field.tqe_next = (listelm); \
  11104. + *(listelm)->field.tqe_prev = (elm); \
  11105. + (listelm)->field.tqe_prev = &(elm)->field.tqe_next; \
  11106. +} while (0)
  11107. +
  11108. +#define DWC_TAILQ_REMOVE(head, elm, field) do { \
  11109. + if (((elm)->field.tqe_next) != NULL) \
  11110. + (elm)->field.tqe_next->field.tqe_prev = \
  11111. + (elm)->field.tqe_prev; \
  11112. + else \
  11113. + (head)->tqh_last = (elm)->field.tqe_prev; \
  11114. + *(elm)->field.tqe_prev = (elm)->field.tqe_next; \
  11115. +} while (0)
  11116. +
  11117. +#define DWC_TAILQ_REPLACE(head, elm, elm2, field) do { \
  11118. + if (((elm2)->field.tqe_next = (elm)->field.tqe_next) != NULL) \
  11119. + (elm2)->field.tqe_next->field.tqe_prev = \
  11120. + &(elm2)->field.tqe_next; \
  11121. + else \
  11122. + (head)->tqh_last = &(elm2)->field.tqe_next; \
  11123. + (elm2)->field.tqe_prev = (elm)->field.tqe_prev; \
  11124. + *(elm2)->field.tqe_prev = (elm2); \
  11125. +} while (0)
  11126. +
  11127. +/*
  11128. + * Circular queue definitions.
  11129. + */
  11130. +#define DWC_CIRCLEQ_HEAD(name, type) \
  11131. +struct name { \
  11132. + struct type *cqh_first; /* first element */ \
  11133. + struct type *cqh_last; /* last element */ \
  11134. +}
  11135. +
  11136. +#define DWC_CIRCLEQ_HEAD_INITIALIZER(head) \
  11137. + { DWC_CIRCLEQ_END(&head), DWC_CIRCLEQ_END(&head) }
  11138. +
  11139. +#define DWC_CIRCLEQ_ENTRY(type) \
  11140. +struct { \
  11141. + struct type *cqe_next; /* next element */ \
  11142. + struct type *cqe_prev; /* previous element */ \
  11143. +}
  11144. +
  11145. +/*
  11146. + * Circular queue access methods
  11147. + */
  11148. +#define DWC_CIRCLEQ_FIRST(head) ((head)->cqh_first)
  11149. +#define DWC_CIRCLEQ_LAST(head) ((head)->cqh_last)
  11150. +#define DWC_CIRCLEQ_END(head) ((void *)(head))
  11151. +#define DWC_CIRCLEQ_NEXT(elm, field) ((elm)->field.cqe_next)
  11152. +#define DWC_CIRCLEQ_PREV(elm, field) ((elm)->field.cqe_prev)
  11153. +#define DWC_CIRCLEQ_EMPTY(head) \
  11154. + (DWC_CIRCLEQ_FIRST(head) == DWC_CIRCLEQ_END(head))
  11155. +
  11156. +#define DWC_CIRCLEQ_EMPTY_ENTRY(elm, field) (((elm)->field.cqe_next == NULL) && ((elm)->field.cqe_prev == NULL))
  11157. +
  11158. +#define DWC_CIRCLEQ_FOREACH(var, head, field) \
  11159. + for((var) = DWC_CIRCLEQ_FIRST(head); \
  11160. + (var) != DWC_CIRCLEQ_END(head); \
  11161. + (var) = DWC_CIRCLEQ_NEXT(var, field))
  11162. +
  11163. +#define DWC_CIRCLEQ_FOREACH_SAFE(var, var2, head, field) \
  11164. + for((var) = DWC_CIRCLEQ_FIRST(head), var2 = DWC_CIRCLEQ_NEXT(var, field); \
  11165. + (var) != DWC_CIRCLEQ_END(head); \
  11166. + (var) = var2, var2 = DWC_CIRCLEQ_NEXT(var, field))
  11167. +
  11168. +#define DWC_CIRCLEQ_FOREACH_REVERSE(var, head, field) \
  11169. + for((var) = DWC_CIRCLEQ_LAST(head); \
  11170. + (var) != DWC_CIRCLEQ_END(head); \
  11171. + (var) = DWC_CIRCLEQ_PREV(var, field))
  11172. +
  11173. +/*
  11174. + * Circular queue functions.
  11175. + */
  11176. +#define DWC_CIRCLEQ_INIT(head) do { \
  11177. + (head)->cqh_first = DWC_CIRCLEQ_END(head); \
  11178. + (head)->cqh_last = DWC_CIRCLEQ_END(head); \
  11179. +} while (0)
  11180. +
  11181. +#define DWC_CIRCLEQ_INIT_ENTRY(elm, field) do { \
  11182. + (elm)->field.cqe_next = NULL; \
  11183. + (elm)->field.cqe_prev = NULL; \
  11184. +} while (0)
  11185. +
  11186. +#define DWC_CIRCLEQ_INSERT_AFTER(head, listelm, elm, field) do { \
  11187. + (elm)->field.cqe_next = (listelm)->field.cqe_next; \
  11188. + (elm)->field.cqe_prev = (listelm); \
  11189. + if ((listelm)->field.cqe_next == DWC_CIRCLEQ_END(head)) \
  11190. + (head)->cqh_last = (elm); \
  11191. + else \
  11192. + (listelm)->field.cqe_next->field.cqe_prev = (elm); \
  11193. + (listelm)->field.cqe_next = (elm); \
  11194. +} while (0)
  11195. +
  11196. +#define DWC_CIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) do { \
  11197. + (elm)->field.cqe_next = (listelm); \
  11198. + (elm)->field.cqe_prev = (listelm)->field.cqe_prev; \
  11199. + if ((listelm)->field.cqe_prev == DWC_CIRCLEQ_END(head)) \
  11200. + (head)->cqh_first = (elm); \
  11201. + else \
  11202. + (listelm)->field.cqe_prev->field.cqe_next = (elm); \
  11203. + (listelm)->field.cqe_prev = (elm); \
  11204. +} while (0)
  11205. +
  11206. +#define DWC_CIRCLEQ_INSERT_HEAD(head, elm, field) do { \
  11207. + (elm)->field.cqe_next = (head)->cqh_first; \
  11208. + (elm)->field.cqe_prev = DWC_CIRCLEQ_END(head); \
  11209. + if ((head)->cqh_last == DWC_CIRCLEQ_END(head)) \
  11210. + (head)->cqh_last = (elm); \
  11211. + else \
  11212. + (head)->cqh_first->field.cqe_prev = (elm); \
  11213. + (head)->cqh_first = (elm); \
  11214. +} while (0)
  11215. +
  11216. +#define DWC_CIRCLEQ_INSERT_TAIL(head, elm, field) do { \
  11217. + (elm)->field.cqe_next = DWC_CIRCLEQ_END(head); \
  11218. + (elm)->field.cqe_prev = (head)->cqh_last; \
  11219. + if ((head)->cqh_first == DWC_CIRCLEQ_END(head)) \
  11220. + (head)->cqh_first = (elm); \
  11221. + else \
  11222. + (head)->cqh_last->field.cqe_next = (elm); \
  11223. + (head)->cqh_last = (elm); \
  11224. +} while (0)
  11225. +
  11226. +#define DWC_CIRCLEQ_REMOVE(head, elm, field) do { \
  11227. + if ((elm)->field.cqe_next == DWC_CIRCLEQ_END(head)) \
  11228. + (head)->cqh_last = (elm)->field.cqe_prev; \
  11229. + else \
  11230. + (elm)->field.cqe_next->field.cqe_prev = \
  11231. + (elm)->field.cqe_prev; \
  11232. + if ((elm)->field.cqe_prev == DWC_CIRCLEQ_END(head)) \
  11233. + (head)->cqh_first = (elm)->field.cqe_next; \
  11234. + else \
  11235. + (elm)->field.cqe_prev->field.cqe_next = \
  11236. + (elm)->field.cqe_next; \
  11237. +} while (0)
  11238. +
  11239. +#define DWC_CIRCLEQ_REMOVE_INIT(head, elm, field) do { \
  11240. + DWC_CIRCLEQ_REMOVE(head, elm, field); \
  11241. + DWC_CIRCLEQ_INIT_ENTRY(elm, field); \
  11242. +} while (0)
  11243. +
  11244. +#define DWC_CIRCLEQ_REPLACE(head, elm, elm2, field) do { \
  11245. + if (((elm2)->field.cqe_next = (elm)->field.cqe_next) == \
  11246. + DWC_CIRCLEQ_END(head)) \
  11247. + (head).cqh_last = (elm2); \
  11248. + else \
  11249. + (elm2)->field.cqe_next->field.cqe_prev = (elm2); \
  11250. + if (((elm2)->field.cqe_prev = (elm)->field.cqe_prev) == \
  11251. + DWC_CIRCLEQ_END(head)) \
  11252. + (head).cqh_first = (elm2); \
  11253. + else \
  11254. + (elm2)->field.cqe_prev->field.cqe_next = (elm2); \
  11255. +} while (0)
  11256. +
  11257. +#ifdef __cplusplus
  11258. +}
  11259. +#endif
  11260. +
  11261. +#endif /* _DWC_LIST_H_ */
  11262. --- /dev/null
  11263. +++ b/drivers/usb/host/dwc_common_port/dwc_mem.c
  11264. @@ -0,0 +1,245 @@
  11265. +/* Memory Debugging */
  11266. +#ifdef DWC_DEBUG_MEMORY
  11267. +
  11268. +#include "dwc_os.h"
  11269. +#include "dwc_list.h"
  11270. +
  11271. +struct allocation {
  11272. + void *addr;
  11273. + void *ctx;
  11274. + char *func;
  11275. + int line;
  11276. + uint32_t size;
  11277. + int dma;
  11278. + DWC_CIRCLEQ_ENTRY(allocation) entry;
  11279. +};
  11280. +
  11281. +DWC_CIRCLEQ_HEAD(allocation_queue, allocation);
  11282. +
  11283. +struct allocation_manager {
  11284. + void *mem_ctx;
  11285. + struct allocation_queue allocations;
  11286. +
  11287. + /* statistics */
  11288. + int num;
  11289. + int num_freed;
  11290. + int num_active;
  11291. + uint32_t total;
  11292. + uint32_t cur;
  11293. + uint32_t max;
  11294. +};
  11295. +
  11296. +static struct allocation_manager *manager = NULL;
  11297. +
  11298. +static int add_allocation(void *ctx, uint32_t size, char const *func, int line, void *addr,
  11299. + int dma)
  11300. +{
  11301. + struct allocation *a;
  11302. +
  11303. + DWC_ASSERT(manager != NULL, "manager not allocated");
  11304. +
  11305. + a = __DWC_ALLOC_ATOMIC(manager->mem_ctx, sizeof(*a));
  11306. + if (!a) {
  11307. + return -DWC_E_NO_MEMORY;
  11308. + }
  11309. +
  11310. + a->func = __DWC_ALLOC_ATOMIC(manager->mem_ctx, DWC_STRLEN(func) + 1);
  11311. + if (!a->func) {
  11312. + __DWC_FREE(manager->mem_ctx, a);
  11313. + return -DWC_E_NO_MEMORY;
  11314. + }
  11315. +
  11316. + DWC_MEMCPY(a->func, func, DWC_STRLEN(func) + 1);
  11317. + a->addr = addr;
  11318. + a->ctx = ctx;
  11319. + a->line = line;
  11320. + a->size = size;
  11321. + a->dma = dma;
  11322. + DWC_CIRCLEQ_INSERT_TAIL(&manager->allocations, a, entry);
  11323. +
  11324. + /* Update stats */
  11325. + manager->num++;
  11326. + manager->num_active++;
  11327. + manager->total += size;
  11328. + manager->cur += size;
  11329. +
  11330. + if (manager->max < manager->cur) {
  11331. + manager->max = manager->cur;
  11332. + }
  11333. +
  11334. + return 0;
  11335. +}
  11336. +
  11337. +static struct allocation *find_allocation(void *ctx, void *addr)
  11338. +{
  11339. + struct allocation *a;
  11340. +
  11341. + DWC_CIRCLEQ_FOREACH(a, &manager->allocations, entry) {
  11342. + if (a->ctx == ctx && a->addr == addr) {
  11343. + return a;
  11344. + }
  11345. + }
  11346. +
  11347. + return NULL;
  11348. +}
  11349. +
  11350. +static void free_allocation(void *ctx, void *addr, char const *func, int line)
  11351. +{
  11352. + struct allocation *a = find_allocation(ctx, addr);
  11353. +
  11354. + if (!a) {
  11355. + DWC_ASSERT(0,
  11356. + "Free of address %p that was never allocated or already freed %s:%d",
  11357. + addr, func, line);
  11358. + return;
  11359. + }
  11360. +
  11361. + DWC_CIRCLEQ_REMOVE(&manager->allocations, a, entry);
  11362. +
  11363. + manager->num_active--;
  11364. + manager->num_freed++;
  11365. + manager->cur -= a->size;
  11366. + __DWC_FREE(manager->mem_ctx, a->func);
  11367. + __DWC_FREE(manager->mem_ctx, a);
  11368. +}
  11369. +
  11370. +int dwc_memory_debug_start(void *mem_ctx)
  11371. +{
  11372. + DWC_ASSERT(manager == NULL, "Memory debugging has already started\n");
  11373. +
  11374. + if (manager) {
  11375. + return -DWC_E_BUSY;
  11376. + }
  11377. +
  11378. + manager = __DWC_ALLOC(mem_ctx, sizeof(*manager));
  11379. + if (!manager) {
  11380. + return -DWC_E_NO_MEMORY;
  11381. + }
  11382. +
  11383. + DWC_CIRCLEQ_INIT(&manager->allocations);
  11384. + manager->mem_ctx = mem_ctx;
  11385. + manager->num = 0;
  11386. + manager->num_freed = 0;
  11387. + manager->num_active = 0;
  11388. + manager->total = 0;
  11389. + manager->cur = 0;
  11390. + manager->max = 0;
  11391. +
  11392. + return 0;
  11393. +}
  11394. +
  11395. +void dwc_memory_debug_stop(void)
  11396. +{
  11397. + struct allocation *a;
  11398. +
  11399. + dwc_memory_debug_report();
  11400. +
  11401. + DWC_CIRCLEQ_FOREACH(a, &manager->allocations, entry) {
  11402. + DWC_ERROR("Memory leaked from %s:%d\n", a->func, a->line);
  11403. + free_allocation(a->ctx, a->addr, NULL, -1);
  11404. + }
  11405. +
  11406. + __DWC_FREE(manager->mem_ctx, manager);
  11407. +}
  11408. +
  11409. +void dwc_memory_debug_report(void)
  11410. +{
  11411. + struct allocation *a;
  11412. +
  11413. + DWC_PRINTF("\n\n\n----------------- Memory Debugging Report -----------------\n\n");
  11414. + DWC_PRINTF("Num Allocations = %d\n", manager->num);
  11415. + DWC_PRINTF("Freed = %d\n", manager->num_freed);
  11416. + DWC_PRINTF("Active = %d\n", manager->num_active);
  11417. + DWC_PRINTF("Current Memory Used = %d\n", manager->cur);
  11418. + DWC_PRINTF("Total Memory Used = %d\n", manager->total);
  11419. + DWC_PRINTF("Maximum Memory Used at Once = %d\n", manager->max);
  11420. + DWC_PRINTF("Unfreed allocations:\n");
  11421. +
  11422. + DWC_CIRCLEQ_FOREACH(a, &manager->allocations, entry) {
  11423. + DWC_PRINTF(" addr=%p, size=%d from %s:%d, DMA=%d\n",
  11424. + a->addr, a->size, a->func, a->line, a->dma);
  11425. + }
  11426. +}
  11427. +
  11428. +/* The replacement functions */
  11429. +void *dwc_alloc_debug(void *mem_ctx, uint32_t size, char const *func, int line)
  11430. +{
  11431. + void *addr = __DWC_ALLOC(mem_ctx, size);
  11432. +
  11433. + if (!addr) {
  11434. + return NULL;
  11435. + }
  11436. +
  11437. + if (add_allocation(mem_ctx, size, func, line, addr, 0)) {
  11438. + __DWC_FREE(mem_ctx, addr);
  11439. + return NULL;
  11440. + }
  11441. +
  11442. + return addr;
  11443. +}
  11444. +
  11445. +void *dwc_alloc_atomic_debug(void *mem_ctx, uint32_t size, char const *func,
  11446. + int line)
  11447. +{
  11448. + void *addr = __DWC_ALLOC_ATOMIC(mem_ctx, size);
  11449. +
  11450. + if (!addr) {
  11451. + return NULL;
  11452. + }
  11453. +
  11454. + if (add_allocation(mem_ctx, size, func, line, addr, 0)) {
  11455. + __DWC_FREE(mem_ctx, addr);
  11456. + return NULL;
  11457. + }
  11458. +
  11459. + return addr;
  11460. +}
  11461. +
  11462. +void dwc_free_debug(void *mem_ctx, void *addr, char const *func, int line)
  11463. +{
  11464. + free_allocation(mem_ctx, addr, func, line);
  11465. + __DWC_FREE(mem_ctx, addr);
  11466. +}
  11467. +
  11468. +void *dwc_dma_alloc_debug(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr,
  11469. + char const *func, int line)
  11470. +{
  11471. + void *addr = __DWC_DMA_ALLOC(dma_ctx, size, dma_addr);
  11472. +
  11473. + if (!addr) {
  11474. + return NULL;
  11475. + }
  11476. +
  11477. + if (add_allocation(dma_ctx, size, func, line, addr, 1)) {
  11478. + __DWC_DMA_FREE(dma_ctx, size, addr, *dma_addr);
  11479. + return NULL;
  11480. + }
  11481. +
  11482. + return addr;
  11483. +}
  11484. +
  11485. +void *dwc_dma_alloc_atomic_debug(void *dma_ctx, uint32_t size,
  11486. + dwc_dma_t *dma_addr, char const *func, int line)
  11487. +{
  11488. + void *addr = __DWC_DMA_ALLOC_ATOMIC(dma_ctx, size, dma_addr);
  11489. +
  11490. + if (!addr) {
  11491. + return NULL;
  11492. + }
  11493. +
  11494. + if (add_allocation(dma_ctx, size, func, line, addr, 1)) {
  11495. + __DWC_DMA_FREE(dma_ctx, size, addr, *dma_addr);
  11496. + return NULL;
  11497. + }
  11498. +
  11499. + return addr;
  11500. +}
  11501. +
  11502. +void dwc_dma_free_debug(void *dma_ctx, uint32_t size, void *virt_addr,
  11503. + dwc_dma_t dma_addr, char const *func, int line)
  11504. +{
  11505. + free_allocation(dma_ctx, virt_addr, func, line);
  11506. + __DWC_DMA_FREE(dma_ctx, size, virt_addr, dma_addr);
  11507. +}
  11508. +
  11509. +#endif /* DWC_DEBUG_MEMORY */
  11510. --- /dev/null
  11511. +++ b/drivers/usb/host/dwc_common_port/dwc_modpow.c
  11512. @@ -0,0 +1,636 @@
  11513. +/* Bignum routines adapted from PUTTY sources. PuTTY copyright notice follows.
  11514. + *
  11515. + * PuTTY is copyright 1997-2007 Simon Tatham.
  11516. + *
  11517. + * Portions copyright Robert de Bath, Joris van Rantwijk, Delian
  11518. + * Delchev, Andreas Schultz, Jeroen Massar, Wez Furlong, Nicolas Barry,
  11519. + * Justin Bradford, Ben Harris, Malcolm Smith, Ahmad Khalifa, Markus
  11520. + * Kuhn, and CORE SDI S.A.
  11521. + *
  11522. + * Permission is hereby granted, free of charge, to any person
  11523. + * obtaining a copy of this software and associated documentation files
  11524. + * (the "Software"), to deal in the Software without restriction,
  11525. + * including without limitation the rights to use, copy, modify, merge,
  11526. + * publish, distribute, sublicense, and/or sell copies of the Software,
  11527. + * and to permit persons to whom the Software is furnished to do so,
  11528. + * subject to the following conditions:
  11529. + *
  11530. + * The above copyright notice and this permission notice shall be
  11531. + * included in all copies or substantial portions of the Software.
  11532. +
  11533. + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  11534. + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  11535. + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  11536. + * NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE
  11537. + * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  11538. + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  11539. + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  11540. + *
  11541. + */
  11542. +#ifdef DWC_CRYPTOLIB
  11543. +
  11544. +#ifndef CONFIG_MACH_IPMATE
  11545. +
  11546. +#include "dwc_modpow.h"
  11547. +
  11548. +#define BIGNUM_INT_MASK 0xFFFFFFFFUL
  11549. +#define BIGNUM_TOP_BIT 0x80000000UL
  11550. +#define BIGNUM_INT_BITS 32
  11551. +
  11552. +
  11553. +static void *snmalloc(void *mem_ctx, size_t n, size_t size)
  11554. +{
  11555. + void *p;
  11556. + size *= n;
  11557. + if (size == 0) size = 1;
  11558. + p = dwc_alloc(mem_ctx, size);
  11559. + return p;
  11560. +}
  11561. +
  11562. +#define snewn(ctx, n, type) ((type *)snmalloc((ctx), (n), sizeof(type)))
  11563. +#define sfree dwc_free
  11564. +
  11565. +/*
  11566. + * Usage notes:
  11567. + * * Do not call the DIVMOD_WORD macro with expressions such as array
  11568. + * subscripts, as some implementations object to this (see below).
  11569. + * * Note that none of the division methods below will cope if the
  11570. + * quotient won't fit into BIGNUM_INT_BITS. Callers should be careful
  11571. + * to avoid this case.
  11572. + * If this condition occurs, in the case of the x86 DIV instruction,
  11573. + * an overflow exception will occur, which (according to a correspondent)
  11574. + * will manifest on Windows as something like
  11575. + * 0xC0000095: Integer overflow
  11576. + * The C variant won't give the right answer, either.
  11577. + */
  11578. +
  11579. +#define MUL_WORD(w1, w2) ((BignumDblInt)w1 * w2)
  11580. +
  11581. +#if defined __GNUC__ && defined __i386__
  11582. +#define DIVMOD_WORD(q, r, hi, lo, w) \
  11583. + __asm__("div %2" : \
  11584. + "=d" (r), "=a" (q) : \
  11585. + "r" (w), "d" (hi), "a" (lo))
  11586. +#else
  11587. +#define DIVMOD_WORD(q, r, hi, lo, w) do { \
  11588. + BignumDblInt n = (((BignumDblInt)hi) << BIGNUM_INT_BITS) | lo; \
  11589. + q = n / w; \
  11590. + r = n % w; \
  11591. +} while (0)
  11592. +#endif
  11593. +
  11594. +// q = n / w;
  11595. +// r = n % w;
  11596. +
  11597. +#define BIGNUM_INT_BYTES (BIGNUM_INT_BITS / 8)
  11598. +
  11599. +#define BIGNUM_INTERNAL
  11600. +
  11601. +static Bignum newbn(void *mem_ctx, int length)
  11602. +{
  11603. + Bignum b = snewn(mem_ctx, length + 1, BignumInt);
  11604. + //if (!b)
  11605. + //abort(); /* FIXME */
  11606. + DWC_MEMSET(b, 0, (length + 1) * sizeof(*b));
  11607. + b[0] = length;
  11608. + return b;
  11609. +}
  11610. +
  11611. +void freebn(void *mem_ctx, Bignum b)
  11612. +{
  11613. + /*
  11614. + * Burn the evidence, just in case.
  11615. + */
  11616. + DWC_MEMSET(b, 0, sizeof(b[0]) * (b[0] + 1));
  11617. + sfree(mem_ctx, b);
  11618. +}
  11619. +
  11620. +/*
  11621. + * Compute c = a * b.
  11622. + * Input is in the first len words of a and b.
  11623. + * Result is returned in the first 2*len words of c.
  11624. + */
  11625. +static void internal_mul(BignumInt *a, BignumInt *b,
  11626. + BignumInt *c, int len)
  11627. +{
  11628. + int i, j;
  11629. + BignumDblInt t;
  11630. +
  11631. + for (j = 0; j < 2 * len; j++)
  11632. + c[j] = 0;
  11633. +
  11634. + for (i = len - 1; i >= 0; i--) {
  11635. + t = 0;
  11636. + for (j = len - 1; j >= 0; j--) {
  11637. + t += MUL_WORD(a[i], (BignumDblInt) b[j]);
  11638. + t += (BignumDblInt) c[i + j + 1];
  11639. + c[i + j + 1] = (BignumInt) t;
  11640. + t = t >> BIGNUM_INT_BITS;
  11641. + }
  11642. + c[i] = (BignumInt) t;
  11643. + }
  11644. +}
  11645. +
  11646. +static void internal_add_shifted(BignumInt *number,
  11647. + unsigned n, int shift)
  11648. +{
  11649. + int word = 1 + (shift / BIGNUM_INT_BITS);
  11650. + int bshift = shift % BIGNUM_INT_BITS;
  11651. + BignumDblInt addend;
  11652. +
  11653. + addend = (BignumDblInt)n << bshift;
  11654. +
  11655. + while (addend) {
  11656. + addend += number[word];
  11657. + number[word] = (BignumInt) addend & BIGNUM_INT_MASK;
  11658. + addend >>= BIGNUM_INT_BITS;
  11659. + word++;
  11660. + }
  11661. +}
  11662. +
  11663. +/*
  11664. + * Compute a = a % m.
  11665. + * Input in first alen words of a and first mlen words of m.
  11666. + * Output in first alen words of a
  11667. + * (of which first alen-mlen words will be zero).
  11668. + * The MSW of m MUST have its high bit set.
  11669. + * Quotient is accumulated in the `quotient' array, which is a Bignum
  11670. + * rather than the internal bigendian format. Quotient parts are shifted
  11671. + * left by `qshift' before adding into quot.
  11672. + */
  11673. +static void internal_mod(BignumInt *a, int alen,
  11674. + BignumInt *m, int mlen,
  11675. + BignumInt *quot, int qshift)
  11676. +{
  11677. + BignumInt m0, m1;
  11678. + unsigned int h;
  11679. + int i, k;
  11680. +
  11681. + m0 = m[0];
  11682. + if (mlen > 1)
  11683. + m1 = m[1];
  11684. + else
  11685. + m1 = 0;
  11686. +
  11687. + for (i = 0; i <= alen - mlen; i++) {
  11688. + BignumDblInt t;
  11689. + unsigned int q, r, c, ai1;
  11690. +
  11691. + if (i == 0) {
  11692. + h = 0;
  11693. + } else {
  11694. + h = a[i - 1];
  11695. + a[i - 1] = 0;
  11696. + }
  11697. +
  11698. + if (i == alen - 1)
  11699. + ai1 = 0;
  11700. + else
  11701. + ai1 = a[i + 1];
  11702. +
  11703. + /* Find q = h:a[i] / m0 */
  11704. + if (h >= m0) {
  11705. + /*
  11706. + * Special case.
  11707. + *
  11708. + * To illustrate it, suppose a BignumInt is 8 bits, and
  11709. + * we are dividing (say) A1:23:45:67 by A1:B2:C3. Then
  11710. + * our initial division will be 0xA123 / 0xA1, which
  11711. + * will give a quotient of 0x100 and a divide overflow.
  11712. + * However, the invariants in this division algorithm
  11713. + * are not violated, since the full number A1:23:... is
  11714. + * _less_ than the quotient prefix A1:B2:... and so the
  11715. + * following correction loop would have sorted it out.
  11716. + *
  11717. + * In this situation we set q to be the largest
  11718. + * quotient we _can_ stomach (0xFF, of course).
  11719. + */
  11720. + q = BIGNUM_INT_MASK;
  11721. + } else {
  11722. + /* Macro doesn't want an array subscript expression passed
  11723. + * into it (see definition), so use a temporary. */
  11724. + BignumInt tmplo = a[i];
  11725. + DIVMOD_WORD(q, r, h, tmplo, m0);
  11726. +
  11727. + /* Refine our estimate of q by looking at
  11728. + h:a[i]:a[i+1] / m0:m1 */
  11729. + t = MUL_WORD(m1, q);
  11730. + if (t > ((BignumDblInt) r << BIGNUM_INT_BITS) + ai1) {
  11731. + q--;
  11732. + t -= m1;
  11733. + r = (r + m0) & BIGNUM_INT_MASK; /* overflow? */
  11734. + if (r >= (BignumDblInt) m0 &&
  11735. + t > ((BignumDblInt) r << BIGNUM_INT_BITS) + ai1) q--;
  11736. + }
  11737. + }
  11738. +
  11739. + /* Subtract q * m from a[i...] */
  11740. + c = 0;
  11741. + for (k = mlen - 1; k >= 0; k--) {
  11742. + t = MUL_WORD(q, m[k]);
  11743. + t += c;
  11744. + c = (unsigned)(t >> BIGNUM_INT_BITS);
  11745. + if ((BignumInt) t > a[i + k])
  11746. + c++;
  11747. + a[i + k] -= (BignumInt) t;
  11748. + }
  11749. +
  11750. + /* Add back m in case of borrow */
  11751. + if (c != h) {
  11752. + t = 0;
  11753. + for (k = mlen - 1; k >= 0; k--) {
  11754. + t += m[k];
  11755. + t += a[i + k];
  11756. + a[i + k] = (BignumInt) t;
  11757. + t = t >> BIGNUM_INT_BITS;
  11758. + }
  11759. + q--;
  11760. + }
  11761. + if (quot)
  11762. + internal_add_shifted(quot, q, qshift + BIGNUM_INT_BITS * (alen - mlen - i));
  11763. + }
  11764. +}
  11765. +
  11766. +/*
  11767. + * Compute p % mod.
  11768. + * The most significant word of mod MUST be non-zero.
  11769. + * We assume that the result array is the same size as the mod array.
  11770. + * We optionally write out a quotient if `quotient' is non-NULL.
  11771. + * We can avoid writing out the result if `result' is NULL.
  11772. + */
  11773. +void bigdivmod(void *mem_ctx, Bignum p, Bignum mod, Bignum result, Bignum quotient)
  11774. +{
  11775. + BignumInt *n, *m;
  11776. + int mshift;
  11777. + int plen, mlen, i, j;
  11778. +
  11779. + /* Allocate m of size mlen, copy mod to m */
  11780. + /* We use big endian internally */
  11781. + mlen = mod[0];
  11782. + m = snewn(mem_ctx, mlen, BignumInt);
  11783. + //if (!m)
  11784. + //abort(); /* FIXME */
  11785. + for (j = 0; j < mlen; j++)
  11786. + m[j] = mod[mod[0] - j];
  11787. +
  11788. + /* Shift m left to make msb bit set */
  11789. + for (mshift = 0; mshift < BIGNUM_INT_BITS-1; mshift++)
  11790. + if ((m[0] << mshift) & BIGNUM_TOP_BIT)
  11791. + break;
  11792. + if (mshift) {
  11793. + for (i = 0; i < mlen - 1; i++)
  11794. + m[i] = (m[i] << mshift) | (m[i + 1] >> (BIGNUM_INT_BITS - mshift));
  11795. + m[mlen - 1] = m[mlen - 1] << mshift;
  11796. + }
  11797. +
  11798. + plen = p[0];
  11799. + /* Ensure plen > mlen */
  11800. + if (plen <= mlen)
  11801. + plen = mlen + 1;
  11802. +
  11803. + /* Allocate n of size plen, copy p to n */
  11804. + n = snewn(mem_ctx, plen, BignumInt);
  11805. + //if (!n)
  11806. + //abort(); /* FIXME */
  11807. + for (j = 0; j < plen; j++)
  11808. + n[j] = 0;
  11809. + for (j = 1; j <= (int)p[0]; j++)
  11810. + n[plen - j] = p[j];
  11811. +
  11812. + /* Main computation */
  11813. + internal_mod(n, plen, m, mlen, quotient, mshift);
  11814. +
  11815. + /* Fixup result in case the modulus was shifted */
  11816. + if (mshift) {
  11817. + for (i = plen - mlen - 1; i < plen - 1; i++)
  11818. + n[i] = (n[i] << mshift) | (n[i + 1] >> (BIGNUM_INT_BITS - mshift));
  11819. + n[plen - 1] = n[plen - 1] << mshift;
  11820. + internal_mod(n, plen, m, mlen, quotient, 0);
  11821. + for (i = plen - 1; i >= plen - mlen; i--)
  11822. + n[i] = (n[i] >> mshift) | (n[i - 1] << (BIGNUM_INT_BITS - mshift));
  11823. + }
  11824. +
  11825. + /* Copy result to buffer */
  11826. + if (result) {
  11827. + for (i = 1; i <= (int)result[0]; i++) {
  11828. + int j = plen - i;
  11829. + result[i] = j >= 0 ? n[j] : 0;
  11830. + }
  11831. + }
  11832. +
  11833. + /* Free temporary arrays */
  11834. + for (i = 0; i < mlen; i++)
  11835. + m[i] = 0;
  11836. + sfree(mem_ctx, m);
  11837. + for (i = 0; i < plen; i++)
  11838. + n[i] = 0;
  11839. + sfree(mem_ctx, n);
  11840. +}
  11841. +
  11842. +/*
  11843. + * Simple remainder.
  11844. + */
  11845. +Bignum bigmod(void *mem_ctx, Bignum a, Bignum b)
  11846. +{
  11847. + Bignum r = newbn(mem_ctx, b[0]);
  11848. + bigdivmod(mem_ctx, a, b, r, NULL);
  11849. + return r;
  11850. +}
  11851. +
  11852. +/*
  11853. + * Compute (base ^ exp) % mod.
  11854. + */
  11855. +Bignum dwc_modpow(void *mem_ctx, Bignum base_in, Bignum exp, Bignum mod)
  11856. +{
  11857. + BignumInt *a, *b, *n, *m;
  11858. + int mshift;
  11859. + int mlen, i, j;
  11860. + Bignum base, result;
  11861. +
  11862. + /*
  11863. + * The most significant word of mod needs to be non-zero. It
  11864. + * should already be, but let's make sure.
  11865. + */
  11866. + //assert(mod[mod[0]] != 0);
  11867. +
  11868. + /*
  11869. + * Make sure the base is smaller than the modulus, by reducing
  11870. + * it modulo the modulus if not.
  11871. + */
  11872. + base = bigmod(mem_ctx, base_in, mod);
  11873. +
  11874. + /* Allocate m of size mlen, copy mod to m */
  11875. + /* We use big endian internally */
  11876. + mlen = mod[0];
  11877. + m = snewn(mem_ctx, mlen, BignumInt);
  11878. + //if (!m)
  11879. + //abort(); /* FIXME */
  11880. + for (j = 0; j < mlen; j++)
  11881. + m[j] = mod[mod[0] - j];
  11882. +
  11883. + /* Shift m left to make msb bit set */
  11884. + for (mshift = 0; mshift < BIGNUM_INT_BITS - 1; mshift++)
  11885. + if ((m[0] << mshift) & BIGNUM_TOP_BIT)
  11886. + break;
  11887. + if (mshift) {
  11888. + for (i = 0; i < mlen - 1; i++)
  11889. + m[i] =
  11890. + (m[i] << mshift) | (m[i + 1] >>
  11891. + (BIGNUM_INT_BITS - mshift));
  11892. + m[mlen - 1] = m[mlen - 1] << mshift;
  11893. + }
  11894. +
  11895. + /* Allocate n of size mlen, copy base to n */
  11896. + n = snewn(mem_ctx, mlen, BignumInt);
  11897. + //if (!n)
  11898. + //abort(); /* FIXME */
  11899. + i = mlen - base[0];
  11900. + for (j = 0; j < i; j++)
  11901. + n[j] = 0;
  11902. + for (j = 0; j < base[0]; j++)
  11903. + n[i + j] = base[base[0] - j];
  11904. +
  11905. + /* Allocate a and b of size 2*mlen. Set a = 1 */
  11906. + a = snewn(mem_ctx, 2 * mlen, BignumInt);
  11907. + //if (!a)
  11908. + //abort(); /* FIXME */
  11909. + b = snewn(mem_ctx, 2 * mlen, BignumInt);
  11910. + //if (!b)
  11911. + //abort(); /* FIXME */
  11912. + for (i = 0; i < 2 * mlen; i++)
  11913. + a[i] = 0;
  11914. + a[2 * mlen - 1] = 1;
  11915. +
  11916. + /* Skip leading zero bits of exp. */
  11917. + i = 0;
  11918. + j = BIGNUM_INT_BITS - 1;
  11919. + while (i < exp[0] && (exp[exp[0] - i] & (1 << j)) == 0) {
  11920. + j--;
  11921. + if (j < 0) {
  11922. + i++;
  11923. + j = BIGNUM_INT_BITS - 1;
  11924. + }
  11925. + }
  11926. +
  11927. + /* Main computation */
  11928. + while (i < exp[0]) {
  11929. + while (j >= 0) {
  11930. + internal_mul(a + mlen, a + mlen, b, mlen);
  11931. + internal_mod(b, mlen * 2, m, mlen, NULL, 0);
  11932. + if ((exp[exp[0] - i] & (1 << j)) != 0) {
  11933. + internal_mul(b + mlen, n, a, mlen);
  11934. + internal_mod(a, mlen * 2, m, mlen, NULL, 0);
  11935. + } else {
  11936. + BignumInt *t;
  11937. + t = a;
  11938. + a = b;
  11939. + b = t;
  11940. + }
  11941. + j--;
  11942. + }
  11943. + i++;
  11944. + j = BIGNUM_INT_BITS - 1;
  11945. + }
  11946. +
  11947. + /* Fixup result in case the modulus was shifted */
  11948. + if (mshift) {
  11949. + for (i = mlen - 1; i < 2 * mlen - 1; i++)
  11950. + a[i] =
  11951. + (a[i] << mshift) | (a[i + 1] >>
  11952. + (BIGNUM_INT_BITS - mshift));
  11953. + a[2 * mlen - 1] = a[2 * mlen - 1] << mshift;
  11954. + internal_mod(a, mlen * 2, m, mlen, NULL, 0);
  11955. + for (i = 2 * mlen - 1; i >= mlen; i--)
  11956. + a[i] =
  11957. + (a[i] >> mshift) | (a[i - 1] <<
  11958. + (BIGNUM_INT_BITS - mshift));
  11959. + }
  11960. +
  11961. + /* Copy result to buffer */
  11962. + result = newbn(mem_ctx, mod[0]);
  11963. + for (i = 0; i < mlen; i++)
  11964. + result[result[0] - i] = a[i + mlen];
  11965. + while (result[0] > 1 && result[result[0]] == 0)
  11966. + result[0]--;
  11967. +
  11968. + /* Free temporary arrays */
  11969. + for (i = 0; i < 2 * mlen; i++)
  11970. + a[i] = 0;
  11971. + sfree(mem_ctx, a);
  11972. + for (i = 0; i < 2 * mlen; i++)
  11973. + b[i] = 0;
  11974. + sfree(mem_ctx, b);
  11975. + for (i = 0; i < mlen; i++)
  11976. + m[i] = 0;
  11977. + sfree(mem_ctx, m);
  11978. + for (i = 0; i < mlen; i++)
  11979. + n[i] = 0;
  11980. + sfree(mem_ctx, n);
  11981. +
  11982. + freebn(mem_ctx, base);
  11983. +
  11984. + return result;
  11985. +}
  11986. +
  11987. +
  11988. +#ifdef UNITTEST
  11989. +
  11990. +static __u32 dh_p[] = {
  11991. + 96,
  11992. + 0xFFFFFFFF,
  11993. + 0xFFFFFFFF,
  11994. + 0xA93AD2CA,
  11995. + 0x4B82D120,
  11996. + 0xE0FD108E,
  11997. + 0x43DB5BFC,
  11998. + 0x74E5AB31,
  11999. + 0x08E24FA0,
  12000. + 0xBAD946E2,
  12001. + 0x770988C0,
  12002. + 0x7A615D6C,
  12003. + 0xBBE11757,
  12004. + 0x177B200C,
  12005. + 0x521F2B18,
  12006. + 0x3EC86A64,
  12007. + 0xD8760273,
  12008. + 0xD98A0864,
  12009. + 0xF12FFA06,
  12010. + 0x1AD2EE6B,
  12011. + 0xCEE3D226,
  12012. + 0x4A25619D,
  12013. + 0x1E8C94E0,
  12014. + 0xDB0933D7,
  12015. + 0xABF5AE8C,
  12016. + 0xA6E1E4C7,
  12017. + 0xB3970F85,
  12018. + 0x5D060C7D,
  12019. + 0x8AEA7157,
  12020. + 0x58DBEF0A,
  12021. + 0xECFB8504,
  12022. + 0xDF1CBA64,
  12023. + 0xA85521AB,
  12024. + 0x04507A33,
  12025. + 0xAD33170D,
  12026. + 0x8AAAC42D,
  12027. + 0x15728E5A,
  12028. + 0x98FA0510,
  12029. + 0x15D22618,
  12030. + 0xEA956AE5,
  12031. + 0x3995497C,
  12032. + 0x95581718,
  12033. + 0xDE2BCBF6,
  12034. + 0x6F4C52C9,
  12035. + 0xB5C55DF0,
  12036. + 0xEC07A28F,
  12037. + 0x9B2783A2,
  12038. + 0x180E8603,
  12039. + 0xE39E772C,
  12040. + 0x2E36CE3B,
  12041. + 0x32905E46,
  12042. + 0xCA18217C,
  12043. + 0xF1746C08,
  12044. + 0x4ABC9804,
  12045. + 0x670C354E,
  12046. + 0x7096966D,
  12047. + 0x9ED52907,
  12048. + 0x208552BB,
  12049. + 0x1C62F356,
  12050. + 0xDCA3AD96,
  12051. + 0x83655D23,
  12052. + 0xFD24CF5F,
  12053. + 0x69163FA8,
  12054. + 0x1C55D39A,
  12055. + 0x98DA4836,
  12056. + 0xA163BF05,
  12057. + 0xC2007CB8,
  12058. + 0xECE45B3D,
  12059. + 0x49286651,
  12060. + 0x7C4B1FE6,
  12061. + 0xAE9F2411,
  12062. + 0x5A899FA5,
  12063. + 0xEE386BFB,
  12064. + 0xF406B7ED,
  12065. + 0x0BFF5CB6,
  12066. + 0xA637ED6B,
  12067. + 0xF44C42E9,
  12068. + 0x625E7EC6,
  12069. + 0xE485B576,
  12070. + 0x6D51C245,
  12071. + 0x4FE1356D,
  12072. + 0xF25F1437,
  12073. + 0x302B0A6D,
  12074. + 0xCD3A431B,
  12075. + 0xEF9519B3,
  12076. + 0x8E3404DD,
  12077. + 0x514A0879,
  12078. + 0x3B139B22,
  12079. + 0x020BBEA6,
  12080. + 0x8A67CC74,
  12081. + 0x29024E08,
  12082. + 0x80DC1CD1,
  12083. + 0xC4C6628B,
  12084. + 0x2168C234,
  12085. + 0xC90FDAA2,
  12086. + 0xFFFFFFFF,
  12087. + 0xFFFFFFFF,
  12088. +};
  12089. +
  12090. +static __u32 dh_a[] = {
  12091. + 8,
  12092. + 0xdf367516,
  12093. + 0x86459caa,
  12094. + 0xe2d459a4,
  12095. + 0xd910dae0,
  12096. + 0x8a8b5e37,
  12097. + 0x67ab31c6,
  12098. + 0xf0b55ea9,
  12099. + 0x440051d6,
  12100. +};
  12101. +
  12102. +static __u32 dh_b[] = {
  12103. + 8,
  12104. + 0xded92656,
  12105. + 0xe07a048a,
  12106. + 0x6fa452cd,
  12107. + 0x2df89d30,
  12108. + 0xc75f1b0f,
  12109. + 0x8ce3578f,
  12110. + 0x7980a324,
  12111. + 0x5daec786,
  12112. +};
  12113. +
  12114. +static __u32 dh_g[] = {
  12115. + 1,
  12116. + 2,
  12117. +};
  12118. +
  12119. +int main(void)
  12120. +{
  12121. + int i;
  12122. + __u32 *k;
  12123. + k = dwc_modpow(NULL, dh_g, dh_a, dh_p);
  12124. +
  12125. + printf("\n\n");
  12126. + for (i=0; i<k[0]; i++) {
  12127. + __u32 word32 = k[k[0] - i];
  12128. + __u16 l = word32 & 0xffff;
  12129. + __u16 m = (word32 & 0xffff0000) >> 16;
  12130. + printf("%04x %04x ", m, l);
  12131. + if (!((i + 1)%13)) printf("\n");
  12132. + }
  12133. + printf("\n\n");
  12134. +
  12135. + if ((k[0] == 0x60) && (k[1] == 0x28e490e5) && (k[0x60] == 0x5a0d3d4e)) {
  12136. + printf("PASS\n\n");
  12137. + }
  12138. + else {
  12139. + printf("FAIL\n\n");
  12140. + }
  12141. +
  12142. +}
  12143. +
  12144. +#endif /* UNITTEST */
  12145. +
  12146. +#endif /* CONFIG_MACH_IPMATE */
  12147. +
  12148. +#endif /*DWC_CRYPTOLIB */
  12149. --- /dev/null
  12150. +++ b/drivers/usb/host/dwc_common_port/dwc_modpow.h
  12151. @@ -0,0 +1,34 @@
  12152. +/*
  12153. + * dwc_modpow.h
  12154. + * See dwc_modpow.c for license and changes
  12155. + */
  12156. +#ifndef _DWC_MODPOW_H
  12157. +#define _DWC_MODPOW_H
  12158. +
  12159. +#ifdef __cplusplus
  12160. +extern "C" {
  12161. +#endif
  12162. +
  12163. +#include "dwc_os.h"
  12164. +
  12165. +/** @file
  12166. + *
  12167. + * This file defines the module exponentiation function which is only used
  12168. + * internally by the DWC UWB modules for calculation of PKs during numeric
  12169. + * association. The routine is taken from the PUTTY, an open source terminal
  12170. + * emulator. The PUTTY License is preserved in the dwc_modpow.c file.
  12171. + *
  12172. + */
  12173. +
  12174. +typedef uint32_t BignumInt;
  12175. +typedef uint64_t BignumDblInt;
  12176. +typedef BignumInt *Bignum;
  12177. +
  12178. +/* Compute modular exponentiaion */
  12179. +extern Bignum dwc_modpow(void *mem_ctx, Bignum base_in, Bignum exp, Bignum mod);
  12180. +
  12181. +#ifdef __cplusplus
  12182. +}
  12183. +#endif
  12184. +
  12185. +#endif /* _LINUX_BIGNUM_H */
  12186. --- /dev/null
  12187. +++ b/drivers/usb/host/dwc_common_port/dwc_notifier.c
  12188. @@ -0,0 +1,319 @@
  12189. +#ifdef DWC_NOTIFYLIB
  12190. +
  12191. +#include "dwc_notifier.h"
  12192. +#include "dwc_list.h"
  12193. +
  12194. +typedef struct dwc_observer {
  12195. + void *observer;
  12196. + dwc_notifier_callback_t callback;
  12197. + void *data;
  12198. + char *notification;
  12199. + DWC_CIRCLEQ_ENTRY(dwc_observer) list_entry;
  12200. +} observer_t;
  12201. +
  12202. +DWC_CIRCLEQ_HEAD(observer_queue, dwc_observer);
  12203. +
  12204. +typedef struct dwc_notifier {
  12205. + void *mem_ctx;
  12206. + void *object;
  12207. + struct observer_queue observers;
  12208. + DWC_CIRCLEQ_ENTRY(dwc_notifier) list_entry;
  12209. +} notifier_t;
  12210. +
  12211. +DWC_CIRCLEQ_HEAD(notifier_queue, dwc_notifier);
  12212. +
  12213. +typedef struct manager {
  12214. + void *mem_ctx;
  12215. + void *wkq_ctx;
  12216. + dwc_workq_t *wq;
  12217. +// dwc_mutex_t *mutex;
  12218. + struct notifier_queue notifiers;
  12219. +} manager_t;
  12220. +
  12221. +static manager_t *manager = NULL;
  12222. +
  12223. +static int create_manager(void *mem_ctx, void *wkq_ctx)
  12224. +{
  12225. + manager = dwc_alloc(mem_ctx, sizeof(manager_t));
  12226. + if (!manager) {
  12227. + return -DWC_E_NO_MEMORY;
  12228. + }
  12229. +
  12230. + DWC_CIRCLEQ_INIT(&manager->notifiers);
  12231. +
  12232. + manager->wq = dwc_workq_alloc(wkq_ctx, "DWC Notification WorkQ");
  12233. + if (!manager->wq) {
  12234. + return -DWC_E_NO_MEMORY;
  12235. + }
  12236. +
  12237. + return 0;
  12238. +}
  12239. +
  12240. +static void free_manager(void)
  12241. +{
  12242. + dwc_workq_free(manager->wq);
  12243. +
  12244. + /* All notifiers must have unregistered themselves before this module
  12245. + * can be removed. Hitting this assertion indicates a programmer
  12246. + * error. */
  12247. + DWC_ASSERT(DWC_CIRCLEQ_EMPTY(&manager->notifiers),
  12248. + "Notification manager being freed before all notifiers have been removed");
  12249. + dwc_free(manager->mem_ctx, manager);
  12250. +}
  12251. +
  12252. +#ifdef DEBUG
  12253. +static void dump_manager(void)
  12254. +{
  12255. + notifier_t *n;
  12256. + observer_t *o;
  12257. +
  12258. + DWC_ASSERT(manager, "Notification manager not found");
  12259. +
  12260. + DWC_DEBUG("List of all notifiers and observers:\n");
  12261. + DWC_CIRCLEQ_FOREACH(n, &manager->notifiers, list_entry) {
  12262. + DWC_DEBUG("Notifier %p has observers:\n", n->object);
  12263. + DWC_CIRCLEQ_FOREACH(o, &n->observers, list_entry) {
  12264. + DWC_DEBUG(" %p watching %s\n", o->observer, o->notification);
  12265. + }
  12266. + }
  12267. +}
  12268. +#else
  12269. +#define dump_manager(...)
  12270. +#endif
  12271. +
  12272. +static observer_t *alloc_observer(void *mem_ctx, void *observer, char *notification,
  12273. + dwc_notifier_callback_t callback, void *data)
  12274. +{
  12275. + observer_t *new_observer = dwc_alloc(mem_ctx, sizeof(observer_t));
  12276. +
  12277. + if (!new_observer) {
  12278. + return NULL;
  12279. + }
  12280. +
  12281. + DWC_CIRCLEQ_INIT_ENTRY(new_observer, list_entry);
  12282. + new_observer->observer = observer;
  12283. + new_observer->notification = notification;
  12284. + new_observer->callback = callback;
  12285. + new_observer->data = data;
  12286. + return new_observer;
  12287. +}
  12288. +
  12289. +static void free_observer(void *mem_ctx, observer_t *observer)
  12290. +{
  12291. + dwc_free(mem_ctx, observer);
  12292. +}
  12293. +
  12294. +static notifier_t *alloc_notifier(void *mem_ctx, void *object)
  12295. +{
  12296. + notifier_t *notifier;
  12297. +
  12298. + if (!object) {
  12299. + return NULL;
  12300. + }
  12301. +
  12302. + notifier = dwc_alloc(mem_ctx, sizeof(notifier_t));
  12303. + if (!notifier) {
  12304. + return NULL;
  12305. + }
  12306. +
  12307. + DWC_CIRCLEQ_INIT(&notifier->observers);
  12308. + DWC_CIRCLEQ_INIT_ENTRY(notifier, list_entry);
  12309. +
  12310. + notifier->mem_ctx = mem_ctx;
  12311. + notifier->object = object;
  12312. + return notifier;
  12313. +}
  12314. +
  12315. +static void free_notifier(notifier_t *notifier)
  12316. +{
  12317. + observer_t *observer;
  12318. +
  12319. + DWC_CIRCLEQ_FOREACH(observer, &notifier->observers, list_entry) {
  12320. + free_observer(notifier->mem_ctx, observer);
  12321. + }
  12322. +
  12323. + dwc_free(notifier->mem_ctx, notifier);
  12324. +}
  12325. +
  12326. +static notifier_t *find_notifier(void *object)
  12327. +{
  12328. + notifier_t *notifier;
  12329. +
  12330. + DWC_ASSERT(manager, "Notification manager not found");
  12331. +
  12332. + if (!object) {
  12333. + return NULL;
  12334. + }
  12335. +
  12336. + DWC_CIRCLEQ_FOREACH(notifier, &manager->notifiers, list_entry) {
  12337. + if (notifier->object == object) {
  12338. + return notifier;
  12339. + }
  12340. + }
  12341. +
  12342. + return NULL;
  12343. +}
  12344. +
  12345. +int dwc_alloc_notification_manager(void *mem_ctx, void *wkq_ctx)
  12346. +{
  12347. + return create_manager(mem_ctx, wkq_ctx);
  12348. +}
  12349. +
  12350. +void dwc_free_notification_manager(void)
  12351. +{
  12352. + free_manager();
  12353. +}
  12354. +
  12355. +dwc_notifier_t *dwc_register_notifier(void *mem_ctx, void *object)
  12356. +{
  12357. + notifier_t *notifier;
  12358. +
  12359. + DWC_ASSERT(manager, "Notification manager not found");
  12360. +
  12361. + notifier = find_notifier(object);
  12362. + if (notifier) {
  12363. + DWC_ERROR("Notifier %p is already registered\n", object);
  12364. + return NULL;
  12365. + }
  12366. +
  12367. + notifier = alloc_notifier(mem_ctx, object);
  12368. + if (!notifier) {
  12369. + return NULL;
  12370. + }
  12371. +
  12372. + DWC_CIRCLEQ_INSERT_TAIL(&manager->notifiers, notifier, list_entry);
  12373. +
  12374. + DWC_INFO("Notifier %p registered", object);
  12375. + dump_manager();
  12376. +
  12377. + return notifier;
  12378. +}
  12379. +
  12380. +void dwc_unregister_notifier(dwc_notifier_t *notifier)
  12381. +{
  12382. + DWC_ASSERT(manager, "Notification manager not found");
  12383. +
  12384. + if (!DWC_CIRCLEQ_EMPTY(&notifier->observers)) {
  12385. + observer_t *o;
  12386. +
  12387. + DWC_ERROR("Notifier %p has active observers when removing\n", notifier->object);
  12388. + DWC_CIRCLEQ_FOREACH(o, &notifier->observers, list_entry) {
  12389. + DWC_DEBUGC(" %p watching %s\n", o->observer, o->notification);
  12390. + }
  12391. +
  12392. + DWC_ASSERT(DWC_CIRCLEQ_EMPTY(&notifier->observers),
  12393. + "Notifier %p has active observers when removing", notifier);
  12394. + }
  12395. +
  12396. + DWC_CIRCLEQ_REMOVE_INIT(&manager->notifiers, notifier, list_entry);
  12397. + free_notifier(notifier);
  12398. +
  12399. + DWC_INFO("Notifier unregistered");
  12400. + dump_manager();
  12401. +}
  12402. +
  12403. +/* Add an observer to observe the notifier for a particular state, event, or notification. */
  12404. +int dwc_add_observer(void *observer, void *object, char *notification,
  12405. + dwc_notifier_callback_t callback, void *data)
  12406. +{
  12407. + notifier_t *notifier = find_notifier(object);
  12408. + observer_t *new_observer;
  12409. +
  12410. + if (!notifier) {
  12411. + DWC_ERROR("Notifier %p is not found when adding observer\n", object);
  12412. + return -DWC_E_INVALID;
  12413. + }
  12414. +
  12415. + new_observer = alloc_observer(notifier->mem_ctx, observer, notification, callback, data);
  12416. + if (!new_observer) {
  12417. + return -DWC_E_NO_MEMORY;
  12418. + }
  12419. +
  12420. + DWC_CIRCLEQ_INSERT_TAIL(&notifier->observers, new_observer, list_entry);
  12421. +
  12422. + DWC_INFO("Added observer %p to notifier %p observing notification %s, callback=%p, data=%p",
  12423. + observer, object, notification, callback, data);
  12424. +
  12425. + dump_manager();
  12426. + return 0;
  12427. +}
  12428. +
  12429. +int dwc_remove_observer(void *observer)
  12430. +{
  12431. + notifier_t *n;
  12432. +
  12433. + DWC_ASSERT(manager, "Notification manager not found");
  12434. +
  12435. + DWC_CIRCLEQ_FOREACH(n, &manager->notifiers, list_entry) {
  12436. + observer_t *o;
  12437. + observer_t *o2;
  12438. +
  12439. + DWC_CIRCLEQ_FOREACH_SAFE(o, o2, &n->observers, list_entry) {
  12440. + if (o->observer == observer) {
  12441. + DWC_CIRCLEQ_REMOVE_INIT(&n->observers, o, list_entry);
  12442. + DWC_INFO("Removing observer %p from notifier %p watching notification %s:",
  12443. + o->observer, n->object, o->notification);
  12444. + free_observer(n->mem_ctx, o);
  12445. + }
  12446. + }
  12447. + }
  12448. +
  12449. + dump_manager();
  12450. + return 0;
  12451. +}
  12452. +
  12453. +typedef struct callback_data {
  12454. + void *mem_ctx;
  12455. + dwc_notifier_callback_t cb;
  12456. + void *observer;
  12457. + void *data;
  12458. + void *object;
  12459. + char *notification;
  12460. + void *notification_data;
  12461. +} cb_data_t;
  12462. +
  12463. +static void cb_task(void *data)
  12464. +{
  12465. + cb_data_t *cb = (cb_data_t *)data;
  12466. +
  12467. + cb->cb(cb->object, cb->notification, cb->observer, cb->notification_data, cb->data);
  12468. + dwc_free(cb->mem_ctx, cb);
  12469. +}
  12470. +
  12471. +void dwc_notify(dwc_notifier_t *notifier, char *notification, void *notification_data)
  12472. +{
  12473. + observer_t *o;
  12474. +
  12475. + DWC_ASSERT(manager, "Notification manager not found");
  12476. +
  12477. + DWC_CIRCLEQ_FOREACH(o, &notifier->observers, list_entry) {
  12478. + int len = DWC_STRLEN(notification);
  12479. +
  12480. + if (DWC_STRLEN(o->notification) != len) {
  12481. + continue;
  12482. + }
  12483. +
  12484. + if (DWC_STRNCMP(o->notification, notification, len) == 0) {
  12485. + cb_data_t *cb_data = dwc_alloc(notifier->mem_ctx, sizeof(cb_data_t));
  12486. +
  12487. + if (!cb_data) {
  12488. + DWC_ERROR("Failed to allocate callback data\n");
  12489. + return;
  12490. + }
  12491. +
  12492. + cb_data->mem_ctx = notifier->mem_ctx;
  12493. + cb_data->cb = o->callback;
  12494. + cb_data->observer = o->observer;
  12495. + cb_data->data = o->data;
  12496. + cb_data->object = notifier->object;
  12497. + cb_data->notification = notification;
  12498. + cb_data->notification_data = notification_data;
  12499. + DWC_DEBUGC("Observer found %p for notification %s\n", o->observer, notification);
  12500. + DWC_WORKQ_SCHEDULE(manager->wq, cb_task, cb_data,
  12501. + "Notify callback from %p for Notification %s, to observer %p",
  12502. + cb_data->object, notification, cb_data->observer);
  12503. + }
  12504. + }
  12505. +}
  12506. +
  12507. +#endif /* DWC_NOTIFYLIB */
  12508. --- /dev/null
  12509. +++ b/drivers/usb/host/dwc_common_port/dwc_notifier.h
  12510. @@ -0,0 +1,122 @@
  12511. +
  12512. +#ifndef __DWC_NOTIFIER_H__
  12513. +#define __DWC_NOTIFIER_H__
  12514. +
  12515. +#ifdef __cplusplus
  12516. +extern "C" {
  12517. +#endif
  12518. +
  12519. +#include "dwc_os.h"
  12520. +
  12521. +/** @file
  12522. + *
  12523. + * A simple implementation of the Observer pattern. Any "module" can
  12524. + * register as an observer or notifier. The notion of "module" is abstract and
  12525. + * can mean anything used to identify either an observer or notifier. Usually
  12526. + * it will be a pointer to a data structure which contains some state, ie an
  12527. + * object.
  12528. + *
  12529. + * Before any notifiers can be added, the global notification manager must be
  12530. + * brought up with dwc_alloc_notification_manager().
  12531. + * dwc_free_notification_manager() will bring it down and free all resources.
  12532. + * These would typically be called upon module load and unload. The
  12533. + * notification manager is a single global instance that handles all registered
  12534. + * observable modules and observers so this should be done only once.
  12535. + *
  12536. + * A module can be observable by using Notifications to publicize some general
  12537. + * information about it's state or operation. It does not care who listens, or
  12538. + * even if anyone listens, or what they do with the information. The observable
  12539. + * modules do not need to know any information about it's observers or their
  12540. + * interface, or their state or data.
  12541. + *
  12542. + * Any module can register to emit Notifications. It should publish a list of
  12543. + * notifications that it can emit and their behavior, such as when they will get
  12544. + * triggered, and what information will be provided to the observer. Then it
  12545. + * should register itself as an observable module. See dwc_register_notifier().
  12546. + *
  12547. + * Any module can observe any observable, registered module, provided it has a
  12548. + * handle to the other module and knows what notifications to observe. See
  12549. + * dwc_add_observer().
  12550. + *
  12551. + * A function of type dwc_notifier_callback_t is called whenever a notification
  12552. + * is triggered with one or more observers observing it. This function is
  12553. + * called in it's own process so it may sleep or block if needed. It is
  12554. + * guaranteed to be called sometime after the notification has occurred and will
  12555. + * be called once per each time the notification is triggered. It will NOT be
  12556. + * called in the same process context used to trigger the notification.
  12557. + *
  12558. + * @section Limitiations
  12559. + *
  12560. + * Keep in mind that Notifications that can be triggered in rapid sucession may
  12561. + * schedule too many processes too handle. Be aware of this limitation when
  12562. + * designing to use notifications, and only add notifications for appropriate
  12563. + * observable information.
  12564. + *
  12565. + * Also Notification callbacks are not synchronous. If you need to synchronize
  12566. + * the behavior between module/observer you must use other means. And perhaps
  12567. + * that will mean Notifications are not the proper solution.
  12568. + */
  12569. +
  12570. +struct dwc_notifier;
  12571. +typedef struct dwc_notifier dwc_notifier_t;
  12572. +
  12573. +/** The callback function must be of this type.
  12574. + *
  12575. + * @param object This is the object that is being observed.
  12576. + * @param notification This is the notification that was triggered.
  12577. + * @param observer This is the observer
  12578. + * @param notification_data This is notification-specific data that the notifier
  12579. + * has included in this notification. The value of this should be published in
  12580. + * the documentation of the observable module with the notifications.
  12581. + * @param user_data This is any custom data that the observer provided when
  12582. + * adding itself as an observer to the notification. */
  12583. +typedef void (*dwc_notifier_callback_t)(void *object, char *notification, void *observer,
  12584. + void *notification_data, void *user_data);
  12585. +
  12586. +/** Brings up the notification manager. */
  12587. +extern int dwc_alloc_notification_manager(void *mem_ctx, void *wkq_ctx);
  12588. +/** Brings down the notification manager. */
  12589. +extern void dwc_free_notification_manager(void);
  12590. +
  12591. +/** This function registers an observable module. A dwc_notifier_t object is
  12592. + * returned to the observable module. This is an opaque object that is used by
  12593. + * the observable module to trigger notifications. This object should only be
  12594. + * accessible to functions that are authorized to trigger notifications for this
  12595. + * module. Observers do not need this object. */
  12596. +extern dwc_notifier_t *dwc_register_notifier(void *mem_ctx, void *object);
  12597. +
  12598. +/** This function unregisters an observable module. All observers have to be
  12599. + * removed prior to unregistration. */
  12600. +extern void dwc_unregister_notifier(dwc_notifier_t *notifier);
  12601. +
  12602. +/** Add a module as an observer to the observable module. The observable module
  12603. + * needs to have previously registered with the notification manager.
  12604. + *
  12605. + * @param observer The observer module
  12606. + * @param object The module to observe
  12607. + * @param notification The notification to observe
  12608. + * @param callback The callback function to call
  12609. + * @param user_data Any additional user data to pass into the callback function */
  12610. +extern int dwc_add_observer(void *observer, void *object, char *notification,
  12611. + dwc_notifier_callback_t callback, void *user_data);
  12612. +
  12613. +/** Removes the specified observer from all notifications that it is currently
  12614. + * observing. */
  12615. +extern int dwc_remove_observer(void *observer);
  12616. +
  12617. +/** This function triggers a Notification. It should be called by the
  12618. + * observable module, or any module or library which the observable module
  12619. + * allows to trigger notification on it's behalf. Such as the dwc_cc_t.
  12620. + *
  12621. + * dwc_notify is a non-blocking function. Callbacks are scheduled called in
  12622. + * their own process context for each trigger. Callbacks can be blocking.
  12623. + * dwc_notify can be called from interrupt context if needed.
  12624. + *
  12625. + */
  12626. +void dwc_notify(dwc_notifier_t *notifier, char *notification, void *notification_data);
  12627. +
  12628. +#ifdef __cplusplus
  12629. +}
  12630. +#endif
  12631. +
  12632. +#endif /* __DWC_NOTIFIER_H__ */
  12633. --- /dev/null
  12634. +++ b/drivers/usb/host/dwc_common_port/dwc_os.h
  12635. @@ -0,0 +1,1276 @@
  12636. +/* =========================================================================
  12637. + * $File: //dwh/usb_iip/dev/software/dwc_common_port_2/dwc_os.h $
  12638. + * $Revision: #14 $
  12639. + * $Date: 2010/11/04 $
  12640. + * $Change: 1621695 $
  12641. + *
  12642. + * Synopsys Portability Library Software and documentation
  12643. + * (hereinafter, "Software") is an Unsupported proprietary work of
  12644. + * Synopsys, Inc. unless otherwise expressly agreed to in writing
  12645. + * between Synopsys and you.
  12646. + *
  12647. + * The Software IS NOT an item of Licensed Software or Licensed Product
  12648. + * under any End User Software License Agreement or Agreement for
  12649. + * Licensed Product with Synopsys or any supplement thereto. You are
  12650. + * permitted to use and redistribute this Software in source and binary
  12651. + * forms, with or without modification, provided that redistributions
  12652. + * of source code must retain this notice. You may not view, use,
  12653. + * disclose, copy or distribute this file or any information contained
  12654. + * herein except pursuant to this license grant from Synopsys. If you
  12655. + * do not agree with this notice, including the disclaimer below, then
  12656. + * you are not authorized to use the Software.
  12657. + *
  12658. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
  12659. + * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  12660. + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  12661. + * FOR A PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL
  12662. + * SYNOPSYS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  12663. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  12664. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  12665. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  12666. + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12667. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  12668. + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  12669. + * DAMAGE.
  12670. + * ========================================================================= */
  12671. +#ifndef _DWC_OS_H_
  12672. +#define _DWC_OS_H_
  12673. +
  12674. +#ifdef __cplusplus
  12675. +extern "C" {
  12676. +#endif
  12677. +
  12678. +/** @file
  12679. + *
  12680. + * DWC portability library, low level os-wrapper functions
  12681. + *
  12682. + */
  12683. +
  12684. +/* These basic types need to be defined by some OS header file or custom header
  12685. + * file for your specific target architecture.
  12686. + *
  12687. + * uint8_t, int8_t, uint16_t, int16_t, uint32_t, int32_t, uint64_t, int64_t
  12688. + *
  12689. + * Any custom or alternate header file must be added and enabled here.
  12690. + */
  12691. +
  12692. +#ifdef DWC_LINUX
  12693. +# include <linux/types.h>
  12694. +# ifdef CONFIG_DEBUG_MUTEXES
  12695. +# include <linux/mutex.h>
  12696. +# endif
  12697. +# include <linux/spinlock.h>
  12698. +# include <linux/errno.h>
  12699. +# include <stdarg.h>
  12700. +#endif
  12701. +
  12702. +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
  12703. +# include <os_dep.h>
  12704. +#endif
  12705. +
  12706. +
  12707. +/** @name Primitive Types and Values */
  12708. +
  12709. +/** We define a boolean type for consistency. Can be either YES or NO */
  12710. +typedef uint8_t dwc_bool_t;
  12711. +#define YES 1
  12712. +#define NO 0
  12713. +
  12714. +#ifdef DWC_LINUX
  12715. +
  12716. +/** @name Error Codes */
  12717. +#define DWC_E_INVALID EINVAL
  12718. +#define DWC_E_NO_MEMORY ENOMEM
  12719. +#define DWC_E_NO_DEVICE ENODEV
  12720. +#define DWC_E_NOT_SUPPORTED EOPNOTSUPP
  12721. +#define DWC_E_TIMEOUT ETIMEDOUT
  12722. +#define DWC_E_BUSY EBUSY
  12723. +#define DWC_E_AGAIN EAGAIN
  12724. +#define DWC_E_RESTART ERESTART
  12725. +#define DWC_E_ABORT ECONNABORTED
  12726. +#define DWC_E_SHUTDOWN ESHUTDOWN
  12727. +#define DWC_E_NO_DATA ENODATA
  12728. +#define DWC_E_DISCONNECT ECONNRESET
  12729. +#define DWC_E_UNKNOWN EINVAL
  12730. +#define DWC_E_NO_STREAM_RES ENOSR
  12731. +#define DWC_E_COMMUNICATION ECOMM
  12732. +#define DWC_E_OVERFLOW EOVERFLOW
  12733. +#define DWC_E_PROTOCOL EPROTO
  12734. +#define DWC_E_IN_PROGRESS EINPROGRESS
  12735. +#define DWC_E_PIPE EPIPE
  12736. +#define DWC_E_IO EIO
  12737. +#define DWC_E_NO_SPACE ENOSPC
  12738. +
  12739. +#else
  12740. +
  12741. +/** @name Error Codes */
  12742. +#define DWC_E_INVALID 1001
  12743. +#define DWC_E_NO_MEMORY 1002
  12744. +#define DWC_E_NO_DEVICE 1003
  12745. +#define DWC_E_NOT_SUPPORTED 1004
  12746. +#define DWC_E_TIMEOUT 1005
  12747. +#define DWC_E_BUSY 1006
  12748. +#define DWC_E_AGAIN 1007
  12749. +#define DWC_E_RESTART 1008
  12750. +#define DWC_E_ABORT 1009
  12751. +#define DWC_E_SHUTDOWN 1010
  12752. +#define DWC_E_NO_DATA 1011
  12753. +#define DWC_E_DISCONNECT 2000
  12754. +#define DWC_E_UNKNOWN 3000
  12755. +#define DWC_E_NO_STREAM_RES 4001
  12756. +#define DWC_E_COMMUNICATION 4002
  12757. +#define DWC_E_OVERFLOW 4003
  12758. +#define DWC_E_PROTOCOL 4004
  12759. +#define DWC_E_IN_PROGRESS 4005
  12760. +#define DWC_E_PIPE 4006
  12761. +#define DWC_E_IO 4007
  12762. +#define DWC_E_NO_SPACE 4008
  12763. +
  12764. +#endif
  12765. +
  12766. +
  12767. +/** @name Tracing/Logging Functions
  12768. + *
  12769. + * These function provide the capability to add tracing, debugging, and error
  12770. + * messages, as well exceptions as assertions. The WUDEV uses these
  12771. + * extensively. These could be logged to the main console, the serial port, an
  12772. + * internal buffer, etc. These functions could also be no-op if they are too
  12773. + * expensive on your system. By default undefining the DEBUG macro already
  12774. + * no-ops some of these functions. */
  12775. +
  12776. +/** Returns non-zero if in interrupt context. */
  12777. +extern dwc_bool_t DWC_IN_IRQ(void);
  12778. +#define dwc_in_irq DWC_IN_IRQ
  12779. +
  12780. +/** Returns "IRQ" if DWC_IN_IRQ is true. */
  12781. +static inline char *dwc_irq(void) {
  12782. + return DWC_IN_IRQ() ? "IRQ" : "";
  12783. +}
  12784. +
  12785. +/** Returns non-zero if in bottom-half context. */
  12786. +extern dwc_bool_t DWC_IN_BH(void);
  12787. +#define dwc_in_bh DWC_IN_BH
  12788. +
  12789. +/** Returns "BH" if DWC_IN_BH is true. */
  12790. +static inline char *dwc_bh(void) {
  12791. + return DWC_IN_BH() ? "BH" : "";
  12792. +}
  12793. +
  12794. +/**
  12795. + * A vprintf() clone. Just call vprintf if you've got it.
  12796. + */
  12797. +extern void DWC_VPRINTF(char *format, va_list args);
  12798. +#define dwc_vprintf DWC_VPRINTF
  12799. +
  12800. +/**
  12801. + * A vsnprintf() clone. Just call vprintf if you've got it.
  12802. + */
  12803. +extern int DWC_VSNPRINTF(char *str, int size, char *format, va_list args);
  12804. +#define dwc_vsnprintf DWC_VSNPRINTF
  12805. +
  12806. +/**
  12807. + * printf() clone. Just call printf if you've go it.
  12808. + */
  12809. +extern void DWC_PRINTF(char *format, ...)
  12810. +/* This provides compiler level static checking of the parameters if you're
  12811. + * using GCC. */
  12812. +#ifdef __GNUC__
  12813. + __attribute__ ((format(printf, 1, 2)));
  12814. +#else
  12815. + ;
  12816. +#endif
  12817. +#define dwc_printf DWC_PRINTF
  12818. +
  12819. +/**
  12820. + * sprintf() clone. Just call sprintf if you've got it.
  12821. + */
  12822. +extern int DWC_SPRINTF(char *string, char *format, ...)
  12823. +#ifdef __GNUC__
  12824. + __attribute__ ((format(printf, 2, 3)));
  12825. +#else
  12826. + ;
  12827. +#endif
  12828. +#define dwc_sprintf DWC_SPRINTF
  12829. +
  12830. +/**
  12831. + * snprintf() clone. Just call snprintf if you've got it.
  12832. + */
  12833. +extern int DWC_SNPRINTF(char *string, int size, char *format, ...)
  12834. +#ifdef __GNUC__
  12835. + __attribute__ ((format(printf, 3, 4)));
  12836. +#else
  12837. + ;
  12838. +#endif
  12839. +#define dwc_snprintf DWC_SNPRINTF
  12840. +
  12841. +/**
  12842. + * Prints a WARNING message. On systems that don't differentiate between
  12843. + * warnings and regular log messages, just print it. Indicates that something
  12844. + * may be wrong with the driver. Works like printf().
  12845. + *
  12846. + * Use the DWC_WARN macro to call this function.
  12847. + */
  12848. +extern void __DWC_WARN(char *format, ...)
  12849. +#ifdef __GNUC__
  12850. + __attribute__ ((format(printf, 1, 2)));
  12851. +#else
  12852. + ;
  12853. +#endif
  12854. +
  12855. +/**
  12856. + * Prints an error message. On systems that don't differentiate between errors
  12857. + * and regular log messages, just print it. Indicates that something went wrong
  12858. + * with the driver. Works like printf().
  12859. + *
  12860. + * Use the DWC_ERROR macro to call this function.
  12861. + */
  12862. +extern void __DWC_ERROR(char *format, ...)
  12863. +#ifdef __GNUC__
  12864. + __attribute__ ((format(printf, 1, 2)));
  12865. +#else
  12866. + ;
  12867. +#endif
  12868. +
  12869. +/**
  12870. + * Prints an exception error message and takes some user-defined action such as
  12871. + * print out a backtrace or trigger a breakpoint. Indicates that something went
  12872. + * abnormally wrong with the driver such as programmer error, or other
  12873. + * exceptional condition. It should not be ignored so even on systems without
  12874. + * printing capability, some action should be taken to notify the developer of
  12875. + * it. Works like printf().
  12876. + */
  12877. +extern void DWC_EXCEPTION(char *format, ...)
  12878. +#ifdef __GNUC__
  12879. + __attribute__ ((format(printf, 1, 2)));
  12880. +#else
  12881. + ;
  12882. +#endif
  12883. +#define dwc_exception DWC_EXCEPTION
  12884. +
  12885. +#ifndef DWC_OTG_DEBUG_LEV
  12886. +#define DWC_OTG_DEBUG_LEV 0
  12887. +#endif
  12888. +
  12889. +#ifdef DEBUG
  12890. +/**
  12891. + * Prints out a debug message. Used for logging/trace messages.
  12892. + *
  12893. + * Use the DWC_DEBUG macro to call this function
  12894. + */
  12895. +extern void __DWC_DEBUG(char *format, ...)
  12896. +#ifdef __GNUC__
  12897. + __attribute__ ((format(printf, 1, 2)));
  12898. +#else
  12899. + ;
  12900. +#endif
  12901. +#else
  12902. +#define __DWC_DEBUG printk
  12903. +#endif
  12904. +
  12905. +/**
  12906. + * Prints out a Debug message.
  12907. + */
  12908. +#define DWC_DEBUG(_format, _args...) __DWC_DEBUG("DEBUG:%s:%s: " _format "\n", \
  12909. + __func__, dwc_irq(), ## _args)
  12910. +#define dwc_debug DWC_DEBUG
  12911. +/**
  12912. + * Prints out a Debug message if enabled at compile time.
  12913. + */
  12914. +#if DWC_OTG_DEBUG_LEV > 0
  12915. +#define DWC_DEBUGC(_format, _args...) DWC_DEBUG(_format, ##_args )
  12916. +#else
  12917. +#define DWC_DEBUGC(_format, _args...)
  12918. +#endif
  12919. +#define dwc_debugc DWC_DEBUGC
  12920. +/**
  12921. + * Prints out an informative message.
  12922. + */
  12923. +#define DWC_INFO(_format, _args...) DWC_PRINTF("INFO:%s: " _format "\n", \
  12924. + dwc_irq(), ## _args)
  12925. +#define dwc_info DWC_INFO
  12926. +/**
  12927. + * Prints out an informative message if enabled at compile time.
  12928. + */
  12929. +#if DWC_OTG_DEBUG_LEV > 1
  12930. +#define DWC_INFOC(_format, _args...) DWC_INFO(_format, ##_args )
  12931. +#else
  12932. +#define DWC_INFOC(_format, _args...)
  12933. +#endif
  12934. +#define dwc_infoc DWC_INFOC
  12935. +/**
  12936. + * Prints out a warning message.
  12937. + */
  12938. +#define DWC_WARN(_format, _args...) __DWC_WARN("WARN:%s:%s:%d: " _format "\n", \
  12939. + dwc_irq(), __func__, __LINE__, ## _args)
  12940. +#define dwc_warn DWC_WARN
  12941. +/**
  12942. + * Prints out an error message.
  12943. + */
  12944. +#define DWC_ERROR(_format, _args...) __DWC_ERROR("ERROR:%s:%s:%d: " _format "\n", \
  12945. + dwc_irq(), __func__, __LINE__, ## _args)
  12946. +#define dwc_error DWC_ERROR
  12947. +
  12948. +#define DWC_PROTO_ERROR(_format, _args...) __DWC_WARN("ERROR:%s:%s:%d: " _format "\n", \
  12949. + dwc_irq(), __func__, __LINE__, ## _args)
  12950. +#define dwc_proto_error DWC_PROTO_ERROR
  12951. +
  12952. +#ifdef DEBUG
  12953. +/** Prints out a exception error message if the _expr expression fails. Disabled
  12954. + * if DEBUG is not enabled. */
  12955. +#define DWC_ASSERT(_expr, _format, _args...) do { \
  12956. + if (!(_expr)) { DWC_EXCEPTION("%s:%s:%d: " _format "\n", dwc_irq(), \
  12957. + __FILE__, __LINE__, ## _args); } \
  12958. + } while (0)
  12959. +#else
  12960. +#define DWC_ASSERT(_x...)
  12961. +#endif
  12962. +#define dwc_assert DWC_ASSERT
  12963. +
  12964. +
  12965. +/** @name Byte Ordering
  12966. + * The following functions are for conversions between processor's byte ordering
  12967. + * and specific ordering you want.
  12968. + */
  12969. +
  12970. +/** Converts 32 bit data in CPU byte ordering to little endian. */
  12971. +extern uint32_t DWC_CPU_TO_LE32(uint32_t *p);
  12972. +#define dwc_cpu_to_le32 DWC_CPU_TO_LE32
  12973. +
  12974. +/** Converts 32 bit data in CPU byte orderint to big endian. */
  12975. +extern uint32_t DWC_CPU_TO_BE32(uint32_t *p);
  12976. +#define dwc_cpu_to_be32 DWC_CPU_TO_BE32
  12977. +
  12978. +/** Converts 32 bit little endian data to CPU byte ordering. */
  12979. +extern uint32_t DWC_LE32_TO_CPU(uint32_t *p);
  12980. +#define dwc_le32_to_cpu DWC_LE32_TO_CPU
  12981. +
  12982. +/** Converts 32 bit big endian data to CPU byte ordering. */
  12983. +extern uint32_t DWC_BE32_TO_CPU(uint32_t *p);
  12984. +#define dwc_be32_to_cpu DWC_BE32_TO_CPU
  12985. +
  12986. +/** Converts 16 bit data in CPU byte ordering to little endian. */
  12987. +extern uint16_t DWC_CPU_TO_LE16(uint16_t *p);
  12988. +#define dwc_cpu_to_le16 DWC_CPU_TO_LE16
  12989. +
  12990. +/** Converts 16 bit data in CPU byte orderint to big endian. */
  12991. +extern uint16_t DWC_CPU_TO_BE16(uint16_t *p);
  12992. +#define dwc_cpu_to_be16 DWC_CPU_TO_BE16
  12993. +
  12994. +/** Converts 16 bit little endian data to CPU byte ordering. */
  12995. +extern uint16_t DWC_LE16_TO_CPU(uint16_t *p);
  12996. +#define dwc_le16_to_cpu DWC_LE16_TO_CPU
  12997. +
  12998. +/** Converts 16 bit bi endian data to CPU byte ordering. */
  12999. +extern uint16_t DWC_BE16_TO_CPU(uint16_t *p);
  13000. +#define dwc_be16_to_cpu DWC_BE16_TO_CPU
  13001. +
  13002. +
  13003. +/** @name Register Read/Write
  13004. + *
  13005. + * The following six functions should be implemented to read/write registers of
  13006. + * 32-bit and 64-bit sizes. All modules use this to read/write register values.
  13007. + * The reg value is a pointer to the register calculated from the void *base
  13008. + * variable passed into the driver when it is started. */
  13009. +
  13010. +#ifdef DWC_LINUX
  13011. +/* Linux doesn't need any extra parameters for register read/write, so we
  13012. + * just throw away the IO context parameter.
  13013. + */
  13014. +/** Reads the content of a 32-bit register. */
  13015. +extern uint32_t DWC_READ_REG32(uint32_t volatile *reg);
  13016. +#define dwc_read_reg32(_ctx_,_reg_) DWC_READ_REG32(_reg_)
  13017. +
  13018. +/** Reads the content of a 64-bit register. */
  13019. +extern uint64_t DWC_READ_REG64(uint64_t volatile *reg);
  13020. +#define dwc_read_reg64(_ctx_,_reg_) DWC_READ_REG64(_reg_)
  13021. +
  13022. +/** Writes to a 32-bit register. */
  13023. +extern void DWC_WRITE_REG32(uint32_t volatile *reg, uint32_t value);
  13024. +#define dwc_write_reg32(_ctx_,_reg_,_val_) DWC_WRITE_REG32(_reg_, _val_)
  13025. +
  13026. +/** Writes to a 64-bit register. */
  13027. +extern void DWC_WRITE_REG64(uint64_t volatile *reg, uint64_t value);
  13028. +#define dwc_write_reg64(_ctx_,_reg_,_val_) DWC_WRITE_REG64(_reg_, _val_)
  13029. +
  13030. +/**
  13031. + * Modify bit values in a register. Using the
  13032. + * algorithm: (reg_contents & ~clear_mask) | set_mask.
  13033. + */
  13034. +extern void DWC_MODIFY_REG32(uint32_t volatile *reg, uint32_t clear_mask, uint32_t set_mask);
  13035. +#define dwc_modify_reg32(_ctx_,_reg_,_cmsk_,_smsk_) DWC_MODIFY_REG32(_reg_,_cmsk_,_smsk_)
  13036. +extern void DWC_MODIFY_REG64(uint64_t volatile *reg, uint64_t clear_mask, uint64_t set_mask);
  13037. +#define dwc_modify_reg64(_ctx_,_reg_,_cmsk_,_smsk_) DWC_MODIFY_REG64(_reg_,_cmsk_,_smsk_)
  13038. +
  13039. +#endif /* DWC_LINUX */
  13040. +
  13041. +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
  13042. +typedef struct dwc_ioctx {
  13043. + struct device *dev;
  13044. + bus_space_tag_t iot;
  13045. + bus_space_handle_t ioh;
  13046. +} dwc_ioctx_t;
  13047. +
  13048. +/** BSD needs two extra parameters for register read/write, so we pass
  13049. + * them in using the IO context parameter.
  13050. + */
  13051. +/** Reads the content of a 32-bit register. */
  13052. +extern uint32_t DWC_READ_REG32(void *io_ctx, uint32_t volatile *reg);
  13053. +#define dwc_read_reg32 DWC_READ_REG32
  13054. +
  13055. +/** Reads the content of a 64-bit register. */
  13056. +extern uint64_t DWC_READ_REG64(void *io_ctx, uint64_t volatile *reg);
  13057. +#define dwc_read_reg64 DWC_READ_REG64
  13058. +
  13059. +/** Writes to a 32-bit register. */
  13060. +extern void DWC_WRITE_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t value);
  13061. +#define dwc_write_reg32 DWC_WRITE_REG32
  13062. +
  13063. +/** Writes to a 64-bit register. */
  13064. +extern void DWC_WRITE_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t value);
  13065. +#define dwc_write_reg64 DWC_WRITE_REG64
  13066. +
  13067. +/**
  13068. + * Modify bit values in a register. Using the
  13069. + * algorithm: (reg_contents & ~clear_mask) | set_mask.
  13070. + */
  13071. +extern void DWC_MODIFY_REG32(void *io_ctx, uint32_t volatile *reg, uint32_t clear_mask, uint32_t set_mask);
  13072. +#define dwc_modify_reg32 DWC_MODIFY_REG32
  13073. +extern void DWC_MODIFY_REG64(void *io_ctx, uint64_t volatile *reg, uint64_t clear_mask, uint64_t set_mask);
  13074. +#define dwc_modify_reg64 DWC_MODIFY_REG64
  13075. +
  13076. +#endif /* DWC_FREEBSD || DWC_NETBSD */
  13077. +
  13078. +/** @cond */
  13079. +
  13080. +/** @name Some convenience MACROS used internally. Define DWC_DEBUG_REGS to log the
  13081. + * register writes. */
  13082. +
  13083. +#ifdef DWC_LINUX
  13084. +
  13085. +# ifdef DWC_DEBUG_REGS
  13086. +
  13087. +#define dwc_define_read_write_reg_n(_reg,_container_type) \
  13088. +static inline uint32_t dwc_read_##_reg##_n(_container_type *container, int num) { \
  13089. + return DWC_READ_REG32(&container->regs->_reg[num]); \
  13090. +} \
  13091. +static inline void dwc_write_##_reg##_n(_container_type *container, int num, uint32_t data) { \
  13092. + DWC_DEBUG("WRITING %8s[%d]: %p: %08x", #_reg, num, \
  13093. + &(((uint32_t*)container->regs->_reg)[num]), data); \
  13094. + DWC_WRITE_REG32(&(((uint32_t*)container->regs->_reg)[num]), data); \
  13095. +}
  13096. +
  13097. +#define dwc_define_read_write_reg(_reg,_container_type) \
  13098. +static inline uint32_t dwc_read_##_reg(_container_type *container) { \
  13099. + return DWC_READ_REG32(&container->regs->_reg); \
  13100. +} \
  13101. +static inline void dwc_write_##_reg(_container_type *container, uint32_t data) { \
  13102. + DWC_DEBUG("WRITING %11s: %p: %08x", #_reg, &container->regs->_reg, data); \
  13103. + DWC_WRITE_REG32(&container->regs->_reg, data); \
  13104. +}
  13105. +
  13106. +# else /* DWC_DEBUG_REGS */
  13107. +
  13108. +#define dwc_define_read_write_reg_n(_reg,_container_type) \
  13109. +static inline uint32_t dwc_read_##_reg##_n(_container_type *container, int num) { \
  13110. + return DWC_READ_REG32(&container->regs->_reg[num]); \
  13111. +} \
  13112. +static inline void dwc_write_##_reg##_n(_container_type *container, int num, uint32_t data) { \
  13113. + DWC_WRITE_REG32(&(((uint32_t*)container->regs->_reg)[num]), data); \
  13114. +}
  13115. +
  13116. +#define dwc_define_read_write_reg(_reg,_container_type) \
  13117. +static inline uint32_t dwc_read_##_reg(_container_type *container) { \
  13118. + return DWC_READ_REG32(&container->regs->_reg); \
  13119. +} \
  13120. +static inline void dwc_write_##_reg(_container_type *container, uint32_t data) { \
  13121. + DWC_WRITE_REG32(&container->regs->_reg, data); \
  13122. +}
  13123. +
  13124. +# endif /* DWC_DEBUG_REGS */
  13125. +
  13126. +#endif /* DWC_LINUX */
  13127. +
  13128. +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
  13129. +
  13130. +# ifdef DWC_DEBUG_REGS
  13131. +
  13132. +#define dwc_define_read_write_reg_n(_reg,_container_type) \
  13133. +static inline uint32_t dwc_read_##_reg##_n(void *io_ctx, _container_type *container, int num) { \
  13134. + return DWC_READ_REG32(io_ctx, &container->regs->_reg[num]); \
  13135. +} \
  13136. +static inline void dwc_write_##_reg##_n(void *io_ctx, _container_type *container, int num, uint32_t data) { \
  13137. + DWC_DEBUG("WRITING %8s[%d]: %p: %08x", #_reg, num, \
  13138. + &(((uint32_t*)container->regs->_reg)[num]), data); \
  13139. + DWC_WRITE_REG32(io_ctx, &(((uint32_t*)container->regs->_reg)[num]), data); \
  13140. +}
  13141. +
  13142. +#define dwc_define_read_write_reg(_reg,_container_type) \
  13143. +static inline uint32_t dwc_read_##_reg(void *io_ctx, _container_type *container) { \
  13144. + return DWC_READ_REG32(io_ctx, &container->regs->_reg); \
  13145. +} \
  13146. +static inline void dwc_write_##_reg(void *io_ctx, _container_type *container, uint32_t data) { \
  13147. + DWC_DEBUG("WRITING %11s: %p: %08x", #_reg, &container->regs->_reg, data); \
  13148. + DWC_WRITE_REG32(io_ctx, &container->regs->_reg, data); \
  13149. +}
  13150. +
  13151. +# else /* DWC_DEBUG_REGS */
  13152. +
  13153. +#define dwc_define_read_write_reg_n(_reg,_container_type) \
  13154. +static inline uint32_t dwc_read_##_reg##_n(void *io_ctx, _container_type *container, int num) { \
  13155. + return DWC_READ_REG32(io_ctx, &container->regs->_reg[num]); \
  13156. +} \
  13157. +static inline void dwc_write_##_reg##_n(void *io_ctx, _container_type *container, int num, uint32_t data) { \
  13158. + DWC_WRITE_REG32(io_ctx, &(((uint32_t*)container->regs->_reg)[num]), data); \
  13159. +}
  13160. +
  13161. +#define dwc_define_read_write_reg(_reg,_container_type) \
  13162. +static inline uint32_t dwc_read_##_reg(void *io_ctx, _container_type *container) { \
  13163. + return DWC_READ_REG32(io_ctx, &container->regs->_reg); \
  13164. +} \
  13165. +static inline void dwc_write_##_reg(void *io_ctx, _container_type *container, uint32_t data) { \
  13166. + DWC_WRITE_REG32(io_ctx, &container->regs->_reg, data); \
  13167. +}
  13168. +
  13169. +# endif /* DWC_DEBUG_REGS */
  13170. +
  13171. +#endif /* DWC_FREEBSD || DWC_NETBSD */
  13172. +
  13173. +/** @endcond */
  13174. +
  13175. +
  13176. +#ifdef DWC_CRYPTOLIB
  13177. +/** @name Crypto Functions
  13178. + *
  13179. + * These are the low-level cryptographic functions used by the driver. */
  13180. +
  13181. +/** Perform AES CBC */
  13182. +extern int DWC_AES_CBC(uint8_t *message, uint32_t messagelen, uint8_t *key, uint32_t keylen, uint8_t iv[16], uint8_t *out);
  13183. +#define dwc_aes_cbc DWC_AES_CBC
  13184. +
  13185. +/** Fill the provided buffer with random bytes. These should be cryptographic grade random numbers. */
  13186. +extern void DWC_RANDOM_BYTES(uint8_t *buffer, uint32_t length);
  13187. +#define dwc_random_bytes DWC_RANDOM_BYTES
  13188. +
  13189. +/** Perform the SHA-256 hash function */
  13190. +extern int DWC_SHA256(uint8_t *message, uint32_t len, uint8_t *out);
  13191. +#define dwc_sha256 DWC_SHA256
  13192. +
  13193. +/** Calculated the HMAC-SHA256 */
  13194. +extern int DWC_HMAC_SHA256(uint8_t *message, uint32_t messagelen, uint8_t *key, uint32_t keylen, uint8_t *out);
  13195. +#define dwc_hmac_sha256 DWC_HMAC_SHA256
  13196. +
  13197. +#endif /* DWC_CRYPTOLIB */
  13198. +
  13199. +
  13200. +/** @name Memory Allocation
  13201. + *
  13202. + * These function provide access to memory allocation. There are only 2 DMA
  13203. + * functions and 3 Regular memory functions that need to be implemented. None
  13204. + * of the memory debugging routines need to be implemented. The allocation
  13205. + * routines all ZERO the contents of the memory.
  13206. + *
  13207. + * Defining DWC_DEBUG_MEMORY turns on memory debugging and statistic gathering.
  13208. + * This checks for memory leaks, keeping track of alloc/free pairs. It also
  13209. + * keeps track of how much memory the driver is using at any given time. */
  13210. +
  13211. +#define DWC_PAGE_SIZE 4096
  13212. +#define DWC_PAGE_OFFSET(addr) (((uint32_t)addr) & 0xfff)
  13213. +#define DWC_PAGE_ALIGNED(addr) ((((uint32_t)addr) & 0xfff) == 0)
  13214. +
  13215. +#define DWC_INVALID_DMA_ADDR 0x0
  13216. +
  13217. +#ifdef DWC_LINUX
  13218. +/** Type for a DMA address */
  13219. +typedef dma_addr_t dwc_dma_t;
  13220. +#endif
  13221. +
  13222. +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
  13223. +typedef bus_addr_t dwc_dma_t;
  13224. +#endif
  13225. +
  13226. +#ifdef DWC_FREEBSD
  13227. +typedef struct dwc_dmactx {
  13228. + struct device *dev;
  13229. + bus_dma_tag_t dma_tag;
  13230. + bus_dmamap_t dma_map;
  13231. + bus_addr_t dma_paddr;
  13232. + void *dma_vaddr;
  13233. +} dwc_dmactx_t;
  13234. +#endif
  13235. +
  13236. +#ifdef DWC_NETBSD
  13237. +typedef struct dwc_dmactx {
  13238. + struct device *dev;
  13239. + bus_dma_tag_t dma_tag;
  13240. + bus_dmamap_t dma_map;
  13241. + bus_dma_segment_t segs[1];
  13242. + int nsegs;
  13243. + bus_addr_t dma_paddr;
  13244. + void *dma_vaddr;
  13245. +} dwc_dmactx_t;
  13246. +#endif
  13247. +
  13248. +/* @todo these functions will be added in the future */
  13249. +#if 0
  13250. +/**
  13251. + * Creates a DMA pool from which you can allocate DMA buffers. Buffers
  13252. + * allocated from this pool will be guaranteed to meet the size, alignment, and
  13253. + * boundary requirements specified.
  13254. + *
  13255. + * @param[in] size Specifies the size of the buffers that will be allocated from
  13256. + * this pool.
  13257. + * @param[in] align Specifies the byte alignment requirements of the buffers
  13258. + * allocated from this pool. Must be a power of 2.
  13259. + * @param[in] boundary Specifies the N-byte boundary that buffers allocated from
  13260. + * this pool must not cross.
  13261. + *
  13262. + * @returns A pointer to an internal opaque structure which is not to be
  13263. + * accessed outside of these library functions. Use this handle to specify
  13264. + * which pools to allocate/free DMA buffers from and also to destroy the pool,
  13265. + * when you are done with it.
  13266. + */
  13267. +extern dwc_pool_t *DWC_DMA_POOL_CREATE(uint32_t size, uint32_t align, uint32_t boundary);
  13268. +
  13269. +/**
  13270. + * Destroy a DMA pool. All buffers allocated from that pool must be freed first.
  13271. + */
  13272. +extern void DWC_DMA_POOL_DESTROY(dwc_pool_t *pool);
  13273. +
  13274. +/**
  13275. + * Allocate a buffer from the specified DMA pool and zeros its contents.
  13276. + */
  13277. +extern void *DWC_DMA_POOL_ALLOC(dwc_pool_t *pool, uint64_t *dma_addr);
  13278. +
  13279. +/**
  13280. + * Free a previously allocated buffer from the DMA pool.
  13281. + */
  13282. +extern void DWC_DMA_POOL_FREE(dwc_pool_t *pool, void *vaddr, void *daddr);
  13283. +#endif
  13284. +
  13285. +/** Allocates a DMA capable buffer and zeroes its contents. */
  13286. +extern void *__DWC_DMA_ALLOC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr);
  13287. +
  13288. +/** Allocates a DMA capable buffer and zeroes its contents in atomic contest */
  13289. +extern void *__DWC_DMA_ALLOC_ATOMIC(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr);
  13290. +
  13291. +/** Frees a previously allocated buffer. */
  13292. +extern void __DWC_DMA_FREE(void *dma_ctx, uint32_t size, void *virt_addr, dwc_dma_t dma_addr);
  13293. +
  13294. +/** Allocates a block of memory and zeroes its contents. */
  13295. +extern void *__DWC_ALLOC(void *mem_ctx, uint32_t size);
  13296. +
  13297. +/** Allocates a block of memory and zeroes its contents, in an atomic manner
  13298. + * which can be used inside interrupt context. The size should be sufficiently
  13299. + * small, a few KB at most, such that failures are not likely to occur. Can just call
  13300. + * __DWC_ALLOC if it is atomic. */
  13301. +extern void *__DWC_ALLOC_ATOMIC(void *mem_ctx, uint32_t size);
  13302. +
  13303. +/** Frees a previously allocated buffer. */
  13304. +extern void __DWC_FREE(void *mem_ctx, void *addr);
  13305. +
  13306. +#ifndef DWC_DEBUG_MEMORY
  13307. +
  13308. +#define DWC_ALLOC(_size_) __DWC_ALLOC(NULL, _size_)
  13309. +#define DWC_ALLOC_ATOMIC(_size_) __DWC_ALLOC_ATOMIC(NULL, _size_)
  13310. +#define DWC_FREE(_addr_) __DWC_FREE(NULL, _addr_)
  13311. +
  13312. +# ifdef DWC_LINUX
  13313. +#define DWC_DMA_ALLOC(_dev, _size_, _dma_) __DWC_DMA_ALLOC(_dev, _size_, _dma_)
  13314. +#define DWC_DMA_ALLOC_ATOMIC(_dev, _size_, _dma_) __DWC_DMA_ALLOC_ATOMIC(_dev, _size_, _dma_)
  13315. +#define DWC_DMA_FREE(_dev, _size_,_virt_, _dma_) __DWC_DMA_FREE(_dev, _size_, _virt_, _dma_)
  13316. +# endif
  13317. +
  13318. +# if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
  13319. +#define DWC_DMA_ALLOC __DWC_DMA_ALLOC
  13320. +#define DWC_DMA_FREE __DWC_DMA_FREE
  13321. +# endif
  13322. +extern void *dwc_dma_alloc_atomic_debug(uint32_t size, dwc_dma_t *dma_addr, char const *func, int line);
  13323. +
  13324. +#else /* DWC_DEBUG_MEMORY */
  13325. +
  13326. +extern void *dwc_alloc_debug(void *mem_ctx, uint32_t size, char const *func, int line);
  13327. +extern void *dwc_alloc_atomic_debug(void *mem_ctx, uint32_t size, char const *func, int line);
  13328. +extern void dwc_free_debug(void *mem_ctx, void *addr, char const *func, int line);
  13329. +extern void *dwc_dma_alloc_debug(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr,
  13330. + char const *func, int line);
  13331. +extern void *dwc_dma_alloc_atomic_debug(void *dma_ctx, uint32_t size, dwc_dma_t *dma_addr,
  13332. + char const *func, int line);
  13333. +extern void dwc_dma_free_debug(void *dma_ctx, uint32_t size, void *virt_addr,
  13334. + dwc_dma_t dma_addr, char const *func, int line);
  13335. +
  13336. +extern int dwc_memory_debug_start(void *mem_ctx);
  13337. +extern void dwc_memory_debug_stop(void);
  13338. +extern void dwc_memory_debug_report(void);
  13339. +
  13340. +#define DWC_ALLOC(_size_) dwc_alloc_debug(NULL, _size_, __func__, __LINE__)
  13341. +#define DWC_ALLOC_ATOMIC(_size_) dwc_alloc_atomic_debug(NULL, _size_, \
  13342. + __func__, __LINE__)
  13343. +#define DWC_FREE(_addr_) dwc_free_debug(NULL, _addr_, __func__, __LINE__)
  13344. +
  13345. +# ifdef DWC_LINUX
  13346. +#define DWC_DMA_ALLOC(_dev, _size_, _dma_) \
  13347. + dwc_dma_alloc_debug(_dev, _size_, _dma_, __func__, __LINE__)
  13348. +#define DWC_DMA_ALLOC_ATOMIC(_dev, _size_, _dma_) \
  13349. + dwc_dma_alloc_atomic_debug(_dev, _size_, _dma_, __func__, __LINE__)
  13350. +#define DWC_DMA_FREE(_dev, _size_, _virt_, _dma_) \
  13351. + dwc_dma_free_debug(_dev, _size_, _virt_, _dma_, __func__, __LINE__)
  13352. +# endif
  13353. +
  13354. +# if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
  13355. +#define DWC_DMA_ALLOC(_ctx_,_size_,_dma_) dwc_dma_alloc_debug(_ctx_, _size_, \
  13356. + _dma_, __func__, __LINE__)
  13357. +#define DWC_DMA_FREE(_ctx_,_size_,_virt_,_dma_) dwc_dma_free_debug(_ctx_, _size_, \
  13358. + _virt_, _dma_, __func__, __LINE__)
  13359. +# endif
  13360. +
  13361. +#endif /* DWC_DEBUG_MEMORY */
  13362. +
  13363. +#define dwc_alloc(_ctx_,_size_) DWC_ALLOC(_size_)
  13364. +#define dwc_alloc_atomic(_ctx_,_size_) DWC_ALLOC_ATOMIC(_size_)
  13365. +#define dwc_free(_ctx_,_addr_) DWC_FREE(_addr_)
  13366. +
  13367. +#ifdef DWC_LINUX
  13368. +/* Linux doesn't need any extra parameters for DMA buffer allocation, so we
  13369. + * just throw away the DMA context parameter.
  13370. + */
  13371. +#define dwc_dma_alloc(_ctx_,_size_,_dma_) DWC_DMA_ALLOC(_size_, _dma_)
  13372. +#define dwc_dma_alloc_atomic(_ctx_,_size_,_dma_) DWC_DMA_ALLOC_ATOMIC(_size_, _dma_)
  13373. +#define dwc_dma_free(_ctx_,_size_,_virt_,_dma_) DWC_DMA_FREE(_size_, _virt_, _dma_)
  13374. +#endif
  13375. +
  13376. +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
  13377. +/** BSD needs several extra parameters for DMA buffer allocation, so we pass
  13378. + * them in using the DMA context parameter.
  13379. + */
  13380. +#define dwc_dma_alloc DWC_DMA_ALLOC
  13381. +#define dwc_dma_free DWC_DMA_FREE
  13382. +#endif
  13383. +
  13384. +
  13385. +/** @name Memory and String Processing */
  13386. +
  13387. +/** memset() clone */
  13388. +extern void *DWC_MEMSET(void *dest, uint8_t byte, uint32_t size);
  13389. +#define dwc_memset DWC_MEMSET
  13390. +
  13391. +/** memcpy() clone */
  13392. +extern void *DWC_MEMCPY(void *dest, void const *src, uint32_t size);
  13393. +#define dwc_memcpy DWC_MEMCPY
  13394. +
  13395. +/** memmove() clone */
  13396. +extern void *DWC_MEMMOVE(void *dest, void *src, uint32_t size);
  13397. +#define dwc_memmove DWC_MEMMOVE
  13398. +
  13399. +/** memcmp() clone */
  13400. +extern int DWC_MEMCMP(void *m1, void *m2, uint32_t size);
  13401. +#define dwc_memcmp DWC_MEMCMP
  13402. +
  13403. +/** strcmp() clone */
  13404. +extern int DWC_STRCMP(void *s1, void *s2);
  13405. +#define dwc_strcmp DWC_STRCMP
  13406. +
  13407. +/** strncmp() clone */
  13408. +extern int DWC_STRNCMP(void *s1, void *s2, uint32_t size);
  13409. +#define dwc_strncmp DWC_STRNCMP
  13410. +
  13411. +/** strlen() clone, for NULL terminated ASCII strings */
  13412. +extern int DWC_STRLEN(char const *str);
  13413. +#define dwc_strlen DWC_STRLEN
  13414. +
  13415. +/** strcpy() clone, for NULL terminated ASCII strings */
  13416. +extern char *DWC_STRCPY(char *to, const char *from);
  13417. +#define dwc_strcpy DWC_STRCPY
  13418. +
  13419. +/** strdup() clone. If you wish to use memory allocation debugging, this
  13420. + * implementation of strdup should use the DWC_* memory routines instead of
  13421. + * calling a predefined strdup. Otherwise the memory allocated by this routine
  13422. + * will not be seen by the debugging routines. */
  13423. +extern char *DWC_STRDUP(char const *str);
  13424. +#define dwc_strdup(_ctx_,_str_) DWC_STRDUP(_str_)
  13425. +
  13426. +/** NOT an atoi() clone. Read the description carefully. Returns an integer
  13427. + * converted from the string str in base 10 unless the string begins with a "0x"
  13428. + * in which case it is base 16. String must be a NULL terminated sequence of
  13429. + * ASCII characters and may optionally begin with whitespace, a + or -, and a
  13430. + * "0x" prefix if base 16. The remaining characters must be valid digits for
  13431. + * the number and end with a NULL character. If any invalid characters are
  13432. + * encountered or it returns with a negative error code and the results of the
  13433. + * conversion are undefined. On sucess it returns 0. Overflow conditions are
  13434. + * undefined. An example implementation using atoi() can be referenced from the
  13435. + * Linux implementation. */
  13436. +extern int DWC_ATOI(const char *str, int32_t *value);
  13437. +#define dwc_atoi DWC_ATOI
  13438. +
  13439. +/** Same as above but for unsigned. */
  13440. +extern int DWC_ATOUI(const char *str, uint32_t *value);
  13441. +#define dwc_atoui DWC_ATOUI
  13442. +
  13443. +#ifdef DWC_UTFLIB
  13444. +/** This routine returns a UTF16LE unicode encoded string from a UTF8 string. */
  13445. +extern int DWC_UTF8_TO_UTF16LE(uint8_t const *utf8string, uint16_t *utf16string, unsigned len);
  13446. +#define dwc_utf8_to_utf16le DWC_UTF8_TO_UTF16LE
  13447. +#endif
  13448. +
  13449. +
  13450. +/** @name Wait queues
  13451. + *
  13452. + * Wait queues provide a means of synchronizing between threads or processes. A
  13453. + * process can block on a waitq if some condition is not true, waiting for it to
  13454. + * become true. When the waitq is triggered all waiting process will get
  13455. + * unblocked and the condition will be check again. Waitqs should be triggered
  13456. + * every time a condition can potentially change.*/
  13457. +struct dwc_waitq;
  13458. +
  13459. +/** Type for a waitq */
  13460. +typedef struct dwc_waitq dwc_waitq_t;
  13461. +
  13462. +/** The type of waitq condition callback function. This is called every time
  13463. + * condition is evaluated. */
  13464. +typedef int (*dwc_waitq_condition_t)(void *data);
  13465. +
  13466. +/** Allocate a waitq */
  13467. +extern dwc_waitq_t *DWC_WAITQ_ALLOC(void);
  13468. +#define dwc_waitq_alloc(_ctx_) DWC_WAITQ_ALLOC()
  13469. +
  13470. +/** Free a waitq */
  13471. +extern void DWC_WAITQ_FREE(dwc_waitq_t *wq);
  13472. +#define dwc_waitq_free DWC_WAITQ_FREE
  13473. +
  13474. +/** Check the condition and if it is false, block on the waitq. When unblocked, check the
  13475. + * condition again. The function returns when the condition becomes true. The return value
  13476. + * is 0 on condition true, DWC_WAITQ_ABORTED on abort or killed, or DWC_WAITQ_UNKNOWN on error. */
  13477. +extern int32_t DWC_WAITQ_WAIT(dwc_waitq_t *wq, dwc_waitq_condition_t cond, void *data);
  13478. +#define dwc_waitq_wait DWC_WAITQ_WAIT
  13479. +
  13480. +/** Check the condition and if it is false, block on the waitq. When unblocked,
  13481. + * check the condition again. The function returns when the condition become
  13482. + * true or the timeout has passed. The return value is 0 on condition true or
  13483. + * DWC_TIMED_OUT on timeout, or DWC_WAITQ_ABORTED, or DWC_WAITQ_UNKNOWN on
  13484. + * error. */
  13485. +extern int32_t DWC_WAITQ_WAIT_TIMEOUT(dwc_waitq_t *wq, dwc_waitq_condition_t cond,
  13486. + void *data, int32_t msecs);
  13487. +#define dwc_waitq_wait_timeout DWC_WAITQ_WAIT_TIMEOUT
  13488. +
  13489. +/** Trigger a waitq, unblocking all processes. This should be called whenever a condition
  13490. + * has potentially changed. */
  13491. +extern void DWC_WAITQ_TRIGGER(dwc_waitq_t *wq);
  13492. +#define dwc_waitq_trigger DWC_WAITQ_TRIGGER
  13493. +
  13494. +/** Unblock all processes waiting on the waitq with an ABORTED result. */
  13495. +extern void DWC_WAITQ_ABORT(dwc_waitq_t *wq);
  13496. +#define dwc_waitq_abort DWC_WAITQ_ABORT
  13497. +
  13498. +
  13499. +/** @name Threads
  13500. + *
  13501. + * A thread must be explicitly stopped. It must check DWC_THREAD_SHOULD_STOP
  13502. + * whenever it is woken up, and then return. The DWC_THREAD_STOP function
  13503. + * returns the value from the thread.
  13504. + */
  13505. +
  13506. +struct dwc_thread;
  13507. +
  13508. +/** Type for a thread */
  13509. +typedef struct dwc_thread dwc_thread_t;
  13510. +
  13511. +/** The thread function */
  13512. +typedef int (*dwc_thread_function_t)(void *data);
  13513. +
  13514. +/** Create a thread and start it running the thread_function. Returns a handle
  13515. + * to the thread */
  13516. +extern dwc_thread_t *DWC_THREAD_RUN(dwc_thread_function_t func, char *name, void *data);
  13517. +#define dwc_thread_run(_ctx_,_func_,_name_,_data_) DWC_THREAD_RUN(_func_, _name_, _data_)
  13518. +
  13519. +/** Stops a thread. Return the value returned by the thread. Or will return
  13520. + * DWC_ABORT if the thread never started. */
  13521. +extern int DWC_THREAD_STOP(dwc_thread_t *thread);
  13522. +#define dwc_thread_stop DWC_THREAD_STOP
  13523. +
  13524. +/** Signifies to the thread that it must stop. */
  13525. +#ifdef DWC_LINUX
  13526. +/* Linux doesn't need any parameters for kthread_should_stop() */
  13527. +extern dwc_bool_t DWC_THREAD_SHOULD_STOP(void);
  13528. +#define dwc_thread_should_stop(_thrd_) DWC_THREAD_SHOULD_STOP()
  13529. +
  13530. +/* No thread_exit function in Linux */
  13531. +#define dwc_thread_exit(_thrd_)
  13532. +#endif
  13533. +
  13534. +#if defined(DWC_FREEBSD) || defined(DWC_NETBSD)
  13535. +/** BSD needs the thread pointer for kthread_suspend_check() */
  13536. +extern dwc_bool_t DWC_THREAD_SHOULD_STOP(dwc_thread_t *thread);
  13537. +#define dwc_thread_should_stop DWC_THREAD_SHOULD_STOP
  13538. +
  13539. +/** The thread must call this to exit. */
  13540. +extern void DWC_THREAD_EXIT(dwc_thread_t *thread);
  13541. +#define dwc_thread_exit DWC_THREAD_EXIT
  13542. +#endif
  13543. +
  13544. +
  13545. +/** @name Work queues
  13546. + *
  13547. + * Workqs are used to queue a callback function to be called at some later time,
  13548. + * in another thread. */
  13549. +struct dwc_workq;
  13550. +
  13551. +/** Type for a workq */
  13552. +typedef struct dwc_workq dwc_workq_t;
  13553. +
  13554. +/** The type of the callback function to be called. */
  13555. +typedef void (*dwc_work_callback_t)(void *data);
  13556. +
  13557. +/** Allocate a workq */
  13558. +extern dwc_workq_t *DWC_WORKQ_ALLOC(char *name);
  13559. +#define dwc_workq_alloc(_ctx_,_name_) DWC_WORKQ_ALLOC(_name_)
  13560. +
  13561. +/** Free a workq. All work must be completed before being freed. */
  13562. +extern void DWC_WORKQ_FREE(dwc_workq_t *workq);
  13563. +#define dwc_workq_free DWC_WORKQ_FREE
  13564. +
  13565. +/** Schedule a callback on the workq, passing in data. The function will be
  13566. + * scheduled at some later time. */
  13567. +extern void DWC_WORKQ_SCHEDULE(dwc_workq_t *workq, dwc_work_callback_t cb,
  13568. + void *data, char *format, ...)
  13569. +#ifdef __GNUC__
  13570. + __attribute__ ((format(printf, 4, 5)));
  13571. +#else
  13572. + ;
  13573. +#endif
  13574. +#define dwc_workq_schedule DWC_WORKQ_SCHEDULE
  13575. +
  13576. +/** Schedule a callback on the workq, that will be called until at least
  13577. + * given number miliseconds have passed. */
  13578. +extern void DWC_WORKQ_SCHEDULE_DELAYED(dwc_workq_t *workq, dwc_work_callback_t cb,
  13579. + void *data, uint32_t time, char *format, ...)
  13580. +#ifdef __GNUC__
  13581. + __attribute__ ((format(printf, 5, 6)));
  13582. +#else
  13583. + ;
  13584. +#endif
  13585. +#define dwc_workq_schedule_delayed DWC_WORKQ_SCHEDULE_DELAYED
  13586. +
  13587. +/** The number of processes in the workq */
  13588. +extern int DWC_WORKQ_PENDING(dwc_workq_t *workq);
  13589. +#define dwc_workq_pending DWC_WORKQ_PENDING
  13590. +
  13591. +/** Blocks until all the work in the workq is complete or timed out. Returns <
  13592. + * 0 on timeout. */
  13593. +extern int DWC_WORKQ_WAIT_WORK_DONE(dwc_workq_t *workq, int timeout);
  13594. +#define dwc_workq_wait_work_done DWC_WORKQ_WAIT_WORK_DONE
  13595. +
  13596. +
  13597. +/** @name Tasklets
  13598. + *
  13599. + */
  13600. +struct dwc_tasklet;
  13601. +
  13602. +/** Type for a tasklet */
  13603. +typedef struct dwc_tasklet dwc_tasklet_t;
  13604. +
  13605. +/** The type of the callback function to be called */
  13606. +typedef void (*dwc_tasklet_callback_t)(void *data);
  13607. +
  13608. +/** Allocates a tasklet */
  13609. +extern dwc_tasklet_t *DWC_TASK_ALLOC(char *name, dwc_tasklet_callback_t cb, void *data);
  13610. +#define dwc_task_alloc(_ctx_,_name_,_cb_,_data_) DWC_TASK_ALLOC(_name_, _cb_, _data_)
  13611. +
  13612. +/** Frees a tasklet */
  13613. +extern void DWC_TASK_FREE(dwc_tasklet_t *task);
  13614. +#define dwc_task_free DWC_TASK_FREE
  13615. +
  13616. +/** Schedules a tasklet to run */
  13617. +extern void DWC_TASK_SCHEDULE(dwc_tasklet_t *task);
  13618. +#define dwc_task_schedule DWC_TASK_SCHEDULE
  13619. +
  13620. +extern void DWC_TASK_HI_SCHEDULE(dwc_tasklet_t *task);
  13621. +#define dwc_task_hi_schedule DWC_TASK_HI_SCHEDULE
  13622. +
  13623. +/** @name Timer
  13624. + *
  13625. + * Callbacks must be small and atomic.
  13626. + */
  13627. +struct dwc_timer;
  13628. +
  13629. +/** Type for a timer */
  13630. +typedef struct dwc_timer dwc_timer_t;
  13631. +
  13632. +/** The type of the callback function to be called */
  13633. +typedef void (*dwc_timer_callback_t)(void *data);
  13634. +
  13635. +/** Allocates a timer */
  13636. +extern dwc_timer_t *DWC_TIMER_ALLOC(char *name, dwc_timer_callback_t cb, void *data);
  13637. +#define dwc_timer_alloc(_ctx_,_name_,_cb_,_data_) DWC_TIMER_ALLOC(_name_,_cb_,_data_)
  13638. +
  13639. +/** Frees a timer */
  13640. +extern void DWC_TIMER_FREE(dwc_timer_t *timer);
  13641. +#define dwc_timer_free DWC_TIMER_FREE
  13642. +
  13643. +/** Schedules the timer to run at time ms from now. And will repeat at every
  13644. + * repeat_interval msec therafter
  13645. + *
  13646. + * Modifies a timer that is still awaiting execution to a new expiration time.
  13647. + * The mod_time is added to the old time. */
  13648. +extern void DWC_TIMER_SCHEDULE(dwc_timer_t *timer, uint32_t time);
  13649. +#define dwc_timer_schedule DWC_TIMER_SCHEDULE
  13650. +
  13651. +/** Disables the timer from execution. */
  13652. +extern void DWC_TIMER_CANCEL(dwc_timer_t *timer);
  13653. +#define dwc_timer_cancel DWC_TIMER_CANCEL
  13654. +
  13655. +
  13656. +/** @name Spinlocks
  13657. + *
  13658. + * These locks are used when the work between the lock/unlock is atomic and
  13659. + * short. Interrupts are also disabled during the lock/unlock and thus they are
  13660. + * suitable to lock between interrupt/non-interrupt context. They also lock
  13661. + * between processes if you have multiple CPUs or Preemption. If you don't have
  13662. + * multiple CPUS or Preemption, then the you can simply implement the
  13663. + * DWC_SPINLOCK and DWC_SPINUNLOCK to disable and enable interrupts. Because
  13664. + * the work between the lock/unlock is atomic, the process context will never
  13665. + * change, and so you never have to lock between processes. */
  13666. +
  13667. +struct dwc_spinlock;
  13668. +
  13669. +/** Type for a spinlock */
  13670. +typedef struct dwc_spinlock dwc_spinlock_t;
  13671. +
  13672. +/** Type for the 'flags' argument to spinlock funtions */
  13673. +typedef unsigned long dwc_irqflags_t;
  13674. +
  13675. +/** Returns an initialized lock variable. This function should allocate and
  13676. + * initialize the OS-specific data structure used for locking. This data
  13677. + * structure is to be used for the DWC_LOCK and DWC_UNLOCK functions and should
  13678. + * be freed by the DWC_FREE_LOCK when it is no longer used.
  13679. + *
  13680. + * For Linux Spinlock Debugging make it macro because the debugging routines use
  13681. + * the symbol name to determine recursive locking. Using a wrapper function
  13682. + * makes it falsely think recursive locking occurs. */
  13683. +#if defined(DWC_LINUX) && defined(CONFIG_DEBUG_SPINLOCK)
  13684. +#define DWC_SPINLOCK_ALLOC_LINUX_DEBUG(lock) ({ \
  13685. + lock = DWC_ALLOC(sizeof(spinlock_t)); \
  13686. + if (lock) { \
  13687. + spin_lock_init((spinlock_t *)lock); \
  13688. + } \
  13689. +})
  13690. +#else
  13691. +extern dwc_spinlock_t *DWC_SPINLOCK_ALLOC(void);
  13692. +#define dwc_spinlock_alloc(_ctx_) DWC_SPINLOCK_ALLOC()
  13693. +#endif
  13694. +
  13695. +/** Frees an initialized lock variable. */
  13696. +extern void DWC_SPINLOCK_FREE(dwc_spinlock_t *lock);
  13697. +#define dwc_spinlock_free(_ctx_,_lock_) DWC_SPINLOCK_FREE(_lock_)
  13698. +
  13699. +/** Disables interrupts and blocks until it acquires the lock.
  13700. + *
  13701. + * @param lock Pointer to the spinlock.
  13702. + * @param flags Unsigned long for irq flags storage.
  13703. + */
  13704. +extern void DWC_SPINLOCK_IRQSAVE(dwc_spinlock_t *lock, dwc_irqflags_t *flags);
  13705. +#define dwc_spinlock_irqsave DWC_SPINLOCK_IRQSAVE
  13706. +
  13707. +/** Re-enables the interrupt and releases the lock.
  13708. + *
  13709. + * @param lock Pointer to the spinlock.
  13710. + * @param flags Unsigned long for irq flags storage. Must be the same as was
  13711. + * passed into DWC_LOCK.
  13712. + */
  13713. +extern void DWC_SPINUNLOCK_IRQRESTORE(dwc_spinlock_t *lock, dwc_irqflags_t flags);
  13714. +#define dwc_spinunlock_irqrestore DWC_SPINUNLOCK_IRQRESTORE
  13715. +
  13716. +/** Blocks until it acquires the lock.
  13717. + *
  13718. + * @param lock Pointer to the spinlock.
  13719. + */
  13720. +extern void DWC_SPINLOCK(dwc_spinlock_t *lock);
  13721. +#define dwc_spinlock DWC_SPINLOCK
  13722. +
  13723. +/** Releases the lock.
  13724. + *
  13725. + * @param lock Pointer to the spinlock.
  13726. + */
  13727. +extern void DWC_SPINUNLOCK(dwc_spinlock_t *lock);
  13728. +#define dwc_spinunlock DWC_SPINUNLOCK
  13729. +
  13730. +
  13731. +/** @name Mutexes
  13732. + *
  13733. + * Unlike spinlocks Mutexes lock only between processes and the work between the
  13734. + * lock/unlock CAN block, therefore it CANNOT be called from interrupt context.
  13735. + */
  13736. +
  13737. +struct dwc_mutex;
  13738. +
  13739. +/** Type for a mutex */
  13740. +typedef struct dwc_mutex dwc_mutex_t;
  13741. +
  13742. +/* For Linux Mutex Debugging make it inline because the debugging routines use
  13743. + * the symbol to determine recursive locking. This makes it falsely think
  13744. + * recursive locking occurs. */
  13745. +#if defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES)
  13746. +#define DWC_MUTEX_ALLOC_LINUX_DEBUG(__mutexp) ({ \
  13747. + __mutexp = (dwc_mutex_t *)DWC_ALLOC(sizeof(struct mutex)); \
  13748. + mutex_init((struct mutex *)__mutexp); \
  13749. +})
  13750. +#endif
  13751. +
  13752. +/** Allocate a mutex */
  13753. +extern dwc_mutex_t *DWC_MUTEX_ALLOC(void);
  13754. +#define dwc_mutex_alloc(_ctx_) DWC_MUTEX_ALLOC()
  13755. +
  13756. +/* For memory leak debugging when using Linux Mutex Debugging */
  13757. +#if defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES)
  13758. +#define DWC_MUTEX_FREE(__mutexp) do { \
  13759. + mutex_destroy((struct mutex *)__mutexp); \
  13760. + DWC_FREE(__mutexp); \
  13761. +} while(0)
  13762. +#else
  13763. +/** Free a mutex */
  13764. +extern void DWC_MUTEX_FREE(dwc_mutex_t *mutex);
  13765. +#define dwc_mutex_free(_ctx_,_mutex_) DWC_MUTEX_FREE(_mutex_)
  13766. +#endif
  13767. +
  13768. +/** Lock a mutex */
  13769. +extern void DWC_MUTEX_LOCK(dwc_mutex_t *mutex);
  13770. +#define dwc_mutex_lock DWC_MUTEX_LOCK
  13771. +
  13772. +/** Non-blocking lock returns 1 on successful lock. */
  13773. +extern int DWC_MUTEX_TRYLOCK(dwc_mutex_t *mutex);
  13774. +#define dwc_mutex_trylock DWC_MUTEX_TRYLOCK
  13775. +
  13776. +/** Unlock a mutex */
  13777. +extern void DWC_MUTEX_UNLOCK(dwc_mutex_t *mutex);
  13778. +#define dwc_mutex_unlock DWC_MUTEX_UNLOCK
  13779. +
  13780. +
  13781. +/** @name Time */
  13782. +
  13783. +/** Microsecond delay.
  13784. + *
  13785. + * @param usecs Microseconds to delay.
  13786. + */
  13787. +extern void DWC_UDELAY(uint32_t usecs);
  13788. +#define dwc_udelay DWC_UDELAY
  13789. +
  13790. +/** Millisecond delay.
  13791. + *
  13792. + * @param msecs Milliseconds to delay.
  13793. + */
  13794. +extern void DWC_MDELAY(uint32_t msecs);
  13795. +#define dwc_mdelay DWC_MDELAY
  13796. +
  13797. +/** Non-busy waiting.
  13798. + * Sleeps for specified number of milliseconds.
  13799. + *
  13800. + * @param msecs Milliseconds to sleep.
  13801. + */
  13802. +extern void DWC_MSLEEP(uint32_t msecs);
  13803. +#define dwc_msleep DWC_MSLEEP
  13804. +
  13805. +/**
  13806. + * Returns number of milliseconds since boot.
  13807. + */
  13808. +extern uint32_t DWC_TIME(void);
  13809. +#define dwc_time DWC_TIME
  13810. +
  13811. +
  13812. +
  13813. +
  13814. +/* @mainpage DWC Portability and Common Library
  13815. + *
  13816. + * This is the documentation for the DWC Portability and Common Library.
  13817. + *
  13818. + * @section intro Introduction
  13819. + *
  13820. + * The DWC Portability library consists of wrapper calls and data structures to
  13821. + * all low-level functions which are typically provided by the OS. The WUDEV
  13822. + * driver uses only these functions. In order to port the WUDEV driver, only
  13823. + * the functions in this library need to be re-implemented, with the same
  13824. + * behavior as documented here.
  13825. + *
  13826. + * The Common library consists of higher level functions, which rely only on
  13827. + * calling the functions from the DWC Portability library. These common
  13828. + * routines are shared across modules. Some of the common libraries need to be
  13829. + * used directly by the driver programmer when porting WUDEV. Such as the
  13830. + * parameter and notification libraries.
  13831. + *
  13832. + * @section low Portability Library OS Wrapper Functions
  13833. + *
  13834. + * Any function starting with DWC and in all CAPS is a low-level OS-wrapper that
  13835. + * needs to be implemented when porting, for example DWC_MUTEX_ALLOC(). All of
  13836. + * these functions are included in the dwc_os.h file.
  13837. + *
  13838. + * There are many functions here covering a wide array of OS services. Please
  13839. + * see dwc_os.h for details, and implementation notes for each function.
  13840. + *
  13841. + * @section common Common Library Functions
  13842. + *
  13843. + * Any function starting with dwc and in all lowercase is a common library
  13844. + * routine. These functions have a portable implementation and do not need to
  13845. + * be reimplemented when porting. The common routines can be used by any
  13846. + * driver, and some must be used by the end user to control the drivers. For
  13847. + * example, you must use the Parameter common library in order to set the
  13848. + * parameters in the WUDEV module.
  13849. + *
  13850. + * The common libraries consist of the following:
  13851. + *
  13852. + * - Connection Contexts - Used internally and can be used by end-user. See dwc_cc.h
  13853. + * - Parameters - Used internally and can be used by end-user. See dwc_params.h
  13854. + * - Notifications - Used internally and can be used by end-user. See dwc_notifier.h
  13855. + * - Lists - Used internally and can be used by end-user. See dwc_list.h
  13856. + * - Memory Debugging - Used internally and can be used by end-user. See dwc_os.h
  13857. + * - Modpow - Used internally only. See dwc_modpow.h
  13858. + * - DH - Used internally only. See dwc_dh.h
  13859. + * - Crypto - Used internally only. See dwc_crypto.h
  13860. + *
  13861. + *
  13862. + * @section prereq Prerequistes For dwc_os.h
  13863. + * @subsection types Data Types
  13864. + *
  13865. + * The dwc_os.h file assumes that several low-level data types are pre defined for the
  13866. + * compilation environment. These data types are:
  13867. + *
  13868. + * - uint8_t - unsigned 8-bit data type
  13869. + * - int8_t - signed 8-bit data type
  13870. + * - uint16_t - unsigned 16-bit data type
  13871. + * - int16_t - signed 16-bit data type
  13872. + * - uint32_t - unsigned 32-bit data type
  13873. + * - int32_t - signed 32-bit data type
  13874. + * - uint64_t - unsigned 64-bit data type
  13875. + * - int64_t - signed 64-bit data type
  13876. + *
  13877. + * Ensure that these are defined before using dwc_os.h. The easiest way to do
  13878. + * that is to modify the top of the file to include the appropriate header.
  13879. + * This is already done for the Linux environment. If the DWC_LINUX macro is
  13880. + * defined, the correct header will be added. A standard header <stdint.h> is
  13881. + * also used for environments where standard C headers are available.
  13882. + *
  13883. + * @subsection stdarg Variable Arguments
  13884. + *
  13885. + * Variable arguments are provided by a standard C header <stdarg.h>. it is
  13886. + * available in Both the Linux and ANSI C enviornment. An equivalent must be
  13887. + * provided in your enviornment in order to use dwc_os.h with the debug and
  13888. + * tracing message functionality.
  13889. + *
  13890. + * @subsection thread Threading
  13891. + *
  13892. + * WUDEV Core must be run on an operating system that provides for multiple
  13893. + * threads/processes. Threading can be implemented in many ways, even in
  13894. + * embedded systems without an operating system. At the bare minimum, the
  13895. + * system should be able to start any number of processes at any time to handle
  13896. + * special work. It need not be a pre-emptive system. Process context can
  13897. + * change upon a call to a blocking function. The hardware interrupt context
  13898. + * that calls the module's ISR() function must be differentiable from process
  13899. + * context, even if your processes are impemented via a hardware interrupt.
  13900. + * Further locking mechanism between process must exist (or be implemented), and
  13901. + * process context must have a way to disable interrupts for a period of time to
  13902. + * lock them out. If all of this exists, the functions in dwc_os.h related to
  13903. + * threading should be able to be implemented with the defined behavior.
  13904. + *
  13905. + */
  13906. +
  13907. +#ifdef __cplusplus
  13908. +}
  13909. +#endif
  13910. +
  13911. +#endif /* _DWC_OS_H_ */
  13912. --- /dev/null
  13913. +++ b/drivers/usb/host/dwc_common_port/usb.h
  13914. @@ -0,0 +1,946 @@
  13915. +/*
  13916. + * Copyright (c) 1998 The NetBSD Foundation, Inc.
  13917. + * All rights reserved.
  13918. + *
  13919. + * This code is derived from software contributed to The NetBSD Foundation
  13920. + * by Lennart Augustsson ([email protected]) at
  13921. + * Carlstedt Research & Technology.
  13922. + *
  13923. + * Redistribution and use in source and binary forms, with or without
  13924. + * modification, are permitted provided that the following conditions
  13925. + * are met:
  13926. + * 1. Redistributions of source code must retain the above copyright
  13927. + * notice, this list of conditions and the following disclaimer.
  13928. + * 2. Redistributions in binary form must reproduce the above copyright
  13929. + * notice, this list of conditions and the following disclaimer in the
  13930. + * documentation and/or other materials provided with the distribution.
  13931. + * 3. All advertising materials mentioning features or use of this software
  13932. + * must display the following acknowledgement:
  13933. + * This product includes software developed by the NetBSD
  13934. + * Foundation, Inc. and its contributors.
  13935. + * 4. Neither the name of The NetBSD Foundation nor the names of its
  13936. + * contributors may be used to endorse or promote products derived
  13937. + * from this software without specific prior written permission.
  13938. + *
  13939. + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
  13940. + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  13941. + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  13942. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
  13943. + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  13944. + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  13945. + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  13946. + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  13947. + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  13948. + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  13949. + * POSSIBILITY OF SUCH DAMAGE.
  13950. + */
  13951. +
  13952. +/* Modified by Synopsys, Inc, 12/12/2007 */
  13953. +
  13954. +
  13955. +#ifndef _USB_H_
  13956. +#define _USB_H_
  13957. +
  13958. +#ifdef __cplusplus
  13959. +extern "C" {
  13960. +#endif
  13961. +
  13962. +/*
  13963. + * The USB records contain some unaligned little-endian word
  13964. + * components. The U[SG]ETW macros take care of both the alignment
  13965. + * and endian problem and should always be used to access non-byte
  13966. + * values.
  13967. + */
  13968. +typedef u_int8_t uByte;
  13969. +typedef u_int8_t uWord[2];
  13970. +typedef u_int8_t uDWord[4];
  13971. +
  13972. +#define USETW2(w,h,l) ((w)[0] = (u_int8_t)(l), (w)[1] = (u_int8_t)(h))
  13973. +#define UCONSTW(x) { (x) & 0xff, ((x) >> 8) & 0xff }
  13974. +#define UCONSTDW(x) { (x) & 0xff, ((x) >> 8) & 0xff, \
  13975. + ((x) >> 16) & 0xff, ((x) >> 24) & 0xff }
  13976. +
  13977. +#if 1
  13978. +#define UGETW(w) ((w)[0] | ((w)[1] << 8))
  13979. +#define USETW(w,v) ((w)[0] = (u_int8_t)(v), (w)[1] = (u_int8_t)((v) >> 8))
  13980. +#define UGETDW(w) ((w)[0] | ((w)[1] << 8) | ((w)[2] << 16) | ((w)[3] << 24))
  13981. +#define USETDW(w,v) ((w)[0] = (u_int8_t)(v), \
  13982. + (w)[1] = (u_int8_t)((v) >> 8), \
  13983. + (w)[2] = (u_int8_t)((v) >> 16), \
  13984. + (w)[3] = (u_int8_t)((v) >> 24))
  13985. +#else
  13986. +/*
  13987. + * On little-endian machines that can handle unanliged accesses
  13988. + * (e.g. i386) these macros can be replaced by the following.
  13989. + */
  13990. +#define UGETW(w) (*(u_int16_t *)(w))
  13991. +#define USETW(w,v) (*(u_int16_t *)(w) = (v))
  13992. +#define UGETDW(w) (*(u_int32_t *)(w))
  13993. +#define USETDW(w,v) (*(u_int32_t *)(w) = (v))
  13994. +#endif
  13995. +
  13996. +/*
  13997. + * Macros for accessing UAS IU fields, which are big-endian
  13998. + */
  13999. +#define IUSETW2(w,h,l) ((w)[0] = (u_int8_t)(h), (w)[1] = (u_int8_t)(l))
  14000. +#define IUCONSTW(x) { ((x) >> 8) & 0xff, (x) & 0xff }
  14001. +#define IUCONSTDW(x) { ((x) >> 24) & 0xff, ((x) >> 16) & 0xff, \
  14002. + ((x) >> 8) & 0xff, (x) & 0xff }
  14003. +#define IUGETW(w) (((w)[0] << 8) | (w)[1])
  14004. +#define IUSETW(w,v) ((w)[0] = (u_int8_t)((v) >> 8), (w)[1] = (u_int8_t)(v))
  14005. +#define IUGETDW(w) (((w)[0] << 24) | ((w)[1] << 16) | ((w)[2] << 8) | (w)[3])
  14006. +#define IUSETDW(w,v) ((w)[0] = (u_int8_t)((v) >> 24), \
  14007. + (w)[1] = (u_int8_t)((v) >> 16), \
  14008. + (w)[2] = (u_int8_t)((v) >> 8), \
  14009. + (w)[3] = (u_int8_t)(v))
  14010. +
  14011. +#define UPACKED __attribute__((__packed__))
  14012. +
  14013. +typedef struct {
  14014. + uByte bmRequestType;
  14015. + uByte bRequest;
  14016. + uWord wValue;
  14017. + uWord wIndex;
  14018. + uWord wLength;
  14019. +} UPACKED usb_device_request_t;
  14020. +
  14021. +#define UT_GET_DIR(a) ((a) & 0x80)
  14022. +#define UT_WRITE 0x00
  14023. +#define UT_READ 0x80
  14024. +
  14025. +#define UT_GET_TYPE(a) ((a) & 0x60)
  14026. +#define UT_STANDARD 0x00
  14027. +#define UT_CLASS 0x20
  14028. +#define UT_VENDOR 0x40
  14029. +
  14030. +#define UT_GET_RECIPIENT(a) ((a) & 0x1f)
  14031. +#define UT_DEVICE 0x00
  14032. +#define UT_INTERFACE 0x01
  14033. +#define UT_ENDPOINT 0x02
  14034. +#define UT_OTHER 0x03
  14035. +
  14036. +#define UT_READ_DEVICE (UT_READ | UT_STANDARD | UT_DEVICE)
  14037. +#define UT_READ_INTERFACE (UT_READ | UT_STANDARD | UT_INTERFACE)
  14038. +#define UT_READ_ENDPOINT (UT_READ | UT_STANDARD | UT_ENDPOINT)
  14039. +#define UT_WRITE_DEVICE (UT_WRITE | UT_STANDARD | UT_DEVICE)
  14040. +#define UT_WRITE_INTERFACE (UT_WRITE | UT_STANDARD | UT_INTERFACE)
  14041. +#define UT_WRITE_ENDPOINT (UT_WRITE | UT_STANDARD | UT_ENDPOINT)
  14042. +#define UT_READ_CLASS_DEVICE (UT_READ | UT_CLASS | UT_DEVICE)
  14043. +#define UT_READ_CLASS_INTERFACE (UT_READ | UT_CLASS | UT_INTERFACE)
  14044. +#define UT_READ_CLASS_OTHER (UT_READ | UT_CLASS | UT_OTHER)
  14045. +#define UT_READ_CLASS_ENDPOINT (UT_READ | UT_CLASS | UT_ENDPOINT)
  14046. +#define UT_WRITE_CLASS_DEVICE (UT_WRITE | UT_CLASS | UT_DEVICE)
  14047. +#define UT_WRITE_CLASS_INTERFACE (UT_WRITE | UT_CLASS | UT_INTERFACE)
  14048. +#define UT_WRITE_CLASS_OTHER (UT_WRITE | UT_CLASS | UT_OTHER)
  14049. +#define UT_WRITE_CLASS_ENDPOINT (UT_WRITE | UT_CLASS | UT_ENDPOINT)
  14050. +#define UT_READ_VENDOR_DEVICE (UT_READ | UT_VENDOR | UT_DEVICE)
  14051. +#define UT_READ_VENDOR_INTERFACE (UT_READ | UT_VENDOR | UT_INTERFACE)
  14052. +#define UT_READ_VENDOR_OTHER (UT_READ | UT_VENDOR | UT_OTHER)
  14053. +#define UT_READ_VENDOR_ENDPOINT (UT_READ | UT_VENDOR | UT_ENDPOINT)
  14054. +#define UT_WRITE_VENDOR_DEVICE (UT_WRITE | UT_VENDOR | UT_DEVICE)
  14055. +#define UT_WRITE_VENDOR_INTERFACE (UT_WRITE | UT_VENDOR | UT_INTERFACE)
  14056. +#define UT_WRITE_VENDOR_OTHER (UT_WRITE | UT_VENDOR | UT_OTHER)
  14057. +#define UT_WRITE_VENDOR_ENDPOINT (UT_WRITE | UT_VENDOR | UT_ENDPOINT)
  14058. +
  14059. +/* Requests */
  14060. +#define UR_GET_STATUS 0x00
  14061. +#define USTAT_STANDARD_STATUS 0x00
  14062. +#define WUSTAT_WUSB_FEATURE 0x01
  14063. +#define WUSTAT_CHANNEL_INFO 0x02
  14064. +#define WUSTAT_RECEIVED_DATA 0x03
  14065. +#define WUSTAT_MAS_AVAILABILITY 0x04
  14066. +#define WUSTAT_CURRENT_TRANSMIT_POWER 0x05
  14067. +#define UR_CLEAR_FEATURE 0x01
  14068. +#define UR_SET_FEATURE 0x03
  14069. +#define UR_SET_AND_TEST_FEATURE 0x0c
  14070. +#define UR_SET_ADDRESS 0x05
  14071. +#define UR_GET_DESCRIPTOR 0x06
  14072. +#define UDESC_DEVICE 0x01
  14073. +#define UDESC_CONFIG 0x02
  14074. +#define UDESC_STRING 0x03
  14075. +#define UDESC_INTERFACE 0x04
  14076. +#define UDESC_ENDPOINT 0x05
  14077. +#define UDESC_SS_USB_COMPANION 0x30
  14078. +#define UDESC_DEVICE_QUALIFIER 0x06
  14079. +#define UDESC_OTHER_SPEED_CONFIGURATION 0x07
  14080. +#define UDESC_INTERFACE_POWER 0x08
  14081. +#define UDESC_OTG 0x09
  14082. +#define WUDESC_SECURITY 0x0c
  14083. +#define WUDESC_KEY 0x0d
  14084. +#define WUD_GET_KEY_INDEX(_wValue_) ((_wValue_) & 0xf)
  14085. +#define WUD_GET_KEY_TYPE(_wValue_) (((_wValue_) & 0x30) >> 4)
  14086. +#define WUD_KEY_TYPE_ASSOC 0x01
  14087. +#define WUD_KEY_TYPE_GTK 0x02
  14088. +#define WUD_GET_KEY_ORIGIN(_wValue_) (((_wValue_) & 0x40) >> 6)
  14089. +#define WUD_KEY_ORIGIN_HOST 0x00
  14090. +#define WUD_KEY_ORIGIN_DEVICE 0x01
  14091. +#define WUDESC_ENCRYPTION_TYPE 0x0e
  14092. +#define WUDESC_BOS 0x0f
  14093. +#define WUDESC_DEVICE_CAPABILITY 0x10
  14094. +#define WUDESC_WIRELESS_ENDPOINT_COMPANION 0x11
  14095. +#define UDESC_BOS 0x0f
  14096. +#define UDESC_DEVICE_CAPABILITY 0x10
  14097. +#define UDESC_CS_DEVICE 0x21 /* class specific */
  14098. +#define UDESC_CS_CONFIG 0x22
  14099. +#define UDESC_CS_STRING 0x23
  14100. +#define UDESC_CS_INTERFACE 0x24
  14101. +#define UDESC_CS_ENDPOINT 0x25
  14102. +#define UDESC_HUB 0x29
  14103. +#define UR_SET_DESCRIPTOR 0x07
  14104. +#define UR_GET_CONFIG 0x08
  14105. +#define UR_SET_CONFIG 0x09
  14106. +#define UR_GET_INTERFACE 0x0a
  14107. +#define UR_SET_INTERFACE 0x0b
  14108. +#define UR_SYNCH_FRAME 0x0c
  14109. +#define WUR_SET_ENCRYPTION 0x0d
  14110. +#define WUR_GET_ENCRYPTION 0x0e
  14111. +#define WUR_SET_HANDSHAKE 0x0f
  14112. +#define WUR_GET_HANDSHAKE 0x10
  14113. +#define WUR_SET_CONNECTION 0x11
  14114. +#define WUR_SET_SECURITY_DATA 0x12
  14115. +#define WUR_GET_SECURITY_DATA 0x13
  14116. +#define WUR_SET_WUSB_DATA 0x14
  14117. +#define WUDATA_DRPIE_INFO 0x01
  14118. +#define WUDATA_TRANSMIT_DATA 0x02
  14119. +#define WUDATA_TRANSMIT_PARAMS 0x03
  14120. +#define WUDATA_RECEIVE_PARAMS 0x04
  14121. +#define WUDATA_TRANSMIT_POWER 0x05
  14122. +#define WUR_LOOPBACK_DATA_WRITE 0x15
  14123. +#define WUR_LOOPBACK_DATA_READ 0x16
  14124. +#define WUR_SET_INTERFACE_DS 0x17
  14125. +
  14126. +/* Feature numbers */
  14127. +#define UF_ENDPOINT_HALT 0
  14128. +#define UF_DEVICE_REMOTE_WAKEUP 1
  14129. +#define UF_TEST_MODE 2
  14130. +#define UF_DEVICE_B_HNP_ENABLE 3
  14131. +#define UF_DEVICE_A_HNP_SUPPORT 4
  14132. +#define UF_DEVICE_A_ALT_HNP_SUPPORT 5
  14133. +#define WUF_WUSB 3
  14134. +#define WUF_TX_DRPIE 0x0
  14135. +#define WUF_DEV_XMIT_PACKET 0x1
  14136. +#define WUF_COUNT_PACKETS 0x2
  14137. +#define WUF_CAPTURE_PACKETS 0x3
  14138. +#define UF_FUNCTION_SUSPEND 0
  14139. +#define UF_U1_ENABLE 48
  14140. +#define UF_U2_ENABLE 49
  14141. +#define UF_LTM_ENABLE 50
  14142. +
  14143. +/* Class requests from the USB 2.0 hub spec, table 11-15 */
  14144. +#define UCR_CLEAR_HUB_FEATURE (0x2000 | UR_CLEAR_FEATURE)
  14145. +#define UCR_CLEAR_PORT_FEATURE (0x2300 | UR_CLEAR_FEATURE)
  14146. +#define UCR_GET_HUB_DESCRIPTOR (0xa000 | UR_GET_DESCRIPTOR)
  14147. +#define UCR_GET_HUB_STATUS (0xa000 | UR_GET_STATUS)
  14148. +#define UCR_GET_PORT_STATUS (0xa300 | UR_GET_STATUS)
  14149. +#define UCR_SET_HUB_FEATURE (0x2000 | UR_SET_FEATURE)
  14150. +#define UCR_SET_PORT_FEATURE (0x2300 | UR_SET_FEATURE)
  14151. +#define UCR_SET_AND_TEST_PORT_FEATURE (0xa300 | UR_SET_AND_TEST_FEATURE)
  14152. +
  14153. +#ifdef _MSC_VER
  14154. +#include <pshpack1.h>
  14155. +#endif
  14156. +
  14157. +typedef struct {
  14158. + uByte bLength;
  14159. + uByte bDescriptorType;
  14160. + uByte bDescriptorSubtype;
  14161. +} UPACKED usb_descriptor_t;
  14162. +
  14163. +typedef struct {
  14164. + uByte bLength;
  14165. + uByte bDescriptorType;
  14166. +} UPACKED usb_descriptor_header_t;
  14167. +
  14168. +typedef struct {
  14169. + uByte bLength;
  14170. + uByte bDescriptorType;
  14171. + uWord bcdUSB;
  14172. +#define UD_USB_2_0 0x0200
  14173. +#define UD_IS_USB2(d) (UGETW((d)->bcdUSB) >= UD_USB_2_0)
  14174. + uByte bDeviceClass;
  14175. + uByte bDeviceSubClass;
  14176. + uByte bDeviceProtocol;
  14177. + uByte bMaxPacketSize;
  14178. + /* The fields below are not part of the initial descriptor. */
  14179. + uWord idVendor;
  14180. + uWord idProduct;
  14181. + uWord bcdDevice;
  14182. + uByte iManufacturer;
  14183. + uByte iProduct;
  14184. + uByte iSerialNumber;
  14185. + uByte bNumConfigurations;
  14186. +} UPACKED usb_device_descriptor_t;
  14187. +#define USB_DEVICE_DESCRIPTOR_SIZE 18
  14188. +
  14189. +typedef struct {
  14190. + uByte bLength;
  14191. + uByte bDescriptorType;
  14192. + uWord wTotalLength;
  14193. + uByte bNumInterface;
  14194. + uByte bConfigurationValue;
  14195. + uByte iConfiguration;
  14196. +#define UC_ATT_ONE (1 << 7) /* must be set */
  14197. +#define UC_ATT_SELFPOWER (1 << 6) /* self powered */
  14198. +#define UC_ATT_WAKEUP (1 << 5) /* can wakeup */
  14199. +#define UC_ATT_BATTERY (1 << 4) /* battery powered */
  14200. + uByte bmAttributes;
  14201. +#define UC_BUS_POWERED 0x80
  14202. +#define UC_SELF_POWERED 0x40
  14203. +#define UC_REMOTE_WAKEUP 0x20
  14204. + uByte bMaxPower; /* max current in 2 mA units */
  14205. +#define UC_POWER_FACTOR 2
  14206. +} UPACKED usb_config_descriptor_t;
  14207. +#define USB_CONFIG_DESCRIPTOR_SIZE 9
  14208. +
  14209. +typedef struct {
  14210. + uByte bLength;
  14211. + uByte bDescriptorType;
  14212. + uByte bInterfaceNumber;
  14213. + uByte bAlternateSetting;
  14214. + uByte bNumEndpoints;
  14215. + uByte bInterfaceClass;
  14216. + uByte bInterfaceSubClass;
  14217. + uByte bInterfaceProtocol;
  14218. + uByte iInterface;
  14219. +} UPACKED usb_interface_descriptor_t;
  14220. +#define USB_INTERFACE_DESCRIPTOR_SIZE 9
  14221. +
  14222. +typedef struct {
  14223. + uByte bLength;
  14224. + uByte bDescriptorType;
  14225. + uByte bEndpointAddress;
  14226. +#define UE_GET_DIR(a) ((a) & 0x80)
  14227. +#define UE_SET_DIR(a,d) ((a) | (((d)&1) << 7))
  14228. +#define UE_DIR_IN 0x80
  14229. +#define UE_DIR_OUT 0x00
  14230. +#define UE_ADDR 0x0f
  14231. +#define UE_GET_ADDR(a) ((a) & UE_ADDR)
  14232. + uByte bmAttributes;
  14233. +#define UE_XFERTYPE 0x03
  14234. +#define UE_CONTROL 0x00
  14235. +#define UE_ISOCHRONOUS 0x01
  14236. +#define UE_BULK 0x02
  14237. +#define UE_INTERRUPT 0x03
  14238. +#define UE_GET_XFERTYPE(a) ((a) & UE_XFERTYPE)
  14239. +#define UE_ISO_TYPE 0x0c
  14240. +#define UE_ISO_ASYNC 0x04
  14241. +#define UE_ISO_ADAPT 0x08
  14242. +#define UE_ISO_SYNC 0x0c
  14243. +#define UE_GET_ISO_TYPE(a) ((a) & UE_ISO_TYPE)
  14244. + uWord wMaxPacketSize;
  14245. + uByte bInterval;
  14246. +} UPACKED usb_endpoint_descriptor_t;
  14247. +#define USB_ENDPOINT_DESCRIPTOR_SIZE 7
  14248. +
  14249. +typedef struct ss_endpoint_companion_descriptor {
  14250. + uByte bLength;
  14251. + uByte bDescriptorType;
  14252. + uByte bMaxBurst;
  14253. +#define USSE_GET_MAX_STREAMS(a) ((a) & 0x1f)
  14254. +#define USSE_SET_MAX_STREAMS(a, b) ((a) | ((b) & 0x1f))
  14255. +#define USSE_GET_MAX_PACKET_NUM(a) ((a) & 0x03)
  14256. +#define USSE_SET_MAX_PACKET_NUM(a, b) ((a) | ((b) & 0x03))
  14257. + uByte bmAttributes;
  14258. + uWord wBytesPerInterval;
  14259. +} UPACKED ss_endpoint_companion_descriptor_t;
  14260. +#define USB_SS_ENDPOINT_COMPANION_DESCRIPTOR_SIZE 6
  14261. +
  14262. +typedef struct {
  14263. + uByte bLength;
  14264. + uByte bDescriptorType;
  14265. + uWord bString[127];
  14266. +} UPACKED usb_string_descriptor_t;
  14267. +#define USB_MAX_STRING_LEN 128
  14268. +#define USB_LANGUAGE_TABLE 0 /* # of the string language id table */
  14269. +
  14270. +/* Hub specific request */
  14271. +#define UR_GET_BUS_STATE 0x02
  14272. +#define UR_CLEAR_TT_BUFFER 0x08
  14273. +#define UR_RESET_TT 0x09
  14274. +#define UR_GET_TT_STATE 0x0a
  14275. +#define UR_STOP_TT 0x0b
  14276. +
  14277. +/* Hub features */
  14278. +#define UHF_C_HUB_LOCAL_POWER 0
  14279. +#define UHF_C_HUB_OVER_CURRENT 1
  14280. +#define UHF_PORT_CONNECTION 0
  14281. +#define UHF_PORT_ENABLE 1
  14282. +#define UHF_PORT_SUSPEND 2
  14283. +#define UHF_PORT_OVER_CURRENT 3
  14284. +#define UHF_PORT_RESET 4
  14285. +#define UHF_PORT_L1 5
  14286. +#define UHF_PORT_POWER 8
  14287. +#define UHF_PORT_LOW_SPEED 9
  14288. +#define UHF_PORT_HIGH_SPEED 10
  14289. +#define UHF_C_PORT_CONNECTION 16
  14290. +#define UHF_C_PORT_ENABLE 17
  14291. +#define UHF_C_PORT_SUSPEND 18
  14292. +#define UHF_C_PORT_OVER_CURRENT 19
  14293. +#define UHF_C_PORT_RESET 20
  14294. +#define UHF_C_PORT_L1 23
  14295. +#define UHF_PORT_TEST 21
  14296. +#define UHF_PORT_INDICATOR 22
  14297. +
  14298. +typedef struct {
  14299. + uByte bDescLength;
  14300. + uByte bDescriptorType;
  14301. + uByte bNbrPorts;
  14302. + uWord wHubCharacteristics;
  14303. +#define UHD_PWR 0x0003
  14304. +#define UHD_PWR_GANGED 0x0000
  14305. +#define UHD_PWR_INDIVIDUAL 0x0001
  14306. +#define UHD_PWR_NO_SWITCH 0x0002
  14307. +#define UHD_COMPOUND 0x0004
  14308. +#define UHD_OC 0x0018
  14309. +#define UHD_OC_GLOBAL 0x0000
  14310. +#define UHD_OC_INDIVIDUAL 0x0008
  14311. +#define UHD_OC_NONE 0x0010
  14312. +#define UHD_TT_THINK 0x0060
  14313. +#define UHD_TT_THINK_8 0x0000
  14314. +#define UHD_TT_THINK_16 0x0020
  14315. +#define UHD_TT_THINK_24 0x0040
  14316. +#define UHD_TT_THINK_32 0x0060
  14317. +#define UHD_PORT_IND 0x0080
  14318. + uByte bPwrOn2PwrGood; /* delay in 2 ms units */
  14319. +#define UHD_PWRON_FACTOR 2
  14320. + uByte bHubContrCurrent;
  14321. + uByte DeviceRemovable[32]; /* max 255 ports */
  14322. +#define UHD_NOT_REMOV(desc, i) \
  14323. + (((desc)->DeviceRemovable[(i)/8] >> ((i) % 8)) & 1)
  14324. + /* deprecated */ uByte PortPowerCtrlMask[1];
  14325. +} UPACKED usb_hub_descriptor_t;
  14326. +#define USB_HUB_DESCRIPTOR_SIZE 9 /* includes deprecated PortPowerCtrlMask */
  14327. +
  14328. +typedef struct {
  14329. + uByte bLength;
  14330. + uByte bDescriptorType;
  14331. + uWord bcdUSB;
  14332. + uByte bDeviceClass;
  14333. + uByte bDeviceSubClass;
  14334. + uByte bDeviceProtocol;
  14335. + uByte bMaxPacketSize0;
  14336. + uByte bNumConfigurations;
  14337. + uByte bReserved;
  14338. +} UPACKED usb_device_qualifier_t;
  14339. +#define USB_DEVICE_QUALIFIER_SIZE 10
  14340. +
  14341. +typedef struct {
  14342. + uByte bLength;
  14343. + uByte bDescriptorType;
  14344. + uByte bmAttributes;
  14345. +#define UOTG_SRP 0x01
  14346. +#define UOTG_HNP 0x02
  14347. +} UPACKED usb_otg_descriptor_t;
  14348. +
  14349. +/* OTG feature selectors */
  14350. +#define UOTG_B_HNP_ENABLE 3
  14351. +#define UOTG_A_HNP_SUPPORT 4
  14352. +#define UOTG_A_ALT_HNP_SUPPORT 5
  14353. +
  14354. +typedef struct {
  14355. + uWord wStatus;
  14356. +/* Device status flags */
  14357. +#define UDS_SELF_POWERED 0x0001
  14358. +#define UDS_REMOTE_WAKEUP 0x0002
  14359. +/* Endpoint status flags */
  14360. +#define UES_HALT 0x0001
  14361. +} UPACKED usb_status_t;
  14362. +
  14363. +typedef struct {
  14364. + uWord wHubStatus;
  14365. +#define UHS_LOCAL_POWER 0x0001
  14366. +#define UHS_OVER_CURRENT 0x0002
  14367. + uWord wHubChange;
  14368. +} UPACKED usb_hub_status_t;
  14369. +
  14370. +typedef struct {
  14371. + uWord wPortStatus;
  14372. +#define UPS_CURRENT_CONNECT_STATUS 0x0001
  14373. +#define UPS_PORT_ENABLED 0x0002
  14374. +#define UPS_SUSPEND 0x0004
  14375. +#define UPS_OVERCURRENT_INDICATOR 0x0008
  14376. +#define UPS_RESET 0x0010
  14377. +#define UPS_PORT_POWER 0x0100
  14378. +#define UPS_LOW_SPEED 0x0200
  14379. +#define UPS_HIGH_SPEED 0x0400
  14380. +#define UPS_PORT_TEST 0x0800
  14381. +#define UPS_PORT_INDICATOR 0x1000
  14382. + uWord wPortChange;
  14383. +#define UPS_C_CONNECT_STATUS 0x0001
  14384. +#define UPS_C_PORT_ENABLED 0x0002
  14385. +#define UPS_C_SUSPEND 0x0004
  14386. +#define UPS_C_OVERCURRENT_INDICATOR 0x0008
  14387. +#define UPS_C_PORT_RESET 0x0010
  14388. +} UPACKED usb_port_status_t;
  14389. +
  14390. +#ifdef _MSC_VER
  14391. +#include <poppack.h>
  14392. +#endif
  14393. +
  14394. +/* Device class codes */
  14395. +#define UDCLASS_IN_INTERFACE 0x00
  14396. +#define UDCLASS_COMM 0x02
  14397. +#define UDCLASS_HUB 0x09
  14398. +#define UDSUBCLASS_HUB 0x00
  14399. +#define UDPROTO_FSHUB 0x00
  14400. +#define UDPROTO_HSHUBSTT 0x01
  14401. +#define UDPROTO_HSHUBMTT 0x02
  14402. +#define UDCLASS_DIAGNOSTIC 0xdc
  14403. +#define UDCLASS_WIRELESS 0xe0
  14404. +#define UDSUBCLASS_RF 0x01
  14405. +#define UDPROTO_BLUETOOTH 0x01
  14406. +#define UDCLASS_VENDOR 0xff
  14407. +
  14408. +/* Interface class codes */
  14409. +#define UICLASS_UNSPEC 0x00
  14410. +
  14411. +#define UICLASS_AUDIO 0x01
  14412. +#define UISUBCLASS_AUDIOCONTROL 1
  14413. +#define UISUBCLASS_AUDIOSTREAM 2
  14414. +#define UISUBCLASS_MIDISTREAM 3
  14415. +
  14416. +#define UICLASS_CDC 0x02 /* communication */
  14417. +#define UISUBCLASS_DIRECT_LINE_CONTROL_MODEL 1
  14418. +#define UISUBCLASS_ABSTRACT_CONTROL_MODEL 2
  14419. +#define UISUBCLASS_TELEPHONE_CONTROL_MODEL 3
  14420. +#define UISUBCLASS_MULTICHANNEL_CONTROL_MODEL 4
  14421. +#define UISUBCLASS_CAPI_CONTROLMODEL 5
  14422. +#define UISUBCLASS_ETHERNET_NETWORKING_CONTROL_MODEL 6
  14423. +#define UISUBCLASS_ATM_NETWORKING_CONTROL_MODEL 7
  14424. +#define UIPROTO_CDC_AT 1
  14425. +
  14426. +#define UICLASS_HID 0x03
  14427. +#define UISUBCLASS_BOOT 1
  14428. +#define UIPROTO_BOOT_KEYBOARD 1
  14429. +
  14430. +#define UICLASS_PHYSICAL 0x05
  14431. +
  14432. +#define UICLASS_IMAGE 0x06
  14433. +
  14434. +#define UICLASS_PRINTER 0x07
  14435. +#define UISUBCLASS_PRINTER 1
  14436. +#define UIPROTO_PRINTER_UNI 1
  14437. +#define UIPROTO_PRINTER_BI 2
  14438. +#define UIPROTO_PRINTER_1284 3
  14439. +
  14440. +#define UICLASS_MASS 0x08
  14441. +#define UISUBCLASS_RBC 1
  14442. +#define UISUBCLASS_SFF8020I 2
  14443. +#define UISUBCLASS_QIC157 3
  14444. +#define UISUBCLASS_UFI 4
  14445. +#define UISUBCLASS_SFF8070I 5
  14446. +#define UISUBCLASS_SCSI 6
  14447. +#define UIPROTO_MASS_CBI_I 0
  14448. +#define UIPROTO_MASS_CBI 1
  14449. +#define UIPROTO_MASS_BBB_OLD 2 /* Not in the spec anymore */
  14450. +#define UIPROTO_MASS_BBB 80 /* 'P' for the Iomega Zip drive */
  14451. +
  14452. +#define UICLASS_HUB 0x09
  14453. +#define UISUBCLASS_HUB 0
  14454. +#define UIPROTO_FSHUB 0
  14455. +#define UIPROTO_HSHUBSTT 0 /* Yes, same as previous */
  14456. +#define UIPROTO_HSHUBMTT 1
  14457. +
  14458. +#define UICLASS_CDC_DATA 0x0a
  14459. +#define UISUBCLASS_DATA 0
  14460. +#define UIPROTO_DATA_ISDNBRI 0x30 /* Physical iface */
  14461. +#define UIPROTO_DATA_HDLC 0x31 /* HDLC */
  14462. +#define UIPROTO_DATA_TRANSPARENT 0x32 /* Transparent */
  14463. +#define UIPROTO_DATA_Q921M 0x50 /* Management for Q921 */
  14464. +#define UIPROTO_DATA_Q921 0x51 /* Data for Q921 */
  14465. +#define UIPROTO_DATA_Q921TM 0x52 /* TEI multiplexer for Q921 */
  14466. +#define UIPROTO_DATA_V42BIS 0x90 /* Data compression */
  14467. +#define UIPROTO_DATA_Q931 0x91 /* Euro-ISDN */
  14468. +#define UIPROTO_DATA_V120 0x92 /* V.24 rate adaption */
  14469. +#define UIPROTO_DATA_CAPI 0x93 /* CAPI 2.0 commands */
  14470. +#define UIPROTO_DATA_HOST_BASED 0xfd /* Host based driver */
  14471. +#define UIPROTO_DATA_PUF 0xfe /* see Prot. Unit Func. Desc.*/
  14472. +#define UIPROTO_DATA_VENDOR 0xff /* Vendor specific */
  14473. +
  14474. +#define UICLASS_SMARTCARD 0x0b
  14475. +
  14476. +/*#define UICLASS_FIRM_UPD 0x0c*/
  14477. +
  14478. +#define UICLASS_SECURITY 0x0d
  14479. +
  14480. +#define UICLASS_DIAGNOSTIC 0xdc
  14481. +
  14482. +#define UICLASS_WIRELESS 0xe0
  14483. +#define UISUBCLASS_RF 0x01
  14484. +#define UIPROTO_BLUETOOTH 0x01
  14485. +
  14486. +#define UICLASS_APPL_SPEC 0xfe
  14487. +#define UISUBCLASS_FIRMWARE_DOWNLOAD 1
  14488. +#define UISUBCLASS_IRDA 2
  14489. +#define UIPROTO_IRDA 0
  14490. +
  14491. +#define UICLASS_VENDOR 0xff
  14492. +
  14493. +#define USB_HUB_MAX_DEPTH 5
  14494. +
  14495. +/*
  14496. + * Minimum time a device needs to be powered down to go through
  14497. + * a power cycle. XXX Are these time in the spec?
  14498. + */
  14499. +#define USB_POWER_DOWN_TIME 200 /* ms */
  14500. +#define USB_PORT_POWER_DOWN_TIME 100 /* ms */
  14501. +
  14502. +#if 0
  14503. +/* These are the values from the spec. */
  14504. +#define USB_PORT_RESET_DELAY 10 /* ms */
  14505. +#define USB_PORT_ROOT_RESET_DELAY 50 /* ms */
  14506. +#define USB_PORT_RESET_RECOVERY 10 /* ms */
  14507. +#define USB_PORT_POWERUP_DELAY 100 /* ms */
  14508. +#define USB_SET_ADDRESS_SETTLE 2 /* ms */
  14509. +#define USB_RESUME_DELAY (20*5) /* ms */
  14510. +#define USB_RESUME_WAIT 10 /* ms */
  14511. +#define USB_RESUME_RECOVERY 10 /* ms */
  14512. +#define USB_EXTRA_POWER_UP_TIME 0 /* ms */
  14513. +#else
  14514. +/* Allow for marginal (i.e. non-conforming) devices. */
  14515. +#define USB_PORT_RESET_DELAY 50 /* ms */
  14516. +#define USB_PORT_ROOT_RESET_DELAY 250 /* ms */
  14517. +#define USB_PORT_RESET_RECOVERY 250 /* ms */
  14518. +#define USB_PORT_POWERUP_DELAY 300 /* ms */
  14519. +#define USB_SET_ADDRESS_SETTLE 10 /* ms */
  14520. +#define USB_RESUME_DELAY (50*5) /* ms */
  14521. +#define USB_RESUME_WAIT 50 /* ms */
  14522. +#define USB_RESUME_RECOVERY 50 /* ms */
  14523. +#define USB_EXTRA_POWER_UP_TIME 20 /* ms */
  14524. +#endif
  14525. +
  14526. +#define USB_MIN_POWER 100 /* mA */
  14527. +#define USB_MAX_POWER 500 /* mA */
  14528. +
  14529. +#define USB_BUS_RESET_DELAY 100 /* ms XXX?*/
  14530. +
  14531. +#define USB_UNCONFIG_NO 0
  14532. +#define USB_UNCONFIG_INDEX (-1)
  14533. +
  14534. +/*** ioctl() related stuff ***/
  14535. +
  14536. +struct usb_ctl_request {
  14537. + int ucr_addr;
  14538. + usb_device_request_t ucr_request;
  14539. + void *ucr_data;
  14540. + int ucr_flags;
  14541. +#define USBD_SHORT_XFER_OK 0x04 /* allow short reads */
  14542. + int ucr_actlen; /* actual length transferred */
  14543. +};
  14544. +
  14545. +struct usb_alt_interface {
  14546. + int uai_config_index;
  14547. + int uai_interface_index;
  14548. + int uai_alt_no;
  14549. +};
  14550. +
  14551. +#define USB_CURRENT_CONFIG_INDEX (-1)
  14552. +#define USB_CURRENT_ALT_INDEX (-1)
  14553. +
  14554. +struct usb_config_desc {
  14555. + int ucd_config_index;
  14556. + usb_config_descriptor_t ucd_desc;
  14557. +};
  14558. +
  14559. +struct usb_interface_desc {
  14560. + int uid_config_index;
  14561. + int uid_interface_index;
  14562. + int uid_alt_index;
  14563. + usb_interface_descriptor_t uid_desc;
  14564. +};
  14565. +
  14566. +struct usb_endpoint_desc {
  14567. + int ued_config_index;
  14568. + int ued_interface_index;
  14569. + int ued_alt_index;
  14570. + int ued_endpoint_index;
  14571. + usb_endpoint_descriptor_t ued_desc;
  14572. +};
  14573. +
  14574. +struct usb_full_desc {
  14575. + int ufd_config_index;
  14576. + u_int ufd_size;
  14577. + u_char *ufd_data;
  14578. +};
  14579. +
  14580. +struct usb_string_desc {
  14581. + int usd_string_index;
  14582. + int usd_language_id;
  14583. + usb_string_descriptor_t usd_desc;
  14584. +};
  14585. +
  14586. +struct usb_ctl_report_desc {
  14587. + int ucrd_size;
  14588. + u_char ucrd_data[1024]; /* filled data size will vary */
  14589. +};
  14590. +
  14591. +typedef struct { u_int32_t cookie; } usb_event_cookie_t;
  14592. +
  14593. +#define USB_MAX_DEVNAMES 4
  14594. +#define USB_MAX_DEVNAMELEN 16
  14595. +struct usb_device_info {
  14596. + u_int8_t udi_bus;
  14597. + u_int8_t udi_addr; /* device address */
  14598. + usb_event_cookie_t udi_cookie;
  14599. + char udi_product[USB_MAX_STRING_LEN];
  14600. + char udi_vendor[USB_MAX_STRING_LEN];
  14601. + char udi_release[8];
  14602. + u_int16_t udi_productNo;
  14603. + u_int16_t udi_vendorNo;
  14604. + u_int16_t udi_releaseNo;
  14605. + u_int8_t udi_class;
  14606. + u_int8_t udi_subclass;
  14607. + u_int8_t udi_protocol;
  14608. + u_int8_t udi_config;
  14609. + u_int8_t udi_speed;
  14610. +#define USB_SPEED_UNKNOWN 0
  14611. +#define USB_SPEED_LOW 1
  14612. +#define USB_SPEED_FULL 2
  14613. +#define USB_SPEED_HIGH 3
  14614. +#define USB_SPEED_VARIABLE 4
  14615. +#define USB_SPEED_SUPER 5
  14616. + int udi_power; /* power consumption in mA, 0 if selfpowered */
  14617. + int udi_nports;
  14618. + char udi_devnames[USB_MAX_DEVNAMES][USB_MAX_DEVNAMELEN];
  14619. + u_int8_t udi_ports[16];/* hub only: addresses of devices on ports */
  14620. +#define USB_PORT_ENABLED 0xff
  14621. +#define USB_PORT_SUSPENDED 0xfe
  14622. +#define USB_PORT_POWERED 0xfd
  14623. +#define USB_PORT_DISABLED 0xfc
  14624. +};
  14625. +
  14626. +struct usb_ctl_report {
  14627. + int ucr_report;
  14628. + u_char ucr_data[1024]; /* filled data size will vary */
  14629. +};
  14630. +
  14631. +struct usb_device_stats {
  14632. + u_long uds_requests[4]; /* indexed by transfer type UE_* */
  14633. +};
  14634. +
  14635. +#define WUSB_MIN_IE 0x80
  14636. +#define WUSB_WCTA_IE 0x80
  14637. +#define WUSB_WCONNECTACK_IE 0x81
  14638. +#define WUSB_WHOSTINFO_IE 0x82
  14639. +#define WUHI_GET_CA(_bmAttributes_) ((_bmAttributes_) & 0x3)
  14640. +#define WUHI_CA_RECONN 0x00
  14641. +#define WUHI_CA_LIMITED 0x01
  14642. +#define WUHI_CA_ALL 0x03
  14643. +#define WUHI_GET_MLSI(_bmAttributes_) (((_bmAttributes_) & 0x38) >> 3)
  14644. +#define WUSB_WCHCHANGEANNOUNCE_IE 0x83
  14645. +#define WUSB_WDEV_DISCONNECT_IE 0x84
  14646. +#define WUSB_WHOST_DISCONNECT_IE 0x85
  14647. +#define WUSB_WRELEASE_CHANNEL_IE 0x86
  14648. +#define WUSB_WWORK_IE 0x87
  14649. +#define WUSB_WCHANNEL_STOP_IE 0x88
  14650. +#define WUSB_WDEV_KEEPALIVE_IE 0x89
  14651. +#define WUSB_WISOCH_DISCARD_IE 0x8A
  14652. +#define WUSB_WRESETDEVICE_IE 0x8B
  14653. +#define WUSB_WXMIT_PACKET_ADJUST_IE 0x8C
  14654. +#define WUSB_MAX_IE 0x8C
  14655. +
  14656. +/* Device Notification Types */
  14657. +
  14658. +#define WUSB_DN_MIN 0x01
  14659. +#define WUSB_DN_CONNECT 0x01
  14660. +# define WUSB_DA_OLDCONN 0x00
  14661. +# define WUSB_DA_NEWCONN 0x01
  14662. +# define WUSB_DA_SELF_BEACON 0x02
  14663. +# define WUSB_DA_DIR_BEACON 0x04
  14664. +# define WUSB_DA_NO_BEACON 0x06
  14665. +#define WUSB_DN_DISCONNECT 0x02
  14666. +#define WUSB_DN_EPRDY 0x03
  14667. +#define WUSB_DN_MASAVAILCHANGED 0x04
  14668. +#define WUSB_DN_REMOTEWAKEUP 0x05
  14669. +#define WUSB_DN_SLEEP 0x06
  14670. +#define WUSB_DN_ALIVE 0x07
  14671. +#define WUSB_DN_MAX 0x07
  14672. +
  14673. +#ifdef _MSC_VER
  14674. +#include <pshpack1.h>
  14675. +#endif
  14676. +
  14677. +/* WUSB Handshake Data. Used during the SET/GET HANDSHAKE requests */
  14678. +typedef struct wusb_hndshk_data {
  14679. + uByte bMessageNumber;
  14680. + uByte bStatus;
  14681. + uByte tTKID[3];
  14682. + uByte bReserved;
  14683. + uByte CDID[16];
  14684. + uByte Nonce[16];
  14685. + uByte MIC[8];
  14686. +} UPACKED wusb_hndshk_data_t;
  14687. +#define WUSB_HANDSHAKE_LEN_FOR_MIC 38
  14688. +
  14689. +/* WUSB Connection Context */
  14690. +typedef struct wusb_conn_context {
  14691. + uByte CHID [16];
  14692. + uByte CDID [16];
  14693. + uByte CK [16];
  14694. +} UPACKED wusb_conn_context_t;
  14695. +
  14696. +/* WUSB Security Descriptor */
  14697. +typedef struct wusb_security_desc {
  14698. + uByte bLength;
  14699. + uByte bDescriptorType;
  14700. + uWord wTotalLength;
  14701. + uByte bNumEncryptionTypes;
  14702. +} UPACKED wusb_security_desc_t;
  14703. +
  14704. +/* WUSB Encryption Type Descriptor */
  14705. +typedef struct wusb_encrypt_type_desc {
  14706. + uByte bLength;
  14707. + uByte bDescriptorType;
  14708. +
  14709. + uByte bEncryptionType;
  14710. +#define WUETD_UNSECURE 0
  14711. +#define WUETD_WIRED 1
  14712. +#define WUETD_CCM_1 2
  14713. +#define WUETD_RSA_1 3
  14714. +
  14715. + uByte bEncryptionValue;
  14716. + uByte bAuthKeyIndex;
  14717. +} UPACKED wusb_encrypt_type_desc_t;
  14718. +
  14719. +/* WUSB Key Descriptor */
  14720. +typedef struct wusb_key_desc {
  14721. + uByte bLength;
  14722. + uByte bDescriptorType;
  14723. + uByte tTKID[3];
  14724. + uByte bReserved;
  14725. + uByte KeyData[1]; /* variable length */
  14726. +} UPACKED wusb_key_desc_t;
  14727. +
  14728. +/* WUSB BOS Descriptor (Binary device Object Store) */
  14729. +typedef struct wusb_bos_desc {
  14730. + uByte bLength;
  14731. + uByte bDescriptorType;
  14732. + uWord wTotalLength;
  14733. + uByte bNumDeviceCaps;
  14734. +} UPACKED wusb_bos_desc_t;
  14735. +
  14736. +#define USB_DEVICE_CAPABILITY_20_EXTENSION 0x02
  14737. +typedef struct usb_dev_cap_20_ext_desc {
  14738. + uByte bLength;
  14739. + uByte bDescriptorType;
  14740. + uByte bDevCapabilityType;
  14741. +#define USB_20_EXT_LPM 0x02
  14742. + uDWord bmAttributes;
  14743. +} UPACKED usb_dev_cap_20_ext_desc_t;
  14744. +
  14745. +#define USB_DEVICE_CAPABILITY_SS_USB 0x03
  14746. +typedef struct usb_dev_cap_ss_usb {
  14747. + uByte bLength;
  14748. + uByte bDescriptorType;
  14749. + uByte bDevCapabilityType;
  14750. +#define USB_DC_SS_USB_LTM_CAPABLE 0x02
  14751. + uByte bmAttributes;
  14752. +#define USB_DC_SS_USB_SPEED_SUPPORT_LOW 0x01
  14753. +#define USB_DC_SS_USB_SPEED_SUPPORT_FULL 0x02
  14754. +#define USB_DC_SS_USB_SPEED_SUPPORT_HIGH 0x04
  14755. +#define USB_DC_SS_USB_SPEED_SUPPORT_SS 0x08
  14756. + uWord wSpeedsSupported;
  14757. + uByte bFunctionalitySupport;
  14758. + uByte bU1DevExitLat;
  14759. + uWord wU2DevExitLat;
  14760. +} UPACKED usb_dev_cap_ss_usb_t;
  14761. +
  14762. +#define USB_DEVICE_CAPABILITY_CONTAINER_ID 0x04
  14763. +typedef struct usb_dev_cap_container_id {
  14764. + uByte bLength;
  14765. + uByte bDescriptorType;
  14766. + uByte bDevCapabilityType;
  14767. + uByte bReserved;
  14768. + uByte containerID[16];
  14769. +} UPACKED usb_dev_cap_container_id_t;
  14770. +
  14771. +/* Device Capability Type Codes */
  14772. +#define WUSB_DEVICE_CAPABILITY_WIRELESS_USB 0x01
  14773. +
  14774. +/* Device Capability Descriptor */
  14775. +typedef struct wusb_dev_cap_desc {
  14776. + uByte bLength;
  14777. + uByte bDescriptorType;
  14778. + uByte bDevCapabilityType;
  14779. + uByte caps[1]; /* Variable length */
  14780. +} UPACKED wusb_dev_cap_desc_t;
  14781. +
  14782. +/* Device Capability Descriptor */
  14783. +typedef struct wusb_dev_cap_uwb_desc {
  14784. + uByte bLength;
  14785. + uByte bDescriptorType;
  14786. + uByte bDevCapabilityType;
  14787. + uByte bmAttributes;
  14788. + uWord wPHYRates; /* Bitmap */
  14789. + uByte bmTFITXPowerInfo;
  14790. + uByte bmFFITXPowerInfo;
  14791. + uWord bmBandGroup;
  14792. + uByte bReserved;
  14793. +} UPACKED wusb_dev_cap_uwb_desc_t;
  14794. +
  14795. +/* Wireless USB Endpoint Companion Descriptor */
  14796. +typedef struct wusb_endpoint_companion_desc {
  14797. + uByte bLength;
  14798. + uByte bDescriptorType;
  14799. + uByte bMaxBurst;
  14800. + uByte bMaxSequence;
  14801. + uWord wMaxStreamDelay;
  14802. + uWord wOverTheAirPacketSize;
  14803. + uByte bOverTheAirInterval;
  14804. + uByte bmCompAttributes;
  14805. +} UPACKED wusb_endpoint_companion_desc_t;
  14806. +
  14807. +/* Wireless USB Numeric Association M1 Data Structure */
  14808. +typedef struct wusb_m1_data {
  14809. + uByte version;
  14810. + uWord langId;
  14811. + uByte deviceFriendlyNameLength;
  14812. + uByte sha_256_m3[32];
  14813. + uByte deviceFriendlyName[256];
  14814. +} UPACKED wusb_m1_data_t;
  14815. +
  14816. +typedef struct wusb_m2_data {
  14817. + uByte version;
  14818. + uWord langId;
  14819. + uByte hostFriendlyNameLength;
  14820. + uByte pkh[384];
  14821. + uByte hostFriendlyName[256];
  14822. +} UPACKED wusb_m2_data_t;
  14823. +
  14824. +typedef struct wusb_m3_data {
  14825. + uByte pkd[384];
  14826. + uByte nd;
  14827. +} UPACKED wusb_m3_data_t;
  14828. +
  14829. +typedef struct wusb_m4_data {
  14830. + uDWord _attributeTypeIdAndLength_1;
  14831. + uWord associationTypeId;
  14832. +
  14833. + uDWord _attributeTypeIdAndLength_2;
  14834. + uWord associationSubTypeId;
  14835. +
  14836. + uDWord _attributeTypeIdAndLength_3;
  14837. + uDWord length;
  14838. +
  14839. + uDWord _attributeTypeIdAndLength_4;
  14840. + uDWord associationStatus;
  14841. +
  14842. + uDWord _attributeTypeIdAndLength_5;
  14843. + uByte chid[16];
  14844. +
  14845. + uDWord _attributeTypeIdAndLength_6;
  14846. + uByte cdid[16];
  14847. +
  14848. + uDWord _attributeTypeIdAndLength_7;
  14849. + uByte bandGroups[2];
  14850. +} UPACKED wusb_m4_data_t;
  14851. +
  14852. +#ifdef _MSC_VER
  14853. +#include <poppack.h>
  14854. +#endif
  14855. +
  14856. +#ifdef __cplusplus
  14857. +}
  14858. +#endif
  14859. +
  14860. +#endif /* _USB_H_ */
  14861. --- /dev/null
  14862. +++ b/drivers/usb/host/dwc_otg/Makefile
  14863. @@ -0,0 +1,82 @@
  14864. +#
  14865. +# Makefile for DWC_otg Highspeed USB controller driver
  14866. +#
  14867. +
  14868. +ifneq ($(KERNELRELEASE),)
  14869. +
  14870. +# Use the BUS_INTERFACE variable to compile the software for either
  14871. +# PCI(PCI_INTERFACE) or LM(LM_INTERFACE) bus.
  14872. +ifeq ($(BUS_INTERFACE),)
  14873. +# BUS_INTERFACE = -DPCI_INTERFACE
  14874. +# BUS_INTERFACE = -DLM_INTERFACE
  14875. + BUS_INTERFACE = -DPLATFORM_INTERFACE
  14876. +endif
  14877. +
  14878. +#ccflags-y += -DDEBUG
  14879. +#ccflags-y += -DDWC_OTG_DEBUGLEV=1 # reduce common debug msgs
  14880. +
  14881. +# Use one of the following flags to compile the software in host-only or
  14882. +# device-only mode.
  14883. +#ccflags-y += -DDWC_HOST_ONLY
  14884. +#ccflags-y += -DDWC_DEVICE_ONLY
  14885. +
  14886. +ccflags-y += -Dlinux -DDWC_HS_ELECT_TST
  14887. +#ccflags-y += -DDWC_EN_ISOC
  14888. +ccflags-y += -I$(obj)/../dwc_common_port
  14889. +#ccflags-y += -I$(PORTLIB)
  14890. +ccflags-y += -DDWC_LINUX
  14891. +ccflags-y += $(CFI)
  14892. +ccflags-y += $(BUS_INTERFACE)
  14893. +#ccflags-y += -DDWC_DEV_SRPCAP
  14894. +
  14895. +obj-$(CONFIG_USB_DWCOTG) += dwc_otg.o
  14896. +
  14897. +dwc_otg-objs := dwc_otg_driver.o dwc_otg_attr.o
  14898. +dwc_otg-objs += dwc_otg_cil.o dwc_otg_cil_intr.o
  14899. +dwc_otg-objs += dwc_otg_pcd_linux.o dwc_otg_pcd.o dwc_otg_pcd_intr.o
  14900. +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
  14901. +dwc_otg-objs += dwc_otg_adp.o
  14902. +dwc_otg-objs += dwc_otg_fiq_fsm.o
  14903. +dwc_otg-objs += dwc_otg_fiq_stub.o
  14904. +ifneq ($(CFI),)
  14905. +dwc_otg-objs += dwc_otg_cfi.o
  14906. +endif
  14907. +
  14908. +kernrelwd := $(subst ., ,$(KERNELRELEASE))
  14909. +kernrel3 := $(word 1,$(kernrelwd)).$(word 2,$(kernrelwd)).$(word 3,$(kernrelwd))
  14910. +
  14911. +ifneq ($(kernrel3),2.6.20)
  14912. +ccflags-y += $(CPPFLAGS)
  14913. +endif
  14914. +
  14915. +else
  14916. +
  14917. +PWD := $(shell pwd)
  14918. +PORTLIB := $(PWD)/../dwc_common_port
  14919. +
  14920. +# Command paths
  14921. +CTAGS := $(CTAGS)
  14922. +DOXYGEN := $(DOXYGEN)
  14923. +
  14924. +default: portlib
  14925. + $(MAKE) -C$(KDIR) M=$(PWD) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) modules
  14926. +
  14927. +install: default
  14928. + $(MAKE) -C$(KDIR) M=$(PORTLIB) modules_install
  14929. + $(MAKE) -C$(KDIR) M=$(PWD) modules_install
  14930. +
  14931. +portlib:
  14932. + $(MAKE) -C$(KDIR) M=$(PORTLIB) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) modules
  14933. + cp $(PORTLIB)/Module.symvers $(PWD)/
  14934. +
  14935. +docs: $(wildcard *.[hc]) doc/doxygen.cfg
  14936. + $(DOXYGEN) doc/doxygen.cfg
  14937. +
  14938. +tags: $(wildcard *.[hc])
  14939. + $(CTAGS) -e $(wildcard *.[hc]) $(wildcard linux/*.[hc]) $(wildcard $(KDIR)/include/linux/usb*.h)
  14940. +
  14941. +
  14942. +clean:
  14943. + rm -rf *.o *.ko .*cmd *.mod.c .tmp_versions Module.symvers
  14944. +
  14945. +endif
  14946. --- /dev/null
  14947. +++ b/drivers/usb/host/dwc_otg/doc/doxygen.cfg
  14948. @@ -0,0 +1,224 @@
  14949. +# Doxyfile 1.3.9.1
  14950. +
  14951. +#---------------------------------------------------------------------------
  14952. +# Project related configuration options
  14953. +#---------------------------------------------------------------------------
  14954. +PROJECT_NAME = "DesignWare USB 2.0 OTG Controller (DWC_otg) Device Driver"
  14955. +PROJECT_NUMBER = v3.00a
  14956. +OUTPUT_DIRECTORY = ./doc/
  14957. +CREATE_SUBDIRS = NO
  14958. +OUTPUT_LANGUAGE = English
  14959. +BRIEF_MEMBER_DESC = YES
  14960. +REPEAT_BRIEF = YES
  14961. +ABBREVIATE_BRIEF = "The $name class" \
  14962. + "The $name widget" \
  14963. + "The $name file" \
  14964. + is \
  14965. + provides \
  14966. + specifies \
  14967. + contains \
  14968. + represents \
  14969. + a \
  14970. + an \
  14971. + the
  14972. +ALWAYS_DETAILED_SEC = NO
  14973. +INLINE_INHERITED_MEMB = NO
  14974. +FULL_PATH_NAMES = NO
  14975. +STRIP_FROM_PATH =
  14976. +STRIP_FROM_INC_PATH =
  14977. +SHORT_NAMES = NO
  14978. +JAVADOC_AUTOBRIEF = YES
  14979. +MULTILINE_CPP_IS_BRIEF = NO
  14980. +INHERIT_DOCS = YES
  14981. +DISTRIBUTE_GROUP_DOC = NO
  14982. +TAB_SIZE = 8
  14983. +ALIASES =
  14984. +OPTIMIZE_OUTPUT_FOR_C = YES
  14985. +OPTIMIZE_OUTPUT_JAVA = NO
  14986. +SUBGROUPING = YES
  14987. +#---------------------------------------------------------------------------
  14988. +# Build related configuration options
  14989. +#---------------------------------------------------------------------------
  14990. +EXTRACT_ALL = NO
  14991. +EXTRACT_PRIVATE = YES
  14992. +EXTRACT_STATIC = YES
  14993. +EXTRACT_LOCAL_CLASSES = YES
  14994. +EXTRACT_LOCAL_METHODS = NO
  14995. +HIDE_UNDOC_MEMBERS = NO
  14996. +HIDE_UNDOC_CLASSES = NO
  14997. +HIDE_FRIEND_COMPOUNDS = NO
  14998. +HIDE_IN_BODY_DOCS = NO
  14999. +INTERNAL_DOCS = NO
  15000. +CASE_SENSE_NAMES = NO
  15001. +HIDE_SCOPE_NAMES = NO
  15002. +SHOW_INCLUDE_FILES = YES
  15003. +INLINE_INFO = YES
  15004. +SORT_MEMBER_DOCS = NO
  15005. +SORT_BRIEF_DOCS = NO
  15006. +SORT_BY_SCOPE_NAME = NO
  15007. +GENERATE_TODOLIST = YES
  15008. +GENERATE_TESTLIST = YES
  15009. +GENERATE_BUGLIST = YES
  15010. +GENERATE_DEPRECATEDLIST= YES
  15011. +ENABLED_SECTIONS =
  15012. +MAX_INITIALIZER_LINES = 30
  15013. +SHOW_USED_FILES = YES
  15014. +SHOW_DIRECTORIES = YES
  15015. +#---------------------------------------------------------------------------
  15016. +# configuration options related to warning and progress messages
  15017. +#---------------------------------------------------------------------------
  15018. +QUIET = YES
  15019. +WARNINGS = YES
  15020. +WARN_IF_UNDOCUMENTED = NO
  15021. +WARN_IF_DOC_ERROR = YES
  15022. +WARN_FORMAT = "$file:$line: $text"
  15023. +WARN_LOGFILE =
  15024. +#---------------------------------------------------------------------------
  15025. +# configuration options related to the input files
  15026. +#---------------------------------------------------------------------------
  15027. +INPUT = .
  15028. +FILE_PATTERNS = *.c \
  15029. + *.h \
  15030. + ./linux/*.c \
  15031. + ./linux/*.h
  15032. +RECURSIVE = NO
  15033. +EXCLUDE = ./test/ \
  15034. + ./dwc_otg/.AppleDouble/
  15035. +EXCLUDE_SYMLINKS = YES
  15036. +EXCLUDE_PATTERNS = *.mod.*
  15037. +EXAMPLE_PATH =
  15038. +EXAMPLE_PATTERNS = *
  15039. +EXAMPLE_RECURSIVE = NO
  15040. +IMAGE_PATH =
  15041. +INPUT_FILTER =
  15042. +FILTER_PATTERNS =
  15043. +FILTER_SOURCE_FILES = NO
  15044. +#---------------------------------------------------------------------------
  15045. +# configuration options related to source browsing
  15046. +#---------------------------------------------------------------------------
  15047. +SOURCE_BROWSER = YES
  15048. +INLINE_SOURCES = NO
  15049. +STRIP_CODE_COMMENTS = YES
  15050. +REFERENCED_BY_RELATION = NO
  15051. +REFERENCES_RELATION = NO
  15052. +VERBATIM_HEADERS = NO
  15053. +#---------------------------------------------------------------------------
  15054. +# configuration options related to the alphabetical class index
  15055. +#---------------------------------------------------------------------------
  15056. +ALPHABETICAL_INDEX = NO
  15057. +COLS_IN_ALPHA_INDEX = 5
  15058. +IGNORE_PREFIX =
  15059. +#---------------------------------------------------------------------------
  15060. +# configuration options related to the HTML output
  15061. +#---------------------------------------------------------------------------
  15062. +GENERATE_HTML = YES
  15063. +HTML_OUTPUT = html
  15064. +HTML_FILE_EXTENSION = .html
  15065. +HTML_HEADER =
  15066. +HTML_FOOTER =
  15067. +HTML_STYLESHEET =
  15068. +HTML_ALIGN_MEMBERS = YES
  15069. +GENERATE_HTMLHELP = NO
  15070. +CHM_FILE =
  15071. +HHC_LOCATION =
  15072. +GENERATE_CHI = NO
  15073. +BINARY_TOC = NO
  15074. +TOC_EXPAND = NO
  15075. +DISABLE_INDEX = NO
  15076. +ENUM_VALUES_PER_LINE = 4
  15077. +GENERATE_TREEVIEW = YES
  15078. +TREEVIEW_WIDTH = 250
  15079. +#---------------------------------------------------------------------------
  15080. +# configuration options related to the LaTeX output
  15081. +#---------------------------------------------------------------------------
  15082. +GENERATE_LATEX = NO
  15083. +LATEX_OUTPUT = latex
  15084. +LATEX_CMD_NAME = latex
  15085. +MAKEINDEX_CMD_NAME = makeindex
  15086. +COMPACT_LATEX = NO
  15087. +PAPER_TYPE = a4wide
  15088. +EXTRA_PACKAGES =
  15089. +LATEX_HEADER =
  15090. +PDF_HYPERLINKS = NO
  15091. +USE_PDFLATEX = NO
  15092. +LATEX_BATCHMODE = NO
  15093. +LATEX_HIDE_INDICES = NO
  15094. +#---------------------------------------------------------------------------
  15095. +# configuration options related to the RTF output
  15096. +#---------------------------------------------------------------------------
  15097. +GENERATE_RTF = NO
  15098. +RTF_OUTPUT = rtf
  15099. +COMPACT_RTF = NO
  15100. +RTF_HYPERLINKS = NO
  15101. +RTF_STYLESHEET_FILE =
  15102. +RTF_EXTENSIONS_FILE =
  15103. +#---------------------------------------------------------------------------
  15104. +# configuration options related to the man page output
  15105. +#---------------------------------------------------------------------------
  15106. +GENERATE_MAN = NO
  15107. +MAN_OUTPUT = man
  15108. +MAN_EXTENSION = .3
  15109. +MAN_LINKS = NO
  15110. +#---------------------------------------------------------------------------
  15111. +# configuration options related to the XML output
  15112. +#---------------------------------------------------------------------------
  15113. +GENERATE_XML = NO
  15114. +XML_OUTPUT = xml
  15115. +XML_SCHEMA =
  15116. +XML_DTD =
  15117. +XML_PROGRAMLISTING = YES
  15118. +#---------------------------------------------------------------------------
  15119. +# configuration options for the AutoGen Definitions output
  15120. +#---------------------------------------------------------------------------
  15121. +GENERATE_AUTOGEN_DEF = NO
  15122. +#---------------------------------------------------------------------------
  15123. +# configuration options related to the Perl module output
  15124. +#---------------------------------------------------------------------------
  15125. +GENERATE_PERLMOD = NO
  15126. +PERLMOD_LATEX = NO
  15127. +PERLMOD_PRETTY = YES
  15128. +PERLMOD_MAKEVAR_PREFIX =
  15129. +#---------------------------------------------------------------------------
  15130. +# Configuration options related to the preprocessor
  15131. +#---------------------------------------------------------------------------
  15132. +ENABLE_PREPROCESSING = YES
  15133. +MACRO_EXPANSION = YES
  15134. +EXPAND_ONLY_PREDEF = YES
  15135. +SEARCH_INCLUDES = YES
  15136. +INCLUDE_PATH =
  15137. +INCLUDE_FILE_PATTERNS =
  15138. +PREDEFINED = DEVICE_ATTR DWC_EN_ISOC
  15139. +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
  15140. +SKIP_FUNCTION_MACROS = NO
  15141. +#---------------------------------------------------------------------------
  15142. +# Configuration::additions related to external references
  15143. +#---------------------------------------------------------------------------
  15144. +TAGFILES =
  15145. +GENERATE_TAGFILE =
  15146. +ALLEXTERNALS = NO
  15147. +EXTERNAL_GROUPS = YES
  15148. +PERL_PATH = /usr/bin/perl
  15149. +#---------------------------------------------------------------------------
  15150. +# Configuration options related to the dot tool
  15151. +#---------------------------------------------------------------------------
  15152. +CLASS_DIAGRAMS = YES
  15153. +HIDE_UNDOC_RELATIONS = YES
  15154. +HAVE_DOT = NO
  15155. +CLASS_GRAPH = YES
  15156. +COLLABORATION_GRAPH = YES
  15157. +UML_LOOK = NO
  15158. +TEMPLATE_RELATIONS = NO
  15159. +INCLUDE_GRAPH = YES
  15160. +INCLUDED_BY_GRAPH = YES
  15161. +CALL_GRAPH = NO
  15162. +GRAPHICAL_HIERARCHY = YES
  15163. +DOT_IMAGE_FORMAT = png
  15164. +DOT_PATH =
  15165. +DOTFILE_DIRS =
  15166. +MAX_DOT_GRAPH_DEPTH = 1000
  15167. +GENERATE_LEGEND = YES
  15168. +DOT_CLEANUP = YES
  15169. +#---------------------------------------------------------------------------
  15170. +# Configuration::additions related to the search engine
  15171. +#---------------------------------------------------------------------------
  15172. +SEARCHENGINE = NO
  15173. --- /dev/null
  15174. +++ b/drivers/usb/host/dwc_otg/dummy_audio.c
  15175. @@ -0,0 +1,1574 @@
  15176. +/*
  15177. + * zero.c -- Gadget Zero, for USB development
  15178. + *
  15179. + * Copyright (C) 2003-2004 David Brownell
  15180. + * All rights reserved.
  15181. + *
  15182. + * Redistribution and use in source and binary forms, with or without
  15183. + * modification, are permitted provided that the following conditions
  15184. + * are met:
  15185. + * 1. Redistributions of source code must retain the above copyright
  15186. + * notice, this list of conditions, and the following disclaimer,
  15187. + * without modification.
  15188. + * 2. Redistributions in binary form must reproduce the above copyright
  15189. + * notice, this list of conditions and the following disclaimer in the
  15190. + * documentation and/or other materials provided with the distribution.
  15191. + * 3. The names of the above-listed copyright holders may not be used
  15192. + * to endorse or promote products derived from this software without
  15193. + * specific prior written permission.
  15194. + *
  15195. + * ALTERNATIVELY, this software may be distributed under the terms of the
  15196. + * GNU General Public License ("GPL") as published by the Free Software
  15197. + * Foundation, either version 2 of that License or (at your option) any
  15198. + * later version.
  15199. + *
  15200. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  15201. + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  15202. + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  15203. + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  15204. + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  15205. + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  15206. + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  15207. + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  15208. + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  15209. + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  15210. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  15211. + */
  15212. +
  15213. +
  15214. +/*
  15215. + * Gadget Zero only needs two bulk endpoints, and is an example of how you
  15216. + * can write a hardware-agnostic gadget driver running inside a USB device.
  15217. + *
  15218. + * Hardware details are visible (see CONFIG_USB_ZERO_* below) but don't
  15219. + * affect most of the driver.
  15220. + *
  15221. + * Use it with the Linux host/master side "usbtest" driver to get a basic
  15222. + * functional test of your device-side usb stack, or with "usb-skeleton".
  15223. + *
  15224. + * It supports two similar configurations. One sinks whatever the usb host
  15225. + * writes, and in return sources zeroes. The other loops whatever the host
  15226. + * writes back, so the host can read it. Module options include:
  15227. + *
  15228. + * buflen=N default N=4096, buffer size used
  15229. + * qlen=N default N=32, how many buffers in the loopback queue
  15230. + * loopdefault default false, list loopback config first
  15231. + *
  15232. + * Many drivers will only have one configuration, letting them be much
  15233. + * simpler if they also don't support high speed operation (like this
  15234. + * driver does).
  15235. + */
  15236. +
  15237. +#include <linux/config.h>
  15238. +#include <linux/module.h>
  15239. +#include <linux/kernel.h>
  15240. +#include <linux/delay.h>
  15241. +#include <linux/ioport.h>
  15242. +#include <linux/sched.h>
  15243. +#include <linux/slab.h>
  15244. +#include <linux/smp_lock.h>
  15245. +#include <linux/errno.h>
  15246. +#include <linux/init.h>
  15247. +#include <linux/timer.h>
  15248. +#include <linux/list.h>
  15249. +#include <linux/interrupt.h>
  15250. +#include <linux/uts.h>
  15251. +#include <linux/version.h>
  15252. +#include <linux/device.h>
  15253. +#include <linux/moduleparam.h>
  15254. +#include <linux/proc_fs.h>
  15255. +
  15256. +#include <asm/byteorder.h>
  15257. +#include <asm/io.h>
  15258. +#include <asm/irq.h>
  15259. +#include <asm/system.h>
  15260. +#include <asm/unaligned.h>
  15261. +
  15262. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
  15263. +# include <linux/usb/ch9.h>
  15264. +#else
  15265. +# include <linux/usb_ch9.h>
  15266. +#endif
  15267. +
  15268. +#include <linux/usb_gadget.h>
  15269. +
  15270. +
  15271. +/*-------------------------------------------------------------------------*/
  15272. +/*-------------------------------------------------------------------------*/
  15273. +
  15274. +
  15275. +static int utf8_to_utf16le(const char *s, u16 *cp, unsigned len)
  15276. +{
  15277. + int count = 0;
  15278. + u8 c;
  15279. + u16 uchar;
  15280. +
  15281. + /* this insists on correct encodings, though not minimal ones.
  15282. + * BUT it currently rejects legit 4-byte UTF-8 code points,
  15283. + * which need surrogate pairs. (Unicode 3.1 can use them.)
  15284. + */
  15285. + while (len != 0 && (c = (u8) *s++) != 0) {
  15286. + if (unlikely(c & 0x80)) {
  15287. + // 2-byte sequence:
  15288. + // 00000yyyyyxxxxxx = 110yyyyy 10xxxxxx
  15289. + if ((c & 0xe0) == 0xc0) {
  15290. + uchar = (c & 0x1f) << 6;
  15291. +
  15292. + c = (u8) *s++;
  15293. + if ((c & 0xc0) != 0xc0)
  15294. + goto fail;
  15295. + c &= 0x3f;
  15296. + uchar |= c;
  15297. +
  15298. + // 3-byte sequence (most CJKV characters):
  15299. + // zzzzyyyyyyxxxxxx = 1110zzzz 10yyyyyy 10xxxxxx
  15300. + } else if ((c & 0xf0) == 0xe0) {
  15301. + uchar = (c & 0x0f) << 12;
  15302. +
  15303. + c = (u8) *s++;
  15304. + if ((c & 0xc0) != 0xc0)
  15305. + goto fail;
  15306. + c &= 0x3f;
  15307. + uchar |= c << 6;
  15308. +
  15309. + c = (u8) *s++;
  15310. + if ((c & 0xc0) != 0xc0)
  15311. + goto fail;
  15312. + c &= 0x3f;
  15313. + uchar |= c;
  15314. +
  15315. + /* no bogus surrogates */
  15316. + if (0xd800 <= uchar && uchar <= 0xdfff)
  15317. + goto fail;
  15318. +
  15319. + // 4-byte sequence (surrogate pairs, currently rare):
  15320. + // 11101110wwwwzzzzyy + 110111yyyyxxxxxx
  15321. + // = 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx
  15322. + // (uuuuu = wwww + 1)
  15323. + // FIXME accept the surrogate code points (only)
  15324. +
  15325. + } else
  15326. + goto fail;
  15327. + } else
  15328. + uchar = c;
  15329. + put_unaligned (cpu_to_le16 (uchar), cp++);
  15330. + count++;
  15331. + len--;
  15332. + }
  15333. + return count;
  15334. +fail:
  15335. + return -1;
  15336. +}
  15337. +
  15338. +
  15339. +/**
  15340. + * usb_gadget_get_string - fill out a string descriptor
  15341. + * @table: of c strings encoded using UTF-8
  15342. + * @id: string id, from low byte of wValue in get string descriptor
  15343. + * @buf: at least 256 bytes
  15344. + *
  15345. + * Finds the UTF-8 string matching the ID, and converts it into a
  15346. + * string descriptor in utf16-le.
  15347. + * Returns length of descriptor (always even) or negative errno
  15348. + *
  15349. + * If your driver needs stings in multiple languages, you'll probably
  15350. + * "switch (wIndex) { ... }" in your ep0 string descriptor logic,
  15351. + * using this routine after choosing which set of UTF-8 strings to use.
  15352. + * Note that US-ASCII is a strict subset of UTF-8; any string bytes with
  15353. + * the eighth bit set will be multibyte UTF-8 characters, not ISO-8859/1
  15354. + * characters (which are also widely used in C strings).
  15355. + */
  15356. +int
  15357. +usb_gadget_get_string (struct usb_gadget_strings *table, int id, u8 *buf)
  15358. +{
  15359. + struct usb_string *s;
  15360. + int len;
  15361. +
  15362. + /* descriptor 0 has the language id */
  15363. + if (id == 0) {
  15364. + buf [0] = 4;
  15365. + buf [1] = USB_DT_STRING;
  15366. + buf [2] = (u8) table->language;
  15367. + buf [3] = (u8) (table->language >> 8);
  15368. + return 4;
  15369. + }
  15370. + for (s = table->strings; s && s->s; s++)
  15371. + if (s->id == id)
  15372. + break;
  15373. +
  15374. + /* unrecognized: stall. */
  15375. + if (!s || !s->s)
  15376. + return -EINVAL;
  15377. +
  15378. + /* string descriptors have length, tag, then UTF16-LE text */
  15379. + len = min ((size_t) 126, strlen (s->s));
  15380. + memset (buf + 2, 0, 2 * len); /* zero all the bytes */
  15381. + len = utf8_to_utf16le(s->s, (u16 *)&buf[2], len);
  15382. + if (len < 0)
  15383. + return -EINVAL;
  15384. + buf [0] = (len + 1) * 2;
  15385. + buf [1] = USB_DT_STRING;
  15386. + return buf [0];
  15387. +}
  15388. +
  15389. +
  15390. +/*-------------------------------------------------------------------------*/
  15391. +/*-------------------------------------------------------------------------*/
  15392. +
  15393. +
  15394. +/**
  15395. + * usb_descriptor_fillbuf - fill buffer with descriptors
  15396. + * @buf: Buffer to be filled
  15397. + * @buflen: Size of buf
  15398. + * @src: Array of descriptor pointers, terminated by null pointer.
  15399. + *
  15400. + * Copies descriptors into the buffer, returning the length or a
  15401. + * negative error code if they can't all be copied. Useful when
  15402. + * assembling descriptors for an associated set of interfaces used
  15403. + * as part of configuring a composite device; or in other cases where
  15404. + * sets of descriptors need to be marshaled.
  15405. + */
  15406. +int
  15407. +usb_descriptor_fillbuf(void *buf, unsigned buflen,
  15408. + const struct usb_descriptor_header **src)
  15409. +{
  15410. + u8 *dest = buf;
  15411. +
  15412. + if (!src)
  15413. + return -EINVAL;
  15414. +
  15415. + /* fill buffer from src[] until null descriptor ptr */
  15416. + for (; 0 != *src; src++) {
  15417. + unsigned len = (*src)->bLength;
  15418. +
  15419. + if (len > buflen)
  15420. + return -EINVAL;
  15421. + memcpy(dest, *src, len);
  15422. + buflen -= len;
  15423. + dest += len;
  15424. + }
  15425. + return dest - (u8 *)buf;
  15426. +}
  15427. +
  15428. +
  15429. +/**
  15430. + * usb_gadget_config_buf - builts a complete configuration descriptor
  15431. + * @config: Header for the descriptor, including characteristics such
  15432. + * as power requirements and number of interfaces.
  15433. + * @desc: Null-terminated vector of pointers to the descriptors (interface,
  15434. + * endpoint, etc) defining all functions in this device configuration.
  15435. + * @buf: Buffer for the resulting configuration descriptor.
  15436. + * @length: Length of buffer. If this is not big enough to hold the
  15437. + * entire configuration descriptor, an error code will be returned.
  15438. + *
  15439. + * This copies descriptors into the response buffer, building a descriptor
  15440. + * for that configuration. It returns the buffer length or a negative
  15441. + * status code. The config.wTotalLength field is set to match the length
  15442. + * of the result, but other descriptor fields (including power usage and
  15443. + * interface count) must be set by the caller.
  15444. + *
  15445. + * Gadget drivers could use this when constructing a config descriptor
  15446. + * in response to USB_REQ_GET_DESCRIPTOR. They will need to patch the
  15447. + * resulting bDescriptorType value if USB_DT_OTHER_SPEED_CONFIG is needed.
  15448. + */
  15449. +int usb_gadget_config_buf(
  15450. + const struct usb_config_descriptor *config,
  15451. + void *buf,
  15452. + unsigned length,
  15453. + const struct usb_descriptor_header **desc
  15454. +)
  15455. +{
  15456. + struct usb_config_descriptor *cp = buf;
  15457. + int len;
  15458. +
  15459. + /* config descriptor first */
  15460. + if (length < USB_DT_CONFIG_SIZE || !desc)
  15461. + return -EINVAL;
  15462. + *cp = *config;
  15463. +
  15464. + /* then interface/endpoint/class/vendor/... */
  15465. + len = usb_descriptor_fillbuf(USB_DT_CONFIG_SIZE + (u8*)buf,
  15466. + length - USB_DT_CONFIG_SIZE, desc);
  15467. + if (len < 0)
  15468. + return len;
  15469. + len += USB_DT_CONFIG_SIZE;
  15470. + if (len > 0xffff)
  15471. + return -EINVAL;
  15472. +
  15473. + /* patch up the config descriptor */
  15474. + cp->bLength = USB_DT_CONFIG_SIZE;
  15475. + cp->bDescriptorType = USB_DT_CONFIG;
  15476. + cp->wTotalLength = cpu_to_le16(len);
  15477. + cp->bmAttributes |= USB_CONFIG_ATT_ONE;
  15478. + return len;
  15479. +}
  15480. +
  15481. +/*-------------------------------------------------------------------------*/
  15482. +/*-------------------------------------------------------------------------*/
  15483. +
  15484. +
  15485. +#define RBUF_LEN (1024*1024)
  15486. +static int rbuf_start;
  15487. +static int rbuf_len;
  15488. +static __u8 rbuf[RBUF_LEN];
  15489. +
  15490. +/*-------------------------------------------------------------------------*/
  15491. +
  15492. +#define DRIVER_VERSION "St Patrick's Day 2004"
  15493. +
  15494. +static const char shortname [] = "zero";
  15495. +static const char longname [] = "YAMAHA YST-MS35D USB Speaker ";
  15496. +
  15497. +static const char source_sink [] = "source and sink data";
  15498. +static const char loopback [] = "loop input to output";
  15499. +
  15500. +/*-------------------------------------------------------------------------*/
  15501. +
  15502. +/*
  15503. + * driver assumes self-powered hardware, and
  15504. + * has no way for users to trigger remote wakeup.
  15505. + *
  15506. + * this version autoconfigures as much as possible,
  15507. + * which is reasonable for most "bulk-only" drivers.
  15508. + */
  15509. +static const char *EP_IN_NAME; /* source */
  15510. +static const char *EP_OUT_NAME; /* sink */
  15511. +
  15512. +/*-------------------------------------------------------------------------*/
  15513. +
  15514. +/* big enough to hold our biggest descriptor */
  15515. +#define USB_BUFSIZ 512
  15516. +
  15517. +struct zero_dev {
  15518. + spinlock_t lock;
  15519. + struct usb_gadget *gadget;
  15520. + struct usb_request *req; /* for control responses */
  15521. +
  15522. + /* when configured, we have one of two configs:
  15523. + * - source data (in to host) and sink it (out from host)
  15524. + * - or loop it back (out from host back in to host)
  15525. + */
  15526. + u8 config;
  15527. + struct usb_ep *in_ep, *out_ep;
  15528. +
  15529. + /* autoresume timer */
  15530. + struct timer_list resume;
  15531. +};
  15532. +
  15533. +#define xprintk(d,level,fmt,args...) \
  15534. + dev_printk(level , &(d)->gadget->dev , fmt , ## args)
  15535. +
  15536. +#ifdef DEBUG
  15537. +#define DBG(dev,fmt,args...) \
  15538. + xprintk(dev , KERN_DEBUG , fmt , ## args)
  15539. +#else
  15540. +#define DBG(dev,fmt,args...) \
  15541. + do { } while (0)
  15542. +#endif /* DEBUG */
  15543. +
  15544. +#ifdef VERBOSE
  15545. +#define VDBG DBG
  15546. +#else
  15547. +#define VDBG(dev,fmt,args...) \
  15548. + do { } while (0)
  15549. +#endif /* VERBOSE */
  15550. +
  15551. +#define ERROR(dev,fmt,args...) \
  15552. + xprintk(dev , KERN_ERR , fmt , ## args)
  15553. +#define WARN(dev,fmt,args...) \
  15554. + xprintk(dev , KERN_WARNING , fmt , ## args)
  15555. +#define INFO(dev,fmt,args...) \
  15556. + xprintk(dev , KERN_INFO , fmt , ## args)
  15557. +
  15558. +/*-------------------------------------------------------------------------*/
  15559. +
  15560. +static unsigned buflen = 4096;
  15561. +static unsigned qlen = 32;
  15562. +static unsigned pattern = 0;
  15563. +
  15564. +module_param (buflen, uint, S_IRUGO|S_IWUSR);
  15565. +module_param (qlen, uint, S_IRUGO|S_IWUSR);
  15566. +module_param (pattern, uint, S_IRUGO|S_IWUSR);
  15567. +
  15568. +/*
  15569. + * if it's nonzero, autoresume says how many seconds to wait
  15570. + * before trying to wake up the host after suspend.
  15571. + */
  15572. +static unsigned autoresume = 0;
  15573. +module_param (autoresume, uint, 0);
  15574. +
  15575. +/*
  15576. + * Normally the "loopback" configuration is second (index 1) so
  15577. + * it's not the default. Here's where to change that order, to
  15578. + * work better with hosts where config changes are problematic.
  15579. + * Or controllers (like superh) that only support one config.
  15580. + */
  15581. +static int loopdefault = 0;
  15582. +
  15583. +module_param (loopdefault, bool, S_IRUGO|S_IWUSR);
  15584. +
  15585. +/*-------------------------------------------------------------------------*/
  15586. +
  15587. +/* Thanks to NetChip Technologies for donating this product ID.
  15588. + *
  15589. + * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
  15590. + * Instead: allocate your own, using normal USB-IF procedures.
  15591. + */
  15592. +#ifndef CONFIG_USB_ZERO_HNPTEST
  15593. +#define DRIVER_VENDOR_NUM 0x0525 /* NetChip */
  15594. +#define DRIVER_PRODUCT_NUM 0xa4a0 /* Linux-USB "Gadget Zero" */
  15595. +#else
  15596. +#define DRIVER_VENDOR_NUM 0x1a0a /* OTG test device IDs */
  15597. +#define DRIVER_PRODUCT_NUM 0xbadd
  15598. +#endif
  15599. +
  15600. +/*-------------------------------------------------------------------------*/
  15601. +
  15602. +/*
  15603. + * DESCRIPTORS ... most are static, but strings and (full)
  15604. + * configuration descriptors are built on demand.
  15605. + */
  15606. +
  15607. +/*
  15608. +#define STRING_MANUFACTURER 25
  15609. +#define STRING_PRODUCT 42
  15610. +#define STRING_SERIAL 101
  15611. +*/
  15612. +#define STRING_MANUFACTURER 1
  15613. +#define STRING_PRODUCT 2
  15614. +#define STRING_SERIAL 3
  15615. +
  15616. +#define STRING_SOURCE_SINK 250
  15617. +#define STRING_LOOPBACK 251
  15618. +
  15619. +/*
  15620. + * This device advertises two configurations; these numbers work
  15621. + * on a pxa250 as well as more flexible hardware.
  15622. + */
  15623. +#define CONFIG_SOURCE_SINK 3
  15624. +#define CONFIG_LOOPBACK 2
  15625. +
  15626. +/*
  15627. +static struct usb_device_descriptor
  15628. +device_desc = {
  15629. + .bLength = sizeof device_desc,
  15630. + .bDescriptorType = USB_DT_DEVICE,
  15631. +
  15632. + .bcdUSB = __constant_cpu_to_le16 (0x0200),
  15633. + .bDeviceClass = USB_CLASS_VENDOR_SPEC,
  15634. +
  15635. + .idVendor = __constant_cpu_to_le16 (DRIVER_VENDOR_NUM),
  15636. + .idProduct = __constant_cpu_to_le16 (DRIVER_PRODUCT_NUM),
  15637. + .iManufacturer = STRING_MANUFACTURER,
  15638. + .iProduct = STRING_PRODUCT,
  15639. + .iSerialNumber = STRING_SERIAL,
  15640. + .bNumConfigurations = 2,
  15641. +};
  15642. +*/
  15643. +static struct usb_device_descriptor
  15644. +device_desc = {
  15645. + .bLength = sizeof device_desc,
  15646. + .bDescriptorType = USB_DT_DEVICE,
  15647. + .bcdUSB = __constant_cpu_to_le16 (0x0100),
  15648. + .bDeviceClass = USB_CLASS_PER_INTERFACE,
  15649. + .bDeviceSubClass = 0,
  15650. + .bDeviceProtocol = 0,
  15651. + .bMaxPacketSize0 = 64,
  15652. + .bcdDevice = __constant_cpu_to_le16 (0x0100),
  15653. + .idVendor = __constant_cpu_to_le16 (0x0499),
  15654. + .idProduct = __constant_cpu_to_le16 (0x3002),
  15655. + .iManufacturer = STRING_MANUFACTURER,
  15656. + .iProduct = STRING_PRODUCT,
  15657. + .iSerialNumber = STRING_SERIAL,
  15658. + .bNumConfigurations = 1,
  15659. +};
  15660. +
  15661. +static struct usb_config_descriptor
  15662. +z_config = {
  15663. + .bLength = sizeof z_config,
  15664. + .bDescriptorType = USB_DT_CONFIG,
  15665. +
  15666. + /* compute wTotalLength on the fly */
  15667. + .bNumInterfaces = 2,
  15668. + .bConfigurationValue = 1,
  15669. + .iConfiguration = 0,
  15670. + .bmAttributes = 0x40,
  15671. + .bMaxPower = 0, /* self-powered */
  15672. +};
  15673. +
  15674. +
  15675. +static struct usb_otg_descriptor
  15676. +otg_descriptor = {
  15677. + .bLength = sizeof otg_descriptor,
  15678. + .bDescriptorType = USB_DT_OTG,
  15679. +
  15680. + .bmAttributes = USB_OTG_SRP,
  15681. +};
  15682. +
  15683. +/* one interface in each configuration */
  15684. +#ifdef CONFIG_USB_GADGET_DUALSPEED
  15685. +
  15686. +/*
  15687. + * usb 2.0 devices need to expose both high speed and full speed
  15688. + * descriptors, unless they only run at full speed.
  15689. + *
  15690. + * that means alternate endpoint descriptors (bigger packets)
  15691. + * and a "device qualifier" ... plus more construction options
  15692. + * for the config descriptor.
  15693. + */
  15694. +
  15695. +static struct usb_qualifier_descriptor
  15696. +dev_qualifier = {
  15697. + .bLength = sizeof dev_qualifier,
  15698. + .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
  15699. +
  15700. + .bcdUSB = __constant_cpu_to_le16 (0x0200),
  15701. + .bDeviceClass = USB_CLASS_VENDOR_SPEC,
  15702. +
  15703. + .bNumConfigurations = 2,
  15704. +};
  15705. +
  15706. +
  15707. +struct usb_cs_as_general_descriptor {
  15708. + __u8 bLength;
  15709. + __u8 bDescriptorType;
  15710. +
  15711. + __u8 bDescriptorSubType;
  15712. + __u8 bTerminalLink;
  15713. + __u8 bDelay;
  15714. + __u16 wFormatTag;
  15715. +} __attribute__ ((packed));
  15716. +
  15717. +struct usb_cs_as_format_descriptor {
  15718. + __u8 bLength;
  15719. + __u8 bDescriptorType;
  15720. +
  15721. + __u8 bDescriptorSubType;
  15722. + __u8 bFormatType;
  15723. + __u8 bNrChannels;
  15724. + __u8 bSubframeSize;
  15725. + __u8 bBitResolution;
  15726. + __u8 bSamfreqType;
  15727. + __u8 tLowerSamFreq[3];
  15728. + __u8 tUpperSamFreq[3];
  15729. +} __attribute__ ((packed));
  15730. +
  15731. +static const struct usb_interface_descriptor
  15732. +z_audio_control_if_desc = {
  15733. + .bLength = sizeof z_audio_control_if_desc,
  15734. + .bDescriptorType = USB_DT_INTERFACE,
  15735. + .bInterfaceNumber = 0,
  15736. + .bAlternateSetting = 0,
  15737. + .bNumEndpoints = 0,
  15738. + .bInterfaceClass = USB_CLASS_AUDIO,
  15739. + .bInterfaceSubClass = 0x1,
  15740. + .bInterfaceProtocol = 0,
  15741. + .iInterface = 0,
  15742. +};
  15743. +
  15744. +static const struct usb_interface_descriptor
  15745. +z_audio_if_desc = {
  15746. + .bLength = sizeof z_audio_if_desc,
  15747. + .bDescriptorType = USB_DT_INTERFACE,
  15748. + .bInterfaceNumber = 1,
  15749. + .bAlternateSetting = 0,
  15750. + .bNumEndpoints = 0,
  15751. + .bInterfaceClass = USB_CLASS_AUDIO,
  15752. + .bInterfaceSubClass = 0x2,
  15753. + .bInterfaceProtocol = 0,
  15754. + .iInterface = 0,
  15755. +};
  15756. +
  15757. +static const struct usb_interface_descriptor
  15758. +z_audio_if_desc2 = {
  15759. + .bLength = sizeof z_audio_if_desc,
  15760. + .bDescriptorType = USB_DT_INTERFACE,
  15761. + .bInterfaceNumber = 1,
  15762. + .bAlternateSetting = 1,
  15763. + .bNumEndpoints = 1,
  15764. + .bInterfaceClass = USB_CLASS_AUDIO,
  15765. + .bInterfaceSubClass = 0x2,
  15766. + .bInterfaceProtocol = 0,
  15767. + .iInterface = 0,
  15768. +};
  15769. +
  15770. +static const struct usb_cs_as_general_descriptor
  15771. +z_audio_cs_as_if_desc = {
  15772. + .bLength = 7,
  15773. + .bDescriptorType = 0x24,
  15774. +
  15775. + .bDescriptorSubType = 0x01,
  15776. + .bTerminalLink = 0x01,
  15777. + .bDelay = 0x0,
  15778. + .wFormatTag = __constant_cpu_to_le16 (0x0001)
  15779. +};
  15780. +
  15781. +
  15782. +static const struct usb_cs_as_format_descriptor
  15783. +z_audio_cs_as_format_desc = {
  15784. + .bLength = 0xe,
  15785. + .bDescriptorType = 0x24,
  15786. +
  15787. + .bDescriptorSubType = 2,
  15788. + .bFormatType = 1,
  15789. + .bNrChannels = 1,
  15790. + .bSubframeSize = 1,
  15791. + .bBitResolution = 8,
  15792. + .bSamfreqType = 0,
  15793. + .tLowerSamFreq = {0x7e, 0x13, 0x00},
  15794. + .tUpperSamFreq = {0xe2, 0xd6, 0x00},
  15795. +};
  15796. +
  15797. +static const struct usb_endpoint_descriptor
  15798. +z_iso_ep = {
  15799. + .bLength = 0x09,
  15800. + .bDescriptorType = 0x05,
  15801. + .bEndpointAddress = 0x04,
  15802. + .bmAttributes = 0x09,
  15803. + .wMaxPacketSize = 0x0038,
  15804. + .bInterval = 0x01,
  15805. + .bRefresh = 0x00,
  15806. + .bSynchAddress = 0x00,
  15807. +};
  15808. +
  15809. +static char z_iso_ep2[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
  15810. +
  15811. +// 9 bytes
  15812. +static char z_ac_interface_header_desc[] =
  15813. +{ 0x09, 0x24, 0x01, 0x00, 0x01, 0x2b, 0x00, 0x01, 0x01 };
  15814. +
  15815. +// 12 bytes
  15816. +static char z_0[] = {0x0c, 0x24, 0x02, 0x01, 0x01, 0x01, 0x00, 0x02,
  15817. + 0x03, 0x00, 0x00, 0x00};
  15818. +// 13 bytes
  15819. +static char z_1[] = {0x0d, 0x24, 0x06, 0x02, 0x01, 0x02, 0x15, 0x00,
  15820. + 0x02, 0x00, 0x02, 0x00, 0x00};
  15821. +// 9 bytes
  15822. +static char z_2[] = {0x09, 0x24, 0x03, 0x03, 0x01, 0x03, 0x00, 0x02,
  15823. + 0x00};
  15824. +
  15825. +static char za_0[] = {0x09, 0x04, 0x01, 0x02, 0x01, 0x01, 0x02, 0x00,
  15826. + 0x00};
  15827. +
  15828. +static char za_1[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
  15829. +
  15830. +static char za_2[] = {0x0e, 0x24, 0x02, 0x01, 0x02, 0x01, 0x08, 0x00,
  15831. + 0x7e, 0x13, 0x00, 0xe2, 0xd6, 0x00};
  15832. +
  15833. +static char za_3[] = {0x09, 0x05, 0x04, 0x09, 0x70, 0x00, 0x01, 0x00,
  15834. + 0x00};
  15835. +
  15836. +static char za_4[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
  15837. +
  15838. +static char za_5[] = {0x09, 0x04, 0x01, 0x03, 0x01, 0x01, 0x02, 0x00,
  15839. + 0x00};
  15840. +
  15841. +static char za_6[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
  15842. +
  15843. +static char za_7[] = {0x0e, 0x24, 0x02, 0x01, 0x01, 0x02, 0x10, 0x00,
  15844. + 0x7e, 0x13, 0x00, 0xe2, 0xd6, 0x00};
  15845. +
  15846. +static char za_8[] = {0x09, 0x05, 0x04, 0x09, 0x70, 0x00, 0x01, 0x00,
  15847. + 0x00};
  15848. +
  15849. +static char za_9[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
  15850. +
  15851. +static char za_10[] = {0x09, 0x04, 0x01, 0x04, 0x01, 0x01, 0x02, 0x00,
  15852. + 0x00};
  15853. +
  15854. +static char za_11[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
  15855. +
  15856. +static char za_12[] = {0x0e, 0x24, 0x02, 0x01, 0x02, 0x02, 0x10, 0x00,
  15857. + 0x73, 0x13, 0x00, 0xe2, 0xd6, 0x00};
  15858. +
  15859. +static char za_13[] = {0x09, 0x05, 0x04, 0x09, 0xe0, 0x00, 0x01, 0x00,
  15860. + 0x00};
  15861. +
  15862. +static char za_14[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
  15863. +
  15864. +static char za_15[] = {0x09, 0x04, 0x01, 0x05, 0x01, 0x01, 0x02, 0x00,
  15865. + 0x00};
  15866. +
  15867. +static char za_16[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
  15868. +
  15869. +static char za_17[] = {0x0e, 0x24, 0x02, 0x01, 0x01, 0x03, 0x14, 0x00,
  15870. + 0x7e, 0x13, 0x00, 0xe2, 0xd6, 0x00};
  15871. +
  15872. +static char za_18[] = {0x09, 0x05, 0x04, 0x09, 0xa8, 0x00, 0x01, 0x00,
  15873. + 0x00};
  15874. +
  15875. +static char za_19[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
  15876. +
  15877. +static char za_20[] = {0x09, 0x04, 0x01, 0x06, 0x01, 0x01, 0x02, 0x00,
  15878. + 0x00};
  15879. +
  15880. +static char za_21[] = {0x07, 0x24, 0x01, 0x01, 0x00, 0x01, 0x00};
  15881. +
  15882. +static char za_22[] = {0x0e, 0x24, 0x02, 0x01, 0x02, 0x03, 0x14, 0x00,
  15883. + 0x7e, 0x13, 0x00, 0xe2, 0xd6, 0x00};
  15884. +
  15885. +static char za_23[] = {0x09, 0x05, 0x04, 0x09, 0x50, 0x01, 0x01, 0x00,
  15886. + 0x00};
  15887. +
  15888. +static char za_24[] = {0x07, 0x25, 0x01, 0x00, 0x02, 0x00, 0x02};
  15889. +
  15890. +
  15891. +
  15892. +static const struct usb_descriptor_header *z_function [] = {
  15893. + (struct usb_descriptor_header *) &z_audio_control_if_desc,
  15894. + (struct usb_descriptor_header *) &z_ac_interface_header_desc,
  15895. + (struct usb_descriptor_header *) &z_0,
  15896. + (struct usb_descriptor_header *) &z_1,
  15897. + (struct usb_descriptor_header *) &z_2,
  15898. + (struct usb_descriptor_header *) &z_audio_if_desc,
  15899. + (struct usb_descriptor_header *) &z_audio_if_desc2,
  15900. + (struct usb_descriptor_header *) &z_audio_cs_as_if_desc,
  15901. + (struct usb_descriptor_header *) &z_audio_cs_as_format_desc,
  15902. + (struct usb_descriptor_header *) &z_iso_ep,
  15903. + (struct usb_descriptor_header *) &z_iso_ep2,
  15904. + (struct usb_descriptor_header *) &za_0,
  15905. + (struct usb_descriptor_header *) &za_1,
  15906. + (struct usb_descriptor_header *) &za_2,
  15907. + (struct usb_descriptor_header *) &za_3,
  15908. + (struct usb_descriptor_header *) &za_4,
  15909. + (struct usb_descriptor_header *) &za_5,
  15910. + (struct usb_descriptor_header *) &za_6,
  15911. + (struct usb_descriptor_header *) &za_7,
  15912. + (struct usb_descriptor_header *) &za_8,
  15913. + (struct usb_descriptor_header *) &za_9,
  15914. + (struct usb_descriptor_header *) &za_10,
  15915. + (struct usb_descriptor_header *) &za_11,
  15916. + (struct usb_descriptor_header *) &za_12,
  15917. + (struct usb_descriptor_header *) &za_13,
  15918. + (struct usb_descriptor_header *) &za_14,
  15919. + (struct usb_descriptor_header *) &za_15,
  15920. + (struct usb_descriptor_header *) &za_16,
  15921. + (struct usb_descriptor_header *) &za_17,
  15922. + (struct usb_descriptor_header *) &za_18,
  15923. + (struct usb_descriptor_header *) &za_19,
  15924. + (struct usb_descriptor_header *) &za_20,
  15925. + (struct usb_descriptor_header *) &za_21,
  15926. + (struct usb_descriptor_header *) &za_22,
  15927. + (struct usb_descriptor_header *) &za_23,
  15928. + (struct usb_descriptor_header *) &za_24,
  15929. + NULL,
  15930. +};
  15931. +
  15932. +/* maxpacket and other transfer characteristics vary by speed. */
  15933. +#define ep_desc(g,hs,fs) (((g)->speed==USB_SPEED_HIGH)?(hs):(fs))
  15934. +
  15935. +#else
  15936. +
  15937. +/* if there's no high speed support, maxpacket doesn't change. */
  15938. +#define ep_desc(g,hs,fs) fs
  15939. +
  15940. +#endif /* !CONFIG_USB_GADGET_DUALSPEED */
  15941. +
  15942. +static char manufacturer [40];
  15943. +//static char serial [40];
  15944. +static char serial [] = "Ser 00 em";
  15945. +
  15946. +/* static strings, in UTF-8 */
  15947. +static struct usb_string strings [] = {
  15948. + { STRING_MANUFACTURER, manufacturer, },
  15949. + { STRING_PRODUCT, longname, },
  15950. + { STRING_SERIAL, serial, },
  15951. + { STRING_LOOPBACK, loopback, },
  15952. + { STRING_SOURCE_SINK, source_sink, },
  15953. + { } /* end of list */
  15954. +};
  15955. +
  15956. +static struct usb_gadget_strings stringtab = {
  15957. + .language = 0x0409, /* en-us */
  15958. + .strings = strings,
  15959. +};
  15960. +
  15961. +/*
  15962. + * config descriptors are also handcrafted. these must agree with code
  15963. + * that sets configurations, and with code managing interfaces and their
  15964. + * altsettings. other complexity may come from:
  15965. + *
  15966. + * - high speed support, including "other speed config" rules
  15967. + * - multiple configurations
  15968. + * - interfaces with alternate settings
  15969. + * - embedded class or vendor-specific descriptors
  15970. + *
  15971. + * this handles high speed, and has a second config that could as easily
  15972. + * have been an alternate interface setting (on most hardware).
  15973. + *
  15974. + * NOTE: to demonstrate (and test) more USB capabilities, this driver
  15975. + * should include an altsetting to test interrupt transfers, including
  15976. + * high bandwidth modes at high speed. (Maybe work like Intel's test
  15977. + * device?)
  15978. + */
  15979. +static int
  15980. +config_buf (struct usb_gadget *gadget, u8 *buf, u8 type, unsigned index)
  15981. +{
  15982. + int len;
  15983. + const struct usb_descriptor_header **function;
  15984. +
  15985. + function = z_function;
  15986. + len = usb_gadget_config_buf (&z_config, buf, USB_BUFSIZ, function);
  15987. + if (len < 0)
  15988. + return len;
  15989. + ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
  15990. + return len;
  15991. +}
  15992. +
  15993. +/*-------------------------------------------------------------------------*/
  15994. +
  15995. +static struct usb_request *
  15996. +alloc_ep_req (struct usb_ep *ep, unsigned length)
  15997. +{
  15998. + struct usb_request *req;
  15999. +
  16000. + req = usb_ep_alloc_request (ep, GFP_ATOMIC);
  16001. + if (req) {
  16002. + req->length = length;
  16003. + req->buf = usb_ep_alloc_buffer (ep, length,
  16004. + &req->dma, GFP_ATOMIC);
  16005. + if (!req->buf) {
  16006. + usb_ep_free_request (ep, req);
  16007. + req = NULL;
  16008. + }
  16009. + }
  16010. + return req;
  16011. +}
  16012. +
  16013. +static void free_ep_req (struct usb_ep *ep, struct usb_request *req)
  16014. +{
  16015. + if (req->buf)
  16016. + usb_ep_free_buffer (ep, req->buf, req->dma, req->length);
  16017. + usb_ep_free_request (ep, req);
  16018. +}
  16019. +
  16020. +/*-------------------------------------------------------------------------*/
  16021. +
  16022. +/* optionally require specific source/sink data patterns */
  16023. +
  16024. +static int
  16025. +check_read_data (
  16026. + struct zero_dev *dev,
  16027. + struct usb_ep *ep,
  16028. + struct usb_request *req
  16029. +)
  16030. +{
  16031. + unsigned i;
  16032. + u8 *buf = req->buf;
  16033. +
  16034. + for (i = 0; i < req->actual; i++, buf++) {
  16035. + switch (pattern) {
  16036. + /* all-zeroes has no synchronization issues */
  16037. + case 0:
  16038. + if (*buf == 0)
  16039. + continue;
  16040. + break;
  16041. + /* mod63 stays in sync with short-terminated transfers,
  16042. + * or otherwise when host and gadget agree on how large
  16043. + * each usb transfer request should be. resync is done
  16044. + * with set_interface or set_config.
  16045. + */
  16046. + case 1:
  16047. + if (*buf == (u8)(i % 63))
  16048. + continue;
  16049. + break;
  16050. + }
  16051. + ERROR (dev, "bad OUT byte, buf [%d] = %d\n", i, *buf);
  16052. + usb_ep_set_halt (ep);
  16053. + return -EINVAL;
  16054. + }
  16055. + return 0;
  16056. +}
  16057. +
  16058. +/*-------------------------------------------------------------------------*/
  16059. +
  16060. +static void zero_reset_config (struct zero_dev *dev)
  16061. +{
  16062. + if (dev->config == 0)
  16063. + return;
  16064. +
  16065. + DBG (dev, "reset config\n");
  16066. +
  16067. + /* just disable endpoints, forcing completion of pending i/o.
  16068. + * all our completion handlers free their requests in this case.
  16069. + */
  16070. + if (dev->in_ep) {
  16071. + usb_ep_disable (dev->in_ep);
  16072. + dev->in_ep = NULL;
  16073. + }
  16074. + if (dev->out_ep) {
  16075. + usb_ep_disable (dev->out_ep);
  16076. + dev->out_ep = NULL;
  16077. + }
  16078. + dev->config = 0;
  16079. + del_timer (&dev->resume);
  16080. +}
  16081. +
  16082. +#define _write(f, buf, sz) (f->f_op->write(f, buf, sz, &f->f_pos))
  16083. +
  16084. +static void
  16085. +zero_isoc_complete (struct usb_ep *ep, struct usb_request *req)
  16086. +{
  16087. + struct zero_dev *dev = ep->driver_data;
  16088. + int status = req->status;
  16089. + int i, j;
  16090. +
  16091. + switch (status) {
  16092. +
  16093. + case 0: /* normal completion? */
  16094. + //printk ("\nzero ---------------> isoc normal completion %d bytes\n", req->actual);
  16095. + for (i=0, j=rbuf_start; i<req->actual; i++) {
  16096. + //printk ("%02x ", ((__u8*)req->buf)[i]);
  16097. + rbuf[j] = ((__u8*)req->buf)[i];
  16098. + j++;
  16099. + if (j >= RBUF_LEN) j=0;
  16100. + }
  16101. + rbuf_start = j;
  16102. + //printk ("\n\n");
  16103. +
  16104. + if (rbuf_len < RBUF_LEN) {
  16105. + rbuf_len += req->actual;
  16106. + if (rbuf_len > RBUF_LEN) {
  16107. + rbuf_len = RBUF_LEN;
  16108. + }
  16109. + }
  16110. +
  16111. + break;
  16112. +
  16113. + /* this endpoint is normally active while we're configured */
  16114. + case -ECONNABORTED: /* hardware forced ep reset */
  16115. + case -ECONNRESET: /* request dequeued */
  16116. + case -ESHUTDOWN: /* disconnect from host */
  16117. + VDBG (dev, "%s gone (%d), %d/%d\n", ep->name, status,
  16118. + req->actual, req->length);
  16119. + if (ep == dev->out_ep)
  16120. + check_read_data (dev, ep, req);
  16121. + free_ep_req (ep, req);
  16122. + return;
  16123. +
  16124. + case -EOVERFLOW: /* buffer overrun on read means that
  16125. + * we didn't provide a big enough
  16126. + * buffer.
  16127. + */
  16128. + default:
  16129. +#if 1
  16130. + DBG (dev, "%s complete --> %d, %d/%d\n", ep->name,
  16131. + status, req->actual, req->length);
  16132. +#endif
  16133. + case -EREMOTEIO: /* short read */
  16134. + break;
  16135. + }
  16136. +
  16137. + status = usb_ep_queue (ep, req, GFP_ATOMIC);
  16138. + if (status) {
  16139. + ERROR (dev, "kill %s: resubmit %d bytes --> %d\n",
  16140. + ep->name, req->length, status);
  16141. + usb_ep_set_halt (ep);
  16142. + /* FIXME recover later ... somehow */
  16143. + }
  16144. +}
  16145. +
  16146. +static struct usb_request *
  16147. +zero_start_isoc_ep (struct usb_ep *ep, int gfp_flags)
  16148. +{
  16149. + struct usb_request *req;
  16150. + int status;
  16151. +
  16152. + req = alloc_ep_req (ep, 512);
  16153. + if (!req)
  16154. + return NULL;
  16155. +
  16156. + req->complete = zero_isoc_complete;
  16157. +
  16158. + status = usb_ep_queue (ep, req, gfp_flags);
  16159. + if (status) {
  16160. + struct zero_dev *dev = ep->driver_data;
  16161. +
  16162. + ERROR (dev, "start %s --> %d\n", ep->name, status);
  16163. + free_ep_req (ep, req);
  16164. + req = NULL;
  16165. + }
  16166. +
  16167. + return req;
  16168. +}
  16169. +
  16170. +/* change our operational config. this code must agree with the code
  16171. + * that returns config descriptors, and altsetting code.
  16172. + *
  16173. + * it's also responsible for power management interactions. some
  16174. + * configurations might not work with our current power sources.
  16175. + *
  16176. + * note that some device controller hardware will constrain what this
  16177. + * code can do, perhaps by disallowing more than one configuration or
  16178. + * by limiting configuration choices (like the pxa2xx).
  16179. + */
  16180. +static int
  16181. +zero_set_config (struct zero_dev *dev, unsigned number, int gfp_flags)
  16182. +{
  16183. + int result = 0;
  16184. + struct usb_gadget *gadget = dev->gadget;
  16185. + const struct usb_endpoint_descriptor *d;
  16186. + struct usb_ep *ep;
  16187. +
  16188. + if (number == dev->config)
  16189. + return 0;
  16190. +
  16191. + zero_reset_config (dev);
  16192. +
  16193. + gadget_for_each_ep (ep, gadget) {
  16194. +
  16195. + if (strcmp (ep->name, "ep4") == 0) {
  16196. +
  16197. + d = (struct usb_endpoint_descripter *)&za_23; // isoc ep desc for audio i/f alt setting 6
  16198. + result = usb_ep_enable (ep, d);
  16199. +
  16200. + if (result == 0) {
  16201. + ep->driver_data = dev;
  16202. + dev->in_ep = ep;
  16203. +
  16204. + if (zero_start_isoc_ep (ep, gfp_flags) != 0) {
  16205. +
  16206. + dev->in_ep = ep;
  16207. + continue;
  16208. + }
  16209. +
  16210. + usb_ep_disable (ep);
  16211. + result = -EIO;
  16212. + }
  16213. + }
  16214. +
  16215. + }
  16216. +
  16217. + dev->config = number;
  16218. + return result;
  16219. +}
  16220. +
  16221. +/*-------------------------------------------------------------------------*/
  16222. +
  16223. +static void zero_setup_complete (struct usb_ep *ep, struct usb_request *req)
  16224. +{
  16225. + if (req->status || req->actual != req->length)
  16226. + DBG ((struct zero_dev *) ep->driver_data,
  16227. + "setup complete --> %d, %d/%d\n",
  16228. + req->status, req->actual, req->length);
  16229. +}
  16230. +
  16231. +/*
  16232. + * The setup() callback implements all the ep0 functionality that's
  16233. + * not handled lower down, in hardware or the hardware driver (like
  16234. + * device and endpoint feature flags, and their status). It's all
  16235. + * housekeeping for the gadget function we're implementing. Most of
  16236. + * the work is in config-specific setup.
  16237. + */
  16238. +static int
  16239. +zero_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
  16240. +{
  16241. + struct zero_dev *dev = get_gadget_data (gadget);
  16242. + struct usb_request *req = dev->req;
  16243. + int value = -EOPNOTSUPP;
  16244. +
  16245. + /* usually this stores reply data in the pre-allocated ep0 buffer,
  16246. + * but config change events will reconfigure hardware.
  16247. + */
  16248. + req->zero = 0;
  16249. + switch (ctrl->bRequest) {
  16250. +
  16251. + case USB_REQ_GET_DESCRIPTOR:
  16252. +
  16253. + switch (ctrl->wValue >> 8) {
  16254. +
  16255. + case USB_DT_DEVICE:
  16256. + value = min (ctrl->wLength, (u16) sizeof device_desc);
  16257. + memcpy (req->buf, &device_desc, value);
  16258. + break;
  16259. +#ifdef CONFIG_USB_GADGET_DUALSPEED
  16260. + case USB_DT_DEVICE_QUALIFIER:
  16261. + if (!gadget->is_dualspeed)
  16262. + break;
  16263. + value = min (ctrl->wLength, (u16) sizeof dev_qualifier);
  16264. + memcpy (req->buf, &dev_qualifier, value);
  16265. + break;
  16266. +
  16267. + case USB_DT_OTHER_SPEED_CONFIG:
  16268. + if (!gadget->is_dualspeed)
  16269. + break;
  16270. + // FALLTHROUGH
  16271. +#endif /* CONFIG_USB_GADGET_DUALSPEED */
  16272. + case USB_DT_CONFIG:
  16273. + value = config_buf (gadget, req->buf,
  16274. + ctrl->wValue >> 8,
  16275. + ctrl->wValue & 0xff);
  16276. + if (value >= 0)
  16277. + value = min (ctrl->wLength, (u16) value);
  16278. + break;
  16279. +
  16280. + case USB_DT_STRING:
  16281. + /* wIndex == language code.
  16282. + * this driver only handles one language, you can
  16283. + * add string tables for other languages, using
  16284. + * any UTF-8 characters
  16285. + */
  16286. + value = usb_gadget_get_string (&stringtab,
  16287. + ctrl->wValue & 0xff, req->buf);
  16288. + if (value >= 0) {
  16289. + value = min (ctrl->wLength, (u16) value);
  16290. + }
  16291. + break;
  16292. + }
  16293. + break;
  16294. +
  16295. + /* currently two configs, two speeds */
  16296. + case USB_REQ_SET_CONFIGURATION:
  16297. + if (ctrl->bRequestType != 0)
  16298. + goto unknown;
  16299. +
  16300. + spin_lock (&dev->lock);
  16301. + value = zero_set_config (dev, ctrl->wValue, GFP_ATOMIC);
  16302. + spin_unlock (&dev->lock);
  16303. + break;
  16304. + case USB_REQ_GET_CONFIGURATION:
  16305. + if (ctrl->bRequestType != USB_DIR_IN)
  16306. + goto unknown;
  16307. + *(u8 *)req->buf = dev->config;
  16308. + value = min (ctrl->wLength, (u16) 1);
  16309. + break;
  16310. +
  16311. + /* until we add altsetting support, or other interfaces,
  16312. + * only 0/0 are possible. pxa2xx only supports 0/0 (poorly)
  16313. + * and already killed pending endpoint I/O.
  16314. + */
  16315. + case USB_REQ_SET_INTERFACE:
  16316. +
  16317. + if (ctrl->bRequestType != USB_RECIP_INTERFACE)
  16318. + goto unknown;
  16319. + spin_lock (&dev->lock);
  16320. + if (dev->config) {
  16321. + u8 config = dev->config;
  16322. +
  16323. + /* resets interface configuration, forgets about
  16324. + * previous transaction state (queued bufs, etc)
  16325. + * and re-inits endpoint state (toggle etc)
  16326. + * no response queued, just zero status == success.
  16327. + * if we had more than one interface we couldn't
  16328. + * use this "reset the config" shortcut.
  16329. + */
  16330. + zero_reset_config (dev);
  16331. + zero_set_config (dev, config, GFP_ATOMIC);
  16332. + value = 0;
  16333. + }
  16334. + spin_unlock (&dev->lock);
  16335. + break;
  16336. + case USB_REQ_GET_INTERFACE:
  16337. + if ((ctrl->bRequestType == 0x21) && (ctrl->wIndex == 0x02)) {
  16338. + value = ctrl->wLength;
  16339. + break;
  16340. + }
  16341. + else {
  16342. + if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
  16343. + goto unknown;
  16344. + if (!dev->config)
  16345. + break;
  16346. + if (ctrl->wIndex != 0) {
  16347. + value = -EDOM;
  16348. + break;
  16349. + }
  16350. + *(u8 *)req->buf = 0;
  16351. + value = min (ctrl->wLength, (u16) 1);
  16352. + }
  16353. + break;
  16354. +
  16355. + /*
  16356. + * These are the same vendor-specific requests supported by
  16357. + * Intel's USB 2.0 compliance test devices. We exceed that
  16358. + * device spec by allowing multiple-packet requests.
  16359. + */
  16360. + case 0x5b: /* control WRITE test -- fill the buffer */
  16361. + if (ctrl->bRequestType != (USB_DIR_OUT|USB_TYPE_VENDOR))
  16362. + goto unknown;
  16363. + if (ctrl->wValue || ctrl->wIndex)
  16364. + break;
  16365. + /* just read that many bytes into the buffer */
  16366. + if (ctrl->wLength > USB_BUFSIZ)
  16367. + break;
  16368. + value = ctrl->wLength;
  16369. + break;
  16370. + case 0x5c: /* control READ test -- return the buffer */
  16371. + if (ctrl->bRequestType != (USB_DIR_IN|USB_TYPE_VENDOR))
  16372. + goto unknown;
  16373. + if (ctrl->wValue || ctrl->wIndex)
  16374. + break;
  16375. + /* expect those bytes are still in the buffer; send back */
  16376. + if (ctrl->wLength > USB_BUFSIZ
  16377. + || ctrl->wLength != req->length)
  16378. + break;
  16379. + value = ctrl->wLength;
  16380. + break;
  16381. +
  16382. + case 0x01: // SET_CUR
  16383. + case 0x02:
  16384. + case 0x03:
  16385. + case 0x04:
  16386. + case 0x05:
  16387. + value = ctrl->wLength;
  16388. + break;
  16389. + case 0x81:
  16390. + switch (ctrl->wValue) {
  16391. + case 0x0201:
  16392. + case 0x0202:
  16393. + ((u8*)req->buf)[0] = 0x00;
  16394. + ((u8*)req->buf)[1] = 0xe3;
  16395. + break;
  16396. + case 0x0300:
  16397. + case 0x0500:
  16398. + ((u8*)req->buf)[0] = 0x00;
  16399. + break;
  16400. + }
  16401. + //((u8*)req->buf)[0] = 0x81;
  16402. + //((u8*)req->buf)[1] = 0x81;
  16403. + value = ctrl->wLength;
  16404. + break;
  16405. + case 0x82:
  16406. + switch (ctrl->wValue) {
  16407. + case 0x0201:
  16408. + case 0x0202:
  16409. + ((u8*)req->buf)[0] = 0x00;
  16410. + ((u8*)req->buf)[1] = 0xc3;
  16411. + break;
  16412. + case 0x0300:
  16413. + case 0x0500:
  16414. + ((u8*)req->buf)[0] = 0x00;
  16415. + break;
  16416. + }
  16417. + //((u8*)req->buf)[0] = 0x82;
  16418. + //((u8*)req->buf)[1] = 0x82;
  16419. + value = ctrl->wLength;
  16420. + break;
  16421. + case 0x83:
  16422. + switch (ctrl->wValue) {
  16423. + case 0x0201:
  16424. + case 0x0202:
  16425. + ((u8*)req->buf)[0] = 0x00;
  16426. + ((u8*)req->buf)[1] = 0x00;
  16427. + break;
  16428. + case 0x0300:
  16429. + ((u8*)req->buf)[0] = 0x60;
  16430. + break;
  16431. + case 0x0500:
  16432. + ((u8*)req->buf)[0] = 0x18;
  16433. + break;
  16434. + }
  16435. + //((u8*)req->buf)[0] = 0x83;
  16436. + //((u8*)req->buf)[1] = 0x83;
  16437. + value = ctrl->wLength;
  16438. + break;
  16439. + case 0x84:
  16440. + switch (ctrl->wValue) {
  16441. + case 0x0201:
  16442. + case 0x0202:
  16443. + ((u8*)req->buf)[0] = 0x00;
  16444. + ((u8*)req->buf)[1] = 0x01;
  16445. + break;
  16446. + case 0x0300:
  16447. + case 0x0500:
  16448. + ((u8*)req->buf)[0] = 0x08;
  16449. + break;
  16450. + }
  16451. + //((u8*)req->buf)[0] = 0x84;
  16452. + //((u8*)req->buf)[1] = 0x84;
  16453. + value = ctrl->wLength;
  16454. + break;
  16455. + case 0x85:
  16456. + ((u8*)req->buf)[0] = 0x85;
  16457. + ((u8*)req->buf)[1] = 0x85;
  16458. + value = ctrl->wLength;
  16459. + break;
  16460. +
  16461. +
  16462. + default:
  16463. +unknown:
  16464. + printk("unknown control req%02x.%02x v%04x i%04x l%d\n",
  16465. + ctrl->bRequestType, ctrl->bRequest,
  16466. + ctrl->wValue, ctrl->wIndex, ctrl->wLength);
  16467. + }
  16468. +
  16469. + /* respond with data transfer before status phase? */
  16470. + if (value >= 0) {
  16471. + req->length = value;
  16472. + req->zero = value < ctrl->wLength
  16473. + && (value % gadget->ep0->maxpacket) == 0;
  16474. + value = usb_ep_queue (gadget->ep0, req, GFP_ATOMIC);
  16475. + if (value < 0) {
  16476. + DBG (dev, "ep_queue < 0 --> %d\n", value);
  16477. + req->status = 0;
  16478. + zero_setup_complete (gadget->ep0, req);
  16479. + }
  16480. + }
  16481. +
  16482. + /* device either stalls (value < 0) or reports success */
  16483. + return value;
  16484. +}
  16485. +
  16486. +static void
  16487. +zero_disconnect (struct usb_gadget *gadget)
  16488. +{
  16489. + struct zero_dev *dev = get_gadget_data (gadget);
  16490. + unsigned long flags;
  16491. +
  16492. + spin_lock_irqsave (&dev->lock, flags);
  16493. + zero_reset_config (dev);
  16494. +
  16495. + /* a more significant application might have some non-usb
  16496. + * activities to quiesce here, saving resources like power
  16497. + * or pushing the notification up a network stack.
  16498. + */
  16499. + spin_unlock_irqrestore (&dev->lock, flags);
  16500. +
  16501. + /* next we may get setup() calls to enumerate new connections;
  16502. + * or an unbind() during shutdown (including removing module).
  16503. + */
  16504. +}
  16505. +
  16506. +static void
  16507. +zero_autoresume (unsigned long _dev)
  16508. +{
  16509. + struct zero_dev *dev = (struct zero_dev *) _dev;
  16510. + int status;
  16511. +
  16512. + /* normally the host would be woken up for something
  16513. + * more significant than just a timer firing...
  16514. + */
  16515. + if (dev->gadget->speed != USB_SPEED_UNKNOWN) {
  16516. + status = usb_gadget_wakeup (dev->gadget);
  16517. + DBG (dev, "wakeup --> %d\n", status);
  16518. + }
  16519. +}
  16520. +
  16521. +/*-------------------------------------------------------------------------*/
  16522. +
  16523. +static void
  16524. +zero_unbind (struct usb_gadget *gadget)
  16525. +{
  16526. + struct zero_dev *dev = get_gadget_data (gadget);
  16527. +
  16528. + DBG (dev, "unbind\n");
  16529. +
  16530. + /* we've already been disconnected ... no i/o is active */
  16531. + if (dev->req)
  16532. + free_ep_req (gadget->ep0, dev->req);
  16533. + del_timer_sync (&dev->resume);
  16534. + kfree (dev);
  16535. + set_gadget_data (gadget, NULL);
  16536. +}
  16537. +
  16538. +static int
  16539. +zero_bind (struct usb_gadget *gadget)
  16540. +{
  16541. + struct zero_dev *dev;
  16542. + //struct usb_ep *ep;
  16543. +
  16544. + printk("binding\n");
  16545. + /*
  16546. + * DRIVER POLICY CHOICE: you may want to do this differently.
  16547. + * One thing to avoid is reusing a bcdDevice revision code
  16548. + * with different host-visible configurations or behavior
  16549. + * restrictions -- using ep1in/ep2out vs ep1out/ep3in, etc
  16550. + */
  16551. + //device_desc.bcdDevice = __constant_cpu_to_le16 (0x0201);
  16552. +
  16553. +
  16554. + /* ok, we made sense of the hardware ... */
  16555. + dev = kzalloc (sizeof *dev, SLAB_KERNEL);
  16556. + if (!dev)
  16557. + return -ENOMEM;
  16558. + spin_lock_init (&dev->lock);
  16559. + dev->gadget = gadget;
  16560. + set_gadget_data (gadget, dev);
  16561. +
  16562. + /* preallocate control response and buffer */
  16563. + dev->req = usb_ep_alloc_request (gadget->ep0, GFP_KERNEL);
  16564. + if (!dev->req)
  16565. + goto enomem;
  16566. + dev->req->buf = usb_ep_alloc_buffer (gadget->ep0, USB_BUFSIZ,
  16567. + &dev->req->dma, GFP_KERNEL);
  16568. + if (!dev->req->buf)
  16569. + goto enomem;
  16570. +
  16571. + dev->req->complete = zero_setup_complete;
  16572. +
  16573. + device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
  16574. +
  16575. +#ifdef CONFIG_USB_GADGET_DUALSPEED
  16576. + /* assume ep0 uses the same value for both speeds ... */
  16577. + dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
  16578. +
  16579. + /* and that all endpoints are dual-speed */
  16580. + //hs_source_desc.bEndpointAddress = fs_source_desc.bEndpointAddress;
  16581. + //hs_sink_desc.bEndpointAddress = fs_sink_desc.bEndpointAddress;
  16582. +#endif
  16583. +
  16584. + usb_gadget_set_selfpowered (gadget);
  16585. +
  16586. + init_timer (&dev->resume);
  16587. + dev->resume.function = zero_autoresume;
  16588. + dev->resume.data = (unsigned long) dev;
  16589. +
  16590. + gadget->ep0->driver_data = dev;
  16591. +
  16592. + INFO (dev, "%s, version: " DRIVER_VERSION "\n", longname);
  16593. + INFO (dev, "using %s, OUT %s IN %s\n", gadget->name,
  16594. + EP_OUT_NAME, EP_IN_NAME);
  16595. +
  16596. + snprintf (manufacturer, sizeof manufacturer,
  16597. + UTS_SYSNAME " " UTS_RELEASE " with %s",
  16598. + gadget->name);
  16599. +
  16600. + return 0;
  16601. +
  16602. +enomem:
  16603. + zero_unbind (gadget);
  16604. + return -ENOMEM;
  16605. +}
  16606. +
  16607. +/*-------------------------------------------------------------------------*/
  16608. +
  16609. +static void
  16610. +zero_suspend (struct usb_gadget *gadget)
  16611. +{
  16612. + struct zero_dev *dev = get_gadget_data (gadget);
  16613. +
  16614. + if (gadget->speed == USB_SPEED_UNKNOWN)
  16615. + return;
  16616. +
  16617. + if (autoresume) {
  16618. + mod_timer (&dev->resume, jiffies + (HZ * autoresume));
  16619. + DBG (dev, "suspend, wakeup in %d seconds\n", autoresume);
  16620. + } else
  16621. + DBG (dev, "suspend\n");
  16622. +}
  16623. +
  16624. +static void
  16625. +zero_resume (struct usb_gadget *gadget)
  16626. +{
  16627. + struct zero_dev *dev = get_gadget_data (gadget);
  16628. +
  16629. + DBG (dev, "resume\n");
  16630. + del_timer (&dev->resume);
  16631. +}
  16632. +
  16633. +
  16634. +/*-------------------------------------------------------------------------*/
  16635. +
  16636. +static struct usb_gadget_driver zero_driver = {
  16637. +#ifdef CONFIG_USB_GADGET_DUALSPEED
  16638. + .speed = USB_SPEED_HIGH,
  16639. +#else
  16640. + .speed = USB_SPEED_FULL,
  16641. +#endif
  16642. + .function = (char *) longname,
  16643. + .bind = zero_bind,
  16644. + .unbind = zero_unbind,
  16645. +
  16646. + .setup = zero_setup,
  16647. + .disconnect = zero_disconnect,
  16648. +
  16649. + .suspend = zero_suspend,
  16650. + .resume = zero_resume,
  16651. +
  16652. + .driver = {
  16653. + .name = (char *) shortname,
  16654. + // .shutdown = ...
  16655. + // .suspend = ...
  16656. + // .resume = ...
  16657. + },
  16658. +};
  16659. +
  16660. +MODULE_AUTHOR ("David Brownell");
  16661. +MODULE_LICENSE ("Dual BSD/GPL");
  16662. +
  16663. +static struct proc_dir_entry *pdir, *pfile;
  16664. +
  16665. +static int isoc_read_data (char *page, char **start,
  16666. + off_t off, int count,
  16667. + int *eof, void *data)
  16668. +{
  16669. + int i;
  16670. + static int c = 0;
  16671. + static int done = 0;
  16672. + static int s = 0;
  16673. +
  16674. +/*
  16675. + printk ("\ncount: %d\n", count);
  16676. + printk ("rbuf_start: %d\n", rbuf_start);
  16677. + printk ("rbuf_len: %d\n", rbuf_len);
  16678. + printk ("off: %d\n", off);
  16679. + printk ("start: %p\n\n", *start);
  16680. +*/
  16681. + if (done) {
  16682. + c = 0;
  16683. + done = 0;
  16684. + *eof = 1;
  16685. + return 0;
  16686. + }
  16687. +
  16688. + if (c == 0) {
  16689. + if (rbuf_len == RBUF_LEN)
  16690. + s = rbuf_start;
  16691. + else s = 0;
  16692. + }
  16693. +
  16694. + for (i=0; i<count && c<rbuf_len; i++, c++) {
  16695. + page[i] = rbuf[(c+s) % RBUF_LEN];
  16696. + }
  16697. + *start = page;
  16698. +
  16699. + if (c >= rbuf_len) {
  16700. + *eof = 1;
  16701. + done = 1;
  16702. + }
  16703. +
  16704. +
  16705. + return i;
  16706. +}
  16707. +
  16708. +static int __init init (void)
  16709. +{
  16710. +
  16711. + int retval = 0;
  16712. +
  16713. + pdir = proc_mkdir("isoc_test", NULL);
  16714. + if(pdir == NULL) {
  16715. + retval = -ENOMEM;
  16716. + printk("Error creating dir\n");
  16717. + goto done;
  16718. + }
  16719. + pdir->owner = THIS_MODULE;
  16720. +
  16721. + pfile = create_proc_read_entry("isoc_data",
  16722. + 0444, pdir,
  16723. + isoc_read_data,
  16724. + NULL);
  16725. + if (pfile == NULL) {
  16726. + retval = -ENOMEM;
  16727. + printk("Error creating file\n");
  16728. + goto no_file;
  16729. + }
  16730. + pfile->owner = THIS_MODULE;
  16731. +
  16732. + return usb_gadget_register_driver (&zero_driver);
  16733. +
  16734. + no_file:
  16735. + remove_proc_entry("isoc_data", NULL);
  16736. + done:
  16737. + return retval;
  16738. +}
  16739. +module_init (init);
  16740. +
  16741. +static void __exit cleanup (void)
  16742. +{
  16743. +
  16744. + usb_gadget_unregister_driver (&zero_driver);
  16745. +
  16746. + remove_proc_entry("isoc_data", pdir);
  16747. + remove_proc_entry("isoc_test", NULL);
  16748. +}
  16749. +module_exit (cleanup);
  16750. --- /dev/null
  16751. +++ b/drivers/usb/host/dwc_otg/dwc_cfi_common.h
  16752. @@ -0,0 +1,142 @@
  16753. +/* ==========================================================================
  16754. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  16755. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  16756. + * otherwise expressly agreed to in writing between Synopsys and you.
  16757. + *
  16758. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  16759. + * any End User Software License Agreement or Agreement for Licensed Product
  16760. + * with Synopsys or any supplement thereto. You are permitted to use and
  16761. + * redistribute this Software in source and binary forms, with or without
  16762. + * modification, provided that redistributions of source code must retain this
  16763. + * notice. You may not view, use, disclose, copy or distribute this file or
  16764. + * any information contained herein except pursuant to this license grant from
  16765. + * Synopsys. If you do not agree with this notice, including the disclaimer
  16766. + * below, then you are not authorized to use the Software.
  16767. + *
  16768. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  16769. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16770. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  16771. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  16772. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  16773. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  16774. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  16775. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  16776. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  16777. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  16778. + * DAMAGE.
  16779. + * ========================================================================== */
  16780. +
  16781. +#if !defined(__DWC_CFI_COMMON_H__)
  16782. +#define __DWC_CFI_COMMON_H__
  16783. +
  16784. +//#include <linux/types.h>
  16785. +
  16786. +/**
  16787. + * @file
  16788. + *
  16789. + * This file contains the CFI specific common constants, interfaces
  16790. + * (functions and macros) and structures for Linux. No PCD specific
  16791. + * data structure or definition is to be included in this file.
  16792. + *
  16793. + */
  16794. +
  16795. +/** This is a request for all Core Features */
  16796. +#define VEN_CORE_GET_FEATURES 0xB1
  16797. +
  16798. +/** This is a request to get the value of a specific Core Feature */
  16799. +#define VEN_CORE_GET_FEATURE 0xB2
  16800. +
  16801. +/** This command allows the host to set the value of a specific Core Feature */
  16802. +#define VEN_CORE_SET_FEATURE 0xB3
  16803. +
  16804. +/** This command allows the host to set the default values of
  16805. + * either all or any specific Core Feature
  16806. + */
  16807. +#define VEN_CORE_RESET_FEATURES 0xB4
  16808. +
  16809. +/** This command forces the PCD to write the deferred values of a Core Features */
  16810. +#define VEN_CORE_ACTIVATE_FEATURES 0xB5
  16811. +
  16812. +/** This request reads a DWORD value from a register at the specified offset */
  16813. +#define VEN_CORE_READ_REGISTER 0xB6
  16814. +
  16815. +/** This request writes a DWORD value into a register at the specified offset */
  16816. +#define VEN_CORE_WRITE_REGISTER 0xB7
  16817. +
  16818. +/** This structure is the header of the Core Features dataset returned to
  16819. + * the Host
  16820. + */
  16821. +struct cfi_all_features_header {
  16822. +/** The features header structure length is */
  16823. +#define CFI_ALL_FEATURES_HDR_LEN 8
  16824. + /**
  16825. + * The total length of the features dataset returned to the Host
  16826. + */
  16827. + uint16_t wTotalLen;
  16828. +
  16829. + /**
  16830. + * CFI version number inBinary-Coded Decimal (i.e., 1.00 is 100H).
  16831. + * This field identifies the version of the CFI Specification with which
  16832. + * the device is compliant.
  16833. + */
  16834. + uint16_t wVersion;
  16835. +
  16836. + /** The ID of the Core */
  16837. + uint16_t wCoreID;
  16838. +#define CFI_CORE_ID_UDC 1
  16839. +#define CFI_CORE_ID_OTG 2
  16840. +#define CFI_CORE_ID_WUDEV 3
  16841. +
  16842. + /** Number of features returned by VEN_CORE_GET_FEATURES request */
  16843. + uint16_t wNumFeatures;
  16844. +} UPACKED;
  16845. +
  16846. +typedef struct cfi_all_features_header cfi_all_features_header_t;
  16847. +
  16848. +/** This structure is a header of the Core Feature descriptor dataset returned to
  16849. + * the Host after the VEN_CORE_GET_FEATURES request
  16850. + */
  16851. +struct cfi_feature_desc_header {
  16852. +#define CFI_FEATURE_DESC_HDR_LEN 8
  16853. +
  16854. + /** The feature ID */
  16855. + uint16_t wFeatureID;
  16856. +
  16857. + /** Length of this feature descriptor in bytes - including the
  16858. + * length of the feature name string
  16859. + */
  16860. + uint16_t wLength;
  16861. +
  16862. + /** The data length of this feature in bytes */
  16863. + uint16_t wDataLength;
  16864. +
  16865. + /**
  16866. + * Attributes of this features
  16867. + * D0: Access rights
  16868. + * 0 - Read/Write
  16869. + * 1 - Read only
  16870. + */
  16871. + uint8_t bmAttributes;
  16872. +#define CFI_FEATURE_ATTR_RO 1
  16873. +#define CFI_FEATURE_ATTR_RW 0
  16874. +
  16875. + /** Length of the feature name in bytes */
  16876. + uint8_t bNameLen;
  16877. +
  16878. + /** The feature name buffer */
  16879. + //uint8_t *name;
  16880. +} UPACKED;
  16881. +
  16882. +typedef struct cfi_feature_desc_header cfi_feature_desc_header_t;
  16883. +
  16884. +/**
  16885. + * This structure describes a NULL terminated string referenced by its id field.
  16886. + * It is very similar to usb_string structure but has the id field type set to 16-bit.
  16887. + */
  16888. +struct cfi_string {
  16889. + uint16_t id;
  16890. + const uint8_t *s;
  16891. +};
  16892. +typedef struct cfi_string cfi_string_t;
  16893. +
  16894. +#endif
  16895. --- /dev/null
  16896. +++ b/drivers/usb/host/dwc_otg/dwc_otg_adp.c
  16897. @@ -0,0 +1,854 @@
  16898. +/* ==========================================================================
  16899. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_adp.c $
  16900. + * $Revision: #12 $
  16901. + * $Date: 2011/10/26 $
  16902. + * $Change: 1873028 $
  16903. + *
  16904. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  16905. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  16906. + * otherwise expressly agreed to in writing between Synopsys and you.
  16907. + *
  16908. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  16909. + * any End User Software License Agreement or Agreement for Licensed Product
  16910. + * with Synopsys or any supplement thereto. You are permitted to use and
  16911. + * redistribute this Software in source and binary forms, with or without
  16912. + * modification, provided that redistributions of source code must retain this
  16913. + * notice. You may not view, use, disclose, copy or distribute this file or
  16914. + * any information contained herein except pursuant to this license grant from
  16915. + * Synopsys. If you do not agree with this notice, including the disclaimer
  16916. + * below, then you are not authorized to use the Software.
  16917. + *
  16918. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  16919. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16920. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  16921. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  16922. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  16923. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  16924. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  16925. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  16926. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  16927. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  16928. + * DAMAGE.
  16929. + * ========================================================================== */
  16930. +
  16931. +#include "dwc_os.h"
  16932. +#include "dwc_otg_regs.h"
  16933. +#include "dwc_otg_cil.h"
  16934. +#include "dwc_otg_adp.h"
  16935. +
  16936. +/** @file
  16937. + *
  16938. + * This file contains the most of the Attach Detect Protocol implementation for
  16939. + * the driver to support OTG Rev2.0.
  16940. + *
  16941. + */
  16942. +
  16943. +void dwc_otg_adp_write_reg(dwc_otg_core_if_t * core_if, uint32_t value)
  16944. +{
  16945. + adpctl_data_t adpctl;
  16946. +
  16947. + adpctl.d32 = value;
  16948. + adpctl.b.ar = 0x2;
  16949. +
  16950. + DWC_WRITE_REG32(&core_if->core_global_regs->adpctl, adpctl.d32);
  16951. +
  16952. + while (adpctl.b.ar) {
  16953. + adpctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->adpctl);
  16954. + }
  16955. +
  16956. +}
  16957. +
  16958. +/**
  16959. + * Function is called to read ADP registers
  16960. + */
  16961. +uint32_t dwc_otg_adp_read_reg(dwc_otg_core_if_t * core_if)
  16962. +{
  16963. + adpctl_data_t adpctl;
  16964. +
  16965. + adpctl.d32 = 0;
  16966. + adpctl.b.ar = 0x1;
  16967. +
  16968. + DWC_WRITE_REG32(&core_if->core_global_regs->adpctl, adpctl.d32);
  16969. +
  16970. + while (adpctl.b.ar) {
  16971. + adpctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->adpctl);
  16972. + }
  16973. +
  16974. + return adpctl.d32;
  16975. +}
  16976. +
  16977. +/**
  16978. + * Function is called to read ADPCTL register and filter Write-clear bits
  16979. + */
  16980. +uint32_t dwc_otg_adp_read_reg_filter(dwc_otg_core_if_t * core_if)
  16981. +{
  16982. + adpctl_data_t adpctl;
  16983. +
  16984. + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  16985. + adpctl.b.adp_tmout_int = 0;
  16986. + adpctl.b.adp_prb_int = 0;
  16987. + adpctl.b.adp_tmout_int = 0;
  16988. +
  16989. + return adpctl.d32;
  16990. +}
  16991. +
  16992. +/**
  16993. + * Function is called to write ADP registers
  16994. + */
  16995. +void dwc_otg_adp_modify_reg(dwc_otg_core_if_t * core_if, uint32_t clr,
  16996. + uint32_t set)
  16997. +{
  16998. + dwc_otg_adp_write_reg(core_if,
  16999. + (dwc_otg_adp_read_reg(core_if) & (~clr)) | set);
  17000. +}
  17001. +
  17002. +static void adp_sense_timeout(void *ptr)
  17003. +{
  17004. + dwc_otg_core_if_t *core_if = (dwc_otg_core_if_t *) ptr;
  17005. + core_if->adp.sense_timer_started = 0;
  17006. + DWC_PRINTF("ADP SENSE TIMEOUT\n");
  17007. + if (core_if->adp_enable) {
  17008. + dwc_otg_adp_sense_stop(core_if);
  17009. + dwc_otg_adp_probe_start(core_if);
  17010. + }
  17011. +}
  17012. +
  17013. +/**
  17014. + * This function is called when the ADP vbus timer expires. Timeout is 1.1s.
  17015. + */
  17016. +static void adp_vbuson_timeout(void *ptr)
  17017. +{
  17018. + gpwrdn_data_t gpwrdn;
  17019. + dwc_otg_core_if_t *core_if = (dwc_otg_core_if_t *) ptr;
  17020. + hprt0_data_t hprt0 = {.d32 = 0 };
  17021. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  17022. + DWC_PRINTF("%s: 1.1 seconds expire after turning on VBUS\n",__FUNCTION__);
  17023. + if (core_if) {
  17024. + core_if->adp.vbuson_timer_started = 0;
  17025. + /* Turn off vbus */
  17026. + hprt0.b.prtpwr = 1;
  17027. + DWC_MODIFY_REG32(core_if->host_if->hprt0, hprt0.d32, 0);
  17028. + gpwrdn.d32 = 0;
  17029. +
  17030. + /* Power off the core */
  17031. + if (core_if->power_down == 2) {
  17032. + /* Enable Wakeup Logic */
  17033. +// gpwrdn.b.wkupactiv = 1;
  17034. + gpwrdn.b.pmuactv = 0;
  17035. + gpwrdn.b.pwrdnrstn = 1;
  17036. + gpwrdn.b.pwrdnclmp = 1;
  17037. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0,
  17038. + gpwrdn.d32);
  17039. +
  17040. + /* Suspend the Phy Clock */
  17041. + pcgcctl.b.stoppclk = 1;
  17042. + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
  17043. +
  17044. + /* Switch on VDD */
  17045. +// gpwrdn.b.wkupactiv = 1;
  17046. + gpwrdn.b.pmuactv = 1;
  17047. + gpwrdn.b.pwrdnrstn = 1;
  17048. + gpwrdn.b.pwrdnclmp = 1;
  17049. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0,
  17050. + gpwrdn.d32);
  17051. + } else {
  17052. + /* Enable Power Down Logic */
  17053. + gpwrdn.b.pmuintsel = 1;
  17054. + gpwrdn.b.pmuactv = 1;
  17055. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  17056. + }
  17057. +
  17058. + /* Power off the core */
  17059. + if (core_if->power_down == 2) {
  17060. + gpwrdn.d32 = 0;
  17061. + gpwrdn.b.pwrdnswtch = 1;
  17062. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn,
  17063. + gpwrdn.d32, 0);
  17064. + }
  17065. +
  17066. + /* Unmask SRP detected interrupt from Power Down Logic */
  17067. + gpwrdn.d32 = 0;
  17068. + gpwrdn.b.srp_det_msk = 1;
  17069. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  17070. +
  17071. + dwc_otg_adp_probe_start(core_if);
  17072. + dwc_otg_dump_global_registers(core_if);
  17073. + dwc_otg_dump_host_registers(core_if);
  17074. + }
  17075. +
  17076. +}
  17077. +
  17078. +/**
  17079. + * Start the ADP Initial Probe timer to detect if Port Connected interrupt is
  17080. + * not asserted within 1.1 seconds.
  17081. + *
  17082. + * @param core_if the pointer to core_if strucure.
  17083. + */
  17084. +void dwc_otg_adp_vbuson_timer_start(dwc_otg_core_if_t * core_if)
  17085. +{
  17086. + core_if->adp.vbuson_timer_started = 1;
  17087. + if (core_if->adp.vbuson_timer)
  17088. + {
  17089. + DWC_PRINTF("SCHEDULING VBUSON TIMER\n");
  17090. + /* 1.1 secs + 60ms necessary for cil_hcd_start*/
  17091. + DWC_TIMER_SCHEDULE(core_if->adp.vbuson_timer, 1160);
  17092. + } else {
  17093. + DWC_WARN("VBUSON_TIMER = %p\n",core_if->adp.vbuson_timer);
  17094. + }
  17095. +}
  17096. +
  17097. +#if 0
  17098. +/**
  17099. + * Masks all DWC OTG core interrupts
  17100. + *
  17101. + */
  17102. +static void mask_all_interrupts(dwc_otg_core_if_t * core_if)
  17103. +{
  17104. + int i;
  17105. + gahbcfg_data_t ahbcfg = {.d32 = 0 };
  17106. +
  17107. + /* Mask Host Interrupts */
  17108. +
  17109. + /* Clear and disable HCINTs */
  17110. + for (i = 0; i < core_if->core_params->host_channels; i++) {
  17111. + DWC_WRITE_REG32(&core_if->host_if->hc_regs[i]->hcintmsk, 0);
  17112. + DWC_WRITE_REG32(&core_if->host_if->hc_regs[i]->hcint, 0xFFFFFFFF);
  17113. +
  17114. + }
  17115. +
  17116. + /* Clear and disable HAINT */
  17117. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->haintmsk, 0x0000);
  17118. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->haint, 0xFFFFFFFF);
  17119. +
  17120. + /* Mask Device Interrupts */
  17121. + if (!core_if->multiproc_int_enable) {
  17122. + /* Clear and disable IN Endpoint interrupts */
  17123. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->diepmsk, 0);
  17124. + for (i = 0; i <= core_if->dev_if->num_in_eps; i++) {
  17125. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[i]->
  17126. + diepint, 0xFFFFFFFF);
  17127. + }
  17128. +
  17129. + /* Clear and disable OUT Endpoint interrupts */
  17130. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->doepmsk, 0);
  17131. + for (i = 0; i <= core_if->dev_if->num_out_eps; i++) {
  17132. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[i]->
  17133. + doepint, 0xFFFFFFFF);
  17134. + }
  17135. +
  17136. + /* Clear and disable DAINT */
  17137. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->daint,
  17138. + 0xFFFFFFFF);
  17139. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->daintmsk, 0);
  17140. + } else {
  17141. + for (i = 0; i < core_if->dev_if->num_in_eps; ++i) {
  17142. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->
  17143. + diepeachintmsk[i], 0);
  17144. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[i]->
  17145. + diepint, 0xFFFFFFFF);
  17146. + }
  17147. +
  17148. + for (i = 0; i < core_if->dev_if->num_out_eps; ++i) {
  17149. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->
  17150. + doepeachintmsk[i], 0);
  17151. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[i]->
  17152. + doepint, 0xFFFFFFFF);
  17153. + }
  17154. +
  17155. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->deachintmsk,
  17156. + 0);
  17157. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->deachint,
  17158. + 0xFFFFFFFF);
  17159. +
  17160. + }
  17161. +
  17162. + /* Disable interrupts */
  17163. + ahbcfg.b.glblintrmsk = 1;
  17164. + DWC_MODIFY_REG32(&core_if->core_global_regs->gahbcfg, ahbcfg.d32, 0);
  17165. +
  17166. + /* Disable all interrupts. */
  17167. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, 0);
  17168. +
  17169. + /* Clear any pending interrupts */
  17170. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  17171. +
  17172. + /* Clear any pending OTG Interrupts */
  17173. + DWC_WRITE_REG32(&core_if->core_global_regs->gotgint, 0xFFFFFFFF);
  17174. +}
  17175. +
  17176. +/**
  17177. + * Unmask Port Connection Detected interrupt
  17178. + *
  17179. + */
  17180. +static void unmask_conn_det_intr(dwc_otg_core_if_t * core_if)
  17181. +{
  17182. + gintmsk_data_t gintmsk = {.d32 = 0,.b.portintr = 1 };
  17183. +
  17184. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, gintmsk.d32);
  17185. +}
  17186. +#endif
  17187. +
  17188. +/**
  17189. + * Starts the ADP Probing
  17190. + *
  17191. + * @param core_if the pointer to core_if structure.
  17192. + */
  17193. +uint32_t dwc_otg_adp_probe_start(dwc_otg_core_if_t * core_if)
  17194. +{
  17195. +
  17196. + adpctl_data_t adpctl = {.d32 = 0};
  17197. + gpwrdn_data_t gpwrdn;
  17198. +#if 0
  17199. + adpctl_data_t adpctl_int = {.d32 = 0, .b.adp_prb_int = 1,
  17200. + .b.adp_sns_int = 1, b.adp_tmout_int};
  17201. +#endif
  17202. + dwc_otg_disable_global_interrupts(core_if);
  17203. + DWC_PRINTF("ADP Probe Start\n");
  17204. + core_if->adp.probe_enabled = 1;
  17205. +
  17206. + adpctl.b.adpres = 1;
  17207. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  17208. +
  17209. + while (adpctl.b.adpres) {
  17210. + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  17211. + }
  17212. +
  17213. + adpctl.d32 = 0;
  17214. + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  17215. +
  17216. + /* In Host mode unmask SRP detected interrupt */
  17217. + gpwrdn.d32 = 0;
  17218. + gpwrdn.b.sts_chngint_msk = 1;
  17219. + if (!gpwrdn.b.idsts) {
  17220. + gpwrdn.b.srp_det_msk = 1;
  17221. + }
  17222. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  17223. +
  17224. + adpctl.b.adp_tmout_int_msk = 1;
  17225. + adpctl.b.adp_prb_int_msk = 1;
  17226. + adpctl.b.prb_dschg = 1;
  17227. + adpctl.b.prb_delta = 1;
  17228. + adpctl.b.prb_per = 1;
  17229. + adpctl.b.adpen = 1;
  17230. + adpctl.b.enaprb = 1;
  17231. +
  17232. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  17233. + DWC_PRINTF("ADP Probe Finish\n");
  17234. + return 0;
  17235. +}
  17236. +
  17237. +/**
  17238. + * Starts the ADP Sense timer to detect if ADP Sense interrupt is not asserted
  17239. + * within 3 seconds.
  17240. + *
  17241. + * @param core_if the pointer to core_if strucure.
  17242. + */
  17243. +void dwc_otg_adp_sense_timer_start(dwc_otg_core_if_t * core_if)
  17244. +{
  17245. + core_if->adp.sense_timer_started = 1;
  17246. + DWC_TIMER_SCHEDULE(core_if->adp.sense_timer, 3000 /* 3 secs */ );
  17247. +}
  17248. +
  17249. +/**
  17250. + * Starts the ADP Sense
  17251. + *
  17252. + * @param core_if the pointer to core_if strucure.
  17253. + */
  17254. +uint32_t dwc_otg_adp_sense_start(dwc_otg_core_if_t * core_if)
  17255. +{
  17256. + adpctl_data_t adpctl;
  17257. +
  17258. + DWC_PRINTF("ADP Sense Start\n");
  17259. +
  17260. + /* Unmask ADP sense interrupt and mask all other from the core */
  17261. + adpctl.d32 = dwc_otg_adp_read_reg_filter(core_if);
  17262. + adpctl.b.adp_sns_int_msk = 1;
  17263. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  17264. + dwc_otg_disable_global_interrupts(core_if); // vahrama
  17265. +
  17266. + /* Set ADP reset bit*/
  17267. + adpctl.d32 = dwc_otg_adp_read_reg_filter(core_if);
  17268. + adpctl.b.adpres = 1;
  17269. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  17270. +
  17271. + while (adpctl.b.adpres) {
  17272. + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  17273. + }
  17274. +
  17275. + adpctl.b.adpres = 0;
  17276. + adpctl.b.adpen = 1;
  17277. + adpctl.b.enasns = 1;
  17278. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  17279. +
  17280. + dwc_otg_adp_sense_timer_start(core_if);
  17281. +
  17282. + return 0;
  17283. +}
  17284. +
  17285. +/**
  17286. + * Stops the ADP Probing
  17287. + *
  17288. + * @param core_if the pointer to core_if strucure.
  17289. + */
  17290. +uint32_t dwc_otg_adp_probe_stop(dwc_otg_core_if_t * core_if)
  17291. +{
  17292. +
  17293. + adpctl_data_t adpctl;
  17294. + DWC_PRINTF("Stop ADP probe\n");
  17295. + core_if->adp.probe_enabled = 0;
  17296. + core_if->adp.probe_counter = 0;
  17297. + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  17298. +
  17299. + adpctl.b.adpen = 0;
  17300. + adpctl.b.adp_prb_int = 1;
  17301. + adpctl.b.adp_tmout_int = 1;
  17302. + adpctl.b.adp_sns_int = 1;
  17303. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  17304. +
  17305. + return 0;
  17306. +}
  17307. +
  17308. +/**
  17309. + * Stops the ADP Sensing
  17310. + *
  17311. + * @param core_if the pointer to core_if strucure.
  17312. + */
  17313. +uint32_t dwc_otg_adp_sense_stop(dwc_otg_core_if_t * core_if)
  17314. +{
  17315. + adpctl_data_t adpctl;
  17316. +
  17317. + core_if->adp.sense_enabled = 0;
  17318. +
  17319. + adpctl.d32 = dwc_otg_adp_read_reg_filter(core_if);
  17320. + adpctl.b.enasns = 0;
  17321. + adpctl.b.adp_sns_int = 1;
  17322. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  17323. +
  17324. + return 0;
  17325. +}
  17326. +
  17327. +/**
  17328. + * Called to turn on the VBUS after initial ADP probe in host mode.
  17329. + * If port power was already enabled in cil_hcd_start function then
  17330. + * only schedule a timer.
  17331. + *
  17332. + * @param core_if the pointer to core_if structure.
  17333. + */
  17334. +void dwc_otg_adp_turnon_vbus(dwc_otg_core_if_t * core_if)
  17335. +{
  17336. + hprt0_data_t hprt0 = {.d32 = 0 };
  17337. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  17338. + DWC_PRINTF("Turn on VBUS for 1.1s, port power is %d\n", hprt0.b.prtpwr);
  17339. +
  17340. + if (hprt0.b.prtpwr == 0) {
  17341. + hprt0.b.prtpwr = 1;
  17342. + //DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  17343. + }
  17344. +
  17345. + dwc_otg_adp_vbuson_timer_start(core_if);
  17346. +}
  17347. +
  17348. +/**
  17349. + * Called right after driver is loaded
  17350. + * to perform initial actions for ADP
  17351. + *
  17352. + * @param core_if the pointer to core_if structure.
  17353. + * @param is_host - flag for current mode of operation either from GINTSTS or GPWRDN
  17354. + */
  17355. +void dwc_otg_adp_start(dwc_otg_core_if_t * core_if, uint8_t is_host)
  17356. +{
  17357. + gpwrdn_data_t gpwrdn;
  17358. +
  17359. + DWC_PRINTF("ADP Initial Start\n");
  17360. + core_if->adp.adp_started = 1;
  17361. +
  17362. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  17363. + dwc_otg_disable_global_interrupts(core_if);
  17364. + if (is_host) {
  17365. + DWC_PRINTF("HOST MODE\n");
  17366. + /* Enable Power Down Logic Interrupt*/
  17367. + gpwrdn.d32 = 0;
  17368. + gpwrdn.b.pmuintsel = 1;
  17369. + gpwrdn.b.pmuactv = 1;
  17370. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  17371. + /* Initialize first ADP probe to obtain Ramp Time value */
  17372. + core_if->adp.initial_probe = 1;
  17373. + dwc_otg_adp_probe_start(core_if);
  17374. + } else {
  17375. + gotgctl_data_t gotgctl;
  17376. + gotgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  17377. + DWC_PRINTF("DEVICE MODE\n");
  17378. + if (gotgctl.b.bsesvld == 0) {
  17379. + /* Enable Power Down Logic Interrupt*/
  17380. + gpwrdn.d32 = 0;
  17381. + DWC_PRINTF("VBUS is not valid - start ADP probe\n");
  17382. + gpwrdn.b.pmuintsel = 1;
  17383. + gpwrdn.b.pmuactv = 1;
  17384. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  17385. + core_if->adp.initial_probe = 1;
  17386. + dwc_otg_adp_probe_start(core_if);
  17387. + } else {
  17388. + DWC_PRINTF("VBUS is valid - initialize core as a Device\n");
  17389. + core_if->op_state = B_PERIPHERAL;
  17390. + dwc_otg_core_init(core_if);
  17391. + dwc_otg_enable_global_interrupts(core_if);
  17392. + cil_pcd_start(core_if);
  17393. + dwc_otg_dump_global_registers(core_if);
  17394. + dwc_otg_dump_dev_registers(core_if);
  17395. + }
  17396. + }
  17397. +}
  17398. +
  17399. +void dwc_otg_adp_init(dwc_otg_core_if_t * core_if)
  17400. +{
  17401. + core_if->adp.adp_started = 0;
  17402. + core_if->adp.initial_probe = 0;
  17403. + core_if->adp.probe_timer_values[0] = -1;
  17404. + core_if->adp.probe_timer_values[1] = -1;
  17405. + core_if->adp.probe_enabled = 0;
  17406. + core_if->adp.sense_enabled = 0;
  17407. + core_if->adp.sense_timer_started = 0;
  17408. + core_if->adp.vbuson_timer_started = 0;
  17409. + core_if->adp.probe_counter = 0;
  17410. + core_if->adp.gpwrdn = 0;
  17411. + core_if->adp.attached = DWC_OTG_ADP_UNKOWN;
  17412. + /* Initialize timers */
  17413. + core_if->adp.sense_timer =
  17414. + DWC_TIMER_ALLOC("ADP SENSE TIMER", adp_sense_timeout, core_if);
  17415. + core_if->adp.vbuson_timer =
  17416. + DWC_TIMER_ALLOC("ADP VBUS ON TIMER", adp_vbuson_timeout, core_if);
  17417. + if (!core_if->adp.sense_timer || !core_if->adp.vbuson_timer)
  17418. + {
  17419. + DWC_ERROR("Could not allocate memory for ADP timers\n");
  17420. + }
  17421. +}
  17422. +
  17423. +void dwc_otg_adp_remove(dwc_otg_core_if_t * core_if)
  17424. +{
  17425. + gpwrdn_data_t gpwrdn = { .d32 = 0 };
  17426. + gpwrdn.b.pmuintsel = 1;
  17427. + gpwrdn.b.pmuactv = 1;
  17428. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  17429. +
  17430. + if (core_if->adp.probe_enabled)
  17431. + dwc_otg_adp_probe_stop(core_if);
  17432. + if (core_if->adp.sense_enabled)
  17433. + dwc_otg_adp_sense_stop(core_if);
  17434. + if (core_if->adp.sense_timer_started)
  17435. + DWC_TIMER_CANCEL(core_if->adp.sense_timer);
  17436. + if (core_if->adp.vbuson_timer_started)
  17437. + DWC_TIMER_CANCEL(core_if->adp.vbuson_timer);
  17438. + DWC_TIMER_FREE(core_if->adp.sense_timer);
  17439. + DWC_TIMER_FREE(core_if->adp.vbuson_timer);
  17440. +}
  17441. +
  17442. +/////////////////////////////////////////////////////////////////////
  17443. +////////////// ADP Interrupt Handlers ///////////////////////////////
  17444. +/////////////////////////////////////////////////////////////////////
  17445. +/**
  17446. + * This function sets Ramp Timer values
  17447. + */
  17448. +static uint32_t set_timer_value(dwc_otg_core_if_t * core_if, uint32_t val)
  17449. +{
  17450. + if (core_if->adp.probe_timer_values[0] == -1) {
  17451. + core_if->adp.probe_timer_values[0] = val;
  17452. + core_if->adp.probe_timer_values[1] = -1;
  17453. + return 1;
  17454. + } else {
  17455. + core_if->adp.probe_timer_values[1] =
  17456. + core_if->adp.probe_timer_values[0];
  17457. + core_if->adp.probe_timer_values[0] = val;
  17458. + return 0;
  17459. + }
  17460. +}
  17461. +
  17462. +/**
  17463. + * This function compares Ramp Timer values
  17464. + */
  17465. +static uint32_t compare_timer_values(dwc_otg_core_if_t * core_if)
  17466. +{
  17467. + uint32_t diff;
  17468. + if (core_if->adp.probe_timer_values[0]>=core_if->adp.probe_timer_values[1])
  17469. + diff = core_if->adp.probe_timer_values[0]-core_if->adp.probe_timer_values[1];
  17470. + else
  17471. + diff = core_if->adp.probe_timer_values[1]-core_if->adp.probe_timer_values[0];
  17472. + if(diff < 2) {
  17473. + return 0;
  17474. + } else {
  17475. + return 1;
  17476. + }
  17477. +}
  17478. +
  17479. +/**
  17480. + * This function handles ADP Probe Interrupts
  17481. + */
  17482. +static int32_t dwc_otg_adp_handle_prb_intr(dwc_otg_core_if_t * core_if,
  17483. + uint32_t val)
  17484. +{
  17485. + adpctl_data_t adpctl = {.d32 = 0 };
  17486. + gpwrdn_data_t gpwrdn, temp;
  17487. + adpctl.d32 = val;
  17488. +
  17489. + temp.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  17490. + core_if->adp.probe_counter++;
  17491. + core_if->adp.gpwrdn = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  17492. + if (adpctl.b.rtim == 0 && !temp.b.idsts){
  17493. + DWC_PRINTF("RTIM value is 0\n");
  17494. + goto exit;
  17495. + }
  17496. + if (set_timer_value(core_if, adpctl.b.rtim) &&
  17497. + core_if->adp.initial_probe) {
  17498. + core_if->adp.initial_probe = 0;
  17499. + dwc_otg_adp_probe_stop(core_if);
  17500. + gpwrdn.d32 = 0;
  17501. + gpwrdn.b.pmuactv = 1;
  17502. + gpwrdn.b.pmuintsel = 1;
  17503. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  17504. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  17505. +
  17506. + /* check which value is for device mode and which for Host mode */
  17507. + if (!temp.b.idsts) { /* considered host mode value is 0 */
  17508. + /*
  17509. + * Turn on VBUS after initial ADP probe.
  17510. + */
  17511. + core_if->op_state = A_HOST;
  17512. + dwc_otg_enable_global_interrupts(core_if);
  17513. + DWC_SPINUNLOCK(core_if->lock);
  17514. + cil_hcd_start(core_if);
  17515. + dwc_otg_adp_turnon_vbus(core_if);
  17516. + DWC_SPINLOCK(core_if->lock);
  17517. + } else {
  17518. + /*
  17519. + * Initiate SRP after initial ADP probe.
  17520. + */
  17521. + dwc_otg_enable_global_interrupts(core_if);
  17522. + dwc_otg_initiate_srp(core_if);
  17523. + }
  17524. + } else if (core_if->adp.probe_counter > 2){
  17525. + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  17526. + if (compare_timer_values(core_if)) {
  17527. + DWC_PRINTF("Difference in timer values !!! \n");
  17528. +// core_if->adp.attached = DWC_OTG_ADP_ATTACHED;
  17529. + dwc_otg_adp_probe_stop(core_if);
  17530. +
  17531. + /* Power on the core */
  17532. + if (core_if->power_down == 2) {
  17533. + gpwrdn.b.pwrdnswtch = 1;
  17534. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  17535. + gpwrdn, 0, gpwrdn.d32);
  17536. + }
  17537. +
  17538. + /* check which value is for device mode and which for Host mode */
  17539. + if (!temp.b.idsts) { /* considered host mode value is 0 */
  17540. + /* Disable Interrupt from Power Down Logic */
  17541. + gpwrdn.d32 = 0;
  17542. + gpwrdn.b.pmuintsel = 1;
  17543. + gpwrdn.b.pmuactv = 1;
  17544. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  17545. + gpwrdn, gpwrdn.d32, 0);
  17546. +
  17547. + /*
  17548. + * Initialize the Core for Host mode.
  17549. + */
  17550. + core_if->op_state = A_HOST;
  17551. + dwc_otg_core_init(core_if);
  17552. + dwc_otg_enable_global_interrupts(core_if);
  17553. + cil_hcd_start(core_if);
  17554. + } else {
  17555. + gotgctl_data_t gotgctl;
  17556. + /* Mask SRP detected interrupt from Power Down Logic */
  17557. + gpwrdn.d32 = 0;
  17558. + gpwrdn.b.srp_det_msk = 1;
  17559. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  17560. + gpwrdn, gpwrdn.d32, 0);
  17561. +
  17562. + /* Disable Power Down Logic */
  17563. + gpwrdn.d32 = 0;
  17564. + gpwrdn.b.pmuintsel = 1;
  17565. + gpwrdn.b.pmuactv = 1;
  17566. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  17567. + gpwrdn, gpwrdn.d32, 0);
  17568. +
  17569. + /*
  17570. + * Initialize the Core for Device mode.
  17571. + */
  17572. + core_if->op_state = B_PERIPHERAL;
  17573. + dwc_otg_core_init(core_if);
  17574. + dwc_otg_enable_global_interrupts(core_if);
  17575. + cil_pcd_start(core_if);
  17576. +
  17577. + gotgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  17578. + if (!gotgctl.b.bsesvld) {
  17579. + dwc_otg_initiate_srp(core_if);
  17580. + }
  17581. + }
  17582. + }
  17583. + if (core_if->power_down == 2) {
  17584. + if (gpwrdn.b.bsessvld) {
  17585. + /* Mask SRP detected interrupt from Power Down Logic */
  17586. + gpwrdn.d32 = 0;
  17587. + gpwrdn.b.srp_det_msk = 1;
  17588. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  17589. +
  17590. + /* Disable Power Down Logic */
  17591. + gpwrdn.d32 = 0;
  17592. + gpwrdn.b.pmuactv = 1;
  17593. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  17594. +
  17595. + /*
  17596. + * Initialize the Core for Device mode.
  17597. + */
  17598. + core_if->op_state = B_PERIPHERAL;
  17599. + dwc_otg_core_init(core_if);
  17600. + dwc_otg_enable_global_interrupts(core_if);
  17601. + cil_pcd_start(core_if);
  17602. + }
  17603. + }
  17604. + }
  17605. +exit:
  17606. + /* Clear interrupt */
  17607. + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  17608. + adpctl.b.adp_prb_int = 1;
  17609. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  17610. +
  17611. + return 0;
  17612. +}
  17613. +
  17614. +/**
  17615. + * This function hadles ADP Sense Interrupt
  17616. + */
  17617. +static int32_t dwc_otg_adp_handle_sns_intr(dwc_otg_core_if_t * core_if)
  17618. +{
  17619. + adpctl_data_t adpctl;
  17620. + /* Stop ADP Sense timer */
  17621. + DWC_TIMER_CANCEL(core_if->adp.sense_timer);
  17622. +
  17623. + /* Restart ADP Sense timer */
  17624. + dwc_otg_adp_sense_timer_start(core_if);
  17625. +
  17626. + /* Clear interrupt */
  17627. + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  17628. + adpctl.b.adp_sns_int = 1;
  17629. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  17630. +
  17631. + return 0;
  17632. +}
  17633. +
  17634. +/**
  17635. + * This function handles ADP Probe Interrupts
  17636. + */
  17637. +static int32_t dwc_otg_adp_handle_prb_tmout_intr(dwc_otg_core_if_t * core_if,
  17638. + uint32_t val)
  17639. +{
  17640. + adpctl_data_t adpctl = {.d32 = 0 };
  17641. + adpctl.d32 = val;
  17642. + set_timer_value(core_if, adpctl.b.rtim);
  17643. +
  17644. + /* Clear interrupt */
  17645. + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  17646. + adpctl.b.adp_tmout_int = 1;
  17647. + dwc_otg_adp_write_reg(core_if, adpctl.d32);
  17648. +
  17649. + return 0;
  17650. +}
  17651. +
  17652. +/**
  17653. + * ADP Interrupt handler.
  17654. + *
  17655. + */
  17656. +int32_t dwc_otg_adp_handle_intr(dwc_otg_core_if_t * core_if)
  17657. +{
  17658. + int retval = 0;
  17659. + adpctl_data_t adpctl = {.d32 = 0};
  17660. +
  17661. + adpctl.d32 = dwc_otg_adp_read_reg(core_if);
  17662. + DWC_PRINTF("ADPCTL = %08x\n",adpctl.d32);
  17663. +
  17664. + if (adpctl.b.adp_sns_int & adpctl.b.adp_sns_int_msk) {
  17665. + DWC_PRINTF("ADP Sense interrupt\n");
  17666. + retval |= dwc_otg_adp_handle_sns_intr(core_if);
  17667. + }
  17668. + if (adpctl.b.adp_tmout_int & adpctl.b.adp_tmout_int_msk) {
  17669. + DWC_PRINTF("ADP timeout interrupt\n");
  17670. + retval |= dwc_otg_adp_handle_prb_tmout_intr(core_if, adpctl.d32);
  17671. + }
  17672. + if (adpctl.b.adp_prb_int & adpctl.b.adp_prb_int_msk) {
  17673. + DWC_PRINTF("ADP Probe interrupt\n");
  17674. + adpctl.b.adp_prb_int = 1;
  17675. + retval |= dwc_otg_adp_handle_prb_intr(core_if, adpctl.d32);
  17676. + }
  17677. +
  17678. +// dwc_otg_adp_modify_reg(core_if, adpctl.d32, 0);
  17679. + //dwc_otg_adp_write_reg(core_if, adpctl.d32);
  17680. + DWC_PRINTF("RETURN FROM ADP ISR\n");
  17681. +
  17682. + return retval;
  17683. +}
  17684. +
  17685. +/**
  17686. + *
  17687. + * @param core_if Programming view of DWC_otg controller.
  17688. + */
  17689. +int32_t dwc_otg_adp_handle_srp_intr(dwc_otg_core_if_t * core_if)
  17690. +{
  17691. +
  17692. +#ifndef DWC_HOST_ONLY
  17693. + hprt0_data_t hprt0;
  17694. + gpwrdn_data_t gpwrdn;
  17695. + DWC_DEBUGPL(DBG_ANY, "++ Power Down Logic Session Request Interrupt++\n");
  17696. +
  17697. + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  17698. + /* check which value is for device mode and which for Host mode */
  17699. + if (!gpwrdn.b.idsts) { /* considered host mode value is 0 */
  17700. + DWC_PRINTF("SRP: Host mode\n");
  17701. +
  17702. + if (core_if->adp_enable) {
  17703. + dwc_otg_adp_probe_stop(core_if);
  17704. +
  17705. + /* Power on the core */
  17706. + if (core_if->power_down == 2) {
  17707. + gpwrdn.b.pwrdnswtch = 1;
  17708. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  17709. + gpwrdn, 0, gpwrdn.d32);
  17710. + }
  17711. +
  17712. + core_if->op_state = A_HOST;
  17713. + dwc_otg_core_init(core_if);
  17714. + dwc_otg_enable_global_interrupts(core_if);
  17715. + cil_hcd_start(core_if);
  17716. + }
  17717. +
  17718. + /* Turn on the port power bit. */
  17719. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  17720. + hprt0.b.prtpwr = 1;
  17721. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  17722. +
  17723. + /* Start the Connection timer. So a message can be displayed
  17724. + * if connect does not occur within 10 seconds. */
  17725. + cil_hcd_session_start(core_if);
  17726. + } else {
  17727. + DWC_PRINTF("SRP: Device mode %s\n", __FUNCTION__);
  17728. + if (core_if->adp_enable) {
  17729. + dwc_otg_adp_probe_stop(core_if);
  17730. +
  17731. + /* Power on the core */
  17732. + if (core_if->power_down == 2) {
  17733. + gpwrdn.b.pwrdnswtch = 1;
  17734. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  17735. + gpwrdn, 0, gpwrdn.d32);
  17736. + }
  17737. +
  17738. + gpwrdn.d32 = 0;
  17739. + gpwrdn.b.pmuactv = 0;
  17740. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0,
  17741. + gpwrdn.d32);
  17742. +
  17743. + core_if->op_state = B_PERIPHERAL;
  17744. + dwc_otg_core_init(core_if);
  17745. + dwc_otg_enable_global_interrupts(core_if);
  17746. + cil_pcd_start(core_if);
  17747. + }
  17748. + }
  17749. +#endif
  17750. + return 1;
  17751. +}
  17752. --- /dev/null
  17753. +++ b/drivers/usb/host/dwc_otg/dwc_otg_adp.h
  17754. @@ -0,0 +1,80 @@
  17755. +/* ==========================================================================
  17756. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_adp.h $
  17757. + * $Revision: #7 $
  17758. + * $Date: 2011/10/24 $
  17759. + * $Change: 1871159 $
  17760. + *
  17761. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  17762. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  17763. + * otherwise expressly agreed to in writing between Synopsys and you.
  17764. + *
  17765. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  17766. + * any End User Software License Agreement or Agreement for Licensed Product
  17767. + * with Synopsys or any supplement thereto. You are permitted to use and
  17768. + * redistribute this Software in source and binary forms, with or without
  17769. + * modification, provided that redistributions of source code must retain this
  17770. + * notice. You may not view, use, disclose, copy or distribute this file or
  17771. + * any information contained herein except pursuant to this license grant from
  17772. + * Synopsys. If you do not agree with this notice, including the disclaimer
  17773. + * below, then you are not authorized to use the Software.
  17774. + *
  17775. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  17776. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17777. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17778. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  17779. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  17780. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  17781. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  17782. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  17783. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  17784. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  17785. + * DAMAGE.
  17786. + * ========================================================================== */
  17787. +
  17788. +#ifndef __DWC_OTG_ADP_H__
  17789. +#define __DWC_OTG_ADP_H__
  17790. +
  17791. +/**
  17792. + * @file
  17793. + *
  17794. + * This file contains the Attach Detect Protocol interfaces and defines
  17795. + * (functions) and structures for Linux.
  17796. + *
  17797. + */
  17798. +
  17799. +#define DWC_OTG_ADP_UNATTACHED 0
  17800. +#define DWC_OTG_ADP_ATTACHED 1
  17801. +#define DWC_OTG_ADP_UNKOWN 2
  17802. +
  17803. +typedef struct dwc_otg_adp {
  17804. + uint32_t adp_started;
  17805. + uint32_t initial_probe;
  17806. + int32_t probe_timer_values[2];
  17807. + uint32_t probe_enabled;
  17808. + uint32_t sense_enabled;
  17809. + dwc_timer_t *sense_timer;
  17810. + uint32_t sense_timer_started;
  17811. + dwc_timer_t *vbuson_timer;
  17812. + uint32_t vbuson_timer_started;
  17813. + uint32_t attached;
  17814. + uint32_t probe_counter;
  17815. + uint32_t gpwrdn;
  17816. +} dwc_otg_adp_t;
  17817. +
  17818. +/**
  17819. + * Attach Detect Protocol functions
  17820. + */
  17821. +
  17822. +extern void dwc_otg_adp_write_reg(dwc_otg_core_if_t * core_if, uint32_t value);
  17823. +extern uint32_t dwc_otg_adp_read_reg(dwc_otg_core_if_t * core_if);
  17824. +extern uint32_t dwc_otg_adp_probe_start(dwc_otg_core_if_t * core_if);
  17825. +extern uint32_t dwc_otg_adp_sense_start(dwc_otg_core_if_t * core_if);
  17826. +extern uint32_t dwc_otg_adp_probe_stop(dwc_otg_core_if_t * core_if);
  17827. +extern uint32_t dwc_otg_adp_sense_stop(dwc_otg_core_if_t * core_if);
  17828. +extern void dwc_otg_adp_start(dwc_otg_core_if_t * core_if, uint8_t is_host);
  17829. +extern void dwc_otg_adp_init(dwc_otg_core_if_t * core_if);
  17830. +extern void dwc_otg_adp_remove(dwc_otg_core_if_t * core_if);
  17831. +extern int32_t dwc_otg_adp_handle_intr(dwc_otg_core_if_t * core_if);
  17832. +extern int32_t dwc_otg_adp_handle_srp_intr(dwc_otg_core_if_t * core_if);
  17833. +
  17834. +#endif //__DWC_OTG_ADP_H__
  17835. --- /dev/null
  17836. +++ b/drivers/usb/host/dwc_otg/dwc_otg_attr.c
  17837. @@ -0,0 +1,1210 @@
  17838. +/* ==========================================================================
  17839. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_attr.c $
  17840. + * $Revision: #44 $
  17841. + * $Date: 2010/11/29 $
  17842. + * $Change: 1636033 $
  17843. + *
  17844. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  17845. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  17846. + * otherwise expressly agreed to in writing between Synopsys and you.
  17847. + *
  17848. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  17849. + * any End User Software License Agreement or Agreement for Licensed Product
  17850. + * with Synopsys or any supplement thereto. You are permitted to use and
  17851. + * redistribute this Software in source and binary forms, with or without
  17852. + * modification, provided that redistributions of source code must retain this
  17853. + * notice. You may not view, use, disclose, copy or distribute this file or
  17854. + * any information contained herein except pursuant to this license grant from
  17855. + * Synopsys. If you do not agree with this notice, including the disclaimer
  17856. + * below, then you are not authorized to use the Software.
  17857. + *
  17858. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  17859. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17860. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17861. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  17862. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  17863. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  17864. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  17865. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  17866. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  17867. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  17868. + * DAMAGE.
  17869. + * ========================================================================== */
  17870. +
  17871. +/** @file
  17872. + *
  17873. + * The diagnostic interface will provide access to the controller for
  17874. + * bringing up the hardware and testing. The Linux driver attributes
  17875. + * feature will be used to provide the Linux Diagnostic
  17876. + * Interface. These attributes are accessed through sysfs.
  17877. + */
  17878. +
  17879. +/** @page "Linux Module Attributes"
  17880. + *
  17881. + * The Linux module attributes feature is used to provide the Linux
  17882. + * Diagnostic Interface. These attributes are accessed through sysfs.
  17883. + * The diagnostic interface will provide access to the controller for
  17884. + * bringing up the hardware and testing.
  17885. +
  17886. + The following table shows the attributes.
  17887. + <table>
  17888. + <tr>
  17889. + <td><b> Name</b></td>
  17890. + <td><b> Description</b></td>
  17891. + <td><b> Access</b></td>
  17892. + </tr>
  17893. +
  17894. + <tr>
  17895. + <td> mode </td>
  17896. + <td> Returns the current mode: 0 for device mode, 1 for host mode</td>
  17897. + <td> Read</td>
  17898. + </tr>
  17899. +
  17900. + <tr>
  17901. + <td> hnpcapable </td>
  17902. + <td> Gets or sets the "HNP-capable" bit in the Core USB Configuraton Register.
  17903. + Read returns the current value.</td>
  17904. + <td> Read/Write</td>
  17905. + </tr>
  17906. +
  17907. + <tr>
  17908. + <td> srpcapable </td>
  17909. + <td> Gets or sets the "SRP-capable" bit in the Core USB Configuraton Register.
  17910. + Read returns the current value.</td>
  17911. + <td> Read/Write</td>
  17912. + </tr>
  17913. +
  17914. + <tr>
  17915. + <td> hsic_connect </td>
  17916. + <td> Gets or sets the "HSIC-Connect" bit in the GLPMCFG Register.
  17917. + Read returns the current value.</td>
  17918. + <td> Read/Write</td>
  17919. + </tr>
  17920. +
  17921. + <tr>
  17922. + <td> inv_sel_hsic </td>
  17923. + <td> Gets or sets the "Invert Select HSIC" bit in the GLPMFG Register.
  17924. + Read returns the current value.</td>
  17925. + <td> Read/Write</td>
  17926. + </tr>
  17927. +
  17928. + <tr>
  17929. + <td> hnp </td>
  17930. + <td> Initiates the Host Negotiation Protocol. Read returns the status.</td>
  17931. + <td> Read/Write</td>
  17932. + </tr>
  17933. +
  17934. + <tr>
  17935. + <td> srp </td>
  17936. + <td> Initiates the Session Request Protocol. Read returns the status.</td>
  17937. + <td> Read/Write</td>
  17938. + </tr>
  17939. +
  17940. + <tr>
  17941. + <td> buspower </td>
  17942. + <td> Gets or sets the Power State of the bus (0 - Off or 1 - On)</td>
  17943. + <td> Read/Write</td>
  17944. + </tr>
  17945. +
  17946. + <tr>
  17947. + <td> bussuspend </td>
  17948. + <td> Suspends the USB bus.</td>
  17949. + <td> Read/Write</td>
  17950. + </tr>
  17951. +
  17952. + <tr>
  17953. + <td> busconnected </td>
  17954. + <td> Gets the connection status of the bus</td>
  17955. + <td> Read</td>
  17956. + </tr>
  17957. +
  17958. + <tr>
  17959. + <td> gotgctl </td>
  17960. + <td> Gets or sets the Core Control Status Register.</td>
  17961. + <td> Read/Write</td>
  17962. + </tr>
  17963. +
  17964. + <tr>
  17965. + <td> gusbcfg </td>
  17966. + <td> Gets or sets the Core USB Configuration Register</td>
  17967. + <td> Read/Write</td>
  17968. + </tr>
  17969. +
  17970. + <tr>
  17971. + <td> grxfsiz </td>
  17972. + <td> Gets or sets the Receive FIFO Size Register</td>
  17973. + <td> Read/Write</td>
  17974. + </tr>
  17975. +
  17976. + <tr>
  17977. + <td> gnptxfsiz </td>
  17978. + <td> Gets or sets the non-periodic Transmit Size Register</td>
  17979. + <td> Read/Write</td>
  17980. + </tr>
  17981. +
  17982. + <tr>
  17983. + <td> gpvndctl </td>
  17984. + <td> Gets or sets the PHY Vendor Control Register</td>
  17985. + <td> Read/Write</td>
  17986. + </tr>
  17987. +
  17988. + <tr>
  17989. + <td> ggpio </td>
  17990. + <td> Gets the value in the lower 16-bits of the General Purpose IO Register
  17991. + or sets the upper 16 bits.</td>
  17992. + <td> Read/Write</td>
  17993. + </tr>
  17994. +
  17995. + <tr>
  17996. + <td> guid </td>
  17997. + <td> Gets or sets the value of the User ID Register</td>
  17998. + <td> Read/Write</td>
  17999. + </tr>
  18000. +
  18001. + <tr>
  18002. + <td> gsnpsid </td>
  18003. + <td> Gets the value of the Synopsys ID Regester</td>
  18004. + <td> Read</td>
  18005. + </tr>
  18006. +
  18007. + <tr>
  18008. + <td> devspeed </td>
  18009. + <td> Gets or sets the device speed setting in the DCFG register</td>
  18010. + <td> Read/Write</td>
  18011. + </tr>
  18012. +
  18013. + <tr>
  18014. + <td> enumspeed </td>
  18015. + <td> Gets the device enumeration Speed.</td>
  18016. + <td> Read</td>
  18017. + </tr>
  18018. +
  18019. + <tr>
  18020. + <td> hptxfsiz </td>
  18021. + <td> Gets the value of the Host Periodic Transmit FIFO</td>
  18022. + <td> Read</td>
  18023. + </tr>
  18024. +
  18025. + <tr>
  18026. + <td> hprt0 </td>
  18027. + <td> Gets or sets the value in the Host Port Control and Status Register</td>
  18028. + <td> Read/Write</td>
  18029. + </tr>
  18030. +
  18031. + <tr>
  18032. + <td> regoffset </td>
  18033. + <td> Sets the register offset for the next Register Access</td>
  18034. + <td> Read/Write</td>
  18035. + </tr>
  18036. +
  18037. + <tr>
  18038. + <td> regvalue </td>
  18039. + <td> Gets or sets the value of the register at the offset in the regoffset attribute.</td>
  18040. + <td> Read/Write</td>
  18041. + </tr>
  18042. +
  18043. + <tr>
  18044. + <td> remote_wakeup </td>
  18045. + <td> On read, shows the status of Remote Wakeup. On write, initiates a remote
  18046. + wakeup of the host. When bit 0 is 1 and Remote Wakeup is enabled, the Remote
  18047. + Wakeup signalling bit in the Device Control Register is set for 1
  18048. + milli-second.</td>
  18049. + <td> Read/Write</td>
  18050. + </tr>
  18051. +
  18052. + <tr>
  18053. + <td> rem_wakeup_pwrdn </td>
  18054. + <td> On read, shows the status core - hibernated or not. On write, initiates
  18055. + a remote wakeup of the device from Hibernation. </td>
  18056. + <td> Read/Write</td>
  18057. + </tr>
  18058. +
  18059. + <tr>
  18060. + <td> mode_ch_tim_en </td>
  18061. + <td> This bit is used to enable or disable the host core to wait for 200 PHY
  18062. + clock cycles at the end of Resume to change the opmode signal to the PHY to 00
  18063. + after Suspend or LPM. </td>
  18064. + <td> Read/Write</td>
  18065. + </tr>
  18066. +
  18067. + <tr>
  18068. + <td> fr_interval </td>
  18069. + <td> On read, shows the value of HFIR Frame Interval. On write, dynamically
  18070. + reload HFIR register during runtime. The application can write a value to this
  18071. + register only after the Port Enable bit of the Host Port Control and Status
  18072. + register (HPRT.PrtEnaPort) has been set </td>
  18073. + <td> Read/Write</td>
  18074. + </tr>
  18075. +
  18076. + <tr>
  18077. + <td> disconnect_us </td>
  18078. + <td> On read, shows the status of disconnect_device_us. On write, sets disconnect_us
  18079. + which causes soft disconnect for 100us. Applicable only for device mode of operation.</td>
  18080. + <td> Read/Write</td>
  18081. + </tr>
  18082. +
  18083. + <tr>
  18084. + <td> regdump </td>
  18085. + <td> Dumps the contents of core registers.</td>
  18086. + <td> Read</td>
  18087. + </tr>
  18088. +
  18089. + <tr>
  18090. + <td> spramdump </td>
  18091. + <td> Dumps the contents of core registers.</td>
  18092. + <td> Read</td>
  18093. + </tr>
  18094. +
  18095. + <tr>
  18096. + <td> hcddump </td>
  18097. + <td> Dumps the current HCD state.</td>
  18098. + <td> Read</td>
  18099. + </tr>
  18100. +
  18101. + <tr>
  18102. + <td> hcd_frrem </td>
  18103. + <td> Shows the average value of the Frame Remaining
  18104. + field in the Host Frame Number/Frame Remaining register when an SOF interrupt
  18105. + occurs. This can be used to determine the average interrupt latency. Also
  18106. + shows the average Frame Remaining value for start_transfer and the "a" and
  18107. + "b" sample points. The "a" and "b" sample points may be used during debugging
  18108. + bto determine how long it takes to execute a section of the HCD code.</td>
  18109. + <td> Read</td>
  18110. + </tr>
  18111. +
  18112. + <tr>
  18113. + <td> rd_reg_test </td>
  18114. + <td> Displays the time required to read the GNPTXFSIZ register many times
  18115. + (the output shows the number of times the register is read).
  18116. + <td> Read</td>
  18117. + </tr>
  18118. +
  18119. + <tr>
  18120. + <td> wr_reg_test </td>
  18121. + <td> Displays the time required to write the GNPTXFSIZ register many times
  18122. + (the output shows the number of times the register is written).
  18123. + <td> Read</td>
  18124. + </tr>
  18125. +
  18126. + <tr>
  18127. + <td> lpm_response </td>
  18128. + <td> Gets or sets lpm_response mode. Applicable only in device mode.
  18129. + <td> Write</td>
  18130. + </tr>
  18131. +
  18132. + <tr>
  18133. + <td> sleep_status </td>
  18134. + <td> Shows sleep status of device.
  18135. + <td> Read</td>
  18136. + </tr>
  18137. +
  18138. + </table>
  18139. +
  18140. + Example usage:
  18141. + To get the current mode:
  18142. + cat /sys/devices/lm0/mode
  18143. +
  18144. + To power down the USB:
  18145. + echo 0 > /sys/devices/lm0/buspower
  18146. + */
  18147. +
  18148. +#include "dwc_otg_os_dep.h"
  18149. +#include "dwc_os.h"
  18150. +#include "dwc_otg_driver.h"
  18151. +#include "dwc_otg_attr.h"
  18152. +#include "dwc_otg_core_if.h"
  18153. +#include "dwc_otg_pcd_if.h"
  18154. +#include "dwc_otg_hcd_if.h"
  18155. +
  18156. +/*
  18157. + * MACROs for defining sysfs attribute
  18158. + */
  18159. +#ifdef LM_INTERFACE
  18160. +
  18161. +#define DWC_OTG_DEVICE_ATTR_BITFIELD_SHOW(_otg_attr_name_,_string_) \
  18162. +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
  18163. +{ \
  18164. + struct lm_device *lm_dev = container_of(_dev, struct lm_device, dev); \
  18165. + dwc_otg_device_t *otg_dev = lm_get_drvdata(lm_dev); \
  18166. + uint32_t val; \
  18167. + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
  18168. + return sprintf (buf, "%s = 0x%x\n", _string_, val); \
  18169. +}
  18170. +#define DWC_OTG_DEVICE_ATTR_BITFIELD_STORE(_otg_attr_name_,_string_) \
  18171. +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
  18172. + const char *buf, size_t count) \
  18173. +{ \
  18174. + struct lm_device *lm_dev = container_of(_dev, struct lm_device, dev); \
  18175. + dwc_otg_device_t *otg_dev = lm_get_drvdata(lm_dev); \
  18176. + uint32_t set = simple_strtoul(buf, NULL, 16); \
  18177. + dwc_otg_set_##_otg_attr_name_(otg_dev->core_if, set);\
  18178. + return count; \
  18179. +}
  18180. +
  18181. +#elif defined(PCI_INTERFACE)
  18182. +
  18183. +#define DWC_OTG_DEVICE_ATTR_BITFIELD_SHOW(_otg_attr_name_,_string_) \
  18184. +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
  18185. +{ \
  18186. + dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev); \
  18187. + uint32_t val; \
  18188. + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
  18189. + return sprintf (buf, "%s = 0x%x\n", _string_, val); \
  18190. +}
  18191. +#define DWC_OTG_DEVICE_ATTR_BITFIELD_STORE(_otg_attr_name_,_string_) \
  18192. +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
  18193. + const char *buf, size_t count) \
  18194. +{ \
  18195. + dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev); \
  18196. + uint32_t set = simple_strtoul(buf, NULL, 16); \
  18197. + dwc_otg_set_##_otg_attr_name_(otg_dev->core_if, set);\
  18198. + return count; \
  18199. +}
  18200. +
  18201. +#elif defined(PLATFORM_INTERFACE)
  18202. +
  18203. +#define DWC_OTG_DEVICE_ATTR_BITFIELD_SHOW(_otg_attr_name_,_string_) \
  18204. +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
  18205. +{ \
  18206. + struct platform_device *platform_dev = \
  18207. + container_of(_dev, struct platform_device, dev); \
  18208. + dwc_otg_device_t *otg_dev = platform_get_drvdata(platform_dev); \
  18209. + uint32_t val; \
  18210. + DWC_PRINTF("%s(%p) -> platform_dev %p, otg_dev %p\n", \
  18211. + __func__, _dev, platform_dev, otg_dev); \
  18212. + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
  18213. + return sprintf (buf, "%s = 0x%x\n", _string_, val); \
  18214. +}
  18215. +#define DWC_OTG_DEVICE_ATTR_BITFIELD_STORE(_otg_attr_name_,_string_) \
  18216. +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
  18217. + const char *buf, size_t count) \
  18218. +{ \
  18219. + struct platform_device *platform_dev = container_of(_dev, struct platform_device, dev); \
  18220. + dwc_otg_device_t *otg_dev = platform_get_drvdata(platform_dev); \
  18221. + uint32_t set = simple_strtoul(buf, NULL, 16); \
  18222. + dwc_otg_set_##_otg_attr_name_(otg_dev->core_if, set);\
  18223. + return count; \
  18224. +}
  18225. +#endif
  18226. +
  18227. +/*
  18228. + * MACROs for defining sysfs attribute for 32-bit registers
  18229. + */
  18230. +#ifdef LM_INTERFACE
  18231. +#define DWC_OTG_DEVICE_ATTR_REG_SHOW(_otg_attr_name_,_string_) \
  18232. +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
  18233. +{ \
  18234. + struct lm_device *lm_dev = container_of(_dev, struct lm_device, dev); \
  18235. + dwc_otg_device_t *otg_dev = lm_get_drvdata(lm_dev); \
  18236. + uint32_t val; \
  18237. + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
  18238. + return sprintf (buf, "%s = 0x%08x\n", _string_, val); \
  18239. +}
  18240. +#define DWC_OTG_DEVICE_ATTR_REG_STORE(_otg_attr_name_,_string_) \
  18241. +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
  18242. + const char *buf, size_t count) \
  18243. +{ \
  18244. + struct lm_device *lm_dev = container_of(_dev, struct lm_device, dev); \
  18245. + dwc_otg_device_t *otg_dev = lm_get_drvdata(lm_dev); \
  18246. + uint32_t val = simple_strtoul(buf, NULL, 16); \
  18247. + dwc_otg_set_##_otg_attr_name_ (otg_dev->core_if, val); \
  18248. + return count; \
  18249. +}
  18250. +#elif defined(PCI_INTERFACE)
  18251. +#define DWC_OTG_DEVICE_ATTR_REG_SHOW(_otg_attr_name_,_string_) \
  18252. +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
  18253. +{ \
  18254. + dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev); \
  18255. + uint32_t val; \
  18256. + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
  18257. + return sprintf (buf, "%s = 0x%08x\n", _string_, val); \
  18258. +}
  18259. +#define DWC_OTG_DEVICE_ATTR_REG_STORE(_otg_attr_name_,_string_) \
  18260. +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
  18261. + const char *buf, size_t count) \
  18262. +{ \
  18263. + dwc_otg_device_t *otg_dev = dev_get_drvdata(_dev); \
  18264. + uint32_t val = simple_strtoul(buf, NULL, 16); \
  18265. + dwc_otg_set_##_otg_attr_name_ (otg_dev->core_if, val); \
  18266. + return count; \
  18267. +}
  18268. +
  18269. +#elif defined(PLATFORM_INTERFACE)
  18270. +#include "dwc_otg_dbg.h"
  18271. +#define DWC_OTG_DEVICE_ATTR_REG_SHOW(_otg_attr_name_,_string_) \
  18272. +static ssize_t _otg_attr_name_##_show (struct device *_dev, struct device_attribute *attr, char *buf) \
  18273. +{ \
  18274. + struct platform_device *platform_dev = container_of(_dev, struct platform_device, dev); \
  18275. + dwc_otg_device_t *otg_dev = platform_get_drvdata(platform_dev); \
  18276. + uint32_t val; \
  18277. + DWC_PRINTF("%s(%p) -> platform_dev %p, otg_dev %p\n", \
  18278. + __func__, _dev, platform_dev, otg_dev); \
  18279. + val = dwc_otg_get_##_otg_attr_name_ (otg_dev->core_if); \
  18280. + return sprintf (buf, "%s = 0x%08x\n", _string_, val); \
  18281. +}
  18282. +#define DWC_OTG_DEVICE_ATTR_REG_STORE(_otg_attr_name_,_string_) \
  18283. +static ssize_t _otg_attr_name_##_store (struct device *_dev, struct device_attribute *attr, \
  18284. + const char *buf, size_t count) \
  18285. +{ \
  18286. + struct platform_device *platform_dev = container_of(_dev, struct platform_device, dev); \
  18287. + dwc_otg_device_t *otg_dev = platform_get_drvdata(platform_dev); \
  18288. + uint32_t val = simple_strtoul(buf, NULL, 16); \
  18289. + dwc_otg_set_##_otg_attr_name_ (otg_dev->core_if, val); \
  18290. + return count; \
  18291. +}
  18292. +
  18293. +#endif
  18294. +
  18295. +#define DWC_OTG_DEVICE_ATTR_BITFIELD_RW(_otg_attr_name_,_string_) \
  18296. +DWC_OTG_DEVICE_ATTR_BITFIELD_SHOW(_otg_attr_name_,_string_) \
  18297. +DWC_OTG_DEVICE_ATTR_BITFIELD_STORE(_otg_attr_name_,_string_) \
  18298. +DEVICE_ATTR(_otg_attr_name_,0644,_otg_attr_name_##_show,_otg_attr_name_##_store);
  18299. +
  18300. +#define DWC_OTG_DEVICE_ATTR_BITFIELD_RO(_otg_attr_name_,_string_) \
  18301. +DWC_OTG_DEVICE_ATTR_BITFIELD_SHOW(_otg_attr_name_,_string_) \
  18302. +DEVICE_ATTR(_otg_attr_name_,0444,_otg_attr_name_##_show,NULL);
  18303. +
  18304. +#define DWC_OTG_DEVICE_ATTR_REG32_RW(_otg_attr_name_,_addr_,_string_) \
  18305. +DWC_OTG_DEVICE_ATTR_REG_SHOW(_otg_attr_name_,_string_) \
  18306. +DWC_OTG_DEVICE_ATTR_REG_STORE(_otg_attr_name_,_string_) \
  18307. +DEVICE_ATTR(_otg_attr_name_,0644,_otg_attr_name_##_show,_otg_attr_name_##_store);
  18308. +
  18309. +#define DWC_OTG_DEVICE_ATTR_REG32_RO(_otg_attr_name_,_addr_,_string_) \
  18310. +DWC_OTG_DEVICE_ATTR_REG_SHOW(_otg_attr_name_,_string_) \
  18311. +DEVICE_ATTR(_otg_attr_name_,0444,_otg_attr_name_##_show,NULL);
  18312. +
  18313. +/** @name Functions for Show/Store of Attributes */
  18314. +/**@{*/
  18315. +
  18316. +/**
  18317. + * Helper function returning the otg_device structure of the given device
  18318. + */
  18319. +static dwc_otg_device_t *dwc_otg_drvdev(struct device *_dev)
  18320. +{
  18321. + dwc_otg_device_t *otg_dev;
  18322. + DWC_OTG_GETDRVDEV(otg_dev, _dev);
  18323. + return otg_dev;
  18324. +}
  18325. +
  18326. +/**
  18327. + * Show the register offset of the Register Access.
  18328. + */
  18329. +static ssize_t regoffset_show(struct device *_dev,
  18330. + struct device_attribute *attr, char *buf)
  18331. +{
  18332. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18333. + return snprintf(buf, sizeof("0xFFFFFFFF\n") + 1, "0x%08x\n",
  18334. + otg_dev->os_dep.reg_offset);
  18335. +}
  18336. +
  18337. +/**
  18338. + * Set the register offset for the next Register Access Read/Write
  18339. + */
  18340. +static ssize_t regoffset_store(struct device *_dev,
  18341. + struct device_attribute *attr,
  18342. + const char *buf, size_t count)
  18343. +{
  18344. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18345. + uint32_t offset = simple_strtoul(buf, NULL, 16);
  18346. +#if defined(LM_INTERFACE) || defined(PLATFORM_INTERFACE)
  18347. + if (offset < SZ_256K) {
  18348. +#elif defined(PCI_INTERFACE)
  18349. + if (offset < 0x00040000) {
  18350. +#endif
  18351. + otg_dev->os_dep.reg_offset = offset;
  18352. + } else {
  18353. + dev_err(_dev, "invalid offset\n");
  18354. + }
  18355. +
  18356. + return count;
  18357. +}
  18358. +
  18359. +DEVICE_ATTR(regoffset, S_IRUGO | S_IWUSR, regoffset_show, regoffset_store);
  18360. +
  18361. +/**
  18362. + * Show the value of the register at the offset in the reg_offset
  18363. + * attribute.
  18364. + */
  18365. +static ssize_t regvalue_show(struct device *_dev,
  18366. + struct device_attribute *attr, char *buf)
  18367. +{
  18368. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18369. + uint32_t val;
  18370. + volatile uint32_t *addr;
  18371. +
  18372. + if (otg_dev->os_dep.reg_offset != 0xFFFFFFFF && 0 != otg_dev->os_dep.base) {
  18373. + /* Calculate the address */
  18374. + addr = (uint32_t *) (otg_dev->os_dep.reg_offset +
  18375. + (uint8_t *) otg_dev->os_dep.base);
  18376. + val = DWC_READ_REG32(addr);
  18377. + return snprintf(buf,
  18378. + sizeof("Reg@0xFFFFFFFF = 0xFFFFFFFF\n") + 1,
  18379. + "Reg@0x%06x = 0x%08x\n", otg_dev->os_dep.reg_offset,
  18380. + val);
  18381. + } else {
  18382. + dev_err(_dev, "Invalid offset (0x%0x)\n", otg_dev->os_dep.reg_offset);
  18383. + return sprintf(buf, "invalid offset\n");
  18384. + }
  18385. +}
  18386. +
  18387. +/**
  18388. + * Store the value in the register at the offset in the reg_offset
  18389. + * attribute.
  18390. + *
  18391. + */
  18392. +static ssize_t regvalue_store(struct device *_dev,
  18393. + struct device_attribute *attr,
  18394. + const char *buf, size_t count)
  18395. +{
  18396. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18397. + volatile uint32_t *addr;
  18398. + uint32_t val = simple_strtoul(buf, NULL, 16);
  18399. + //dev_dbg(_dev, "Offset=0x%08x Val=0x%08x\n", otg_dev->reg_offset, val);
  18400. + if (otg_dev->os_dep.reg_offset != 0xFFFFFFFF && 0 != otg_dev->os_dep.base) {
  18401. + /* Calculate the address */
  18402. + addr = (uint32_t *) (otg_dev->os_dep.reg_offset +
  18403. + (uint8_t *) otg_dev->os_dep.base);
  18404. + DWC_WRITE_REG32(addr, val);
  18405. + } else {
  18406. + dev_err(_dev, "Invalid Register Offset (0x%08x)\n",
  18407. + otg_dev->os_dep.reg_offset);
  18408. + }
  18409. + return count;
  18410. +}
  18411. +
  18412. +DEVICE_ATTR(regvalue, S_IRUGO | S_IWUSR, regvalue_show, regvalue_store);
  18413. +
  18414. +/*
  18415. + * Attributes
  18416. + */
  18417. +DWC_OTG_DEVICE_ATTR_BITFIELD_RO(mode, "Mode");
  18418. +DWC_OTG_DEVICE_ATTR_BITFIELD_RW(hnpcapable, "HNPCapable");
  18419. +DWC_OTG_DEVICE_ATTR_BITFIELD_RW(srpcapable, "SRPCapable");
  18420. +DWC_OTG_DEVICE_ATTR_BITFIELD_RW(hsic_connect, "HSIC Connect");
  18421. +DWC_OTG_DEVICE_ATTR_BITFIELD_RW(inv_sel_hsic, "Invert Select HSIC");
  18422. +
  18423. +//DWC_OTG_DEVICE_ATTR_BITFIELD_RW(buspower,&(otg_dev->core_if->core_global_regs->gotgctl),(1<<8),8,"Mode");
  18424. +//DWC_OTG_DEVICE_ATTR_BITFIELD_RW(bussuspend,&(otg_dev->core_if->core_global_regs->gotgctl),(1<<8),8,"Mode");
  18425. +DWC_OTG_DEVICE_ATTR_BITFIELD_RO(busconnected, "Bus Connected");
  18426. +
  18427. +DWC_OTG_DEVICE_ATTR_REG32_RW(gotgctl, 0, "GOTGCTL");
  18428. +DWC_OTG_DEVICE_ATTR_REG32_RW(gusbcfg,
  18429. + &(otg_dev->core_if->core_global_regs->gusbcfg),
  18430. + "GUSBCFG");
  18431. +DWC_OTG_DEVICE_ATTR_REG32_RW(grxfsiz,
  18432. + &(otg_dev->core_if->core_global_regs->grxfsiz),
  18433. + "GRXFSIZ");
  18434. +DWC_OTG_DEVICE_ATTR_REG32_RW(gnptxfsiz,
  18435. + &(otg_dev->core_if->core_global_regs->gnptxfsiz),
  18436. + "GNPTXFSIZ");
  18437. +DWC_OTG_DEVICE_ATTR_REG32_RW(gpvndctl,
  18438. + &(otg_dev->core_if->core_global_regs->gpvndctl),
  18439. + "GPVNDCTL");
  18440. +DWC_OTG_DEVICE_ATTR_REG32_RW(ggpio,
  18441. + &(otg_dev->core_if->core_global_regs->ggpio),
  18442. + "GGPIO");
  18443. +DWC_OTG_DEVICE_ATTR_REG32_RW(guid, &(otg_dev->core_if->core_global_regs->guid),
  18444. + "GUID");
  18445. +DWC_OTG_DEVICE_ATTR_REG32_RO(gsnpsid,
  18446. + &(otg_dev->core_if->core_global_regs->gsnpsid),
  18447. + "GSNPSID");
  18448. +DWC_OTG_DEVICE_ATTR_BITFIELD_RW(devspeed, "Device Speed");
  18449. +DWC_OTG_DEVICE_ATTR_BITFIELD_RO(enumspeed, "Device Enumeration Speed");
  18450. +
  18451. +DWC_OTG_DEVICE_ATTR_REG32_RO(hptxfsiz,
  18452. + &(otg_dev->core_if->core_global_regs->hptxfsiz),
  18453. + "HPTXFSIZ");
  18454. +DWC_OTG_DEVICE_ATTR_REG32_RW(hprt0, otg_dev->core_if->host_if->hprt0, "HPRT0");
  18455. +
  18456. +/**
  18457. + * @todo Add code to initiate the HNP.
  18458. + */
  18459. +/**
  18460. + * Show the HNP status bit
  18461. + */
  18462. +static ssize_t hnp_show(struct device *_dev,
  18463. + struct device_attribute *attr, char *buf)
  18464. +{
  18465. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18466. + return sprintf(buf, "HstNegScs = 0x%x\n",
  18467. + dwc_otg_get_hnpstatus(otg_dev->core_if));
  18468. +}
  18469. +
  18470. +/**
  18471. + * Set the HNP Request bit
  18472. + */
  18473. +static ssize_t hnp_store(struct device *_dev,
  18474. + struct device_attribute *attr,
  18475. + const char *buf, size_t count)
  18476. +{
  18477. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18478. + uint32_t in = simple_strtoul(buf, NULL, 16);
  18479. + dwc_otg_set_hnpreq(otg_dev->core_if, in);
  18480. + return count;
  18481. +}
  18482. +
  18483. +DEVICE_ATTR(hnp, 0644, hnp_show, hnp_store);
  18484. +
  18485. +/**
  18486. + * @todo Add code to initiate the SRP.
  18487. + */
  18488. +/**
  18489. + * Show the SRP status bit
  18490. + */
  18491. +static ssize_t srp_show(struct device *_dev,
  18492. + struct device_attribute *attr, char *buf)
  18493. +{
  18494. +#ifndef DWC_HOST_ONLY
  18495. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18496. + return sprintf(buf, "SesReqScs = 0x%x\n",
  18497. + dwc_otg_get_srpstatus(otg_dev->core_if));
  18498. +#else
  18499. + return sprintf(buf, "Host Only Mode!\n");
  18500. +#endif
  18501. +}
  18502. +
  18503. +/**
  18504. + * Set the SRP Request bit
  18505. + */
  18506. +static ssize_t srp_store(struct device *_dev,
  18507. + struct device_attribute *attr,
  18508. + const char *buf, size_t count)
  18509. +{
  18510. +#ifndef DWC_HOST_ONLY
  18511. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18512. + dwc_otg_pcd_initiate_srp(otg_dev->pcd);
  18513. +#endif
  18514. + return count;
  18515. +}
  18516. +
  18517. +DEVICE_ATTR(srp, 0644, srp_show, srp_store);
  18518. +
  18519. +/**
  18520. + * @todo Need to do more for power on/off?
  18521. + */
  18522. +/**
  18523. + * Show the Bus Power status
  18524. + */
  18525. +static ssize_t buspower_show(struct device *_dev,
  18526. + struct device_attribute *attr, char *buf)
  18527. +{
  18528. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18529. + return sprintf(buf, "Bus Power = 0x%x\n",
  18530. + dwc_otg_get_prtpower(otg_dev->core_if));
  18531. +}
  18532. +
  18533. +/**
  18534. + * Set the Bus Power status
  18535. + */
  18536. +static ssize_t buspower_store(struct device *_dev,
  18537. + struct device_attribute *attr,
  18538. + const char *buf, size_t count)
  18539. +{
  18540. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18541. + uint32_t on = simple_strtoul(buf, NULL, 16);
  18542. + dwc_otg_set_prtpower(otg_dev->core_if, on);
  18543. + return count;
  18544. +}
  18545. +
  18546. +DEVICE_ATTR(buspower, 0644, buspower_show, buspower_store);
  18547. +
  18548. +/**
  18549. + * @todo Need to do more for suspend?
  18550. + */
  18551. +/**
  18552. + * Show the Bus Suspend status
  18553. + */
  18554. +static ssize_t bussuspend_show(struct device *_dev,
  18555. + struct device_attribute *attr, char *buf)
  18556. +{
  18557. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18558. + return sprintf(buf, "Bus Suspend = 0x%x\n",
  18559. + dwc_otg_get_prtsuspend(otg_dev->core_if));
  18560. +}
  18561. +
  18562. +/**
  18563. + * Set the Bus Suspend status
  18564. + */
  18565. +static ssize_t bussuspend_store(struct device *_dev,
  18566. + struct device_attribute *attr,
  18567. + const char *buf, size_t count)
  18568. +{
  18569. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18570. + uint32_t in = simple_strtoul(buf, NULL, 16);
  18571. + dwc_otg_set_prtsuspend(otg_dev->core_if, in);
  18572. + return count;
  18573. +}
  18574. +
  18575. +DEVICE_ATTR(bussuspend, 0644, bussuspend_show, bussuspend_store);
  18576. +
  18577. +/**
  18578. + * Show the Mode Change Ready Timer status
  18579. + */
  18580. +static ssize_t mode_ch_tim_en_show(struct device *_dev,
  18581. + struct device_attribute *attr, char *buf)
  18582. +{
  18583. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18584. + return sprintf(buf, "Mode Change Ready Timer Enable = 0x%x\n",
  18585. + dwc_otg_get_mode_ch_tim(otg_dev->core_if));
  18586. +}
  18587. +
  18588. +/**
  18589. + * Set the Mode Change Ready Timer status
  18590. + */
  18591. +static ssize_t mode_ch_tim_en_store(struct device *_dev,
  18592. + struct device_attribute *attr,
  18593. + const char *buf, size_t count)
  18594. +{
  18595. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18596. + uint32_t in = simple_strtoul(buf, NULL, 16);
  18597. + dwc_otg_set_mode_ch_tim(otg_dev->core_if, in);
  18598. + return count;
  18599. +}
  18600. +
  18601. +DEVICE_ATTR(mode_ch_tim_en, 0644, mode_ch_tim_en_show, mode_ch_tim_en_store);
  18602. +
  18603. +/**
  18604. + * Show the value of HFIR Frame Interval bitfield
  18605. + */
  18606. +static ssize_t fr_interval_show(struct device *_dev,
  18607. + struct device_attribute *attr, char *buf)
  18608. +{
  18609. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18610. + return sprintf(buf, "Frame Interval = 0x%x\n",
  18611. + dwc_otg_get_fr_interval(otg_dev->core_if));
  18612. +}
  18613. +
  18614. +/**
  18615. + * Set the HFIR Frame Interval value
  18616. + */
  18617. +static ssize_t fr_interval_store(struct device *_dev,
  18618. + struct device_attribute *attr,
  18619. + const char *buf, size_t count)
  18620. +{
  18621. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18622. + uint32_t in = simple_strtoul(buf, NULL, 10);
  18623. + dwc_otg_set_fr_interval(otg_dev->core_if, in);
  18624. + return count;
  18625. +}
  18626. +
  18627. +DEVICE_ATTR(fr_interval, 0644, fr_interval_show, fr_interval_store);
  18628. +
  18629. +/**
  18630. + * Show the status of Remote Wakeup.
  18631. + */
  18632. +static ssize_t remote_wakeup_show(struct device *_dev,
  18633. + struct device_attribute *attr, char *buf)
  18634. +{
  18635. +#ifndef DWC_HOST_ONLY
  18636. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18637. +
  18638. + return sprintf(buf,
  18639. + "Remote Wakeup Sig = %d Enabled = %d LPM Remote Wakeup = %d\n",
  18640. + dwc_otg_get_remotewakesig(otg_dev->core_if),
  18641. + dwc_otg_pcd_get_rmwkup_enable(otg_dev->pcd),
  18642. + dwc_otg_get_lpm_remotewakeenabled(otg_dev->core_if));
  18643. +#else
  18644. + return sprintf(buf, "Host Only Mode!\n");
  18645. +#endif /* DWC_HOST_ONLY */
  18646. +}
  18647. +
  18648. +/**
  18649. + * Initiate a remote wakeup of the host. The Device control register
  18650. + * Remote Wakeup Signal bit is written if the PCD Remote wakeup enable
  18651. + * flag is set.
  18652. + *
  18653. + */
  18654. +static ssize_t remote_wakeup_store(struct device *_dev,
  18655. + struct device_attribute *attr,
  18656. + const char *buf, size_t count)
  18657. +{
  18658. +#ifndef DWC_HOST_ONLY
  18659. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18660. + uint32_t val = simple_strtoul(buf, NULL, 16);
  18661. +
  18662. + if (val & 1) {
  18663. + dwc_otg_pcd_remote_wakeup(otg_dev->pcd, 1);
  18664. + } else {
  18665. + dwc_otg_pcd_remote_wakeup(otg_dev->pcd, 0);
  18666. + }
  18667. +#endif /* DWC_HOST_ONLY */
  18668. + return count;
  18669. +}
  18670. +
  18671. +DEVICE_ATTR(remote_wakeup, S_IRUGO | S_IWUSR, remote_wakeup_show,
  18672. + remote_wakeup_store);
  18673. +
  18674. +/**
  18675. + * Show the whether core is hibernated or not.
  18676. + */
  18677. +static ssize_t rem_wakeup_pwrdn_show(struct device *_dev,
  18678. + struct device_attribute *attr, char *buf)
  18679. +{
  18680. +#ifndef DWC_HOST_ONLY
  18681. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18682. +
  18683. + if (dwc_otg_get_core_state(otg_dev->core_if)) {
  18684. + DWC_PRINTF("Core is in hibernation\n");
  18685. + } else {
  18686. + DWC_PRINTF("Core is not in hibernation\n");
  18687. + }
  18688. +#endif /* DWC_HOST_ONLY */
  18689. + return 0;
  18690. +}
  18691. +
  18692. +extern int dwc_otg_device_hibernation_restore(dwc_otg_core_if_t * core_if,
  18693. + int rem_wakeup, int reset);
  18694. +
  18695. +/**
  18696. + * Initiate a remote wakeup of the device to exit from hibernation.
  18697. + */
  18698. +static ssize_t rem_wakeup_pwrdn_store(struct device *_dev,
  18699. + struct device_attribute *attr,
  18700. + const char *buf, size_t count)
  18701. +{
  18702. +#ifndef DWC_HOST_ONLY
  18703. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18704. + dwc_otg_device_hibernation_restore(otg_dev->core_if, 1, 0);
  18705. +#endif
  18706. + return count;
  18707. +}
  18708. +
  18709. +DEVICE_ATTR(rem_wakeup_pwrdn, S_IRUGO | S_IWUSR, rem_wakeup_pwrdn_show,
  18710. + rem_wakeup_pwrdn_store);
  18711. +
  18712. +static ssize_t disconnect_us(struct device *_dev,
  18713. + struct device_attribute *attr,
  18714. + const char *buf, size_t count)
  18715. +{
  18716. +
  18717. +#ifndef DWC_HOST_ONLY
  18718. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18719. + uint32_t val = simple_strtoul(buf, NULL, 16);
  18720. + DWC_PRINTF("The Passed value is %04x\n", val);
  18721. +
  18722. + dwc_otg_pcd_disconnect_us(otg_dev->pcd, 50);
  18723. +
  18724. +#endif /* DWC_HOST_ONLY */
  18725. + return count;
  18726. +}
  18727. +
  18728. +DEVICE_ATTR(disconnect_us, S_IWUSR, 0, disconnect_us);
  18729. +
  18730. +/**
  18731. + * Dump global registers and either host or device registers (depending on the
  18732. + * current mode of the core).
  18733. + */
  18734. +static ssize_t regdump_show(struct device *_dev,
  18735. + struct device_attribute *attr, char *buf)
  18736. +{
  18737. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18738. +
  18739. + dwc_otg_dump_global_registers(otg_dev->core_if);
  18740. + if (dwc_otg_is_host_mode(otg_dev->core_if)) {
  18741. + dwc_otg_dump_host_registers(otg_dev->core_if);
  18742. + } else {
  18743. + dwc_otg_dump_dev_registers(otg_dev->core_if);
  18744. +
  18745. + }
  18746. + return sprintf(buf, "Register Dump\n");
  18747. +}
  18748. +
  18749. +DEVICE_ATTR(regdump, S_IRUGO, regdump_show, 0);
  18750. +
  18751. +/**
  18752. + * Dump global registers and either host or device registers (depending on the
  18753. + * current mode of the core).
  18754. + */
  18755. +static ssize_t spramdump_show(struct device *_dev,
  18756. + struct device_attribute *attr, char *buf)
  18757. +{
  18758. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18759. +
  18760. + //dwc_otg_dump_spram(otg_dev->core_if);
  18761. +
  18762. + return sprintf(buf, "SPRAM Dump\n");
  18763. +}
  18764. +
  18765. +DEVICE_ATTR(spramdump, S_IRUGO, spramdump_show, 0);
  18766. +
  18767. +/**
  18768. + * Dump the current hcd state.
  18769. + */
  18770. +static ssize_t hcddump_show(struct device *_dev,
  18771. + struct device_attribute *attr, char *buf)
  18772. +{
  18773. +#ifndef DWC_DEVICE_ONLY
  18774. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18775. + dwc_otg_hcd_dump_state(otg_dev->hcd);
  18776. +#endif /* DWC_DEVICE_ONLY */
  18777. + return sprintf(buf, "HCD Dump\n");
  18778. +}
  18779. +
  18780. +DEVICE_ATTR(hcddump, S_IRUGO, hcddump_show, 0);
  18781. +
  18782. +/**
  18783. + * Dump the average frame remaining at SOF. This can be used to
  18784. + * determine average interrupt latency. Frame remaining is also shown for
  18785. + * start transfer and two additional sample points.
  18786. + */
  18787. +static ssize_t hcd_frrem_show(struct device *_dev,
  18788. + struct device_attribute *attr, char *buf)
  18789. +{
  18790. +#ifndef DWC_DEVICE_ONLY
  18791. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18792. +
  18793. + dwc_otg_hcd_dump_frrem(otg_dev->hcd);
  18794. +#endif /* DWC_DEVICE_ONLY */
  18795. + return sprintf(buf, "HCD Dump Frame Remaining\n");
  18796. +}
  18797. +
  18798. +DEVICE_ATTR(hcd_frrem, S_IRUGO, hcd_frrem_show, 0);
  18799. +
  18800. +/**
  18801. + * Displays the time required to read the GNPTXFSIZ register many times (the
  18802. + * output shows the number of times the register is read).
  18803. + */
  18804. +#define RW_REG_COUNT 10000000
  18805. +#define MSEC_PER_JIFFIE 1000/HZ
  18806. +static ssize_t rd_reg_test_show(struct device *_dev,
  18807. + struct device_attribute *attr, char *buf)
  18808. +{
  18809. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18810. + int i;
  18811. + int time;
  18812. + int start_jiffies;
  18813. +
  18814. + printk("HZ %d, MSEC_PER_JIFFIE %d, loops_per_jiffy %lu\n",
  18815. + HZ, MSEC_PER_JIFFIE, loops_per_jiffy);
  18816. + start_jiffies = jiffies;
  18817. + for (i = 0; i < RW_REG_COUNT; i++) {
  18818. + dwc_otg_get_gnptxfsiz(otg_dev->core_if);
  18819. + }
  18820. + time = jiffies - start_jiffies;
  18821. + return sprintf(buf,
  18822. + "Time to read GNPTXFSIZ reg %d times: %d msecs (%d jiffies)\n",
  18823. + RW_REG_COUNT, time * MSEC_PER_JIFFIE, time);
  18824. +}
  18825. +
  18826. +DEVICE_ATTR(rd_reg_test, S_IRUGO, rd_reg_test_show, 0);
  18827. +
  18828. +/**
  18829. + * Displays the time required to write the GNPTXFSIZ register many times (the
  18830. + * output shows the number of times the register is written).
  18831. + */
  18832. +static ssize_t wr_reg_test_show(struct device *_dev,
  18833. + struct device_attribute *attr, char *buf)
  18834. +{
  18835. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18836. + uint32_t reg_val;
  18837. + int i;
  18838. + int time;
  18839. + int start_jiffies;
  18840. +
  18841. + printk("HZ %d, MSEC_PER_JIFFIE %d, loops_per_jiffy %lu\n",
  18842. + HZ, MSEC_PER_JIFFIE, loops_per_jiffy);
  18843. + reg_val = dwc_otg_get_gnptxfsiz(otg_dev->core_if);
  18844. + start_jiffies = jiffies;
  18845. + for (i = 0; i < RW_REG_COUNT; i++) {
  18846. + dwc_otg_set_gnptxfsiz(otg_dev->core_if, reg_val);
  18847. + }
  18848. + time = jiffies - start_jiffies;
  18849. + return sprintf(buf,
  18850. + "Time to write GNPTXFSIZ reg %d times: %d msecs (%d jiffies)\n",
  18851. + RW_REG_COUNT, time * MSEC_PER_JIFFIE, time);
  18852. +}
  18853. +
  18854. +DEVICE_ATTR(wr_reg_test, S_IRUGO, wr_reg_test_show, 0);
  18855. +
  18856. +#ifdef CONFIG_USB_DWC_OTG_LPM
  18857. +
  18858. +/**
  18859. +* Show the lpm_response attribute.
  18860. +*/
  18861. +static ssize_t lpmresp_show(struct device *_dev,
  18862. + struct device_attribute *attr, char *buf)
  18863. +{
  18864. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18865. +
  18866. + if (!dwc_otg_get_param_lpm_enable(otg_dev->core_if))
  18867. + return sprintf(buf, "** LPM is DISABLED **\n");
  18868. +
  18869. + if (!dwc_otg_is_device_mode(otg_dev->core_if)) {
  18870. + return sprintf(buf, "** Current mode is not device mode\n");
  18871. + }
  18872. + return sprintf(buf, "lpm_response = %d\n",
  18873. + dwc_otg_get_lpmresponse(otg_dev->core_if));
  18874. +}
  18875. +
  18876. +/**
  18877. +* Store the lpm_response attribute.
  18878. +*/
  18879. +static ssize_t lpmresp_store(struct device *_dev,
  18880. + struct device_attribute *attr,
  18881. + const char *buf, size_t count)
  18882. +{
  18883. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18884. + uint32_t val = simple_strtoul(buf, NULL, 16);
  18885. +
  18886. + if (!dwc_otg_get_param_lpm_enable(otg_dev->core_if)) {
  18887. + return 0;
  18888. + }
  18889. +
  18890. + if (!dwc_otg_is_device_mode(otg_dev->core_if)) {
  18891. + return 0;
  18892. + }
  18893. +
  18894. + dwc_otg_set_lpmresponse(otg_dev->core_if, val);
  18895. + return count;
  18896. +}
  18897. +
  18898. +DEVICE_ATTR(lpm_response, S_IRUGO | S_IWUSR, lpmresp_show, lpmresp_store);
  18899. +
  18900. +/**
  18901. +* Show the sleep_status attribute.
  18902. +*/
  18903. +static ssize_t sleepstatus_show(struct device *_dev,
  18904. + struct device_attribute *attr, char *buf)
  18905. +{
  18906. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18907. + return sprintf(buf, "Sleep Status = %d\n",
  18908. + dwc_otg_get_lpm_portsleepstatus(otg_dev->core_if));
  18909. +}
  18910. +
  18911. +/**
  18912. + * Store the sleep_status attribure.
  18913. + */
  18914. +static ssize_t sleepstatus_store(struct device *_dev,
  18915. + struct device_attribute *attr,
  18916. + const char *buf, size_t count)
  18917. +{
  18918. + dwc_otg_device_t *otg_dev = dwc_otg_drvdev(_dev);
  18919. + dwc_otg_core_if_t *core_if = otg_dev->core_if;
  18920. +
  18921. + if (dwc_otg_get_lpm_portsleepstatus(otg_dev->core_if)) {
  18922. + if (dwc_otg_is_host_mode(core_if)) {
  18923. +
  18924. + DWC_PRINTF("Host initiated resume\n");
  18925. + dwc_otg_set_prtresume(otg_dev->core_if, 1);
  18926. + }
  18927. + }
  18928. +
  18929. + return count;
  18930. +}
  18931. +
  18932. +DEVICE_ATTR(sleep_status, S_IRUGO | S_IWUSR, sleepstatus_show,
  18933. + sleepstatus_store);
  18934. +
  18935. +#endif /* CONFIG_USB_DWC_OTG_LPM_ENABLE */
  18936. +
  18937. +/**@}*/
  18938. +
  18939. +/**
  18940. + * Create the device files
  18941. + */
  18942. +void dwc_otg_attr_create(
  18943. +#ifdef LM_INTERFACE
  18944. + struct lm_device *dev
  18945. +#elif defined(PCI_INTERFACE)
  18946. + struct pci_dev *dev
  18947. +#elif defined(PLATFORM_INTERFACE)
  18948. + struct platform_device *dev
  18949. +#endif
  18950. + )
  18951. +{
  18952. + int error;
  18953. +
  18954. + error = device_create_file(&dev->dev, &dev_attr_regoffset);
  18955. + error = device_create_file(&dev->dev, &dev_attr_regvalue);
  18956. + error = device_create_file(&dev->dev, &dev_attr_mode);
  18957. + error = device_create_file(&dev->dev, &dev_attr_hnpcapable);
  18958. + error = device_create_file(&dev->dev, &dev_attr_srpcapable);
  18959. + error = device_create_file(&dev->dev, &dev_attr_hsic_connect);
  18960. + error = device_create_file(&dev->dev, &dev_attr_inv_sel_hsic);
  18961. + error = device_create_file(&dev->dev, &dev_attr_hnp);
  18962. + error = device_create_file(&dev->dev, &dev_attr_srp);
  18963. + error = device_create_file(&dev->dev, &dev_attr_buspower);
  18964. + error = device_create_file(&dev->dev, &dev_attr_bussuspend);
  18965. + error = device_create_file(&dev->dev, &dev_attr_mode_ch_tim_en);
  18966. + error = device_create_file(&dev->dev, &dev_attr_fr_interval);
  18967. + error = device_create_file(&dev->dev, &dev_attr_busconnected);
  18968. + error = device_create_file(&dev->dev, &dev_attr_gotgctl);
  18969. + error = device_create_file(&dev->dev, &dev_attr_gusbcfg);
  18970. + error = device_create_file(&dev->dev, &dev_attr_grxfsiz);
  18971. + error = device_create_file(&dev->dev, &dev_attr_gnptxfsiz);
  18972. + error = device_create_file(&dev->dev, &dev_attr_gpvndctl);
  18973. + error = device_create_file(&dev->dev, &dev_attr_ggpio);
  18974. + error = device_create_file(&dev->dev, &dev_attr_guid);
  18975. + error = device_create_file(&dev->dev, &dev_attr_gsnpsid);
  18976. + error = device_create_file(&dev->dev, &dev_attr_devspeed);
  18977. + error = device_create_file(&dev->dev, &dev_attr_enumspeed);
  18978. + error = device_create_file(&dev->dev, &dev_attr_hptxfsiz);
  18979. + error = device_create_file(&dev->dev, &dev_attr_hprt0);
  18980. + error = device_create_file(&dev->dev, &dev_attr_remote_wakeup);
  18981. + error = device_create_file(&dev->dev, &dev_attr_rem_wakeup_pwrdn);
  18982. + error = device_create_file(&dev->dev, &dev_attr_disconnect_us);
  18983. + error = device_create_file(&dev->dev, &dev_attr_regdump);
  18984. + error = device_create_file(&dev->dev, &dev_attr_spramdump);
  18985. + error = device_create_file(&dev->dev, &dev_attr_hcddump);
  18986. + error = device_create_file(&dev->dev, &dev_attr_hcd_frrem);
  18987. + error = device_create_file(&dev->dev, &dev_attr_rd_reg_test);
  18988. + error = device_create_file(&dev->dev, &dev_attr_wr_reg_test);
  18989. +#ifdef CONFIG_USB_DWC_OTG_LPM
  18990. + error = device_create_file(&dev->dev, &dev_attr_lpm_response);
  18991. + error = device_create_file(&dev->dev, &dev_attr_sleep_status);
  18992. +#endif
  18993. +}
  18994. +
  18995. +/**
  18996. + * Remove the device files
  18997. + */
  18998. +void dwc_otg_attr_remove(
  18999. +#ifdef LM_INTERFACE
  19000. + struct lm_device *dev
  19001. +#elif defined(PCI_INTERFACE)
  19002. + struct pci_dev *dev
  19003. +#elif defined(PLATFORM_INTERFACE)
  19004. + struct platform_device *dev
  19005. +#endif
  19006. + )
  19007. +{
  19008. + device_remove_file(&dev->dev, &dev_attr_regoffset);
  19009. + device_remove_file(&dev->dev, &dev_attr_regvalue);
  19010. + device_remove_file(&dev->dev, &dev_attr_mode);
  19011. + device_remove_file(&dev->dev, &dev_attr_hnpcapable);
  19012. + device_remove_file(&dev->dev, &dev_attr_srpcapable);
  19013. + device_remove_file(&dev->dev, &dev_attr_hsic_connect);
  19014. + device_remove_file(&dev->dev, &dev_attr_inv_sel_hsic);
  19015. + device_remove_file(&dev->dev, &dev_attr_hnp);
  19016. + device_remove_file(&dev->dev, &dev_attr_srp);
  19017. + device_remove_file(&dev->dev, &dev_attr_buspower);
  19018. + device_remove_file(&dev->dev, &dev_attr_bussuspend);
  19019. + device_remove_file(&dev->dev, &dev_attr_mode_ch_tim_en);
  19020. + device_remove_file(&dev->dev, &dev_attr_fr_interval);
  19021. + device_remove_file(&dev->dev, &dev_attr_busconnected);
  19022. + device_remove_file(&dev->dev, &dev_attr_gotgctl);
  19023. + device_remove_file(&dev->dev, &dev_attr_gusbcfg);
  19024. + device_remove_file(&dev->dev, &dev_attr_grxfsiz);
  19025. + device_remove_file(&dev->dev, &dev_attr_gnptxfsiz);
  19026. + device_remove_file(&dev->dev, &dev_attr_gpvndctl);
  19027. + device_remove_file(&dev->dev, &dev_attr_ggpio);
  19028. + device_remove_file(&dev->dev, &dev_attr_guid);
  19029. + device_remove_file(&dev->dev, &dev_attr_gsnpsid);
  19030. + device_remove_file(&dev->dev, &dev_attr_devspeed);
  19031. + device_remove_file(&dev->dev, &dev_attr_enumspeed);
  19032. + device_remove_file(&dev->dev, &dev_attr_hptxfsiz);
  19033. + device_remove_file(&dev->dev, &dev_attr_hprt0);
  19034. + device_remove_file(&dev->dev, &dev_attr_remote_wakeup);
  19035. + device_remove_file(&dev->dev, &dev_attr_rem_wakeup_pwrdn);
  19036. + device_remove_file(&dev->dev, &dev_attr_disconnect_us);
  19037. + device_remove_file(&dev->dev, &dev_attr_regdump);
  19038. + device_remove_file(&dev->dev, &dev_attr_spramdump);
  19039. + device_remove_file(&dev->dev, &dev_attr_hcddump);
  19040. + device_remove_file(&dev->dev, &dev_attr_hcd_frrem);
  19041. + device_remove_file(&dev->dev, &dev_attr_rd_reg_test);
  19042. + device_remove_file(&dev->dev, &dev_attr_wr_reg_test);
  19043. +#ifdef CONFIG_USB_DWC_OTG_LPM
  19044. + device_remove_file(&dev->dev, &dev_attr_lpm_response);
  19045. + device_remove_file(&dev->dev, &dev_attr_sleep_status);
  19046. +#endif
  19047. +}
  19048. --- /dev/null
  19049. +++ b/drivers/usb/host/dwc_otg/dwc_otg_attr.h
  19050. @@ -0,0 +1,89 @@
  19051. +/* ==========================================================================
  19052. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_attr.h $
  19053. + * $Revision: #13 $
  19054. + * $Date: 2010/06/21 $
  19055. + * $Change: 1532021 $
  19056. + *
  19057. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  19058. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  19059. + * otherwise expressly agreed to in writing between Synopsys and you.
  19060. + *
  19061. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  19062. + * any End User Software License Agreement or Agreement for Licensed Product
  19063. + * with Synopsys or any supplement thereto. You are permitted to use and
  19064. + * redistribute this Software in source and binary forms, with or without
  19065. + * modification, provided that redistributions of source code must retain this
  19066. + * notice. You may not view, use, disclose, copy or distribute this file or
  19067. + * any information contained herein except pursuant to this license grant from
  19068. + * Synopsys. If you do not agree with this notice, including the disclaimer
  19069. + * below, then you are not authorized to use the Software.
  19070. + *
  19071. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  19072. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19073. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19074. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  19075. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  19076. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19077. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  19078. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  19079. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  19080. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  19081. + * DAMAGE.
  19082. + * ========================================================================== */
  19083. +
  19084. +#if !defined(__DWC_OTG_ATTR_H__)
  19085. +#define __DWC_OTG_ATTR_H__
  19086. +
  19087. +/** @file
  19088. + * This file contains the interface to the Linux device attributes.
  19089. + */
  19090. +extern struct device_attribute dev_attr_regoffset;
  19091. +extern struct device_attribute dev_attr_regvalue;
  19092. +
  19093. +extern struct device_attribute dev_attr_mode;
  19094. +extern struct device_attribute dev_attr_hnpcapable;
  19095. +extern struct device_attribute dev_attr_srpcapable;
  19096. +extern struct device_attribute dev_attr_hnp;
  19097. +extern struct device_attribute dev_attr_srp;
  19098. +extern struct device_attribute dev_attr_buspower;
  19099. +extern struct device_attribute dev_attr_bussuspend;
  19100. +extern struct device_attribute dev_attr_mode_ch_tim_en;
  19101. +extern struct device_attribute dev_attr_fr_interval;
  19102. +extern struct device_attribute dev_attr_busconnected;
  19103. +extern struct device_attribute dev_attr_gotgctl;
  19104. +extern struct device_attribute dev_attr_gusbcfg;
  19105. +extern struct device_attribute dev_attr_grxfsiz;
  19106. +extern struct device_attribute dev_attr_gnptxfsiz;
  19107. +extern struct device_attribute dev_attr_gpvndctl;
  19108. +extern struct device_attribute dev_attr_ggpio;
  19109. +extern struct device_attribute dev_attr_guid;
  19110. +extern struct device_attribute dev_attr_gsnpsid;
  19111. +extern struct device_attribute dev_attr_devspeed;
  19112. +extern struct device_attribute dev_attr_enumspeed;
  19113. +extern struct device_attribute dev_attr_hptxfsiz;
  19114. +extern struct device_attribute dev_attr_hprt0;
  19115. +#ifdef CONFIG_USB_DWC_OTG_LPM
  19116. +extern struct device_attribute dev_attr_lpm_response;
  19117. +extern struct device_attribute devi_attr_sleep_status;
  19118. +#endif
  19119. +
  19120. +void dwc_otg_attr_create(
  19121. +#ifdef LM_INTERFACE
  19122. + struct lm_device *dev
  19123. +#elif defined(PCI_INTERFACE)
  19124. + struct pci_dev *dev
  19125. +#elif defined(PLATFORM_INTERFACE)
  19126. + struct platform_device *dev
  19127. +#endif
  19128. + );
  19129. +
  19130. +void dwc_otg_attr_remove(
  19131. +#ifdef LM_INTERFACE
  19132. + struct lm_device *dev
  19133. +#elif defined(PCI_INTERFACE)
  19134. + struct pci_dev *dev
  19135. +#elif defined(PLATFORM_INTERFACE)
  19136. + struct platform_device *dev
  19137. +#endif
  19138. + );
  19139. +#endif
  19140. --- /dev/null
  19141. +++ b/drivers/usb/host/dwc_otg/dwc_otg_cfi.c
  19142. @@ -0,0 +1,1876 @@
  19143. +/* ==========================================================================
  19144. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  19145. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  19146. + * otherwise expressly agreed to in writing between Synopsys and you.
  19147. + *
  19148. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  19149. + * any End User Software License Agreement or Agreement for Licensed Product
  19150. + * with Synopsys or any supplement thereto. You are permitted to use and
  19151. + * redistribute this Software in source and binary forms, with or without
  19152. + * modification, provided that redistributions of source code must retain this
  19153. + * notice. You may not view, use, disclose, copy or distribute this file or
  19154. + * any information contained herein except pursuant to this license grant from
  19155. + * Synopsys. If you do not agree with this notice, including the disclaimer
  19156. + * below, then you are not authorized to use the Software.
  19157. + *
  19158. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  19159. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19160. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19161. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  19162. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  19163. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19164. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  19165. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  19166. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  19167. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  19168. + * DAMAGE.
  19169. + * ========================================================================== */
  19170. +
  19171. +/** @file
  19172. + *
  19173. + * This file contains the most of the CFI(Core Feature Interface)
  19174. + * implementation for the OTG.
  19175. + */
  19176. +
  19177. +#ifdef DWC_UTE_CFI
  19178. +
  19179. +#include "dwc_otg_pcd.h"
  19180. +#include "dwc_otg_cfi.h"
  19181. +
  19182. +/** This definition should actually migrate to the Portability Library */
  19183. +#define DWC_CONSTANT_CPU_TO_LE16(x) (x)
  19184. +
  19185. +extern dwc_otg_pcd_ep_t *get_ep_by_addr(dwc_otg_pcd_t * pcd, u16 wIndex);
  19186. +
  19187. +static int cfi_core_features_buf(uint8_t * buf, uint16_t buflen);
  19188. +static int cfi_get_feature_value(uint8_t * buf, uint16_t buflen,
  19189. + struct dwc_otg_pcd *pcd,
  19190. + struct cfi_usb_ctrlrequest *ctrl_req);
  19191. +static int cfi_set_feature_value(struct dwc_otg_pcd *pcd);
  19192. +static int cfi_ep_get_sg_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
  19193. + struct cfi_usb_ctrlrequest *req);
  19194. +static int cfi_ep_get_concat_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
  19195. + struct cfi_usb_ctrlrequest *req);
  19196. +static int cfi_ep_get_align_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
  19197. + struct cfi_usb_ctrlrequest *req);
  19198. +static int cfi_preproc_reset(struct dwc_otg_pcd *pcd,
  19199. + struct cfi_usb_ctrlrequest *req);
  19200. +static void cfi_free_ep_bs_dyn_data(cfi_ep_t * cfiep);
  19201. +
  19202. +static uint16_t get_dfifo_size(dwc_otg_core_if_t * core_if);
  19203. +static int32_t get_rxfifo_size(dwc_otg_core_if_t * core_if, uint16_t wValue);
  19204. +static int32_t get_txfifo_size(struct dwc_otg_pcd *pcd, uint16_t wValue);
  19205. +
  19206. +static uint8_t resize_fifos(dwc_otg_core_if_t * core_if);
  19207. +
  19208. +/** This is the header of the all features descriptor */
  19209. +static cfi_all_features_header_t all_props_desc_header = {
  19210. + .wVersion = DWC_CONSTANT_CPU_TO_LE16(0x100),
  19211. + .wCoreID = DWC_CONSTANT_CPU_TO_LE16(CFI_CORE_ID_OTG),
  19212. + .wNumFeatures = DWC_CONSTANT_CPU_TO_LE16(9),
  19213. +};
  19214. +
  19215. +/** This is an array of statically allocated feature descriptors */
  19216. +static cfi_feature_desc_header_t prop_descs[] = {
  19217. +
  19218. + /* FT_ID_DMA_MODE */
  19219. + {
  19220. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DMA_MODE),
  19221. + .bmAttributes = CFI_FEATURE_ATTR_RW,
  19222. + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(1),
  19223. + },
  19224. +
  19225. + /* FT_ID_DMA_BUFFER_SETUP */
  19226. + {
  19227. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DMA_BUFFER_SETUP),
  19228. + .bmAttributes = CFI_FEATURE_ATTR_RW,
  19229. + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(6),
  19230. + },
  19231. +
  19232. + /* FT_ID_DMA_BUFF_ALIGN */
  19233. + {
  19234. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DMA_BUFF_ALIGN),
  19235. + .bmAttributes = CFI_FEATURE_ATTR_RW,
  19236. + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(2),
  19237. + },
  19238. +
  19239. + /* FT_ID_DMA_CONCAT_SETUP */
  19240. + {
  19241. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DMA_CONCAT_SETUP),
  19242. + .bmAttributes = CFI_FEATURE_ATTR_RW,
  19243. + //.wDataLength = DWC_CONSTANT_CPU_TO_LE16(6),
  19244. + },
  19245. +
  19246. + /* FT_ID_DMA_CIRCULAR */
  19247. + {
  19248. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DMA_CIRCULAR),
  19249. + .bmAttributes = CFI_FEATURE_ATTR_RW,
  19250. + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(6),
  19251. + },
  19252. +
  19253. + /* FT_ID_THRESHOLD_SETUP */
  19254. + {
  19255. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_THRESHOLD_SETUP),
  19256. + .bmAttributes = CFI_FEATURE_ATTR_RW,
  19257. + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(6),
  19258. + },
  19259. +
  19260. + /* FT_ID_DFIFO_DEPTH */
  19261. + {
  19262. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_DFIFO_DEPTH),
  19263. + .bmAttributes = CFI_FEATURE_ATTR_RO,
  19264. + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(2),
  19265. + },
  19266. +
  19267. + /* FT_ID_TX_FIFO_DEPTH */
  19268. + {
  19269. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_TX_FIFO_DEPTH),
  19270. + .bmAttributes = CFI_FEATURE_ATTR_RW,
  19271. + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(2),
  19272. + },
  19273. +
  19274. + /* FT_ID_RX_FIFO_DEPTH */
  19275. + {
  19276. + .wFeatureID = DWC_CONSTANT_CPU_TO_LE16(FT_ID_RX_FIFO_DEPTH),
  19277. + .bmAttributes = CFI_FEATURE_ATTR_RW,
  19278. + .wDataLength = DWC_CONSTANT_CPU_TO_LE16(2),
  19279. + }
  19280. +};
  19281. +
  19282. +/** The table of feature names */
  19283. +cfi_string_t prop_name_table[] = {
  19284. + {FT_ID_DMA_MODE, "dma_mode"},
  19285. + {FT_ID_DMA_BUFFER_SETUP, "buffer_setup"},
  19286. + {FT_ID_DMA_BUFF_ALIGN, "buffer_align"},
  19287. + {FT_ID_DMA_CONCAT_SETUP, "concat_setup"},
  19288. + {FT_ID_DMA_CIRCULAR, "buffer_circular"},
  19289. + {FT_ID_THRESHOLD_SETUP, "threshold_setup"},
  19290. + {FT_ID_DFIFO_DEPTH, "dfifo_depth"},
  19291. + {FT_ID_TX_FIFO_DEPTH, "txfifo_depth"},
  19292. + {FT_ID_RX_FIFO_DEPTH, "rxfifo_depth"},
  19293. + {}
  19294. +};
  19295. +
  19296. +/************************************************************************/
  19297. +
  19298. +/**
  19299. + * Returns the name of the feature by its ID
  19300. + * or NULL if no featute ID matches.
  19301. + *
  19302. + */
  19303. +const uint8_t *get_prop_name(uint16_t prop_id, int *len)
  19304. +{
  19305. + cfi_string_t *pstr;
  19306. + *len = 0;
  19307. +
  19308. + for (pstr = prop_name_table; pstr && pstr->s; pstr++) {
  19309. + if (pstr->id == prop_id) {
  19310. + *len = DWC_STRLEN(pstr->s);
  19311. + return pstr->s;
  19312. + }
  19313. + }
  19314. + return NULL;
  19315. +}
  19316. +
  19317. +/**
  19318. + * This function handles all CFI specific control requests.
  19319. + *
  19320. + * Return a negative value to stall the DCE.
  19321. + */
  19322. +int cfi_setup(struct dwc_otg_pcd *pcd, struct cfi_usb_ctrlrequest *ctrl)
  19323. +{
  19324. + int retval = 0;
  19325. + dwc_otg_pcd_ep_t *ep = NULL;
  19326. + cfiobject_t *cfi = pcd->cfi;
  19327. + struct dwc_otg_core_if *coreif = GET_CORE_IF(pcd);
  19328. + uint16_t wLen = DWC_LE16_TO_CPU(&ctrl->wLength);
  19329. + uint16_t wValue = DWC_LE16_TO_CPU(&ctrl->wValue);
  19330. + uint16_t wIndex = DWC_LE16_TO_CPU(&ctrl->wIndex);
  19331. + uint32_t regaddr = 0;
  19332. + uint32_t regval = 0;
  19333. +
  19334. + /* Save this Control Request in the CFI object.
  19335. + * The data field will be assigned in the data stage completion CB function.
  19336. + */
  19337. + cfi->ctrl_req = *ctrl;
  19338. + cfi->ctrl_req.data = NULL;
  19339. +
  19340. + cfi->need_gadget_att = 0;
  19341. + cfi->need_status_in_complete = 0;
  19342. +
  19343. + switch (ctrl->bRequest) {
  19344. + case VEN_CORE_GET_FEATURES:
  19345. + retval = cfi_core_features_buf(cfi->buf_in.buf, CFI_IN_BUF_LEN);
  19346. + if (retval >= 0) {
  19347. + //dump_msg(cfi->buf_in.buf, retval);
  19348. + ep = &pcd->ep0;
  19349. +
  19350. + retval = min((uint16_t) retval, wLen);
  19351. + /* Transfer this buffer to the host through the EP0-IN EP */
  19352. + ep->dwc_ep.dma_addr = cfi->buf_in.addr;
  19353. + ep->dwc_ep.start_xfer_buff = cfi->buf_in.buf;
  19354. + ep->dwc_ep.xfer_buff = cfi->buf_in.buf;
  19355. + ep->dwc_ep.xfer_len = retval;
  19356. + ep->dwc_ep.xfer_count = 0;
  19357. + ep->dwc_ep.sent_zlp = 0;
  19358. + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
  19359. +
  19360. + pcd->ep0_pending = 1;
  19361. + dwc_otg_ep0_start_transfer(coreif, &ep->dwc_ep);
  19362. + }
  19363. + retval = 0;
  19364. + break;
  19365. +
  19366. + case VEN_CORE_GET_FEATURE:
  19367. + CFI_INFO("VEN_CORE_GET_FEATURE\n");
  19368. + retval = cfi_get_feature_value(cfi->buf_in.buf, CFI_IN_BUF_LEN,
  19369. + pcd, ctrl);
  19370. + if (retval >= 0) {
  19371. + ep = &pcd->ep0;
  19372. +
  19373. + retval = min((uint16_t) retval, wLen);
  19374. + /* Transfer this buffer to the host through the EP0-IN EP */
  19375. + ep->dwc_ep.dma_addr = cfi->buf_in.addr;
  19376. + ep->dwc_ep.start_xfer_buff = cfi->buf_in.buf;
  19377. + ep->dwc_ep.xfer_buff = cfi->buf_in.buf;
  19378. + ep->dwc_ep.xfer_len = retval;
  19379. + ep->dwc_ep.xfer_count = 0;
  19380. + ep->dwc_ep.sent_zlp = 0;
  19381. + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
  19382. +
  19383. + pcd->ep0_pending = 1;
  19384. + dwc_otg_ep0_start_transfer(coreif, &ep->dwc_ep);
  19385. + }
  19386. + CFI_INFO("VEN_CORE_GET_FEATURE=%d\n", retval);
  19387. + dump_msg(cfi->buf_in.buf, retval);
  19388. + break;
  19389. +
  19390. + case VEN_CORE_SET_FEATURE:
  19391. + CFI_INFO("VEN_CORE_SET_FEATURE\n");
  19392. + /* Set up an XFER to get the data stage of the control request,
  19393. + * which is the new value of the feature to be modified.
  19394. + */
  19395. + ep = &pcd->ep0;
  19396. + ep->dwc_ep.is_in = 0;
  19397. + ep->dwc_ep.dma_addr = cfi->buf_out.addr;
  19398. + ep->dwc_ep.start_xfer_buff = cfi->buf_out.buf;
  19399. + ep->dwc_ep.xfer_buff = cfi->buf_out.buf;
  19400. + ep->dwc_ep.xfer_len = wLen;
  19401. + ep->dwc_ep.xfer_count = 0;
  19402. + ep->dwc_ep.sent_zlp = 0;
  19403. + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
  19404. +
  19405. + pcd->ep0_pending = 1;
  19406. + /* Read the control write's data stage */
  19407. + dwc_otg_ep0_start_transfer(coreif, &ep->dwc_ep);
  19408. + retval = 0;
  19409. + break;
  19410. +
  19411. + case VEN_CORE_RESET_FEATURES:
  19412. + CFI_INFO("VEN_CORE_RESET_FEATURES\n");
  19413. + cfi->need_gadget_att = 1;
  19414. + cfi->need_status_in_complete = 1;
  19415. + retval = cfi_preproc_reset(pcd, ctrl);
  19416. + CFI_INFO("VEN_CORE_RESET_FEATURES = (%d)\n", retval);
  19417. + break;
  19418. +
  19419. + case VEN_CORE_ACTIVATE_FEATURES:
  19420. + CFI_INFO("VEN_CORE_ACTIVATE_FEATURES\n");
  19421. + break;
  19422. +
  19423. + case VEN_CORE_READ_REGISTER:
  19424. + CFI_INFO("VEN_CORE_READ_REGISTER\n");
  19425. + /* wValue optionally contains the HI WORD of the register offset and
  19426. + * wIndex contains the LOW WORD of the register offset
  19427. + */
  19428. + if (wValue == 0) {
  19429. + /* @TODO - MAS - fix the access to the base field */
  19430. + regaddr = 0;
  19431. + //regaddr = (uint32_t) pcd->otg_dev->os_dep.base;
  19432. + //GET_CORE_IF(pcd)->co
  19433. + regaddr |= wIndex;
  19434. + } else {
  19435. + regaddr = (wValue << 16) | wIndex;
  19436. + }
  19437. +
  19438. + /* Read a 32-bit value of the memory at the regaddr */
  19439. + regval = DWC_READ_REG32((uint32_t *) regaddr);
  19440. +
  19441. + ep = &pcd->ep0;
  19442. + dwc_memcpy(cfi->buf_in.buf, &regval, sizeof(uint32_t));
  19443. + ep->dwc_ep.is_in = 1;
  19444. + ep->dwc_ep.dma_addr = cfi->buf_in.addr;
  19445. + ep->dwc_ep.start_xfer_buff = cfi->buf_in.buf;
  19446. + ep->dwc_ep.xfer_buff = cfi->buf_in.buf;
  19447. + ep->dwc_ep.xfer_len = wLen;
  19448. + ep->dwc_ep.xfer_count = 0;
  19449. + ep->dwc_ep.sent_zlp = 0;
  19450. + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
  19451. +
  19452. + pcd->ep0_pending = 1;
  19453. + dwc_otg_ep0_start_transfer(coreif, &ep->dwc_ep);
  19454. + cfi->need_gadget_att = 0;
  19455. + retval = 0;
  19456. + break;
  19457. +
  19458. + case VEN_CORE_WRITE_REGISTER:
  19459. + CFI_INFO("VEN_CORE_WRITE_REGISTER\n");
  19460. + /* Set up an XFER to get the data stage of the control request,
  19461. + * which is the new value of the register to be modified.
  19462. + */
  19463. + ep = &pcd->ep0;
  19464. + ep->dwc_ep.is_in = 0;
  19465. + ep->dwc_ep.dma_addr = cfi->buf_out.addr;
  19466. + ep->dwc_ep.start_xfer_buff = cfi->buf_out.buf;
  19467. + ep->dwc_ep.xfer_buff = cfi->buf_out.buf;
  19468. + ep->dwc_ep.xfer_len = wLen;
  19469. + ep->dwc_ep.xfer_count = 0;
  19470. + ep->dwc_ep.sent_zlp = 0;
  19471. + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
  19472. +
  19473. + pcd->ep0_pending = 1;
  19474. + /* Read the control write's data stage */
  19475. + dwc_otg_ep0_start_transfer(coreif, &ep->dwc_ep);
  19476. + retval = 0;
  19477. + break;
  19478. +
  19479. + default:
  19480. + retval = -DWC_E_NOT_SUPPORTED;
  19481. + break;
  19482. + }
  19483. +
  19484. + return retval;
  19485. +}
  19486. +
  19487. +/**
  19488. + * This function prepares the core features descriptors and copies its
  19489. + * raw representation into the buffer <buf>.
  19490. + *
  19491. + * The buffer structure is as follows:
  19492. + * all_features_header (8 bytes)
  19493. + * features_#1 (8 bytes + feature name string length)
  19494. + * features_#2 (8 bytes + feature name string length)
  19495. + * .....
  19496. + * features_#n - where n=the total count of feature descriptors
  19497. + */
  19498. +static int cfi_core_features_buf(uint8_t * buf, uint16_t buflen)
  19499. +{
  19500. + cfi_feature_desc_header_t *prop_hdr = prop_descs;
  19501. + cfi_feature_desc_header_t *prop;
  19502. + cfi_all_features_header_t *all_props_hdr = &all_props_desc_header;
  19503. + cfi_all_features_header_t *tmp;
  19504. + uint8_t *tmpbuf = buf;
  19505. + const uint8_t *pname = NULL;
  19506. + int i, j, namelen = 0, totlen;
  19507. +
  19508. + /* Prepare and copy the core features into the buffer */
  19509. + CFI_INFO("%s:\n", __func__);
  19510. +
  19511. + tmp = (cfi_all_features_header_t *) tmpbuf;
  19512. + *tmp = *all_props_hdr;
  19513. + tmpbuf += CFI_ALL_FEATURES_HDR_LEN;
  19514. +
  19515. + j = sizeof(prop_descs) / sizeof(cfi_all_features_header_t);
  19516. + for (i = 0; i < j; i++, prop_hdr++) {
  19517. + pname = get_prop_name(prop_hdr->wFeatureID, &namelen);
  19518. + prop = (cfi_feature_desc_header_t *) tmpbuf;
  19519. + *prop = *prop_hdr;
  19520. +
  19521. + prop->bNameLen = namelen;
  19522. + prop->wLength =
  19523. + DWC_CONSTANT_CPU_TO_LE16(CFI_FEATURE_DESC_HDR_LEN +
  19524. + namelen);
  19525. +
  19526. + tmpbuf += CFI_FEATURE_DESC_HDR_LEN;
  19527. + dwc_memcpy(tmpbuf, pname, namelen);
  19528. + tmpbuf += namelen;
  19529. + }
  19530. +
  19531. + totlen = tmpbuf - buf;
  19532. +
  19533. + if (totlen > 0) {
  19534. + tmp = (cfi_all_features_header_t *) buf;
  19535. + tmp->wTotalLen = DWC_CONSTANT_CPU_TO_LE16(totlen);
  19536. + }
  19537. +
  19538. + return totlen;
  19539. +}
  19540. +
  19541. +/**
  19542. + * This function releases all the dynamic memory in the CFI object.
  19543. + */
  19544. +static void cfi_release(cfiobject_t * cfiobj)
  19545. +{
  19546. + cfi_ep_t *cfiep;
  19547. + dwc_list_link_t *tmp;
  19548. +
  19549. + CFI_INFO("%s\n", __func__);
  19550. +
  19551. + if (cfiobj->buf_in.buf) {
  19552. + DWC_DMA_FREE(CFI_IN_BUF_LEN, cfiobj->buf_in.buf,
  19553. + cfiobj->buf_in.addr);
  19554. + cfiobj->buf_in.buf = NULL;
  19555. + }
  19556. +
  19557. + if (cfiobj->buf_out.buf) {
  19558. + DWC_DMA_FREE(CFI_OUT_BUF_LEN, cfiobj->buf_out.buf,
  19559. + cfiobj->buf_out.addr);
  19560. + cfiobj->buf_out.buf = NULL;
  19561. + }
  19562. +
  19563. + /* Free the Buffer Setup values for each EP */
  19564. + //list_for_each_entry(cfiep, &cfiobj->active_eps, lh) {
  19565. + DWC_LIST_FOREACH(tmp, &cfiobj->active_eps) {
  19566. + cfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
  19567. + cfi_free_ep_bs_dyn_data(cfiep);
  19568. + }
  19569. +}
  19570. +
  19571. +/**
  19572. + * This function frees the dynamically allocated EP buffer setup data.
  19573. + */
  19574. +static void cfi_free_ep_bs_dyn_data(cfi_ep_t * cfiep)
  19575. +{
  19576. + if (cfiep->bm_sg) {
  19577. + DWC_FREE(cfiep->bm_sg);
  19578. + cfiep->bm_sg = NULL;
  19579. + }
  19580. +
  19581. + if (cfiep->bm_align) {
  19582. + DWC_FREE(cfiep->bm_align);
  19583. + cfiep->bm_align = NULL;
  19584. + }
  19585. +
  19586. + if (cfiep->bm_concat) {
  19587. + if (NULL != cfiep->bm_concat->wTxBytes) {
  19588. + DWC_FREE(cfiep->bm_concat->wTxBytes);
  19589. + cfiep->bm_concat->wTxBytes = NULL;
  19590. + }
  19591. + DWC_FREE(cfiep->bm_concat);
  19592. + cfiep->bm_concat = NULL;
  19593. + }
  19594. +}
  19595. +
  19596. +/**
  19597. + * This function initializes the default values of the features
  19598. + * for a specific endpoint and should be called only once when
  19599. + * the EP is enabled first time.
  19600. + */
  19601. +static int cfi_ep_init_defaults(struct dwc_otg_pcd *pcd, cfi_ep_t * cfiep)
  19602. +{
  19603. + int retval = 0;
  19604. +
  19605. + cfiep->bm_sg = DWC_ALLOC(sizeof(ddma_sg_buffer_setup_t));
  19606. + if (NULL == cfiep->bm_sg) {
  19607. + CFI_INFO("Failed to allocate memory for SG feature value\n");
  19608. + return -DWC_E_NO_MEMORY;
  19609. + }
  19610. + dwc_memset(cfiep->bm_sg, 0, sizeof(ddma_sg_buffer_setup_t));
  19611. +
  19612. + /* For the Concatenation feature's default value we do not allocate
  19613. + * memory for the wTxBytes field - it will be done in the set_feature_value
  19614. + * request handler.
  19615. + */
  19616. + cfiep->bm_concat = DWC_ALLOC(sizeof(ddma_concat_buffer_setup_t));
  19617. + if (NULL == cfiep->bm_concat) {
  19618. + CFI_INFO
  19619. + ("Failed to allocate memory for CONCATENATION feature value\n");
  19620. + DWC_FREE(cfiep->bm_sg);
  19621. + return -DWC_E_NO_MEMORY;
  19622. + }
  19623. + dwc_memset(cfiep->bm_concat, 0, sizeof(ddma_concat_buffer_setup_t));
  19624. +
  19625. + cfiep->bm_align = DWC_ALLOC(sizeof(ddma_align_buffer_setup_t));
  19626. + if (NULL == cfiep->bm_align) {
  19627. + CFI_INFO
  19628. + ("Failed to allocate memory for Alignment feature value\n");
  19629. + DWC_FREE(cfiep->bm_sg);
  19630. + DWC_FREE(cfiep->bm_concat);
  19631. + return -DWC_E_NO_MEMORY;
  19632. + }
  19633. + dwc_memset(cfiep->bm_align, 0, sizeof(ddma_align_buffer_setup_t));
  19634. +
  19635. + return retval;
  19636. +}
  19637. +
  19638. +/**
  19639. + * The callback function that notifies the CFI on the activation of
  19640. + * an endpoint in the PCD. The following steps are done in this function:
  19641. + *
  19642. + * Create a dynamically allocated cfi_ep_t object (a CFI wrapper to the PCD's
  19643. + * active endpoint)
  19644. + * Create MAX_DMA_DESCS_PER_EP count DMA Descriptors for the EP
  19645. + * Set the Buffer Mode to standard
  19646. + * Initialize the default values for all EP modes (SG, Circular, Concat, Align)
  19647. + * Add the cfi_ep_t object to the list of active endpoints in the CFI object
  19648. + */
  19649. +static int cfi_ep_enable(struct cfiobject *cfi, struct dwc_otg_pcd *pcd,
  19650. + struct dwc_otg_pcd_ep *ep)
  19651. +{
  19652. + cfi_ep_t *cfiep;
  19653. + int retval = -DWC_E_NOT_SUPPORTED;
  19654. +
  19655. + CFI_INFO("%s: epname=%s; epnum=0x%02x\n", __func__,
  19656. + "EP_" /*ep->ep.name */ , ep->desc->bEndpointAddress);
  19657. + /* MAS - Check whether this endpoint already is in the list */
  19658. + cfiep = get_cfi_ep_by_pcd_ep(cfi, ep);
  19659. +
  19660. + if (NULL == cfiep) {
  19661. + /* Allocate a cfi_ep_t object */
  19662. + cfiep = DWC_ALLOC(sizeof(cfi_ep_t));
  19663. + if (NULL == cfiep) {
  19664. + CFI_INFO
  19665. + ("Unable to allocate memory for <cfiep> in function %s\n",
  19666. + __func__);
  19667. + return -DWC_E_NO_MEMORY;
  19668. + }
  19669. + dwc_memset(cfiep, 0, sizeof(cfi_ep_t));
  19670. +
  19671. + /* Save the dwc_otg_pcd_ep pointer in the cfiep object */
  19672. + cfiep->ep = ep;
  19673. +
  19674. + /* Allocate the DMA Descriptors chain of MAX_DMA_DESCS_PER_EP count */
  19675. + ep->dwc_ep.descs =
  19676. + DWC_DMA_ALLOC(MAX_DMA_DESCS_PER_EP *
  19677. + sizeof(dwc_otg_dma_desc_t),
  19678. + &ep->dwc_ep.descs_dma_addr);
  19679. +
  19680. + if (NULL == ep->dwc_ep.descs) {
  19681. + DWC_FREE(cfiep);
  19682. + return -DWC_E_NO_MEMORY;
  19683. + }
  19684. +
  19685. + DWC_LIST_INIT(&cfiep->lh);
  19686. +
  19687. + /* Set the buffer mode to BM_STANDARD. It will be modified
  19688. + * when building descriptors for a specific buffer mode */
  19689. + ep->dwc_ep.buff_mode = BM_STANDARD;
  19690. +
  19691. + /* Create and initialize the default values for this EP's Buffer modes */
  19692. + if ((retval = cfi_ep_init_defaults(pcd, cfiep)) < 0)
  19693. + return retval;
  19694. +
  19695. + /* Add the cfi_ep_t object to the CFI object's list of active endpoints */
  19696. + DWC_LIST_INSERT_TAIL(&cfi->active_eps, &cfiep->lh);
  19697. + retval = 0;
  19698. + } else { /* The sought EP already is in the list */
  19699. + CFI_INFO("%s: The sought EP already is in the list\n",
  19700. + __func__);
  19701. + }
  19702. +
  19703. + return retval;
  19704. +}
  19705. +
  19706. +/**
  19707. + * This function is called when the data stage of a 3-stage Control Write request
  19708. + * is complete.
  19709. + *
  19710. + */
  19711. +static int cfi_ctrl_write_complete(struct cfiobject *cfi,
  19712. + struct dwc_otg_pcd *pcd)
  19713. +{
  19714. + uint32_t addr, reg_value;
  19715. + uint16_t wIndex, wValue;
  19716. + uint8_t bRequest;
  19717. + uint8_t *buf = cfi->buf_out.buf;
  19718. + //struct usb_ctrlrequest *ctrl_req = &cfi->ctrl_req_saved;
  19719. + struct cfi_usb_ctrlrequest *ctrl_req = &cfi->ctrl_req;
  19720. + int retval = -DWC_E_NOT_SUPPORTED;
  19721. +
  19722. + CFI_INFO("%s\n", __func__);
  19723. +
  19724. + bRequest = ctrl_req->bRequest;
  19725. + wIndex = DWC_CONSTANT_CPU_TO_LE16(ctrl_req->wIndex);
  19726. + wValue = DWC_CONSTANT_CPU_TO_LE16(ctrl_req->wValue);
  19727. +
  19728. + /*
  19729. + * Save the pointer to the data stage in the ctrl_req's <data> field.
  19730. + * The request should be already saved in the command stage by now.
  19731. + */
  19732. + ctrl_req->data = cfi->buf_out.buf;
  19733. + cfi->need_status_in_complete = 0;
  19734. + cfi->need_gadget_att = 0;
  19735. +
  19736. + switch (bRequest) {
  19737. + case VEN_CORE_WRITE_REGISTER:
  19738. + /* The buffer contains raw data of the new value for the register */
  19739. + reg_value = *((uint32_t *) buf);
  19740. + if (wValue == 0) {
  19741. + addr = 0;
  19742. + //addr = (uint32_t) pcd->otg_dev->os_dep.base;
  19743. + addr += wIndex;
  19744. + } else {
  19745. + addr = (wValue << 16) | wIndex;
  19746. + }
  19747. +
  19748. + //writel(reg_value, addr);
  19749. +
  19750. + retval = 0;
  19751. + cfi->need_status_in_complete = 1;
  19752. + break;
  19753. +
  19754. + case VEN_CORE_SET_FEATURE:
  19755. + /* The buffer contains raw data of the new value of the feature */
  19756. + retval = cfi_set_feature_value(pcd);
  19757. + if (retval < 0)
  19758. + return retval;
  19759. +
  19760. + cfi->need_status_in_complete = 1;
  19761. + break;
  19762. +
  19763. + default:
  19764. + break;
  19765. + }
  19766. +
  19767. + return retval;
  19768. +}
  19769. +
  19770. +/**
  19771. + * This function builds the DMA descriptors for the SG buffer mode.
  19772. + */
  19773. +static void cfi_build_sg_descs(struct cfiobject *cfi, cfi_ep_t * cfiep,
  19774. + dwc_otg_pcd_request_t * req)
  19775. +{
  19776. + struct dwc_otg_pcd_ep *ep = cfiep->ep;
  19777. + ddma_sg_buffer_setup_t *sgval = cfiep->bm_sg;
  19778. + struct dwc_otg_dma_desc *desc = cfiep->ep->dwc_ep.descs;
  19779. + struct dwc_otg_dma_desc *desc_last = cfiep->ep->dwc_ep.descs;
  19780. + dma_addr_t buff_addr = req->dma;
  19781. + int i;
  19782. + uint32_t txsize, off;
  19783. +
  19784. + txsize = sgval->wSize;
  19785. + off = sgval->bOffset;
  19786. +
  19787. +// CFI_INFO("%s: %s TXSIZE=0x%08x; OFFSET=0x%08x\n",
  19788. +// __func__, cfiep->ep->ep.name, txsize, off);
  19789. +
  19790. + for (i = 0; i < sgval->bCount; i++) {
  19791. + desc->status.b.bs = BS_HOST_BUSY;
  19792. + desc->buf = buff_addr;
  19793. + desc->status.b.l = 0;
  19794. + desc->status.b.ioc = 0;
  19795. + desc->status.b.sp = 0;
  19796. + desc->status.b.bytes = txsize;
  19797. + desc->status.b.bs = BS_HOST_READY;
  19798. +
  19799. + /* Set the next address of the buffer */
  19800. + buff_addr += txsize + off;
  19801. + desc_last = desc;
  19802. + desc++;
  19803. + }
  19804. +
  19805. + /* Set the last, ioc and sp bits on the Last DMA Descriptor */
  19806. + desc_last->status.b.l = 1;
  19807. + desc_last->status.b.ioc = 1;
  19808. + desc_last->status.b.sp = ep->dwc_ep.sent_zlp;
  19809. + /* Save the last DMA descriptor pointer */
  19810. + cfiep->dma_desc_last = desc_last;
  19811. + cfiep->desc_count = sgval->bCount;
  19812. +}
  19813. +
  19814. +/**
  19815. + * This function builds the DMA descriptors for the Concatenation buffer mode.
  19816. + */
  19817. +static void cfi_build_concat_descs(struct cfiobject *cfi, cfi_ep_t * cfiep,
  19818. + dwc_otg_pcd_request_t * req)
  19819. +{
  19820. + struct dwc_otg_pcd_ep *ep = cfiep->ep;
  19821. + ddma_concat_buffer_setup_t *concatval = cfiep->bm_concat;
  19822. + struct dwc_otg_dma_desc *desc = cfiep->ep->dwc_ep.descs;
  19823. + struct dwc_otg_dma_desc *desc_last = cfiep->ep->dwc_ep.descs;
  19824. + dma_addr_t buff_addr = req->dma;
  19825. + int i;
  19826. + uint16_t *txsize;
  19827. +
  19828. + txsize = concatval->wTxBytes;
  19829. +
  19830. + for (i = 0; i < concatval->hdr.bDescCount; i++) {
  19831. + desc->buf = buff_addr;
  19832. + desc->status.b.bs = BS_HOST_BUSY;
  19833. + desc->status.b.l = 0;
  19834. + desc->status.b.ioc = 0;
  19835. + desc->status.b.sp = 0;
  19836. + desc->status.b.bytes = *txsize;
  19837. + desc->status.b.bs = BS_HOST_READY;
  19838. +
  19839. + txsize++;
  19840. + /* Set the next address of the buffer */
  19841. + buff_addr += UGETW(ep->desc->wMaxPacketSize);
  19842. + desc_last = desc;
  19843. + desc++;
  19844. + }
  19845. +
  19846. + /* Set the last, ioc and sp bits on the Last DMA Descriptor */
  19847. + desc_last->status.b.l = 1;
  19848. + desc_last->status.b.ioc = 1;
  19849. + desc_last->status.b.sp = ep->dwc_ep.sent_zlp;
  19850. + cfiep->dma_desc_last = desc_last;
  19851. + cfiep->desc_count = concatval->hdr.bDescCount;
  19852. +}
  19853. +
  19854. +/**
  19855. + * This function builds the DMA descriptors for the Circular buffer mode
  19856. + */
  19857. +static void cfi_build_circ_descs(struct cfiobject *cfi, cfi_ep_t * cfiep,
  19858. + dwc_otg_pcd_request_t * req)
  19859. +{
  19860. + /* @todo: MAS - add implementation when this feature needs to be tested */
  19861. +}
  19862. +
  19863. +/**
  19864. + * This function builds the DMA descriptors for the Alignment buffer mode
  19865. + */
  19866. +static void cfi_build_align_descs(struct cfiobject *cfi, cfi_ep_t * cfiep,
  19867. + dwc_otg_pcd_request_t * req)
  19868. +{
  19869. + struct dwc_otg_pcd_ep *ep = cfiep->ep;
  19870. + ddma_align_buffer_setup_t *alignval = cfiep->bm_align;
  19871. + struct dwc_otg_dma_desc *desc = cfiep->ep->dwc_ep.descs;
  19872. + dma_addr_t buff_addr = req->dma;
  19873. +
  19874. + desc->status.b.bs = BS_HOST_BUSY;
  19875. + desc->status.b.l = 1;
  19876. + desc->status.b.ioc = 1;
  19877. + desc->status.b.sp = ep->dwc_ep.sent_zlp;
  19878. + desc->status.b.bytes = req->length;
  19879. + /* Adjust the buffer alignment */
  19880. + desc->buf = (buff_addr + alignval->bAlign);
  19881. + desc->status.b.bs = BS_HOST_READY;
  19882. + cfiep->dma_desc_last = desc;
  19883. + cfiep->desc_count = 1;
  19884. +}
  19885. +
  19886. +/**
  19887. + * This function builds the DMA descriptors chain for different modes of the
  19888. + * buffer setup of an endpoint.
  19889. + */
  19890. +static void cfi_build_descriptors(struct cfiobject *cfi,
  19891. + struct dwc_otg_pcd *pcd,
  19892. + struct dwc_otg_pcd_ep *ep,
  19893. + dwc_otg_pcd_request_t * req)
  19894. +{
  19895. + cfi_ep_t *cfiep;
  19896. +
  19897. + /* Get the cfiep by the dwc_otg_pcd_ep */
  19898. + cfiep = get_cfi_ep_by_pcd_ep(cfi, ep);
  19899. + if (NULL == cfiep) {
  19900. + CFI_INFO("%s: Unable to find a matching active endpoint\n",
  19901. + __func__);
  19902. + return;
  19903. + }
  19904. +
  19905. + cfiep->xfer_len = req->length;
  19906. +
  19907. + /* Iterate through all the DMA descriptors */
  19908. + switch (cfiep->ep->dwc_ep.buff_mode) {
  19909. + case BM_SG:
  19910. + cfi_build_sg_descs(cfi, cfiep, req);
  19911. + break;
  19912. +
  19913. + case BM_CONCAT:
  19914. + cfi_build_concat_descs(cfi, cfiep, req);
  19915. + break;
  19916. +
  19917. + case BM_CIRCULAR:
  19918. + cfi_build_circ_descs(cfi, cfiep, req);
  19919. + break;
  19920. +
  19921. + case BM_ALIGN:
  19922. + cfi_build_align_descs(cfi, cfiep, req);
  19923. + break;
  19924. +
  19925. + default:
  19926. + break;
  19927. + }
  19928. +}
  19929. +
  19930. +/**
  19931. + * Allocate DMA buffer for different Buffer modes.
  19932. + */
  19933. +static void *cfi_ep_alloc_buf(struct cfiobject *cfi, struct dwc_otg_pcd *pcd,
  19934. + struct dwc_otg_pcd_ep *ep, dma_addr_t * dma,
  19935. + unsigned size, gfp_t flags)
  19936. +{
  19937. + return DWC_DMA_ALLOC(size, dma);
  19938. +}
  19939. +
  19940. +/**
  19941. + * This function initializes the CFI object.
  19942. + */
  19943. +int init_cfi(cfiobject_t * cfiobj)
  19944. +{
  19945. + CFI_INFO("%s\n", __func__);
  19946. +
  19947. + /* Allocate a buffer for IN XFERs */
  19948. + cfiobj->buf_in.buf =
  19949. + DWC_DMA_ALLOC(CFI_IN_BUF_LEN, &cfiobj->buf_in.addr);
  19950. + if (NULL == cfiobj->buf_in.buf) {
  19951. + CFI_INFO("Unable to allocate buffer for INs\n");
  19952. + return -DWC_E_NO_MEMORY;
  19953. + }
  19954. +
  19955. + /* Allocate a buffer for OUT XFERs */
  19956. + cfiobj->buf_out.buf =
  19957. + DWC_DMA_ALLOC(CFI_OUT_BUF_LEN, &cfiobj->buf_out.addr);
  19958. + if (NULL == cfiobj->buf_out.buf) {
  19959. + CFI_INFO("Unable to allocate buffer for OUT\n");
  19960. + return -DWC_E_NO_MEMORY;
  19961. + }
  19962. +
  19963. + /* Initialize the callback function pointers */
  19964. + cfiobj->ops.release = cfi_release;
  19965. + cfiobj->ops.ep_enable = cfi_ep_enable;
  19966. + cfiobj->ops.ctrl_write_complete = cfi_ctrl_write_complete;
  19967. + cfiobj->ops.build_descriptors = cfi_build_descriptors;
  19968. + cfiobj->ops.ep_alloc_buf = cfi_ep_alloc_buf;
  19969. +
  19970. + /* Initialize the list of active endpoints in the CFI object */
  19971. + DWC_LIST_INIT(&cfiobj->active_eps);
  19972. +
  19973. + return 0;
  19974. +}
  19975. +
  19976. +/**
  19977. + * This function reads the required feature's current value into the buffer
  19978. + *
  19979. + * @retval: Returns negative as error, or the data length of the feature
  19980. + */
  19981. +static int cfi_get_feature_value(uint8_t * buf, uint16_t buflen,
  19982. + struct dwc_otg_pcd *pcd,
  19983. + struct cfi_usb_ctrlrequest *ctrl_req)
  19984. +{
  19985. + int retval = -DWC_E_NOT_SUPPORTED;
  19986. + struct dwc_otg_core_if *coreif = GET_CORE_IF(pcd);
  19987. + uint16_t dfifo, rxfifo, txfifo;
  19988. +
  19989. + switch (ctrl_req->wIndex) {
  19990. + /* Whether the DDMA is enabled or not */
  19991. + case FT_ID_DMA_MODE:
  19992. + *buf = (coreif->dma_enable && coreif->dma_desc_enable) ? 1 : 0;
  19993. + retval = 1;
  19994. + break;
  19995. +
  19996. + case FT_ID_DMA_BUFFER_SETUP:
  19997. + retval = cfi_ep_get_sg_val(buf, pcd, ctrl_req);
  19998. + break;
  19999. +
  20000. + case FT_ID_DMA_BUFF_ALIGN:
  20001. + retval = cfi_ep_get_align_val(buf, pcd, ctrl_req);
  20002. + break;
  20003. +
  20004. + case FT_ID_DMA_CONCAT_SETUP:
  20005. + retval = cfi_ep_get_concat_val(buf, pcd, ctrl_req);
  20006. + break;
  20007. +
  20008. + case FT_ID_DMA_CIRCULAR:
  20009. + CFI_INFO("GetFeature value (FT_ID_DMA_CIRCULAR)\n");
  20010. + break;
  20011. +
  20012. + case FT_ID_THRESHOLD_SETUP:
  20013. + CFI_INFO("GetFeature value (FT_ID_THRESHOLD_SETUP)\n");
  20014. + break;
  20015. +
  20016. + case FT_ID_DFIFO_DEPTH:
  20017. + dfifo = get_dfifo_size(coreif);
  20018. + *((uint16_t *) buf) = dfifo;
  20019. + retval = sizeof(uint16_t);
  20020. + break;
  20021. +
  20022. + case FT_ID_TX_FIFO_DEPTH:
  20023. + retval = get_txfifo_size(pcd, ctrl_req->wValue);
  20024. + if (retval >= 0) {
  20025. + txfifo = retval;
  20026. + *((uint16_t *) buf) = txfifo;
  20027. + retval = sizeof(uint16_t);
  20028. + }
  20029. + break;
  20030. +
  20031. + case FT_ID_RX_FIFO_DEPTH:
  20032. + retval = get_rxfifo_size(coreif, ctrl_req->wValue);
  20033. + if (retval >= 0) {
  20034. + rxfifo = retval;
  20035. + *((uint16_t *) buf) = rxfifo;
  20036. + retval = sizeof(uint16_t);
  20037. + }
  20038. + break;
  20039. + }
  20040. +
  20041. + return retval;
  20042. +}
  20043. +
  20044. +/**
  20045. + * This function resets the SG for the specified EP to its default value
  20046. + */
  20047. +static int cfi_reset_sg_val(cfi_ep_t * cfiep)
  20048. +{
  20049. + dwc_memset(cfiep->bm_sg, 0, sizeof(ddma_sg_buffer_setup_t));
  20050. + return 0;
  20051. +}
  20052. +
  20053. +/**
  20054. + * This function resets the Alignment for the specified EP to its default value
  20055. + */
  20056. +static int cfi_reset_align_val(cfi_ep_t * cfiep)
  20057. +{
  20058. + dwc_memset(cfiep->bm_sg, 0, sizeof(ddma_sg_buffer_setup_t));
  20059. + return 0;
  20060. +}
  20061. +
  20062. +/**
  20063. + * This function resets the Concatenation for the specified EP to its default value
  20064. + * This function will also set the value of the wTxBytes field to NULL after
  20065. + * freeing the memory previously allocated for this field.
  20066. + */
  20067. +static int cfi_reset_concat_val(cfi_ep_t * cfiep)
  20068. +{
  20069. + /* First we need to free the wTxBytes field */
  20070. + if (cfiep->bm_concat->wTxBytes) {
  20071. + DWC_FREE(cfiep->bm_concat->wTxBytes);
  20072. + cfiep->bm_concat->wTxBytes = NULL;
  20073. + }
  20074. +
  20075. + dwc_memset(cfiep->bm_concat, 0, sizeof(ddma_concat_buffer_setup_t));
  20076. + return 0;
  20077. +}
  20078. +
  20079. +/**
  20080. + * This function resets all the buffer setups of the specified endpoint
  20081. + */
  20082. +static int cfi_ep_reset_all_setup_vals(cfi_ep_t * cfiep)
  20083. +{
  20084. + cfi_reset_sg_val(cfiep);
  20085. + cfi_reset_align_val(cfiep);
  20086. + cfi_reset_concat_val(cfiep);
  20087. + return 0;
  20088. +}
  20089. +
  20090. +static int cfi_handle_reset_fifo_val(struct dwc_otg_pcd *pcd, uint8_t ep_addr,
  20091. + uint8_t rx_rst, uint8_t tx_rst)
  20092. +{
  20093. + int retval = -DWC_E_INVALID;
  20094. + uint16_t tx_siz[15];
  20095. + uint16_t rx_siz = 0;
  20096. + dwc_otg_pcd_ep_t *ep = NULL;
  20097. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  20098. + dwc_otg_core_params_t *params = GET_CORE_IF(pcd)->core_params;
  20099. +
  20100. + if (rx_rst) {
  20101. + rx_siz = params->dev_rx_fifo_size;
  20102. + params->dev_rx_fifo_size = GET_CORE_IF(pcd)->init_rxfsiz;
  20103. + }
  20104. +
  20105. + if (tx_rst) {
  20106. + if (ep_addr == 0) {
  20107. + int i;
  20108. +
  20109. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  20110. + tx_siz[i] =
  20111. + core_if->core_params->dev_tx_fifo_size[i];
  20112. + core_if->core_params->dev_tx_fifo_size[i] =
  20113. + core_if->init_txfsiz[i];
  20114. + }
  20115. + } else {
  20116. +
  20117. + ep = get_ep_by_addr(pcd, ep_addr);
  20118. +
  20119. + if (NULL == ep) {
  20120. + CFI_INFO
  20121. + ("%s: Unable to get the endpoint addr=0x%02x\n",
  20122. + __func__, ep_addr);
  20123. + return -DWC_E_INVALID;
  20124. + }
  20125. +
  20126. + tx_siz[0] =
  20127. + params->dev_tx_fifo_size[ep->dwc_ep.tx_fifo_num -
  20128. + 1];
  20129. + params->dev_tx_fifo_size[ep->dwc_ep.tx_fifo_num - 1] =
  20130. + GET_CORE_IF(pcd)->init_txfsiz[ep->
  20131. + dwc_ep.tx_fifo_num -
  20132. + 1];
  20133. + }
  20134. + }
  20135. +
  20136. + if (resize_fifos(GET_CORE_IF(pcd))) {
  20137. + retval = 0;
  20138. + } else {
  20139. + CFI_INFO
  20140. + ("%s: Error resetting the feature Reset All(FIFO size)\n",
  20141. + __func__);
  20142. + if (rx_rst) {
  20143. + params->dev_rx_fifo_size = rx_siz;
  20144. + }
  20145. +
  20146. + if (tx_rst) {
  20147. + if (ep_addr == 0) {
  20148. + int i;
  20149. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps;
  20150. + i++) {
  20151. + core_if->
  20152. + core_params->dev_tx_fifo_size[i] =
  20153. + tx_siz[i];
  20154. + }
  20155. + } else {
  20156. + params->dev_tx_fifo_size[ep->
  20157. + dwc_ep.tx_fifo_num -
  20158. + 1] = tx_siz[0];
  20159. + }
  20160. + }
  20161. + retval = -DWC_E_INVALID;
  20162. + }
  20163. + return retval;
  20164. +}
  20165. +
  20166. +static int cfi_handle_reset_all(struct dwc_otg_pcd *pcd, uint8_t addr)
  20167. +{
  20168. + int retval = 0;
  20169. + cfi_ep_t *cfiep;
  20170. + cfiobject_t *cfi = pcd->cfi;
  20171. + dwc_list_link_t *tmp;
  20172. +
  20173. + retval = cfi_handle_reset_fifo_val(pcd, addr, 1, 1);
  20174. + if (retval < 0) {
  20175. + return retval;
  20176. + }
  20177. +
  20178. + /* If the EP address is known then reset the features for only that EP */
  20179. + if (addr) {
  20180. + cfiep = get_cfi_ep_by_addr(pcd->cfi, addr);
  20181. + if (NULL == cfiep) {
  20182. + CFI_INFO("%s: Error getting the EP address 0x%02x\n",
  20183. + __func__, addr);
  20184. + return -DWC_E_INVALID;
  20185. + }
  20186. + retval = cfi_ep_reset_all_setup_vals(cfiep);
  20187. + cfiep->ep->dwc_ep.buff_mode = BM_STANDARD;
  20188. + }
  20189. + /* Otherwise (wValue == 0), reset all features of all EP's */
  20190. + else {
  20191. + /* Traverse all the active EP's and reset the feature(s) value(s) */
  20192. + //list_for_each_entry(cfiep, &cfi->active_eps, lh) {
  20193. + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
  20194. + cfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
  20195. + retval = cfi_ep_reset_all_setup_vals(cfiep);
  20196. + cfiep->ep->dwc_ep.buff_mode = BM_STANDARD;
  20197. + if (retval < 0) {
  20198. + CFI_INFO
  20199. + ("%s: Error resetting the feature Reset All\n",
  20200. + __func__);
  20201. + return retval;
  20202. + }
  20203. + }
  20204. + }
  20205. + return retval;
  20206. +}
  20207. +
  20208. +static int cfi_handle_reset_dma_buff_setup(struct dwc_otg_pcd *pcd,
  20209. + uint8_t addr)
  20210. +{
  20211. + int retval = 0;
  20212. + cfi_ep_t *cfiep;
  20213. + cfiobject_t *cfi = pcd->cfi;
  20214. + dwc_list_link_t *tmp;
  20215. +
  20216. + /* If the EP address is known then reset the features for only that EP */
  20217. + if (addr) {
  20218. + cfiep = get_cfi_ep_by_addr(pcd->cfi, addr);
  20219. + if (NULL == cfiep) {
  20220. + CFI_INFO("%s: Error getting the EP address 0x%02x\n",
  20221. + __func__, addr);
  20222. + return -DWC_E_INVALID;
  20223. + }
  20224. + retval = cfi_reset_sg_val(cfiep);
  20225. + }
  20226. + /* Otherwise (wValue == 0), reset all features of all EP's */
  20227. + else {
  20228. + /* Traverse all the active EP's and reset the feature(s) value(s) */
  20229. + //list_for_each_entry(cfiep, &cfi->active_eps, lh) {
  20230. + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
  20231. + cfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
  20232. + retval = cfi_reset_sg_val(cfiep);
  20233. + if (retval < 0) {
  20234. + CFI_INFO
  20235. + ("%s: Error resetting the feature Buffer Setup\n",
  20236. + __func__);
  20237. + return retval;
  20238. + }
  20239. + }
  20240. + }
  20241. + return retval;
  20242. +}
  20243. +
  20244. +static int cfi_handle_reset_concat_val(struct dwc_otg_pcd *pcd, uint8_t addr)
  20245. +{
  20246. + int retval = 0;
  20247. + cfi_ep_t *cfiep;
  20248. + cfiobject_t *cfi = pcd->cfi;
  20249. + dwc_list_link_t *tmp;
  20250. +
  20251. + /* If the EP address is known then reset the features for only that EP */
  20252. + if (addr) {
  20253. + cfiep = get_cfi_ep_by_addr(pcd->cfi, addr);
  20254. + if (NULL == cfiep) {
  20255. + CFI_INFO("%s: Error getting the EP address 0x%02x\n",
  20256. + __func__, addr);
  20257. + return -DWC_E_INVALID;
  20258. + }
  20259. + retval = cfi_reset_concat_val(cfiep);
  20260. + }
  20261. + /* Otherwise (wValue == 0), reset all features of all EP's */
  20262. + else {
  20263. + /* Traverse all the active EP's and reset the feature(s) value(s) */
  20264. + //list_for_each_entry(cfiep, &cfi->active_eps, lh) {
  20265. + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
  20266. + cfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
  20267. + retval = cfi_reset_concat_val(cfiep);
  20268. + if (retval < 0) {
  20269. + CFI_INFO
  20270. + ("%s: Error resetting the feature Concatenation Value\n",
  20271. + __func__);
  20272. + return retval;
  20273. + }
  20274. + }
  20275. + }
  20276. + return retval;
  20277. +}
  20278. +
  20279. +static int cfi_handle_reset_align_val(struct dwc_otg_pcd *pcd, uint8_t addr)
  20280. +{
  20281. + int retval = 0;
  20282. + cfi_ep_t *cfiep;
  20283. + cfiobject_t *cfi = pcd->cfi;
  20284. + dwc_list_link_t *tmp;
  20285. +
  20286. + /* If the EP address is known then reset the features for only that EP */
  20287. + if (addr) {
  20288. + cfiep = get_cfi_ep_by_addr(pcd->cfi, addr);
  20289. + if (NULL == cfiep) {
  20290. + CFI_INFO("%s: Error getting the EP address 0x%02x\n",
  20291. + __func__, addr);
  20292. + return -DWC_E_INVALID;
  20293. + }
  20294. + retval = cfi_reset_align_val(cfiep);
  20295. + }
  20296. + /* Otherwise (wValue == 0), reset all features of all EP's */
  20297. + else {
  20298. + /* Traverse all the active EP's and reset the feature(s) value(s) */
  20299. + //list_for_each_entry(cfiep, &cfi->active_eps, lh) {
  20300. + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
  20301. + cfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
  20302. + retval = cfi_reset_align_val(cfiep);
  20303. + if (retval < 0) {
  20304. + CFI_INFO
  20305. + ("%s: Error resetting the feature Aliignment Value\n",
  20306. + __func__);
  20307. + return retval;
  20308. + }
  20309. + }
  20310. + }
  20311. + return retval;
  20312. +
  20313. +}
  20314. +
  20315. +static int cfi_preproc_reset(struct dwc_otg_pcd *pcd,
  20316. + struct cfi_usb_ctrlrequest *req)
  20317. +{
  20318. + int retval = 0;
  20319. +
  20320. + switch (req->wIndex) {
  20321. + case 0:
  20322. + /* Reset all features */
  20323. + retval = cfi_handle_reset_all(pcd, req->wValue & 0xff);
  20324. + break;
  20325. +
  20326. + case FT_ID_DMA_BUFFER_SETUP:
  20327. + /* Reset the SG buffer setup */
  20328. + retval =
  20329. + cfi_handle_reset_dma_buff_setup(pcd, req->wValue & 0xff);
  20330. + break;
  20331. +
  20332. + case FT_ID_DMA_CONCAT_SETUP:
  20333. + /* Reset the Concatenation buffer setup */
  20334. + retval = cfi_handle_reset_concat_val(pcd, req->wValue & 0xff);
  20335. + break;
  20336. +
  20337. + case FT_ID_DMA_BUFF_ALIGN:
  20338. + /* Reset the Alignment buffer setup */
  20339. + retval = cfi_handle_reset_align_val(pcd, req->wValue & 0xff);
  20340. + break;
  20341. +
  20342. + case FT_ID_TX_FIFO_DEPTH:
  20343. + retval =
  20344. + cfi_handle_reset_fifo_val(pcd, req->wValue & 0xff, 0, 1);
  20345. + pcd->cfi->need_gadget_att = 0;
  20346. + break;
  20347. +
  20348. + case FT_ID_RX_FIFO_DEPTH:
  20349. + retval = cfi_handle_reset_fifo_val(pcd, 0, 1, 0);
  20350. + pcd->cfi->need_gadget_att = 0;
  20351. + break;
  20352. + default:
  20353. + break;
  20354. + }
  20355. + return retval;
  20356. +}
  20357. +
  20358. +/**
  20359. + * This function sets a new value for the SG buffer setup.
  20360. + */
  20361. +static int cfi_ep_set_sg_val(uint8_t * buf, struct dwc_otg_pcd *pcd)
  20362. +{
  20363. + uint8_t inaddr, outaddr;
  20364. + cfi_ep_t *epin, *epout;
  20365. + ddma_sg_buffer_setup_t *psgval;
  20366. + uint32_t desccount, size;
  20367. +
  20368. + CFI_INFO("%s\n", __func__);
  20369. +
  20370. + psgval = (ddma_sg_buffer_setup_t *) buf;
  20371. + desccount = (uint32_t) psgval->bCount;
  20372. + size = (uint32_t) psgval->wSize;
  20373. +
  20374. + /* Check the DMA descriptor count */
  20375. + if ((desccount > MAX_DMA_DESCS_PER_EP) || (desccount == 0)) {
  20376. + CFI_INFO
  20377. + ("%s: The count of DMA Descriptors should be between 1 and %d\n",
  20378. + __func__, MAX_DMA_DESCS_PER_EP);
  20379. + return -DWC_E_INVALID;
  20380. + }
  20381. +
  20382. + /* Check the DMA descriptor count */
  20383. +
  20384. + if (size == 0) {
  20385. +
  20386. + CFI_INFO("%s: The transfer size should be at least 1 byte\n",
  20387. + __func__);
  20388. +
  20389. + return -DWC_E_INVALID;
  20390. +
  20391. + }
  20392. +
  20393. + inaddr = psgval->bInEndpointAddress;
  20394. + outaddr = psgval->bOutEndpointAddress;
  20395. +
  20396. + epin = get_cfi_ep_by_addr(pcd->cfi, inaddr);
  20397. + epout = get_cfi_ep_by_addr(pcd->cfi, outaddr);
  20398. +
  20399. + if (NULL == epin || NULL == epout) {
  20400. + CFI_INFO
  20401. + ("%s: Unable to get the endpoints inaddr=0x%02x outaddr=0x%02x\n",
  20402. + __func__, inaddr, outaddr);
  20403. + return -DWC_E_INVALID;
  20404. + }
  20405. +
  20406. + epin->ep->dwc_ep.buff_mode = BM_SG;
  20407. + dwc_memcpy(epin->bm_sg, psgval, sizeof(ddma_sg_buffer_setup_t));
  20408. +
  20409. + epout->ep->dwc_ep.buff_mode = BM_SG;
  20410. + dwc_memcpy(epout->bm_sg, psgval, sizeof(ddma_sg_buffer_setup_t));
  20411. +
  20412. + return 0;
  20413. +}
  20414. +
  20415. +/**
  20416. + * This function sets a new value for the buffer Alignment setup.
  20417. + */
  20418. +static int cfi_ep_set_alignment_val(uint8_t * buf, struct dwc_otg_pcd *pcd)
  20419. +{
  20420. + cfi_ep_t *ep;
  20421. + uint8_t addr;
  20422. + ddma_align_buffer_setup_t *palignval;
  20423. +
  20424. + palignval = (ddma_align_buffer_setup_t *) buf;
  20425. + addr = palignval->bEndpointAddress;
  20426. +
  20427. + ep = get_cfi_ep_by_addr(pcd->cfi, addr);
  20428. +
  20429. + if (NULL == ep) {
  20430. + CFI_INFO("%s: Unable to get the endpoint addr=0x%02x\n",
  20431. + __func__, addr);
  20432. + return -DWC_E_INVALID;
  20433. + }
  20434. +
  20435. + ep->ep->dwc_ep.buff_mode = BM_ALIGN;
  20436. + dwc_memcpy(ep->bm_align, palignval, sizeof(ddma_align_buffer_setup_t));
  20437. +
  20438. + return 0;
  20439. +}
  20440. +
  20441. +/**
  20442. + * This function sets a new value for the Concatenation buffer setup.
  20443. + */
  20444. +static int cfi_ep_set_concat_val(uint8_t * buf, struct dwc_otg_pcd *pcd)
  20445. +{
  20446. + uint8_t addr;
  20447. + cfi_ep_t *ep;
  20448. + struct _ddma_concat_buffer_setup_hdr *pConcatValHdr;
  20449. + uint16_t *pVals;
  20450. + uint32_t desccount;
  20451. + int i;
  20452. + uint16_t mps;
  20453. +
  20454. + pConcatValHdr = (struct _ddma_concat_buffer_setup_hdr *)buf;
  20455. + desccount = (uint32_t) pConcatValHdr->bDescCount;
  20456. + pVals = (uint16_t *) (buf + BS_CONCAT_VAL_HDR_LEN);
  20457. +
  20458. + /* Check the DMA descriptor count */
  20459. + if (desccount > MAX_DMA_DESCS_PER_EP) {
  20460. + CFI_INFO("%s: Maximum DMA Descriptor count should be %d\n",
  20461. + __func__, MAX_DMA_DESCS_PER_EP);
  20462. + return -DWC_E_INVALID;
  20463. + }
  20464. +
  20465. + addr = pConcatValHdr->bEndpointAddress;
  20466. + ep = get_cfi_ep_by_addr(pcd->cfi, addr);
  20467. + if (NULL == ep) {
  20468. + CFI_INFO("%s: Unable to get the endpoint addr=0x%02x\n",
  20469. + __func__, addr);
  20470. + return -DWC_E_INVALID;
  20471. + }
  20472. +
  20473. + mps = UGETW(ep->ep->desc->wMaxPacketSize);
  20474. +
  20475. +#if 0
  20476. + for (i = 0; i < desccount; i++) {
  20477. + CFI_INFO("%s: wTxSize[%d]=0x%04x\n", __func__, i, pVals[i]);
  20478. + }
  20479. + CFI_INFO("%s: epname=%s; mps=%d\n", __func__, ep->ep->ep.name, mps);
  20480. +#endif
  20481. +
  20482. + /* Check the wTxSizes to be less than or equal to the mps */
  20483. + for (i = 0; i < desccount; i++) {
  20484. + if (pVals[i] > mps) {
  20485. + CFI_INFO
  20486. + ("%s: ERROR - the wTxSize[%d] should be <= MPS (wTxSize=%d)\n",
  20487. + __func__, i, pVals[i]);
  20488. + return -DWC_E_INVALID;
  20489. + }
  20490. + }
  20491. +
  20492. + ep->ep->dwc_ep.buff_mode = BM_CONCAT;
  20493. + dwc_memcpy(ep->bm_concat, pConcatValHdr, BS_CONCAT_VAL_HDR_LEN);
  20494. +
  20495. + /* Free the previously allocated storage for the wTxBytes */
  20496. + if (ep->bm_concat->wTxBytes) {
  20497. + DWC_FREE(ep->bm_concat->wTxBytes);
  20498. + }
  20499. +
  20500. + /* Allocate a new storage for the wTxBytes field */
  20501. + ep->bm_concat->wTxBytes =
  20502. + DWC_ALLOC(sizeof(uint16_t) * pConcatValHdr->bDescCount);
  20503. + if (NULL == ep->bm_concat->wTxBytes) {
  20504. + CFI_INFO("%s: Unable to allocate memory\n", __func__);
  20505. + return -DWC_E_NO_MEMORY;
  20506. + }
  20507. +
  20508. + /* Copy the new values into the wTxBytes filed */
  20509. + dwc_memcpy(ep->bm_concat->wTxBytes, buf + BS_CONCAT_VAL_HDR_LEN,
  20510. + sizeof(uint16_t) * pConcatValHdr->bDescCount);
  20511. +
  20512. + return 0;
  20513. +}
  20514. +
  20515. +/**
  20516. + * This function calculates the total of all FIFO sizes
  20517. + *
  20518. + * @param core_if Programming view of DWC_otg controller
  20519. + *
  20520. + * @return The total of data FIFO sizes.
  20521. + *
  20522. + */
  20523. +static uint16_t get_dfifo_size(dwc_otg_core_if_t * core_if)
  20524. +{
  20525. + dwc_otg_core_params_t *params = core_if->core_params;
  20526. + uint16_t dfifo_total = 0;
  20527. + int i;
  20528. +
  20529. + /* The shared RxFIFO size */
  20530. + dfifo_total =
  20531. + params->dev_rx_fifo_size + params->dev_nperio_tx_fifo_size;
  20532. +
  20533. + /* Add up each TxFIFO size to the total */
  20534. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  20535. + dfifo_total += params->dev_tx_fifo_size[i];
  20536. + }
  20537. +
  20538. + return dfifo_total;
  20539. +}
  20540. +
  20541. +/**
  20542. + * This function returns Rx FIFO size
  20543. + *
  20544. + * @param core_if Programming view of DWC_otg controller
  20545. + *
  20546. + * @return The total of data FIFO sizes.
  20547. + *
  20548. + */
  20549. +static int32_t get_rxfifo_size(dwc_otg_core_if_t * core_if, uint16_t wValue)
  20550. +{
  20551. + switch (wValue >> 8) {
  20552. + case 0:
  20553. + return (core_if->pwron_rxfsiz <
  20554. + 32768) ? core_if->pwron_rxfsiz : 32768;
  20555. + break;
  20556. + case 1:
  20557. + return core_if->core_params->dev_rx_fifo_size;
  20558. + break;
  20559. + default:
  20560. + return -DWC_E_INVALID;
  20561. + break;
  20562. + }
  20563. +}
  20564. +
  20565. +/**
  20566. + * This function returns Tx FIFO size for IN EP
  20567. + *
  20568. + * @param core_if Programming view of DWC_otg controller
  20569. + *
  20570. + * @return The total of data FIFO sizes.
  20571. + *
  20572. + */
  20573. +static int32_t get_txfifo_size(struct dwc_otg_pcd *pcd, uint16_t wValue)
  20574. +{
  20575. + dwc_otg_pcd_ep_t *ep;
  20576. +
  20577. + ep = get_ep_by_addr(pcd, wValue & 0xff);
  20578. +
  20579. + if (NULL == ep) {
  20580. + CFI_INFO("%s: Unable to get the endpoint addr=0x%02x\n",
  20581. + __func__, wValue & 0xff);
  20582. + return -DWC_E_INVALID;
  20583. + }
  20584. +
  20585. + if (!ep->dwc_ep.is_in) {
  20586. + CFI_INFO
  20587. + ("%s: No Tx FIFO assingned to the Out endpoint addr=0x%02x\n",
  20588. + __func__, wValue & 0xff);
  20589. + return -DWC_E_INVALID;
  20590. + }
  20591. +
  20592. + switch (wValue >> 8) {
  20593. + case 0:
  20594. + return (GET_CORE_IF(pcd)->pwron_txfsiz
  20595. + [ep->dwc_ep.tx_fifo_num - 1] <
  20596. + 768) ? GET_CORE_IF(pcd)->pwron_txfsiz[ep->
  20597. + dwc_ep.tx_fifo_num
  20598. + - 1] : 32768;
  20599. + break;
  20600. + case 1:
  20601. + return GET_CORE_IF(pcd)->core_params->
  20602. + dev_tx_fifo_size[ep->dwc_ep.num - 1];
  20603. + break;
  20604. + default:
  20605. + return -DWC_E_INVALID;
  20606. + break;
  20607. + }
  20608. +}
  20609. +
  20610. +/**
  20611. + * This function checks if the submitted combination of
  20612. + * device mode FIFO sizes is possible or not.
  20613. + *
  20614. + * @param core_if Programming view of DWC_otg controller
  20615. + *
  20616. + * @return 1 if possible, 0 otherwise.
  20617. + *
  20618. + */
  20619. +static uint8_t check_fifo_sizes(dwc_otg_core_if_t * core_if)
  20620. +{
  20621. + uint16_t dfifo_actual = 0;
  20622. + dwc_otg_core_params_t *params = core_if->core_params;
  20623. + uint16_t start_addr = 0;
  20624. + int i;
  20625. +
  20626. + dfifo_actual =
  20627. + params->dev_rx_fifo_size + params->dev_nperio_tx_fifo_size;
  20628. +
  20629. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  20630. + dfifo_actual += params->dev_tx_fifo_size[i];
  20631. + }
  20632. +
  20633. + if (dfifo_actual > core_if->total_fifo_size) {
  20634. + return 0;
  20635. + }
  20636. +
  20637. + if (params->dev_rx_fifo_size > 32768 || params->dev_rx_fifo_size < 16)
  20638. + return 0;
  20639. +
  20640. + if (params->dev_nperio_tx_fifo_size > 32768
  20641. + || params->dev_nperio_tx_fifo_size < 16)
  20642. + return 0;
  20643. +
  20644. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  20645. +
  20646. + if (params->dev_tx_fifo_size[i] > 768
  20647. + || params->dev_tx_fifo_size[i] < 4)
  20648. + return 0;
  20649. + }
  20650. +
  20651. + if (params->dev_rx_fifo_size > core_if->pwron_rxfsiz)
  20652. + return 0;
  20653. + start_addr = params->dev_rx_fifo_size;
  20654. +
  20655. + if (params->dev_nperio_tx_fifo_size > core_if->pwron_gnptxfsiz)
  20656. + return 0;
  20657. + start_addr += params->dev_nperio_tx_fifo_size;
  20658. +
  20659. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  20660. +
  20661. + if (params->dev_tx_fifo_size[i] > core_if->pwron_txfsiz[i])
  20662. + return 0;
  20663. + start_addr += params->dev_tx_fifo_size[i];
  20664. + }
  20665. +
  20666. + return 1;
  20667. +}
  20668. +
  20669. +/**
  20670. + * This function resizes Device mode FIFOs
  20671. + *
  20672. + * @param core_if Programming view of DWC_otg controller
  20673. + *
  20674. + * @return 1 if successful, 0 otherwise
  20675. + *
  20676. + */
  20677. +static uint8_t resize_fifos(dwc_otg_core_if_t * core_if)
  20678. +{
  20679. + int i = 0;
  20680. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  20681. + dwc_otg_core_params_t *params = core_if->core_params;
  20682. + uint32_t rx_fifo_size;
  20683. + fifosize_data_t nptxfifosize;
  20684. + fifosize_data_t txfifosize[15];
  20685. +
  20686. + uint32_t rx_fsz_bak;
  20687. + uint32_t nptxfsz_bak;
  20688. + uint32_t txfsz_bak[15];
  20689. +
  20690. + uint16_t start_address;
  20691. + uint8_t retval = 1;
  20692. +
  20693. + if (!check_fifo_sizes(core_if)) {
  20694. + return 0;
  20695. + }
  20696. +
  20697. + /* Configure data FIFO sizes */
  20698. + if (core_if->hwcfg2.b.dynamic_fifo && params->enable_dynamic_fifo) {
  20699. + rx_fsz_bak = DWC_READ_REG32(&global_regs->grxfsiz);
  20700. + rx_fifo_size = params->dev_rx_fifo_size;
  20701. + DWC_WRITE_REG32(&global_regs->grxfsiz, rx_fifo_size);
  20702. +
  20703. + /*
  20704. + * Tx FIFOs These FIFOs are numbered from 1 to 15.
  20705. + * Indexes of the FIFO size module parameters in the
  20706. + * dev_tx_fifo_size array and the FIFO size registers in
  20707. + * the dtxfsiz array run from 0 to 14.
  20708. + */
  20709. +
  20710. + /* Non-periodic Tx FIFO */
  20711. + nptxfsz_bak = DWC_READ_REG32(&global_regs->gnptxfsiz);
  20712. + nptxfifosize.b.depth = params->dev_nperio_tx_fifo_size;
  20713. + start_address = params->dev_rx_fifo_size;
  20714. + nptxfifosize.b.startaddr = start_address;
  20715. +
  20716. + DWC_WRITE_REG32(&global_regs->gnptxfsiz, nptxfifosize.d32);
  20717. +
  20718. + start_address += nptxfifosize.b.depth;
  20719. +
  20720. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  20721. + txfsz_bak[i] = DWC_READ_REG32(&global_regs->dtxfsiz[i]);
  20722. +
  20723. + txfifosize[i].b.depth = params->dev_tx_fifo_size[i];
  20724. + txfifosize[i].b.startaddr = start_address;
  20725. + DWC_WRITE_REG32(&global_regs->dtxfsiz[i],
  20726. + txfifosize[i].d32);
  20727. +
  20728. + start_address += txfifosize[i].b.depth;
  20729. + }
  20730. +
  20731. + /** Check if register values are set correctly */
  20732. + if (rx_fifo_size != DWC_READ_REG32(&global_regs->grxfsiz)) {
  20733. + retval = 0;
  20734. + }
  20735. +
  20736. + if (nptxfifosize.d32 != DWC_READ_REG32(&global_regs->gnptxfsiz)) {
  20737. + retval = 0;
  20738. + }
  20739. +
  20740. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  20741. + if (txfifosize[i].d32 !=
  20742. + DWC_READ_REG32(&global_regs->dtxfsiz[i])) {
  20743. + retval = 0;
  20744. + }
  20745. + }
  20746. +
  20747. + /** If register values are not set correctly, reset old values */
  20748. + if (retval == 0) {
  20749. + DWC_WRITE_REG32(&global_regs->grxfsiz, rx_fsz_bak);
  20750. +
  20751. + /* Non-periodic Tx FIFO */
  20752. + DWC_WRITE_REG32(&global_regs->gnptxfsiz, nptxfsz_bak);
  20753. +
  20754. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  20755. + DWC_WRITE_REG32(&global_regs->dtxfsiz[i],
  20756. + txfsz_bak[i]);
  20757. + }
  20758. + }
  20759. + } else {
  20760. + return 0;
  20761. + }
  20762. +
  20763. + /* Flush the FIFOs */
  20764. + dwc_otg_flush_tx_fifo(core_if, 0x10); /* all Tx FIFOs */
  20765. + dwc_otg_flush_rx_fifo(core_if);
  20766. +
  20767. + return retval;
  20768. +}
  20769. +
  20770. +/**
  20771. + * This function sets a new value for the buffer Alignment setup.
  20772. + */
  20773. +static int cfi_ep_set_tx_fifo_val(uint8_t * buf, dwc_otg_pcd_t * pcd)
  20774. +{
  20775. + int retval;
  20776. + uint32_t fsiz;
  20777. + uint16_t size;
  20778. + uint16_t ep_addr;
  20779. + dwc_otg_pcd_ep_t *ep;
  20780. + dwc_otg_core_params_t *params = GET_CORE_IF(pcd)->core_params;
  20781. + tx_fifo_size_setup_t *ptxfifoval;
  20782. +
  20783. + ptxfifoval = (tx_fifo_size_setup_t *) buf;
  20784. + ep_addr = ptxfifoval->bEndpointAddress;
  20785. + size = ptxfifoval->wDepth;
  20786. +
  20787. + ep = get_ep_by_addr(pcd, ep_addr);
  20788. +
  20789. + CFI_INFO
  20790. + ("%s: Set Tx FIFO size: endpoint addr=0x%02x, depth=%d, FIFO Num=%d\n",
  20791. + __func__, ep_addr, size, ep->dwc_ep.tx_fifo_num);
  20792. +
  20793. + if (NULL == ep) {
  20794. + CFI_INFO("%s: Unable to get the endpoint addr=0x%02x\n",
  20795. + __func__, ep_addr);
  20796. + return -DWC_E_INVALID;
  20797. + }
  20798. +
  20799. + fsiz = params->dev_tx_fifo_size[ep->dwc_ep.tx_fifo_num - 1];
  20800. + params->dev_tx_fifo_size[ep->dwc_ep.tx_fifo_num - 1] = size;
  20801. +
  20802. + if (resize_fifos(GET_CORE_IF(pcd))) {
  20803. + retval = 0;
  20804. + } else {
  20805. + CFI_INFO
  20806. + ("%s: Error setting the feature Tx FIFO Size for EP%d\n",
  20807. + __func__, ep_addr);
  20808. + params->dev_tx_fifo_size[ep->dwc_ep.tx_fifo_num - 1] = fsiz;
  20809. + retval = -DWC_E_INVALID;
  20810. + }
  20811. +
  20812. + return retval;
  20813. +}
  20814. +
  20815. +/**
  20816. + * This function sets a new value for the buffer Alignment setup.
  20817. + */
  20818. +static int cfi_set_rx_fifo_val(uint8_t * buf, dwc_otg_pcd_t * pcd)
  20819. +{
  20820. + int retval;
  20821. + uint32_t fsiz;
  20822. + uint16_t size;
  20823. + dwc_otg_core_params_t *params = GET_CORE_IF(pcd)->core_params;
  20824. + rx_fifo_size_setup_t *prxfifoval;
  20825. +
  20826. + prxfifoval = (rx_fifo_size_setup_t *) buf;
  20827. + size = prxfifoval->wDepth;
  20828. +
  20829. + fsiz = params->dev_rx_fifo_size;
  20830. + params->dev_rx_fifo_size = size;
  20831. +
  20832. + if (resize_fifos(GET_CORE_IF(pcd))) {
  20833. + retval = 0;
  20834. + } else {
  20835. + CFI_INFO("%s: Error setting the feature Rx FIFO Size\n",
  20836. + __func__);
  20837. + params->dev_rx_fifo_size = fsiz;
  20838. + retval = -DWC_E_INVALID;
  20839. + }
  20840. +
  20841. + return retval;
  20842. +}
  20843. +
  20844. +/**
  20845. + * This function reads the SG of an EP's buffer setup into the buffer buf
  20846. + */
  20847. +static int cfi_ep_get_sg_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
  20848. + struct cfi_usb_ctrlrequest *req)
  20849. +{
  20850. + int retval = -DWC_E_INVALID;
  20851. + uint8_t addr;
  20852. + cfi_ep_t *ep;
  20853. +
  20854. + /* The Low Byte of the wValue contains a non-zero address of the endpoint */
  20855. + addr = req->wValue & 0xFF;
  20856. + if (addr == 0) /* The address should be non-zero */
  20857. + return retval;
  20858. +
  20859. + ep = get_cfi_ep_by_addr(pcd->cfi, addr);
  20860. + if (NULL == ep) {
  20861. + CFI_INFO("%s: Unable to get the endpoint address(0x%02x)\n",
  20862. + __func__, addr);
  20863. + return retval;
  20864. + }
  20865. +
  20866. + dwc_memcpy(buf, ep->bm_sg, BS_SG_VAL_DESC_LEN);
  20867. + retval = BS_SG_VAL_DESC_LEN;
  20868. + return retval;
  20869. +}
  20870. +
  20871. +/**
  20872. + * This function reads the Concatenation value of an EP's buffer mode into
  20873. + * the buffer buf
  20874. + */
  20875. +static int cfi_ep_get_concat_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
  20876. + struct cfi_usb_ctrlrequest *req)
  20877. +{
  20878. + int retval = -DWC_E_INVALID;
  20879. + uint8_t addr;
  20880. + cfi_ep_t *ep;
  20881. + uint8_t desc_count;
  20882. +
  20883. + /* The Low Byte of the wValue contains a non-zero address of the endpoint */
  20884. + addr = req->wValue & 0xFF;
  20885. + if (addr == 0) /* The address should be non-zero */
  20886. + return retval;
  20887. +
  20888. + ep = get_cfi_ep_by_addr(pcd->cfi, addr);
  20889. + if (NULL == ep) {
  20890. + CFI_INFO("%s: Unable to get the endpoint address(0x%02x)\n",
  20891. + __func__, addr);
  20892. + return retval;
  20893. + }
  20894. +
  20895. + /* Copy the header to the buffer */
  20896. + dwc_memcpy(buf, ep->bm_concat, BS_CONCAT_VAL_HDR_LEN);
  20897. + /* Advance the buffer pointer by the header size */
  20898. + buf += BS_CONCAT_VAL_HDR_LEN;
  20899. +
  20900. + desc_count = ep->bm_concat->hdr.bDescCount;
  20901. + /* Copy alll the wTxBytes to the buffer */
  20902. + dwc_memcpy(buf, ep->bm_concat->wTxBytes, sizeof(uid16_t) * desc_count);
  20903. +
  20904. + retval = BS_CONCAT_VAL_HDR_LEN + sizeof(uid16_t) * desc_count;
  20905. + return retval;
  20906. +}
  20907. +
  20908. +/**
  20909. + * This function reads the buffer Alignment value of an EP's buffer mode into
  20910. + * the buffer buf
  20911. + *
  20912. + * @return The total number of bytes copied to the buffer or negative error code.
  20913. + */
  20914. +static int cfi_ep_get_align_val(uint8_t * buf, struct dwc_otg_pcd *pcd,
  20915. + struct cfi_usb_ctrlrequest *req)
  20916. +{
  20917. + int retval = -DWC_E_INVALID;
  20918. + uint8_t addr;
  20919. + cfi_ep_t *ep;
  20920. +
  20921. + /* The Low Byte of the wValue contains a non-zero address of the endpoint */
  20922. + addr = req->wValue & 0xFF;
  20923. + if (addr == 0) /* The address should be non-zero */
  20924. + return retval;
  20925. +
  20926. + ep = get_cfi_ep_by_addr(pcd->cfi, addr);
  20927. + if (NULL == ep) {
  20928. + CFI_INFO("%s: Unable to get the endpoint address(0x%02x)\n",
  20929. + __func__, addr);
  20930. + return retval;
  20931. + }
  20932. +
  20933. + dwc_memcpy(buf, ep->bm_align, BS_ALIGN_VAL_HDR_LEN);
  20934. + retval = BS_ALIGN_VAL_HDR_LEN;
  20935. +
  20936. + return retval;
  20937. +}
  20938. +
  20939. +/**
  20940. + * This function sets a new value for the specified feature
  20941. + *
  20942. + * @param pcd A pointer to the PCD object
  20943. + *
  20944. + * @return 0 if successful, negative error code otherwise to stall the DCE.
  20945. + */
  20946. +static int cfi_set_feature_value(struct dwc_otg_pcd *pcd)
  20947. +{
  20948. + int retval = -DWC_E_NOT_SUPPORTED;
  20949. + uint16_t wIndex, wValue;
  20950. + uint8_t bRequest;
  20951. + struct dwc_otg_core_if *coreif;
  20952. + cfiobject_t *cfi = pcd->cfi;
  20953. + struct cfi_usb_ctrlrequest *ctrl_req;
  20954. + uint8_t *buf;
  20955. + ctrl_req = &cfi->ctrl_req;
  20956. +
  20957. + buf = pcd->cfi->ctrl_req.data;
  20958. +
  20959. + coreif = GET_CORE_IF(pcd);
  20960. + bRequest = ctrl_req->bRequest;
  20961. + wIndex = DWC_CONSTANT_CPU_TO_LE16(ctrl_req->wIndex);
  20962. + wValue = DWC_CONSTANT_CPU_TO_LE16(ctrl_req->wValue);
  20963. +
  20964. + /* See which feature is to be modified */
  20965. + switch (wIndex) {
  20966. + case FT_ID_DMA_BUFFER_SETUP:
  20967. + /* Modify the feature */
  20968. + if ((retval = cfi_ep_set_sg_val(buf, pcd)) < 0)
  20969. + return retval;
  20970. +
  20971. + /* And send this request to the gadget */
  20972. + cfi->need_gadget_att = 1;
  20973. + break;
  20974. +
  20975. + case FT_ID_DMA_BUFF_ALIGN:
  20976. + if ((retval = cfi_ep_set_alignment_val(buf, pcd)) < 0)
  20977. + return retval;
  20978. + cfi->need_gadget_att = 1;
  20979. + break;
  20980. +
  20981. + case FT_ID_DMA_CONCAT_SETUP:
  20982. + /* Modify the feature */
  20983. + if ((retval = cfi_ep_set_concat_val(buf, pcd)) < 0)
  20984. + return retval;
  20985. + cfi->need_gadget_att = 1;
  20986. + break;
  20987. +
  20988. + case FT_ID_DMA_CIRCULAR:
  20989. + CFI_INFO("FT_ID_DMA_CIRCULAR\n");
  20990. + break;
  20991. +
  20992. + case FT_ID_THRESHOLD_SETUP:
  20993. + CFI_INFO("FT_ID_THRESHOLD_SETUP\n");
  20994. + break;
  20995. +
  20996. + case FT_ID_DFIFO_DEPTH:
  20997. + CFI_INFO("FT_ID_DFIFO_DEPTH\n");
  20998. + break;
  20999. +
  21000. + case FT_ID_TX_FIFO_DEPTH:
  21001. + CFI_INFO("FT_ID_TX_FIFO_DEPTH\n");
  21002. + if ((retval = cfi_ep_set_tx_fifo_val(buf, pcd)) < 0)
  21003. + return retval;
  21004. + cfi->need_gadget_att = 0;
  21005. + break;
  21006. +
  21007. + case FT_ID_RX_FIFO_DEPTH:
  21008. + CFI_INFO("FT_ID_RX_FIFO_DEPTH\n");
  21009. + if ((retval = cfi_set_rx_fifo_val(buf, pcd)) < 0)
  21010. + return retval;
  21011. + cfi->need_gadget_att = 0;
  21012. + break;
  21013. + }
  21014. +
  21015. + return retval;
  21016. +}
  21017. +
  21018. +#endif //DWC_UTE_CFI
  21019. --- /dev/null
  21020. +++ b/drivers/usb/host/dwc_otg/dwc_otg_cfi.h
  21021. @@ -0,0 +1,320 @@
  21022. +/* ==========================================================================
  21023. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  21024. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  21025. + * otherwise expressly agreed to in writing between Synopsys and you.
  21026. + *
  21027. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  21028. + * any End User Software License Agreement or Agreement for Licensed Product
  21029. + * with Synopsys or any supplement thereto. You are permitted to use and
  21030. + * redistribute this Software in source and binary forms, with or without
  21031. + * modification, provided that redistributions of source code must retain this
  21032. + * notice. You may not view, use, disclose, copy or distribute this file or
  21033. + * any information contained herein except pursuant to this license grant from
  21034. + * Synopsys. If you do not agree with this notice, including the disclaimer
  21035. + * below, then you are not authorized to use the Software.
  21036. + *
  21037. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  21038. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21039. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21040. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  21041. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21042. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21043. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  21044. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21045. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  21046. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  21047. + * DAMAGE.
  21048. + * ========================================================================== */
  21049. +
  21050. +#if !defined(__DWC_OTG_CFI_H__)
  21051. +#define __DWC_OTG_CFI_H__
  21052. +
  21053. +#include "dwc_otg_pcd.h"
  21054. +#include "dwc_cfi_common.h"
  21055. +
  21056. +/**
  21057. + * @file
  21058. + * This file contains the CFI related OTG PCD specific common constants,
  21059. + * interfaces(functions and macros) and data structures.The CFI Protocol is an
  21060. + * optional interface for internal testing purposes that a DUT may implement to
  21061. + * support testing of configurable features.
  21062. + *
  21063. + */
  21064. +
  21065. +struct dwc_otg_pcd;
  21066. +struct dwc_otg_pcd_ep;
  21067. +
  21068. +/** OTG CFI Features (properties) ID constants */
  21069. +/** This is a request for all Core Features */
  21070. +#define FT_ID_DMA_MODE 0x0001
  21071. +#define FT_ID_DMA_BUFFER_SETUP 0x0002
  21072. +#define FT_ID_DMA_BUFF_ALIGN 0x0003
  21073. +#define FT_ID_DMA_CONCAT_SETUP 0x0004
  21074. +#define FT_ID_DMA_CIRCULAR 0x0005
  21075. +#define FT_ID_THRESHOLD_SETUP 0x0006
  21076. +#define FT_ID_DFIFO_DEPTH 0x0007
  21077. +#define FT_ID_TX_FIFO_DEPTH 0x0008
  21078. +#define FT_ID_RX_FIFO_DEPTH 0x0009
  21079. +
  21080. +/**********************************************************/
  21081. +#define CFI_INFO_DEF
  21082. +
  21083. +#ifdef CFI_INFO_DEF
  21084. +#define CFI_INFO(fmt...) DWC_PRINTF("CFI: " fmt);
  21085. +#else
  21086. +#define CFI_INFO(fmt...)
  21087. +#endif
  21088. +
  21089. +#define min(x,y) ({ \
  21090. + x < y ? x : y; })
  21091. +
  21092. +#define max(x,y) ({ \
  21093. + x > y ? x : y; })
  21094. +
  21095. +/**
  21096. + * Descriptor DMA SG Buffer setup structure (SG buffer). This structure is
  21097. + * also used for setting up a buffer for Circular DDMA.
  21098. + */
  21099. +struct _ddma_sg_buffer_setup {
  21100. +#define BS_SG_VAL_DESC_LEN 6
  21101. + /* The OUT EP address */
  21102. + uint8_t bOutEndpointAddress;
  21103. + /* The IN EP address */
  21104. + uint8_t bInEndpointAddress;
  21105. + /* Number of bytes to put between transfer segments (must be DWORD boundaries) */
  21106. + uint8_t bOffset;
  21107. + /* The number of transfer segments (a DMA descriptors per each segment) */
  21108. + uint8_t bCount;
  21109. + /* Size (in byte) of each transfer segment */
  21110. + uint16_t wSize;
  21111. +} __attribute__ ((packed));
  21112. +typedef struct _ddma_sg_buffer_setup ddma_sg_buffer_setup_t;
  21113. +
  21114. +/** Descriptor DMA Concatenation Buffer setup structure */
  21115. +struct _ddma_concat_buffer_setup_hdr {
  21116. +#define BS_CONCAT_VAL_HDR_LEN 4
  21117. + /* The endpoint for which the buffer is to be set up */
  21118. + uint8_t bEndpointAddress;
  21119. + /* The count of descriptors to be used */
  21120. + uint8_t bDescCount;
  21121. + /* The total size of the transfer */
  21122. + uint16_t wSize;
  21123. +} __attribute__ ((packed));
  21124. +typedef struct _ddma_concat_buffer_setup_hdr ddma_concat_buffer_setup_hdr_t;
  21125. +
  21126. +/** Descriptor DMA Concatenation Buffer setup structure */
  21127. +struct _ddma_concat_buffer_setup {
  21128. + /* The SG header */
  21129. + ddma_concat_buffer_setup_hdr_t hdr;
  21130. +
  21131. + /* The XFER sizes pointer (allocated dynamically) */
  21132. + uint16_t *wTxBytes;
  21133. +} __attribute__ ((packed));
  21134. +typedef struct _ddma_concat_buffer_setup ddma_concat_buffer_setup_t;
  21135. +
  21136. +/** Descriptor DMA Alignment Buffer setup structure */
  21137. +struct _ddma_align_buffer_setup {
  21138. +#define BS_ALIGN_VAL_HDR_LEN 2
  21139. + uint8_t bEndpointAddress;
  21140. + uint8_t bAlign;
  21141. +} __attribute__ ((packed));
  21142. +typedef struct _ddma_align_buffer_setup ddma_align_buffer_setup_t;
  21143. +
  21144. +/** Transmit FIFO Size setup structure */
  21145. +struct _tx_fifo_size_setup {
  21146. + uint8_t bEndpointAddress;
  21147. + uint16_t wDepth;
  21148. +} __attribute__ ((packed));
  21149. +typedef struct _tx_fifo_size_setup tx_fifo_size_setup_t;
  21150. +
  21151. +/** Transmit FIFO Size setup structure */
  21152. +struct _rx_fifo_size_setup {
  21153. + uint16_t wDepth;
  21154. +} __attribute__ ((packed));
  21155. +typedef struct _rx_fifo_size_setup rx_fifo_size_setup_t;
  21156. +
  21157. +/**
  21158. + * struct cfi_usb_ctrlrequest - the CFI implementation of the struct usb_ctrlrequest
  21159. + * This structure encapsulates the standard usb_ctrlrequest and adds a pointer
  21160. + * to the data returned in the data stage of a 3-stage Control Write requests.
  21161. + */
  21162. +struct cfi_usb_ctrlrequest {
  21163. + uint8_t bRequestType;
  21164. + uint8_t bRequest;
  21165. + uint16_t wValue;
  21166. + uint16_t wIndex;
  21167. + uint16_t wLength;
  21168. + uint8_t *data;
  21169. +} UPACKED;
  21170. +
  21171. +/*---------------------------------------------------------------------------*/
  21172. +
  21173. +/**
  21174. + * The CFI wrapper of the enabled and activated dwc_otg_pcd_ep structures.
  21175. + * This structure is used to store the buffer setup data for any
  21176. + * enabled endpoint in the PCD.
  21177. + */
  21178. +struct cfi_ep {
  21179. + /* Entry for the list container */
  21180. + dwc_list_link_t lh;
  21181. + /* Pointer to the active PCD endpoint structure */
  21182. + struct dwc_otg_pcd_ep *ep;
  21183. + /* The last descriptor in the chain of DMA descriptors of the endpoint */
  21184. + struct dwc_otg_dma_desc *dma_desc_last;
  21185. + /* The SG feature value */
  21186. + ddma_sg_buffer_setup_t *bm_sg;
  21187. + /* The Circular feature value */
  21188. + ddma_sg_buffer_setup_t *bm_circ;
  21189. + /* The Concatenation feature value */
  21190. + ddma_concat_buffer_setup_t *bm_concat;
  21191. + /* The Alignment feature value */
  21192. + ddma_align_buffer_setup_t *bm_align;
  21193. + /* XFER length */
  21194. + uint32_t xfer_len;
  21195. + /*
  21196. + * Count of DMA descriptors currently used.
  21197. + * The total should not exceed the MAX_DMA_DESCS_PER_EP value
  21198. + * defined in the dwc_otg_cil.h
  21199. + */
  21200. + uint32_t desc_count;
  21201. +};
  21202. +typedef struct cfi_ep cfi_ep_t;
  21203. +
  21204. +typedef struct cfi_dma_buff {
  21205. +#define CFI_IN_BUF_LEN 1024
  21206. +#define CFI_OUT_BUF_LEN 1024
  21207. + dma_addr_t addr;
  21208. + uint8_t *buf;
  21209. +} cfi_dma_buff_t;
  21210. +
  21211. +struct cfiobject;
  21212. +
  21213. +/**
  21214. + * This is the interface for the CFI operations.
  21215. + *
  21216. + * @param ep_enable Called when any endpoint is enabled and activated.
  21217. + * @param release Called when the CFI object is released and it needs to correctly
  21218. + * deallocate the dynamic memory
  21219. + * @param ctrl_write_complete Called when the data stage of the request is complete
  21220. + */
  21221. +typedef struct cfi_ops {
  21222. + int (*ep_enable) (struct cfiobject * cfi, struct dwc_otg_pcd * pcd,
  21223. + struct dwc_otg_pcd_ep * ep);
  21224. + void *(*ep_alloc_buf) (struct cfiobject * cfi, struct dwc_otg_pcd * pcd,
  21225. + struct dwc_otg_pcd_ep * ep, dma_addr_t * dma,
  21226. + unsigned size, gfp_t flags);
  21227. + void (*release) (struct cfiobject * cfi);
  21228. + int (*ctrl_write_complete) (struct cfiobject * cfi,
  21229. + struct dwc_otg_pcd * pcd);
  21230. + void (*build_descriptors) (struct cfiobject * cfi,
  21231. + struct dwc_otg_pcd * pcd,
  21232. + struct dwc_otg_pcd_ep * ep,
  21233. + dwc_otg_pcd_request_t * req);
  21234. +} cfi_ops_t;
  21235. +
  21236. +struct cfiobject {
  21237. + cfi_ops_t ops;
  21238. + struct dwc_otg_pcd *pcd;
  21239. + struct usb_gadget *gadget;
  21240. +
  21241. + /* Buffers used to send/receive CFI-related request data */
  21242. + cfi_dma_buff_t buf_in;
  21243. + cfi_dma_buff_t buf_out;
  21244. +
  21245. + /* CFI specific Control request wrapper */
  21246. + struct cfi_usb_ctrlrequest ctrl_req;
  21247. +
  21248. + /* The list of active EP's in the PCD of type cfi_ep_t */
  21249. + dwc_list_link_t active_eps;
  21250. +
  21251. + /* This flag shall control the propagation of a specific request
  21252. + * to the gadget's processing routines.
  21253. + * 0 - no gadget handling
  21254. + * 1 - the gadget needs to know about this request (w/o completing a status
  21255. + * phase - just return a 0 to the _setup callback)
  21256. + */
  21257. + uint8_t need_gadget_att;
  21258. +
  21259. + /* Flag indicating whether the status IN phase needs to be
  21260. + * completed by the PCD
  21261. + */
  21262. + uint8_t need_status_in_complete;
  21263. +};
  21264. +typedef struct cfiobject cfiobject_t;
  21265. +
  21266. +#define DUMP_MSG
  21267. +
  21268. +#if defined(DUMP_MSG)
  21269. +static inline void dump_msg(const u8 * buf, unsigned int length)
  21270. +{
  21271. + unsigned int start, num, i;
  21272. + char line[52], *p;
  21273. +
  21274. + if (length >= 512)
  21275. + return;
  21276. +
  21277. + start = 0;
  21278. + while (length > 0) {
  21279. + num = min(length, 16u);
  21280. + p = line;
  21281. + for (i = 0; i < num; ++i) {
  21282. + if (i == 8)
  21283. + *p++ = ' ';
  21284. + DWC_SPRINTF(p, " %02x", buf[i]);
  21285. + p += 3;
  21286. + }
  21287. + *p = 0;
  21288. + DWC_DEBUG("%6x: %s\n", start, line);
  21289. + buf += num;
  21290. + start += num;
  21291. + length -= num;
  21292. + }
  21293. +}
  21294. +#else
  21295. +static inline void dump_msg(const u8 * buf, unsigned int length)
  21296. +{
  21297. +}
  21298. +#endif
  21299. +
  21300. +/**
  21301. + * This function returns a pointer to cfi_ep_t object with the addr address.
  21302. + */
  21303. +static inline struct cfi_ep *get_cfi_ep_by_addr(struct cfiobject *cfi,
  21304. + uint8_t addr)
  21305. +{
  21306. + struct cfi_ep *pcfiep;
  21307. + dwc_list_link_t *tmp;
  21308. +
  21309. + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
  21310. + pcfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
  21311. +
  21312. + if (pcfiep->ep->desc->bEndpointAddress == addr) {
  21313. + return pcfiep;
  21314. + }
  21315. + }
  21316. +
  21317. + return NULL;
  21318. +}
  21319. +
  21320. +/**
  21321. + * This function returns a pointer to cfi_ep_t object that matches
  21322. + * the dwc_otg_pcd_ep object.
  21323. + */
  21324. +static inline struct cfi_ep *get_cfi_ep_by_pcd_ep(struct cfiobject *cfi,
  21325. + struct dwc_otg_pcd_ep *ep)
  21326. +{
  21327. + struct cfi_ep *pcfiep = NULL;
  21328. + dwc_list_link_t *tmp;
  21329. +
  21330. + DWC_LIST_FOREACH(tmp, &cfi->active_eps) {
  21331. + pcfiep = DWC_LIST_ENTRY(tmp, struct cfi_ep, lh);
  21332. + if (pcfiep->ep == ep) {
  21333. + return pcfiep;
  21334. + }
  21335. + }
  21336. + return NULL;
  21337. +}
  21338. +
  21339. +int cfi_setup(struct dwc_otg_pcd *pcd, struct cfi_usb_ctrlrequest *ctrl);
  21340. +
  21341. +#endif /* (__DWC_OTG_CFI_H__) */
  21342. --- /dev/null
  21343. +++ b/drivers/usb/host/dwc_otg/dwc_otg_cil.c
  21344. @@ -0,0 +1,7141 @@
  21345. +/* ==========================================================================
  21346. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_cil.c $
  21347. + * $Revision: #191 $
  21348. + * $Date: 2012/08/10 $
  21349. + * $Change: 2047372 $
  21350. + *
  21351. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  21352. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  21353. + * otherwise expressly agreed to in writing between Synopsys and you.
  21354. + *
  21355. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  21356. + * any End User Software License Agreement or Agreement for Licensed Product
  21357. + * with Synopsys or any supplement thereto. You are permitted to use and
  21358. + * redistribute this Software in source and binary forms, with or without
  21359. + * modification, provided that redistributions of source code must retain this
  21360. + * notice. You may not view, use, disclose, copy or distribute this file or
  21361. + * any information contained herein except pursuant to this license grant from
  21362. + * Synopsys. If you do not agree with this notice, including the disclaimer
  21363. + * below, then you are not authorized to use the Software.
  21364. + *
  21365. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  21366. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21367. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21368. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  21369. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21370. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21371. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  21372. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21373. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  21374. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  21375. + * DAMAGE.
  21376. + * ========================================================================== */
  21377. +
  21378. +/** @file
  21379. + *
  21380. + * The Core Interface Layer provides basic services for accessing and
  21381. + * managing the DWC_otg hardware. These services are used by both the
  21382. + * Host Controller Driver and the Peripheral Controller Driver.
  21383. + *
  21384. + * The CIL manages the memory map for the core so that the HCD and PCD
  21385. + * don't have to do this separately. It also handles basic tasks like
  21386. + * reading/writing the registers and data FIFOs in the controller.
  21387. + * Some of the data access functions provide encapsulation of several
  21388. + * operations required to perform a task, such as writing multiple
  21389. + * registers to start a transfer. Finally, the CIL performs basic
  21390. + * services that are not specific to either the host or device modes
  21391. + * of operation. These services include management of the OTG Host
  21392. + * Negotiation Protocol (HNP) and Session Request Protocol (SRP). A
  21393. + * Diagnostic API is also provided to allow testing of the controller
  21394. + * hardware.
  21395. + *
  21396. + * The Core Interface Layer has the following requirements:
  21397. + * - Provides basic controller operations.
  21398. + * - Minimal use of OS services.
  21399. + * - The OS services used will be abstracted by using inline functions
  21400. + * or macros.
  21401. + *
  21402. + */
  21403. +
  21404. +#include "dwc_os.h"
  21405. +#include "dwc_otg_regs.h"
  21406. +#include "dwc_otg_cil.h"
  21407. +
  21408. +static int dwc_otg_setup_params(dwc_otg_core_if_t * core_if);
  21409. +
  21410. +/**
  21411. + * This function is called to initialize the DWC_otg CSR data
  21412. + * structures. The register addresses in the device and host
  21413. + * structures are initialized from the base address supplied by the
  21414. + * caller. The calling function must make the OS calls to get the
  21415. + * base address of the DWC_otg controller registers. The core_params
  21416. + * argument holds the parameters that specify how the core should be
  21417. + * configured.
  21418. + *
  21419. + * @param reg_base_addr Base address of DWC_otg core registers
  21420. + *
  21421. + */
  21422. +dwc_otg_core_if_t *dwc_otg_cil_init(const uint32_t * reg_base_addr)
  21423. +{
  21424. + dwc_otg_core_if_t *core_if = 0;
  21425. + dwc_otg_dev_if_t *dev_if = 0;
  21426. + dwc_otg_host_if_t *host_if = 0;
  21427. + uint8_t *reg_base = (uint8_t *) reg_base_addr;
  21428. + int i = 0;
  21429. +
  21430. + DWC_DEBUGPL(DBG_CILV, "%s(%p)\n", __func__, reg_base_addr);
  21431. +
  21432. + core_if = DWC_ALLOC(sizeof(dwc_otg_core_if_t));
  21433. +
  21434. + if (core_if == NULL) {
  21435. + DWC_DEBUGPL(DBG_CIL,
  21436. + "Allocation of dwc_otg_core_if_t failed\n");
  21437. + return 0;
  21438. + }
  21439. + core_if->core_global_regs = (dwc_otg_core_global_regs_t *) reg_base;
  21440. +
  21441. + /*
  21442. + * Allocate the Device Mode structures.
  21443. + */
  21444. + dev_if = DWC_ALLOC(sizeof(dwc_otg_dev_if_t));
  21445. +
  21446. + if (dev_if == NULL) {
  21447. + DWC_DEBUGPL(DBG_CIL, "Allocation of dwc_otg_dev_if_t failed\n");
  21448. + DWC_FREE(core_if);
  21449. + return 0;
  21450. + }
  21451. +
  21452. + dev_if->dev_global_regs =
  21453. + (dwc_otg_device_global_regs_t *) (reg_base +
  21454. + DWC_DEV_GLOBAL_REG_OFFSET);
  21455. +
  21456. + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  21457. + dev_if->in_ep_regs[i] = (dwc_otg_dev_in_ep_regs_t *)
  21458. + (reg_base + DWC_DEV_IN_EP_REG_OFFSET +
  21459. + (i * DWC_EP_REG_OFFSET));
  21460. +
  21461. + dev_if->out_ep_regs[i] = (dwc_otg_dev_out_ep_regs_t *)
  21462. + (reg_base + DWC_DEV_OUT_EP_REG_OFFSET +
  21463. + (i * DWC_EP_REG_OFFSET));
  21464. + DWC_DEBUGPL(DBG_CILV, "in_ep_regs[%d]->diepctl=%p\n",
  21465. + i, &dev_if->in_ep_regs[i]->diepctl);
  21466. + DWC_DEBUGPL(DBG_CILV, "out_ep_regs[%d]->doepctl=%p\n",
  21467. + i, &dev_if->out_ep_regs[i]->doepctl);
  21468. + }
  21469. +
  21470. + dev_if->speed = 0; // unknown
  21471. +
  21472. + core_if->dev_if = dev_if;
  21473. +
  21474. + /*
  21475. + * Allocate the Host Mode structures.
  21476. + */
  21477. + host_if = DWC_ALLOC(sizeof(dwc_otg_host_if_t));
  21478. +
  21479. + if (host_if == NULL) {
  21480. + DWC_DEBUGPL(DBG_CIL,
  21481. + "Allocation of dwc_otg_host_if_t failed\n");
  21482. + DWC_FREE(dev_if);
  21483. + DWC_FREE(core_if);
  21484. + return 0;
  21485. + }
  21486. +
  21487. + host_if->host_global_regs = (dwc_otg_host_global_regs_t *)
  21488. + (reg_base + DWC_OTG_HOST_GLOBAL_REG_OFFSET);
  21489. +
  21490. + host_if->hprt0 =
  21491. + (uint32_t *) (reg_base + DWC_OTG_HOST_PORT_REGS_OFFSET);
  21492. +
  21493. + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  21494. + host_if->hc_regs[i] = (dwc_otg_hc_regs_t *)
  21495. + (reg_base + DWC_OTG_HOST_CHAN_REGS_OFFSET +
  21496. + (i * DWC_OTG_CHAN_REGS_OFFSET));
  21497. + DWC_DEBUGPL(DBG_CILV, "hc_reg[%d]->hcchar=%p\n",
  21498. + i, &host_if->hc_regs[i]->hcchar);
  21499. + }
  21500. +
  21501. + host_if->num_host_channels = MAX_EPS_CHANNELS;
  21502. + core_if->host_if = host_if;
  21503. +
  21504. + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  21505. + core_if->data_fifo[i] =
  21506. + (uint32_t *) (reg_base + DWC_OTG_DATA_FIFO_OFFSET +
  21507. + (i * DWC_OTG_DATA_FIFO_SIZE));
  21508. + DWC_DEBUGPL(DBG_CILV, "data_fifo[%d]=0x%08lx\n",
  21509. + i, (unsigned long)core_if->data_fifo[i]);
  21510. + }
  21511. +
  21512. + core_if->pcgcctl = (uint32_t *) (reg_base + DWC_OTG_PCGCCTL_OFFSET);
  21513. +
  21514. + /* Initiate lx_state to L3 disconnected state */
  21515. + core_if->lx_state = DWC_OTG_L3;
  21516. + /*
  21517. + * Store the contents of the hardware configuration registers here for
  21518. + * easy access later.
  21519. + */
  21520. + core_if->hwcfg1.d32 =
  21521. + DWC_READ_REG32(&core_if->core_global_regs->ghwcfg1);
  21522. + core_if->hwcfg2.d32 =
  21523. + DWC_READ_REG32(&core_if->core_global_regs->ghwcfg2);
  21524. + core_if->hwcfg3.d32 =
  21525. + DWC_READ_REG32(&core_if->core_global_regs->ghwcfg3);
  21526. + core_if->hwcfg4.d32 =
  21527. + DWC_READ_REG32(&core_if->core_global_regs->ghwcfg4);
  21528. +
  21529. + /* Force host mode to get HPTXFSIZ exact power on value */
  21530. + {
  21531. + gusbcfg_data_t gusbcfg = {.d32 = 0 };
  21532. + gusbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  21533. + gusbcfg.b.force_host_mode = 1;
  21534. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, gusbcfg.d32);
  21535. + dwc_mdelay(100);
  21536. + core_if->hptxfsiz.d32 =
  21537. + DWC_READ_REG32(&core_if->core_global_regs->hptxfsiz);
  21538. + gusbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  21539. + gusbcfg.b.force_host_mode = 1;
  21540. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, gusbcfg.d32);
  21541. + dwc_mdelay(100);
  21542. + }
  21543. +
  21544. + DWC_DEBUGPL(DBG_CILV, "hwcfg1=%08x\n", core_if->hwcfg1.d32);
  21545. + DWC_DEBUGPL(DBG_CILV, "hwcfg2=%08x\n", core_if->hwcfg2.d32);
  21546. + DWC_DEBUGPL(DBG_CILV, "hwcfg3=%08x\n", core_if->hwcfg3.d32);
  21547. + DWC_DEBUGPL(DBG_CILV, "hwcfg4=%08x\n", core_if->hwcfg4.d32);
  21548. +
  21549. + core_if->hcfg.d32 =
  21550. + DWC_READ_REG32(&core_if->host_if->host_global_regs->hcfg);
  21551. + core_if->dcfg.d32 =
  21552. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  21553. +
  21554. + DWC_DEBUGPL(DBG_CILV, "hcfg=%08x\n", core_if->hcfg.d32);
  21555. + DWC_DEBUGPL(DBG_CILV, "dcfg=%08x\n", core_if->dcfg.d32);
  21556. +
  21557. + DWC_DEBUGPL(DBG_CILV, "op_mode=%0x\n", core_if->hwcfg2.b.op_mode);
  21558. + DWC_DEBUGPL(DBG_CILV, "arch=%0x\n", core_if->hwcfg2.b.architecture);
  21559. + DWC_DEBUGPL(DBG_CILV, "num_dev_ep=%d\n", core_if->hwcfg2.b.num_dev_ep);
  21560. + DWC_DEBUGPL(DBG_CILV, "num_host_chan=%d\n",
  21561. + core_if->hwcfg2.b.num_host_chan);
  21562. + DWC_DEBUGPL(DBG_CILV, "nonperio_tx_q_depth=0x%0x\n",
  21563. + core_if->hwcfg2.b.nonperio_tx_q_depth);
  21564. + DWC_DEBUGPL(DBG_CILV, "host_perio_tx_q_depth=0x%0x\n",
  21565. + core_if->hwcfg2.b.host_perio_tx_q_depth);
  21566. + DWC_DEBUGPL(DBG_CILV, "dev_token_q_depth=0x%0x\n",
  21567. + core_if->hwcfg2.b.dev_token_q_depth);
  21568. +
  21569. + DWC_DEBUGPL(DBG_CILV, "Total FIFO SZ=%d\n",
  21570. + core_if->hwcfg3.b.dfifo_depth);
  21571. + DWC_DEBUGPL(DBG_CILV, "xfer_size_cntr_width=%0x\n",
  21572. + core_if->hwcfg3.b.xfer_size_cntr_width);
  21573. +
  21574. + /*
  21575. + * Set the SRP sucess bit for FS-I2c
  21576. + */
  21577. + core_if->srp_success = 0;
  21578. + core_if->srp_timer_started = 0;
  21579. +
  21580. + /*
  21581. + * Create new workqueue and init works
  21582. + */
  21583. + core_if->wq_otg = DWC_WORKQ_ALLOC("dwc_otg");
  21584. + if (core_if->wq_otg == 0) {
  21585. + DWC_WARN("DWC_WORKQ_ALLOC failed\n");
  21586. + DWC_FREE(host_if);
  21587. + DWC_FREE(dev_if);
  21588. + DWC_FREE(core_if);
  21589. + return 0;
  21590. + }
  21591. +
  21592. + core_if->snpsid = DWC_READ_REG32(&core_if->core_global_regs->gsnpsid);
  21593. +
  21594. + DWC_PRINTF("Core Release: %x.%x%x%x\n",
  21595. + (core_if->snpsid >> 12 & 0xF),
  21596. + (core_if->snpsid >> 8 & 0xF),
  21597. + (core_if->snpsid >> 4 & 0xF), (core_if->snpsid & 0xF));
  21598. +
  21599. + core_if->wkp_timer = DWC_TIMER_ALLOC("Wake Up Timer",
  21600. + w_wakeup_detected, core_if);
  21601. + if (core_if->wkp_timer == 0) {
  21602. + DWC_WARN("DWC_TIMER_ALLOC failed\n");
  21603. + DWC_FREE(host_if);
  21604. + DWC_FREE(dev_if);
  21605. + DWC_WORKQ_FREE(core_if->wq_otg);
  21606. + DWC_FREE(core_if);
  21607. + return 0;
  21608. + }
  21609. +
  21610. + if (dwc_otg_setup_params(core_if)) {
  21611. + DWC_WARN("Error while setting core params\n");
  21612. + }
  21613. +
  21614. + core_if->hibernation_suspend = 0;
  21615. +
  21616. + /** ADP initialization */
  21617. + dwc_otg_adp_init(core_if);
  21618. +
  21619. + return core_if;
  21620. +}
  21621. +
  21622. +/**
  21623. + * This function frees the structures allocated by dwc_otg_cil_init().
  21624. + *
  21625. + * @param core_if The core interface pointer returned from
  21626. + * dwc_otg_cil_init().
  21627. + *
  21628. + */
  21629. +void dwc_otg_cil_remove(dwc_otg_core_if_t * core_if)
  21630. +{
  21631. + dctl_data_t dctl = {.d32 = 0 };
  21632. + DWC_DEBUGPL(DBG_CILV, "%s(%p)\n", __func__, core_if);
  21633. +
  21634. + /* Disable all interrupts */
  21635. + DWC_MODIFY_REG32(&core_if->core_global_regs->gahbcfg, 1, 0);
  21636. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, 0);
  21637. +
  21638. + dctl.b.sftdiscon = 1;
  21639. + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
  21640. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0,
  21641. + dctl.d32);
  21642. + }
  21643. +
  21644. + if (core_if->wq_otg) {
  21645. + DWC_WORKQ_WAIT_WORK_DONE(core_if->wq_otg, 500);
  21646. + DWC_WORKQ_FREE(core_if->wq_otg);
  21647. + }
  21648. + if (core_if->dev_if) {
  21649. + DWC_FREE(core_if->dev_if);
  21650. + }
  21651. + if (core_if->host_if) {
  21652. + DWC_FREE(core_if->host_if);
  21653. + }
  21654. +
  21655. + /** Remove ADP Stuff */
  21656. + dwc_otg_adp_remove(core_if);
  21657. + if (core_if->core_params) {
  21658. + DWC_FREE(core_if->core_params);
  21659. + }
  21660. + if (core_if->wkp_timer) {
  21661. + DWC_TIMER_FREE(core_if->wkp_timer);
  21662. + }
  21663. + if (core_if->srp_timer) {
  21664. + DWC_TIMER_FREE(core_if->srp_timer);
  21665. + }
  21666. + DWC_FREE(core_if);
  21667. +}
  21668. +
  21669. +/**
  21670. + * This function enables the controller's Global Interrupt in the AHB Config
  21671. + * register.
  21672. + *
  21673. + * @param core_if Programming view of DWC_otg controller.
  21674. + */
  21675. +void dwc_otg_enable_global_interrupts(dwc_otg_core_if_t * core_if)
  21676. +{
  21677. + gahbcfg_data_t ahbcfg = {.d32 = 0 };
  21678. + ahbcfg.b.glblintrmsk = 1; /* Enable interrupts */
  21679. + DWC_MODIFY_REG32(&core_if->core_global_regs->gahbcfg, 0, ahbcfg.d32);
  21680. +}
  21681. +
  21682. +/**
  21683. + * This function disables the controller's Global Interrupt in the AHB Config
  21684. + * register.
  21685. + *
  21686. + * @param core_if Programming view of DWC_otg controller.
  21687. + */
  21688. +void dwc_otg_disable_global_interrupts(dwc_otg_core_if_t * core_if)
  21689. +{
  21690. + gahbcfg_data_t ahbcfg = {.d32 = 0 };
  21691. + ahbcfg.b.glblintrmsk = 1; /* Disable interrupts */
  21692. + DWC_MODIFY_REG32(&core_if->core_global_regs->gahbcfg, ahbcfg.d32, 0);
  21693. +}
  21694. +
  21695. +/**
  21696. + * This function initializes the commmon interrupts, used in both
  21697. + * device and host modes.
  21698. + *
  21699. + * @param core_if Programming view of the DWC_otg controller
  21700. + *
  21701. + */
  21702. +static void dwc_otg_enable_common_interrupts(dwc_otg_core_if_t * core_if)
  21703. +{
  21704. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  21705. + gintmsk_data_t intr_mask = {.d32 = 0 };
  21706. +
  21707. + /* Clear any pending OTG Interrupts */
  21708. + DWC_WRITE_REG32(&global_regs->gotgint, 0xFFFFFFFF);
  21709. +
  21710. + /* Clear any pending interrupts */
  21711. + DWC_WRITE_REG32(&global_regs->gintsts, 0xFFFFFFFF);
  21712. +
  21713. + /*
  21714. + * Enable the interrupts in the GINTMSK.
  21715. + */
  21716. + intr_mask.b.modemismatch = 1;
  21717. + intr_mask.b.otgintr = 1;
  21718. +
  21719. + if (!core_if->dma_enable) {
  21720. + intr_mask.b.rxstsqlvl = 1;
  21721. + }
  21722. +
  21723. + intr_mask.b.conidstschng = 1;
  21724. + intr_mask.b.wkupintr = 1;
  21725. + intr_mask.b.disconnect = 0;
  21726. + intr_mask.b.usbsuspend = 1;
  21727. + intr_mask.b.sessreqintr = 1;
  21728. +#ifdef CONFIG_USB_DWC_OTG_LPM
  21729. + if (core_if->core_params->lpm_enable) {
  21730. + intr_mask.b.lpmtranrcvd = 1;
  21731. + }
  21732. +#endif
  21733. + DWC_WRITE_REG32(&global_regs->gintmsk, intr_mask.d32);
  21734. +}
  21735. +
  21736. +/*
  21737. + * The restore operation is modified to support Synopsys Emulated Powerdown and
  21738. + * Hibernation. This function is for exiting from Device mode hibernation by
  21739. + * Host Initiated Resume/Reset and Device Initiated Remote-Wakeup.
  21740. + * @param core_if Programming view of DWC_otg controller.
  21741. + * @param rem_wakeup - indicates whether resume is initiated by Device or Host.
  21742. + * @param reset - indicates whether resume is initiated by Reset.
  21743. + */
  21744. +int dwc_otg_device_hibernation_restore(dwc_otg_core_if_t * core_if,
  21745. + int rem_wakeup, int reset)
  21746. +{
  21747. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  21748. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  21749. + dctl_data_t dctl = {.d32 = 0 };
  21750. +
  21751. + int timeout = 2000;
  21752. +
  21753. + if (!core_if->hibernation_suspend) {
  21754. + DWC_PRINTF("Already exited from Hibernation\n");
  21755. + return 1;
  21756. + }
  21757. +
  21758. + DWC_DEBUGPL(DBG_PCD, "%s called\n", __FUNCTION__);
  21759. + /* Switch-on voltage to the core */
  21760. + gpwrdn.b.pwrdnswtch = 1;
  21761. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  21762. + dwc_udelay(10);
  21763. +
  21764. + /* Reset core */
  21765. + gpwrdn.d32 = 0;
  21766. + gpwrdn.b.pwrdnrstn = 1;
  21767. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  21768. + dwc_udelay(10);
  21769. +
  21770. + /* Assert Restore signal */
  21771. + gpwrdn.d32 = 0;
  21772. + gpwrdn.b.restore = 1;
  21773. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  21774. + dwc_udelay(10);
  21775. +
  21776. + /* Disable power clamps */
  21777. + gpwrdn.d32 = 0;
  21778. + gpwrdn.b.pwrdnclmp = 1;
  21779. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  21780. +
  21781. + if (rem_wakeup) {
  21782. + dwc_udelay(70);
  21783. + }
  21784. +
  21785. + /* Deassert Reset core */
  21786. + gpwrdn.d32 = 0;
  21787. + gpwrdn.b.pwrdnrstn = 1;
  21788. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  21789. + dwc_udelay(10);
  21790. +
  21791. + /* Disable PMU interrupt */
  21792. + gpwrdn.d32 = 0;
  21793. + gpwrdn.b.pmuintsel = 1;
  21794. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  21795. +
  21796. + /* Mask interrupts from gpwrdn */
  21797. + gpwrdn.d32 = 0;
  21798. + gpwrdn.b.connect_det_msk = 1;
  21799. + gpwrdn.b.srp_det_msk = 1;
  21800. + gpwrdn.b.disconn_det_msk = 1;
  21801. + gpwrdn.b.rst_det_msk = 1;
  21802. + gpwrdn.b.lnstchng_msk = 1;
  21803. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  21804. +
  21805. + /* Indicates that we are going out from hibernation */
  21806. + core_if->hibernation_suspend = 0;
  21807. +
  21808. + /*
  21809. + * Set Restore Essential Regs bit in PCGCCTL register, restore_mode = 1
  21810. + * indicates restore from remote_wakeup
  21811. + */
  21812. + restore_essential_regs(core_if, rem_wakeup, 0);
  21813. +
  21814. + /*
  21815. + * Wait a little for seeing new value of variable hibernation_suspend if
  21816. + * Restore done interrupt received before polling
  21817. + */
  21818. + dwc_udelay(10);
  21819. +
  21820. + if (core_if->hibernation_suspend == 0) {
  21821. + /*
  21822. + * Wait For Restore_done Interrupt. This mechanism of polling the
  21823. + * interrupt is introduced to avoid any possible race conditions
  21824. + */
  21825. + do {
  21826. + gintsts_data_t gintsts;
  21827. + gintsts.d32 =
  21828. + DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  21829. + if (gintsts.b.restoredone) {
  21830. + gintsts.d32 = 0;
  21831. + gintsts.b.restoredone = 1;
  21832. + DWC_WRITE_REG32(&core_if->core_global_regs->
  21833. + gintsts, gintsts.d32);
  21834. + DWC_PRINTF("Restore Done Interrupt seen\n");
  21835. + break;
  21836. + }
  21837. + dwc_udelay(10);
  21838. + } while (--timeout);
  21839. + if (!timeout) {
  21840. + DWC_PRINTF("Restore Done interrupt wasn't generated here\n");
  21841. + }
  21842. + }
  21843. + /* Clear all pending interupts */
  21844. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  21845. +
  21846. + /* De-assert Restore */
  21847. + gpwrdn.d32 = 0;
  21848. + gpwrdn.b.restore = 1;
  21849. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  21850. + dwc_udelay(10);
  21851. +
  21852. + if (!rem_wakeup) {
  21853. + pcgcctl.d32 = 0;
  21854. + pcgcctl.b.rstpdwnmodule = 1;
  21855. + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
  21856. + }
  21857. +
  21858. + /* Restore GUSBCFG and DCFG */
  21859. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg,
  21860. + core_if->gr_backup->gusbcfg_local);
  21861. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg,
  21862. + core_if->dr_backup->dcfg);
  21863. +
  21864. + /* De-assert Wakeup Logic */
  21865. + gpwrdn.d32 = 0;
  21866. + gpwrdn.b.pmuactv = 1;
  21867. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  21868. + dwc_udelay(10);
  21869. +
  21870. + if (!rem_wakeup) {
  21871. + /* Set Device programming done bit */
  21872. + dctl.b.pwronprgdone = 1;
  21873. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
  21874. + } else {
  21875. + /* Start Remote Wakeup Signaling */
  21876. + dctl.d32 = core_if->dr_backup->dctl;
  21877. + dctl.b.rmtwkupsig = 1;
  21878. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32);
  21879. + }
  21880. +
  21881. + dwc_mdelay(2);
  21882. + /* Clear all pending interupts */
  21883. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  21884. +
  21885. + /* Restore global registers */
  21886. + dwc_otg_restore_global_regs(core_if);
  21887. + /* Restore device global registers */
  21888. + dwc_otg_restore_dev_regs(core_if, rem_wakeup);
  21889. +
  21890. + if (rem_wakeup) {
  21891. + dwc_mdelay(7);
  21892. + dctl.d32 = 0;
  21893. + dctl.b.rmtwkupsig = 1;
  21894. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32, 0);
  21895. + }
  21896. +
  21897. + core_if->hibernation_suspend = 0;
  21898. + /* The core will be in ON STATE */
  21899. + core_if->lx_state = DWC_OTG_L0;
  21900. + DWC_PRINTF("Hibernation recovery completes here\n");
  21901. +
  21902. + return 1;
  21903. +}
  21904. +
  21905. +/*
  21906. + * The restore operation is modified to support Synopsys Emulated Powerdown and
  21907. + * Hibernation. This function is for exiting from Host mode hibernation by
  21908. + * Host Initiated Resume/Reset and Device Initiated Remote-Wakeup.
  21909. + * @param core_if Programming view of DWC_otg controller.
  21910. + * @param rem_wakeup - indicates whether resume is initiated by Device or Host.
  21911. + * @param reset - indicates whether resume is initiated by Reset.
  21912. + */
  21913. +int dwc_otg_host_hibernation_restore(dwc_otg_core_if_t * core_if,
  21914. + int rem_wakeup, int reset)
  21915. +{
  21916. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  21917. + hprt0_data_t hprt0 = {.d32 = 0 };
  21918. +
  21919. + int timeout = 2000;
  21920. +
  21921. + DWC_DEBUGPL(DBG_HCD, "%s called\n", __FUNCTION__);
  21922. + /* Switch-on voltage to the core */
  21923. + gpwrdn.b.pwrdnswtch = 1;
  21924. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  21925. + dwc_udelay(10);
  21926. +
  21927. + /* Reset core */
  21928. + gpwrdn.d32 = 0;
  21929. + gpwrdn.b.pwrdnrstn = 1;
  21930. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  21931. + dwc_udelay(10);
  21932. +
  21933. + /* Assert Restore signal */
  21934. + gpwrdn.d32 = 0;
  21935. + gpwrdn.b.restore = 1;
  21936. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  21937. + dwc_udelay(10);
  21938. +
  21939. + /* Disable power clamps */
  21940. + gpwrdn.d32 = 0;
  21941. + gpwrdn.b.pwrdnclmp = 1;
  21942. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  21943. +
  21944. + if (!rem_wakeup) {
  21945. + dwc_udelay(50);
  21946. + }
  21947. +
  21948. + /* Deassert Reset core */
  21949. + gpwrdn.d32 = 0;
  21950. + gpwrdn.b.pwrdnrstn = 1;
  21951. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  21952. + dwc_udelay(10);
  21953. +
  21954. + /* Disable PMU interrupt */
  21955. + gpwrdn.d32 = 0;
  21956. + gpwrdn.b.pmuintsel = 1;
  21957. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  21958. +
  21959. + gpwrdn.d32 = 0;
  21960. + gpwrdn.b.connect_det_msk = 1;
  21961. + gpwrdn.b.srp_det_msk = 1;
  21962. + gpwrdn.b.disconn_det_msk = 1;
  21963. + gpwrdn.b.rst_det_msk = 1;
  21964. + gpwrdn.b.lnstchng_msk = 1;
  21965. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  21966. +
  21967. + /* Indicates that we are going out from hibernation */
  21968. + core_if->hibernation_suspend = 0;
  21969. +
  21970. + /* Set Restore Essential Regs bit in PCGCCTL register */
  21971. + restore_essential_regs(core_if, rem_wakeup, 1);
  21972. +
  21973. + /* Wait a little for seeing new value of variable hibernation_suspend if
  21974. + * Restore done interrupt received before polling */
  21975. + dwc_udelay(10);
  21976. +
  21977. + if (core_if->hibernation_suspend == 0) {
  21978. + /* Wait For Restore_done Interrupt. This mechanism of polling the
  21979. + * interrupt is introduced to avoid any possible race conditions
  21980. + */
  21981. + do {
  21982. + gintsts_data_t gintsts;
  21983. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  21984. + if (gintsts.b.restoredone) {
  21985. + gintsts.d32 = 0;
  21986. + gintsts.b.restoredone = 1;
  21987. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  21988. + DWC_DEBUGPL(DBG_HCD,"Restore Done Interrupt seen\n");
  21989. + break;
  21990. + }
  21991. + dwc_udelay(10);
  21992. + } while (--timeout);
  21993. + if (!timeout) {
  21994. + DWC_WARN("Restore Done interrupt wasn't generated\n");
  21995. + }
  21996. + }
  21997. +
  21998. + /* Set the flag's value to 0 again after receiving restore done interrupt */
  21999. + core_if->hibernation_suspend = 0;
  22000. +
  22001. + /* This step is not described in functional spec but if not wait for this
  22002. + * delay, mismatch interrupts occurred because just after restore core is
  22003. + * in Device mode(gintsts.curmode == 0) */
  22004. + dwc_mdelay(100);
  22005. +
  22006. + /* Clear all pending interrupts */
  22007. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  22008. +
  22009. + /* De-assert Restore */
  22010. + gpwrdn.d32 = 0;
  22011. + gpwrdn.b.restore = 1;
  22012. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  22013. + dwc_udelay(10);
  22014. +
  22015. + /* Restore GUSBCFG and HCFG */
  22016. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg,
  22017. + core_if->gr_backup->gusbcfg_local);
  22018. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hcfg,
  22019. + core_if->hr_backup->hcfg_local);
  22020. +
  22021. + /* De-assert Wakeup Logic */
  22022. + gpwrdn.d32 = 0;
  22023. + gpwrdn.b.pmuactv = 1;
  22024. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  22025. + dwc_udelay(10);
  22026. +
  22027. + /* Start the Resume operation by programming HPRT0 */
  22028. + hprt0.d32 = core_if->hr_backup->hprt0_local;
  22029. + hprt0.b.prtpwr = 1;
  22030. + hprt0.b.prtena = 0;
  22031. + hprt0.b.prtsusp = 0;
  22032. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  22033. +
  22034. + DWC_PRINTF("Resume Starts Now\n");
  22035. + if (!reset) { // Indicates it is Resume Operation
  22036. + hprt0.d32 = core_if->hr_backup->hprt0_local;
  22037. + hprt0.b.prtres = 1;
  22038. + hprt0.b.prtpwr = 1;
  22039. + hprt0.b.prtena = 0;
  22040. + hprt0.b.prtsusp = 0;
  22041. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  22042. +
  22043. + if (!rem_wakeup)
  22044. + hprt0.b.prtres = 0;
  22045. + /* Wait for Resume time and then program HPRT again */
  22046. + dwc_mdelay(100);
  22047. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  22048. +
  22049. + } else { // Indicates it is Reset Operation
  22050. + hprt0.d32 = core_if->hr_backup->hprt0_local;
  22051. + hprt0.b.prtrst = 1;
  22052. + hprt0.b.prtpwr = 1;
  22053. + hprt0.b.prtena = 0;
  22054. + hprt0.b.prtsusp = 0;
  22055. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  22056. + /* Wait for Reset time and then program HPRT again */
  22057. + dwc_mdelay(60);
  22058. + hprt0.b.prtrst = 0;
  22059. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  22060. + }
  22061. + /* Clear all interrupt status */
  22062. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  22063. + hprt0.b.prtconndet = 1;
  22064. + hprt0.b.prtenchng = 1;
  22065. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  22066. +
  22067. + /* Clear all pending interupts */
  22068. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  22069. +
  22070. + /* Restore global registers */
  22071. + dwc_otg_restore_global_regs(core_if);
  22072. + /* Restore host global registers */
  22073. + dwc_otg_restore_host_regs(core_if, reset);
  22074. +
  22075. + /* The core will be in ON STATE */
  22076. + core_if->lx_state = DWC_OTG_L0;
  22077. + DWC_PRINTF("Hibernation recovery is complete here\n");
  22078. + return 0;
  22079. +}
  22080. +
  22081. +/** Saves some register values into system memory. */
  22082. +int dwc_otg_save_global_regs(dwc_otg_core_if_t * core_if)
  22083. +{
  22084. + struct dwc_otg_global_regs_backup *gr;
  22085. + int i;
  22086. +
  22087. + gr = core_if->gr_backup;
  22088. + if (!gr) {
  22089. + gr = DWC_ALLOC(sizeof(*gr));
  22090. + if (!gr) {
  22091. + return -DWC_E_NO_MEMORY;
  22092. + }
  22093. + core_if->gr_backup = gr;
  22094. + }
  22095. +
  22096. + gr->gotgctl_local = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  22097. + gr->gintmsk_local = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
  22098. + gr->gahbcfg_local = DWC_READ_REG32(&core_if->core_global_regs->gahbcfg);
  22099. + gr->gusbcfg_local = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  22100. + gr->grxfsiz_local = DWC_READ_REG32(&core_if->core_global_regs->grxfsiz);
  22101. + gr->gnptxfsiz_local = DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz);
  22102. + gr->hptxfsiz_local = DWC_READ_REG32(&core_if->core_global_regs->hptxfsiz);
  22103. +#ifdef CONFIG_USB_DWC_OTG_LPM
  22104. + gr->glpmcfg_local = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  22105. +#endif
  22106. + gr->gi2cctl_local = DWC_READ_REG32(&core_if->core_global_regs->gi2cctl);
  22107. + gr->pcgcctl_local = DWC_READ_REG32(core_if->pcgcctl);
  22108. + gr->gdfifocfg_local =
  22109. + DWC_READ_REG32(&core_if->core_global_regs->gdfifocfg);
  22110. + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  22111. + gr->dtxfsiz_local[i] =
  22112. + DWC_READ_REG32(&(core_if->core_global_regs->dtxfsiz[i]));
  22113. + }
  22114. +
  22115. + DWC_DEBUGPL(DBG_ANY, "===========Backing Global registers==========\n");
  22116. + DWC_DEBUGPL(DBG_ANY, "Backed up gotgctl = %08x\n", gr->gotgctl_local);
  22117. + DWC_DEBUGPL(DBG_ANY, "Backed up gintmsk = %08x\n", gr->gintmsk_local);
  22118. + DWC_DEBUGPL(DBG_ANY, "Backed up gahbcfg = %08x\n", gr->gahbcfg_local);
  22119. + DWC_DEBUGPL(DBG_ANY, "Backed up gusbcfg = %08x\n", gr->gusbcfg_local);
  22120. + DWC_DEBUGPL(DBG_ANY, "Backed up grxfsiz = %08x\n", gr->grxfsiz_local);
  22121. + DWC_DEBUGPL(DBG_ANY, "Backed up gnptxfsiz = %08x\n",
  22122. + gr->gnptxfsiz_local);
  22123. + DWC_DEBUGPL(DBG_ANY, "Backed up hptxfsiz = %08x\n",
  22124. + gr->hptxfsiz_local);
  22125. +#ifdef CONFIG_USB_DWC_OTG_LPM
  22126. + DWC_DEBUGPL(DBG_ANY, "Backed up glpmcfg = %08x\n", gr->glpmcfg_local);
  22127. +#endif
  22128. + DWC_DEBUGPL(DBG_ANY, "Backed up gi2cctl = %08x\n", gr->gi2cctl_local);
  22129. + DWC_DEBUGPL(DBG_ANY, "Backed up pcgcctl = %08x\n", gr->pcgcctl_local);
  22130. + DWC_DEBUGPL(DBG_ANY,"Backed up gdfifocfg = %08x\n",gr->gdfifocfg_local);
  22131. +
  22132. + return 0;
  22133. +}
  22134. +
  22135. +/** Saves GINTMSK register before setting the msk bits. */
  22136. +int dwc_otg_save_gintmsk_reg(dwc_otg_core_if_t * core_if)
  22137. +{
  22138. + struct dwc_otg_global_regs_backup *gr;
  22139. +
  22140. + gr = core_if->gr_backup;
  22141. + if (!gr) {
  22142. + gr = DWC_ALLOC(sizeof(*gr));
  22143. + if (!gr) {
  22144. + return -DWC_E_NO_MEMORY;
  22145. + }
  22146. + core_if->gr_backup = gr;
  22147. + }
  22148. +
  22149. + gr->gintmsk_local = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
  22150. +
  22151. + DWC_DEBUGPL(DBG_ANY,"=============Backing GINTMSK registers============\n");
  22152. + DWC_DEBUGPL(DBG_ANY, "Backed up gintmsk = %08x\n", gr->gintmsk_local);
  22153. +
  22154. + return 0;
  22155. +}
  22156. +
  22157. +int dwc_otg_save_dev_regs(dwc_otg_core_if_t * core_if)
  22158. +{
  22159. + struct dwc_otg_dev_regs_backup *dr;
  22160. + int i;
  22161. +
  22162. + dr = core_if->dr_backup;
  22163. + if (!dr) {
  22164. + dr = DWC_ALLOC(sizeof(*dr));
  22165. + if (!dr) {
  22166. + return -DWC_E_NO_MEMORY;
  22167. + }
  22168. + core_if->dr_backup = dr;
  22169. + }
  22170. +
  22171. + dr->dcfg = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  22172. + dr->dctl = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dctl);
  22173. + dr->daintmsk =
  22174. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->daintmsk);
  22175. + dr->diepmsk =
  22176. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->diepmsk);
  22177. + dr->doepmsk =
  22178. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->doepmsk);
  22179. +
  22180. + for (i = 0; i < core_if->dev_if->num_in_eps; ++i) {
  22181. + dr->diepctl[i] =
  22182. + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[i]->diepctl);
  22183. + dr->dieptsiz[i] =
  22184. + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[i]->dieptsiz);
  22185. + dr->diepdma[i] =
  22186. + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[i]->diepdma);
  22187. + }
  22188. +
  22189. + DWC_DEBUGPL(DBG_ANY,
  22190. + "=============Backing Host registers==============\n");
  22191. + DWC_DEBUGPL(DBG_ANY, "Backed up dcfg = %08x\n", dr->dcfg);
  22192. + DWC_DEBUGPL(DBG_ANY, "Backed up dctl = %08x\n", dr->dctl);
  22193. + DWC_DEBUGPL(DBG_ANY, "Backed up daintmsk = %08x\n",
  22194. + dr->daintmsk);
  22195. + DWC_DEBUGPL(DBG_ANY, "Backed up diepmsk = %08x\n", dr->diepmsk);
  22196. + DWC_DEBUGPL(DBG_ANY, "Backed up doepmsk = %08x\n", dr->doepmsk);
  22197. + for (i = 0; i < core_if->dev_if->num_in_eps; ++i) {
  22198. + DWC_DEBUGPL(DBG_ANY, "Backed up diepctl[%d] = %08x\n", i,
  22199. + dr->diepctl[i]);
  22200. + DWC_DEBUGPL(DBG_ANY, "Backed up dieptsiz[%d] = %08x\n",
  22201. + i, dr->dieptsiz[i]);
  22202. + DWC_DEBUGPL(DBG_ANY, "Backed up diepdma[%d] = %08x\n", i,
  22203. + dr->diepdma[i]);
  22204. + }
  22205. +
  22206. + return 0;
  22207. +}
  22208. +
  22209. +int dwc_otg_save_host_regs(dwc_otg_core_if_t * core_if)
  22210. +{
  22211. + struct dwc_otg_host_regs_backup *hr;
  22212. + int i;
  22213. +
  22214. + hr = core_if->hr_backup;
  22215. + if (!hr) {
  22216. + hr = DWC_ALLOC(sizeof(*hr));
  22217. + if (!hr) {
  22218. + return -DWC_E_NO_MEMORY;
  22219. + }
  22220. + core_if->hr_backup = hr;
  22221. + }
  22222. +
  22223. + hr->hcfg_local =
  22224. + DWC_READ_REG32(&core_if->host_if->host_global_regs->hcfg);
  22225. + hr->haintmsk_local =
  22226. + DWC_READ_REG32(&core_if->host_if->host_global_regs->haintmsk);
  22227. + for (i = 0; i < dwc_otg_get_param_host_channels(core_if); ++i) {
  22228. + hr->hcintmsk_local[i] =
  22229. + DWC_READ_REG32(&core_if->host_if->hc_regs[i]->hcintmsk);
  22230. + }
  22231. + hr->hprt0_local = DWC_READ_REG32(core_if->host_if->hprt0);
  22232. + hr->hfir_local =
  22233. + DWC_READ_REG32(&core_if->host_if->host_global_regs->hfir);
  22234. +
  22235. + DWC_DEBUGPL(DBG_ANY,
  22236. + "=============Backing Host registers===============\n");
  22237. + DWC_DEBUGPL(DBG_ANY, "Backed up hcfg = %08x\n",
  22238. + hr->hcfg_local);
  22239. + DWC_DEBUGPL(DBG_ANY, "Backed up haintmsk = %08x\n", hr->haintmsk_local);
  22240. + for (i = 0; i < dwc_otg_get_param_host_channels(core_if); ++i) {
  22241. + DWC_DEBUGPL(DBG_ANY, "Backed up hcintmsk[%02d]=%08x\n", i,
  22242. + hr->hcintmsk_local[i]);
  22243. + }
  22244. + DWC_DEBUGPL(DBG_ANY, "Backed up hprt0 = %08x\n",
  22245. + hr->hprt0_local);
  22246. + DWC_DEBUGPL(DBG_ANY, "Backed up hfir = %08x\n",
  22247. + hr->hfir_local);
  22248. +
  22249. + return 0;
  22250. +}
  22251. +
  22252. +int dwc_otg_restore_global_regs(dwc_otg_core_if_t *core_if)
  22253. +{
  22254. + struct dwc_otg_global_regs_backup *gr;
  22255. + int i;
  22256. +
  22257. + gr = core_if->gr_backup;
  22258. + if (!gr) {
  22259. + return -DWC_E_INVALID;
  22260. + }
  22261. +
  22262. + DWC_WRITE_REG32(&core_if->core_global_regs->gotgctl, gr->gotgctl_local);
  22263. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, gr->gintmsk_local);
  22264. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, gr->gusbcfg_local);
  22265. + DWC_WRITE_REG32(&core_if->core_global_regs->gahbcfg, gr->gahbcfg_local);
  22266. + DWC_WRITE_REG32(&core_if->core_global_regs->grxfsiz, gr->grxfsiz_local);
  22267. + DWC_WRITE_REG32(&core_if->core_global_regs->gnptxfsiz,
  22268. + gr->gnptxfsiz_local);
  22269. + DWC_WRITE_REG32(&core_if->core_global_regs->hptxfsiz,
  22270. + gr->hptxfsiz_local);
  22271. + DWC_WRITE_REG32(&core_if->core_global_regs->gdfifocfg,
  22272. + gr->gdfifocfg_local);
  22273. + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  22274. + DWC_WRITE_REG32(&core_if->core_global_regs->dtxfsiz[i],
  22275. + gr->dtxfsiz_local[i]);
  22276. + }
  22277. +
  22278. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  22279. + DWC_WRITE_REG32(core_if->host_if->hprt0, 0x0000100A);
  22280. + DWC_WRITE_REG32(&core_if->core_global_regs->gahbcfg,
  22281. + (gr->gahbcfg_local));
  22282. + return 0;
  22283. +}
  22284. +
  22285. +int dwc_otg_restore_dev_regs(dwc_otg_core_if_t * core_if, int rem_wakeup)
  22286. +{
  22287. + struct dwc_otg_dev_regs_backup *dr;
  22288. + int i;
  22289. +
  22290. + dr = core_if->dr_backup;
  22291. +
  22292. + if (!dr) {
  22293. + return -DWC_E_INVALID;
  22294. + }
  22295. +
  22296. + if (!rem_wakeup) {
  22297. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl,
  22298. + dr->dctl);
  22299. + }
  22300. +
  22301. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->daintmsk, dr->daintmsk);
  22302. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->diepmsk, dr->diepmsk);
  22303. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->doepmsk, dr->doepmsk);
  22304. +
  22305. + for (i = 0; i < core_if->dev_if->num_in_eps; ++i) {
  22306. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[i]->dieptsiz, dr->dieptsiz[i]);
  22307. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[i]->diepdma, dr->diepdma[i]);
  22308. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[i]->diepctl, dr->diepctl[i]);
  22309. + }
  22310. +
  22311. + return 0;
  22312. +}
  22313. +
  22314. +int dwc_otg_restore_host_regs(dwc_otg_core_if_t * core_if, int reset)
  22315. +{
  22316. + struct dwc_otg_host_regs_backup *hr;
  22317. + int i;
  22318. + hr = core_if->hr_backup;
  22319. +
  22320. + if (!hr) {
  22321. + return -DWC_E_INVALID;
  22322. + }
  22323. +
  22324. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hcfg, hr->hcfg_local);
  22325. + //if (!reset)
  22326. + //{
  22327. + // DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hfir, hr->hfir_local);
  22328. + //}
  22329. +
  22330. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->haintmsk,
  22331. + hr->haintmsk_local);
  22332. + for (i = 0; i < dwc_otg_get_param_host_channels(core_if); ++i) {
  22333. + DWC_WRITE_REG32(&core_if->host_if->hc_regs[i]->hcintmsk,
  22334. + hr->hcintmsk_local[i]);
  22335. + }
  22336. +
  22337. + return 0;
  22338. +}
  22339. +
  22340. +int restore_lpm_i2c_regs(dwc_otg_core_if_t * core_if)
  22341. +{
  22342. + struct dwc_otg_global_regs_backup *gr;
  22343. +
  22344. + gr = core_if->gr_backup;
  22345. +
  22346. + /* Restore values for LPM and I2C */
  22347. +#ifdef CONFIG_USB_DWC_OTG_LPM
  22348. + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg, gr->glpmcfg_local);
  22349. +#endif
  22350. + DWC_WRITE_REG32(&core_if->core_global_regs->gi2cctl, gr->gi2cctl_local);
  22351. +
  22352. + return 0;
  22353. +}
  22354. +
  22355. +int restore_essential_regs(dwc_otg_core_if_t * core_if, int rmode, int is_host)
  22356. +{
  22357. + struct dwc_otg_global_regs_backup *gr;
  22358. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  22359. + gahbcfg_data_t gahbcfg = {.d32 = 0 };
  22360. + gusbcfg_data_t gusbcfg = {.d32 = 0 };
  22361. + gintmsk_data_t gintmsk = {.d32 = 0 };
  22362. +
  22363. + /* Restore LPM and I2C registers */
  22364. + restore_lpm_i2c_regs(core_if);
  22365. +
  22366. + /* Set PCGCCTL to 0 */
  22367. + DWC_WRITE_REG32(core_if->pcgcctl, 0x00000000);
  22368. +
  22369. + gr = core_if->gr_backup;
  22370. + /* Load restore values for [31:14] bits */
  22371. + DWC_WRITE_REG32(core_if->pcgcctl,
  22372. + ((gr->pcgcctl_local & 0xffffc000) | 0x00020000));
  22373. +
  22374. + /* Umnask global Interrupt in GAHBCFG and restore it */
  22375. + gahbcfg.d32 = gr->gahbcfg_local;
  22376. + gahbcfg.b.glblintrmsk = 1;
  22377. + DWC_WRITE_REG32(&core_if->core_global_regs->gahbcfg, gahbcfg.d32);
  22378. +
  22379. + /* Clear all pending interupts */
  22380. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  22381. +
  22382. + /* Unmask restore done interrupt */
  22383. + gintmsk.b.restoredone = 1;
  22384. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, gintmsk.d32);
  22385. +
  22386. + /* Restore GUSBCFG and HCFG/DCFG */
  22387. + gusbcfg.d32 = core_if->gr_backup->gusbcfg_local;
  22388. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, gusbcfg.d32);
  22389. +
  22390. + if (is_host) {
  22391. + hcfg_data_t hcfg = {.d32 = 0 };
  22392. + hcfg.d32 = core_if->hr_backup->hcfg_local;
  22393. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hcfg,
  22394. + hcfg.d32);
  22395. +
  22396. + /* Load restore values for [31:14] bits */
  22397. + pcgcctl.d32 = gr->pcgcctl_local & 0xffffc000;
  22398. + pcgcctl.d32 = gr->pcgcctl_local | 0x00020000;
  22399. +
  22400. + if (rmode)
  22401. + pcgcctl.b.restoremode = 1;
  22402. + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
  22403. + dwc_udelay(10);
  22404. +
  22405. + /* Load restore values for [31:14] bits and set EssRegRestored bit */
  22406. + pcgcctl.d32 = gr->pcgcctl_local | 0xffffc000;
  22407. + pcgcctl.d32 = gr->pcgcctl_local & 0xffffc000;
  22408. + pcgcctl.b.ess_reg_restored = 1;
  22409. + if (rmode)
  22410. + pcgcctl.b.restoremode = 1;
  22411. + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
  22412. + } else {
  22413. + dcfg_data_t dcfg = {.d32 = 0 };
  22414. + dcfg.d32 = core_if->dr_backup->dcfg;
  22415. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg, dcfg.d32);
  22416. +
  22417. + /* Load restore values for [31:14] bits */
  22418. + pcgcctl.d32 = gr->pcgcctl_local & 0xffffc000;
  22419. + pcgcctl.d32 = gr->pcgcctl_local | 0x00020000;
  22420. + if (!rmode) {
  22421. + pcgcctl.d32 |= 0x208;
  22422. + }
  22423. + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
  22424. + dwc_udelay(10);
  22425. +
  22426. + /* Load restore values for [31:14] bits */
  22427. + pcgcctl.d32 = gr->pcgcctl_local & 0xffffc000;
  22428. + pcgcctl.d32 = gr->pcgcctl_local | 0x00020000;
  22429. + pcgcctl.b.ess_reg_restored = 1;
  22430. + if (!rmode)
  22431. + pcgcctl.d32 |= 0x208;
  22432. + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
  22433. + }
  22434. +
  22435. + return 0;
  22436. +}
  22437. +
  22438. +/**
  22439. + * Initializes the FSLSPClkSel field of the HCFG register depending on the PHY
  22440. + * type.
  22441. + */
  22442. +static void init_fslspclksel(dwc_otg_core_if_t * core_if)
  22443. +{
  22444. + uint32_t val;
  22445. + hcfg_data_t hcfg;
  22446. +
  22447. + if (((core_if->hwcfg2.b.hs_phy_type == 2) &&
  22448. + (core_if->hwcfg2.b.fs_phy_type == 1) &&
  22449. + (core_if->core_params->ulpi_fs_ls)) ||
  22450. + (core_if->core_params->phy_type == DWC_PHY_TYPE_PARAM_FS)) {
  22451. + /* Full speed PHY */
  22452. + val = DWC_HCFG_48_MHZ;
  22453. + } else {
  22454. + /* High speed PHY running at full speed or high speed */
  22455. + val = DWC_HCFG_30_60_MHZ;
  22456. + }
  22457. +
  22458. + DWC_DEBUGPL(DBG_CIL, "Initializing HCFG.FSLSPClkSel to 0x%1x\n", val);
  22459. + hcfg.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->hcfg);
  22460. + hcfg.b.fslspclksel = val;
  22461. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hcfg, hcfg.d32);
  22462. +}
  22463. +
  22464. +/**
  22465. + * Initializes the DevSpd field of the DCFG register depending on the PHY type
  22466. + * and the enumeration speed of the device.
  22467. + */
  22468. +static void init_devspd(dwc_otg_core_if_t * core_if)
  22469. +{
  22470. + uint32_t val;
  22471. + dcfg_data_t dcfg;
  22472. +
  22473. + if (((core_if->hwcfg2.b.hs_phy_type == 2) &&
  22474. + (core_if->hwcfg2.b.fs_phy_type == 1) &&
  22475. + (core_if->core_params->ulpi_fs_ls)) ||
  22476. + (core_if->core_params->phy_type == DWC_PHY_TYPE_PARAM_FS)) {
  22477. + /* Full speed PHY */
  22478. + val = 0x3;
  22479. + } else if (core_if->core_params->speed == DWC_SPEED_PARAM_FULL) {
  22480. + /* High speed PHY running at full speed */
  22481. + val = 0x1;
  22482. + } else {
  22483. + /* High speed PHY running at high speed */
  22484. + val = 0x0;
  22485. + }
  22486. +
  22487. + DWC_DEBUGPL(DBG_CIL, "Initializing DCFG.DevSpd to 0x%1x\n", val);
  22488. +
  22489. + dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  22490. + dcfg.b.devspd = val;
  22491. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg, dcfg.d32);
  22492. +}
  22493. +
  22494. +/**
  22495. + * This function calculates the number of IN EPS
  22496. + * using GHWCFG1 and GHWCFG2 registers values
  22497. + *
  22498. + * @param core_if Programming view of the DWC_otg controller
  22499. + */
  22500. +static uint32_t calc_num_in_eps(dwc_otg_core_if_t * core_if)
  22501. +{
  22502. + uint32_t num_in_eps = 0;
  22503. + uint32_t num_eps = core_if->hwcfg2.b.num_dev_ep;
  22504. + uint32_t hwcfg1 = core_if->hwcfg1.d32 >> 3;
  22505. + uint32_t num_tx_fifos = core_if->hwcfg4.b.num_in_eps;
  22506. + int i;
  22507. +
  22508. + for (i = 0; i < num_eps; ++i) {
  22509. + if (!(hwcfg1 & 0x1))
  22510. + num_in_eps++;
  22511. +
  22512. + hwcfg1 >>= 2;
  22513. + }
  22514. +
  22515. + if (core_if->hwcfg4.b.ded_fifo_en) {
  22516. + num_in_eps =
  22517. + (num_in_eps > num_tx_fifos) ? num_tx_fifos : num_in_eps;
  22518. + }
  22519. +
  22520. + return num_in_eps;
  22521. +}
  22522. +
  22523. +/**
  22524. + * This function calculates the number of OUT EPS
  22525. + * using GHWCFG1 and GHWCFG2 registers values
  22526. + *
  22527. + * @param core_if Programming view of the DWC_otg controller
  22528. + */
  22529. +static uint32_t calc_num_out_eps(dwc_otg_core_if_t * core_if)
  22530. +{
  22531. + uint32_t num_out_eps = 0;
  22532. + uint32_t num_eps = core_if->hwcfg2.b.num_dev_ep;
  22533. + uint32_t hwcfg1 = core_if->hwcfg1.d32 >> 2;
  22534. + int i;
  22535. +
  22536. + for (i = 0; i < num_eps; ++i) {
  22537. + if (!(hwcfg1 & 0x1))
  22538. + num_out_eps++;
  22539. +
  22540. + hwcfg1 >>= 2;
  22541. + }
  22542. + return num_out_eps;
  22543. +}
  22544. +
  22545. +/**
  22546. + * This function initializes the DWC_otg controller registers and
  22547. + * prepares the core for device mode or host mode operation.
  22548. + *
  22549. + * @param core_if Programming view of the DWC_otg controller
  22550. + *
  22551. + */
  22552. +void dwc_otg_core_init(dwc_otg_core_if_t * core_if)
  22553. +{
  22554. + int i = 0;
  22555. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  22556. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  22557. + gahbcfg_data_t ahbcfg = {.d32 = 0 };
  22558. + gusbcfg_data_t usbcfg = {.d32 = 0 };
  22559. + gi2cctl_data_t i2cctl = {.d32 = 0 };
  22560. +
  22561. + DWC_DEBUGPL(DBG_CILV, "dwc_otg_core_init(%p) regs at %p\n",
  22562. + core_if, global_regs);
  22563. +
  22564. + /* Common Initialization */
  22565. + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
  22566. +
  22567. + /* Program the ULPI External VBUS bit if needed */
  22568. + usbcfg.b.ulpi_ext_vbus_drv =
  22569. + (core_if->core_params->phy_ulpi_ext_vbus ==
  22570. + DWC_PHY_ULPI_EXTERNAL_VBUS) ? 1 : 0;
  22571. +
  22572. + /* Set external TS Dline pulsing */
  22573. + usbcfg.b.term_sel_dl_pulse =
  22574. + (core_if->core_params->ts_dline == 1) ? 1 : 0;
  22575. + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
  22576. +
  22577. + /* Reset the Controller */
  22578. + dwc_otg_core_reset(core_if);
  22579. +
  22580. + core_if->adp_enable = core_if->core_params->adp_supp_enable;
  22581. + core_if->power_down = core_if->core_params->power_down;
  22582. + core_if->otg_sts = 0;
  22583. +
  22584. + /* Initialize parameters from Hardware configuration registers. */
  22585. + dev_if->num_in_eps = calc_num_in_eps(core_if);
  22586. + dev_if->num_out_eps = calc_num_out_eps(core_if);
  22587. +
  22588. + DWC_DEBUGPL(DBG_CIL, "num_dev_perio_in_ep=%d\n",
  22589. + core_if->hwcfg4.b.num_dev_perio_in_ep);
  22590. +
  22591. + for (i = 0; i < core_if->hwcfg4.b.num_dev_perio_in_ep; i++) {
  22592. + dev_if->perio_tx_fifo_size[i] =
  22593. + DWC_READ_REG32(&global_regs->dtxfsiz[i]) >> 16;
  22594. + DWC_DEBUGPL(DBG_CIL, "Periodic Tx FIFO SZ #%d=0x%0x\n",
  22595. + i, dev_if->perio_tx_fifo_size[i]);
  22596. + }
  22597. +
  22598. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  22599. + dev_if->tx_fifo_size[i] =
  22600. + DWC_READ_REG32(&global_regs->dtxfsiz[i]) >> 16;
  22601. + DWC_DEBUGPL(DBG_CIL, "Tx FIFO SZ #%d=0x%0x\n",
  22602. + i, dev_if->tx_fifo_size[i]);
  22603. + }
  22604. +
  22605. + core_if->total_fifo_size = core_if->hwcfg3.b.dfifo_depth;
  22606. + core_if->rx_fifo_size = DWC_READ_REG32(&global_regs->grxfsiz);
  22607. + core_if->nperio_tx_fifo_size =
  22608. + DWC_READ_REG32(&global_regs->gnptxfsiz) >> 16;
  22609. +
  22610. + DWC_DEBUGPL(DBG_CIL, "Total FIFO SZ=%d\n", core_if->total_fifo_size);
  22611. + DWC_DEBUGPL(DBG_CIL, "Rx FIFO SZ=%d\n", core_if->rx_fifo_size);
  22612. + DWC_DEBUGPL(DBG_CIL, "NP Tx FIFO SZ=%d\n",
  22613. + core_if->nperio_tx_fifo_size);
  22614. +
  22615. + /* This programming sequence needs to happen in FS mode before any other
  22616. + * programming occurs */
  22617. + if ((core_if->core_params->speed == DWC_SPEED_PARAM_FULL) &&
  22618. + (core_if->core_params->phy_type == DWC_PHY_TYPE_PARAM_FS)) {
  22619. + /* If FS mode with FS PHY */
  22620. +
  22621. + /* core_init() is now called on every switch so only call the
  22622. + * following for the first time through. */
  22623. + if (!core_if->phy_init_done) {
  22624. + core_if->phy_init_done = 1;
  22625. + DWC_DEBUGPL(DBG_CIL, "FS_PHY detected\n");
  22626. + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
  22627. + usbcfg.b.physel = 1;
  22628. + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
  22629. +
  22630. + /* Reset after a PHY select */
  22631. + dwc_otg_core_reset(core_if);
  22632. + }
  22633. +
  22634. + /* Program DCFG.DevSpd or HCFG.FSLSPclkSel to 48Mhz in FS. Also
  22635. + * do this on HNP Dev/Host mode switches (done in dev_init and
  22636. + * host_init). */
  22637. + if (dwc_otg_is_host_mode(core_if)) {
  22638. + init_fslspclksel(core_if);
  22639. + } else {
  22640. + init_devspd(core_if);
  22641. + }
  22642. +
  22643. + if (core_if->core_params->i2c_enable) {
  22644. + DWC_DEBUGPL(DBG_CIL, "FS_PHY Enabling I2c\n");
  22645. + /* Program GUSBCFG.OtgUtmifsSel to I2C */
  22646. + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
  22647. + usbcfg.b.otgutmifssel = 1;
  22648. + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
  22649. +
  22650. + /* Program GI2CCTL.I2CEn */
  22651. + i2cctl.d32 = DWC_READ_REG32(&global_regs->gi2cctl);
  22652. + i2cctl.b.i2cdevaddr = 1;
  22653. + i2cctl.b.i2cen = 0;
  22654. + DWC_WRITE_REG32(&global_regs->gi2cctl, i2cctl.d32);
  22655. + i2cctl.b.i2cen = 1;
  22656. + DWC_WRITE_REG32(&global_regs->gi2cctl, i2cctl.d32);
  22657. + }
  22658. +
  22659. + } /* endif speed == DWC_SPEED_PARAM_FULL */
  22660. + else {
  22661. + /* High speed PHY. */
  22662. + if (!core_if->phy_init_done) {
  22663. + core_if->phy_init_done = 1;
  22664. + /* HS PHY parameters. These parameters are preserved
  22665. + * during soft reset so only program the first time. Do
  22666. + * a soft reset immediately after setting phyif. */
  22667. +
  22668. + if (core_if->core_params->phy_type == 2) {
  22669. + /* ULPI interface */
  22670. + usbcfg.b.ulpi_utmi_sel = 1;
  22671. + usbcfg.b.phyif = 0;
  22672. + usbcfg.b.ddrsel =
  22673. + core_if->core_params->phy_ulpi_ddr;
  22674. + } else if (core_if->core_params->phy_type == 1) {
  22675. + /* UTMI+ interface */
  22676. + usbcfg.b.ulpi_utmi_sel = 0;
  22677. + if (core_if->core_params->phy_utmi_width == 16) {
  22678. + usbcfg.b.phyif = 1;
  22679. +
  22680. + } else {
  22681. + usbcfg.b.phyif = 0;
  22682. + }
  22683. + } else {
  22684. + DWC_ERROR("FS PHY TYPE\n");
  22685. + }
  22686. + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
  22687. + /* Reset after setting the PHY parameters */
  22688. + dwc_otg_core_reset(core_if);
  22689. + }
  22690. + }
  22691. +
  22692. + if ((core_if->hwcfg2.b.hs_phy_type == 2) &&
  22693. + (core_if->hwcfg2.b.fs_phy_type == 1) &&
  22694. + (core_if->core_params->ulpi_fs_ls)) {
  22695. + DWC_DEBUGPL(DBG_CIL, "Setting ULPI FSLS\n");
  22696. + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
  22697. + usbcfg.b.ulpi_fsls = 1;
  22698. + usbcfg.b.ulpi_clk_sus_m = 1;
  22699. + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
  22700. + } else {
  22701. + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
  22702. + usbcfg.b.ulpi_fsls = 0;
  22703. + usbcfg.b.ulpi_clk_sus_m = 0;
  22704. + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
  22705. + }
  22706. +
  22707. + /* Program the GAHBCFG Register. */
  22708. + switch (core_if->hwcfg2.b.architecture) {
  22709. +
  22710. + case DWC_SLAVE_ONLY_ARCH:
  22711. + DWC_DEBUGPL(DBG_CIL, "Slave Only Mode\n");
  22712. + ahbcfg.b.nptxfemplvl_txfemplvl =
  22713. + DWC_GAHBCFG_TXFEMPTYLVL_HALFEMPTY;
  22714. + ahbcfg.b.ptxfemplvl = DWC_GAHBCFG_TXFEMPTYLVL_HALFEMPTY;
  22715. + core_if->dma_enable = 0;
  22716. + core_if->dma_desc_enable = 0;
  22717. + break;
  22718. +
  22719. + case DWC_EXT_DMA_ARCH:
  22720. + DWC_DEBUGPL(DBG_CIL, "External DMA Mode\n");
  22721. + {
  22722. + uint8_t brst_sz = core_if->core_params->dma_burst_size;
  22723. + ahbcfg.b.hburstlen = 0;
  22724. + while (brst_sz > 1) {
  22725. + ahbcfg.b.hburstlen++;
  22726. + brst_sz >>= 1;
  22727. + }
  22728. + }
  22729. + core_if->dma_enable = (core_if->core_params->dma_enable != 0);
  22730. + core_if->dma_desc_enable =
  22731. + (core_if->core_params->dma_desc_enable != 0);
  22732. + break;
  22733. +
  22734. + case DWC_INT_DMA_ARCH:
  22735. + DWC_DEBUGPL(DBG_CIL, "Internal DMA Mode\n");
  22736. + /* Old value was DWC_GAHBCFG_INT_DMA_BURST_INCR - done for
  22737. + Host mode ISOC in issue fix - vahrama */
  22738. + /* Broadcom had altered to (1<<3)|(0<<0) - WRESP=1, max 4 beats */
  22739. + ahbcfg.b.hburstlen = (1<<3)|(0<<0);//DWC_GAHBCFG_INT_DMA_BURST_INCR4;
  22740. + core_if->dma_enable = (core_if->core_params->dma_enable != 0);
  22741. + core_if->dma_desc_enable =
  22742. + (core_if->core_params->dma_desc_enable != 0);
  22743. + break;
  22744. +
  22745. + }
  22746. + if (core_if->dma_enable) {
  22747. + if (core_if->dma_desc_enable) {
  22748. + DWC_PRINTF("Using Descriptor DMA mode\n");
  22749. + } else {
  22750. + DWC_PRINTF("Using Buffer DMA mode\n");
  22751. +
  22752. + }
  22753. + } else {
  22754. + DWC_PRINTF("Using Slave mode\n");
  22755. + core_if->dma_desc_enable = 0;
  22756. + }
  22757. +
  22758. + if (core_if->core_params->ahb_single) {
  22759. + ahbcfg.b.ahbsingle = 1;
  22760. + }
  22761. +
  22762. + ahbcfg.b.dmaenable = core_if->dma_enable;
  22763. + DWC_WRITE_REG32(&global_regs->gahbcfg, ahbcfg.d32);
  22764. +
  22765. + core_if->en_multiple_tx_fifo = core_if->hwcfg4.b.ded_fifo_en;
  22766. +
  22767. + core_if->pti_enh_enable = core_if->core_params->pti_enable != 0;
  22768. + core_if->multiproc_int_enable = core_if->core_params->mpi_enable;
  22769. + DWC_PRINTF("Periodic Transfer Interrupt Enhancement - %s\n",
  22770. + ((core_if->pti_enh_enable) ? "enabled" : "disabled"));
  22771. + DWC_PRINTF("Multiprocessor Interrupt Enhancement - %s\n",
  22772. + ((core_if->multiproc_int_enable) ? "enabled" : "disabled"));
  22773. +
  22774. + /*
  22775. + * Program the GUSBCFG register.
  22776. + */
  22777. + usbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
  22778. +
  22779. + switch (core_if->hwcfg2.b.op_mode) {
  22780. + case DWC_MODE_HNP_SRP_CAPABLE:
  22781. + usbcfg.b.hnpcap = (core_if->core_params->otg_cap ==
  22782. + DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE);
  22783. + usbcfg.b.srpcap = (core_if->core_params->otg_cap !=
  22784. + DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE);
  22785. + break;
  22786. +
  22787. + case DWC_MODE_SRP_ONLY_CAPABLE:
  22788. + usbcfg.b.hnpcap = 0;
  22789. + usbcfg.b.srpcap = (core_if->core_params->otg_cap !=
  22790. + DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE);
  22791. + break;
  22792. +
  22793. + case DWC_MODE_NO_HNP_SRP_CAPABLE:
  22794. + usbcfg.b.hnpcap = 0;
  22795. + usbcfg.b.srpcap = 0;
  22796. + break;
  22797. +
  22798. + case DWC_MODE_SRP_CAPABLE_DEVICE:
  22799. + usbcfg.b.hnpcap = 0;
  22800. + usbcfg.b.srpcap = (core_if->core_params->otg_cap !=
  22801. + DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE);
  22802. + break;
  22803. +
  22804. + case DWC_MODE_NO_SRP_CAPABLE_DEVICE:
  22805. + usbcfg.b.hnpcap = 0;
  22806. + usbcfg.b.srpcap = 0;
  22807. + break;
  22808. +
  22809. + case DWC_MODE_SRP_CAPABLE_HOST:
  22810. + usbcfg.b.hnpcap = 0;
  22811. + usbcfg.b.srpcap = (core_if->core_params->otg_cap !=
  22812. + DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE);
  22813. + break;
  22814. +
  22815. + case DWC_MODE_NO_SRP_CAPABLE_HOST:
  22816. + usbcfg.b.hnpcap = 0;
  22817. + usbcfg.b.srpcap = 0;
  22818. + break;
  22819. + }
  22820. +
  22821. + DWC_WRITE_REG32(&global_regs->gusbcfg, usbcfg.d32);
  22822. +
  22823. +#ifdef CONFIG_USB_DWC_OTG_LPM
  22824. + if (core_if->core_params->lpm_enable) {
  22825. + glpmcfg_data_t lpmcfg = {.d32 = 0 };
  22826. +
  22827. + /* To enable LPM support set lpm_cap_en bit */
  22828. + lpmcfg.b.lpm_cap_en = 1;
  22829. +
  22830. + /* Make AppL1Res ACK */
  22831. + lpmcfg.b.appl_resp = 1;
  22832. +
  22833. + /* Retry 3 times */
  22834. + lpmcfg.b.retry_count = 3;
  22835. +
  22836. + DWC_MODIFY_REG32(&core_if->core_global_regs->glpmcfg,
  22837. + 0, lpmcfg.d32);
  22838. +
  22839. + }
  22840. +#endif
  22841. + if (core_if->core_params->ic_usb_cap) {
  22842. + gusbcfg_data_t gusbcfg = {.d32 = 0 };
  22843. + gusbcfg.b.ic_usb_cap = 1;
  22844. + DWC_MODIFY_REG32(&core_if->core_global_regs->gusbcfg,
  22845. + 0, gusbcfg.d32);
  22846. + }
  22847. + {
  22848. + gotgctl_data_t gotgctl = {.d32 = 0 };
  22849. + gotgctl.b.otgver = core_if->core_params->otg_ver;
  22850. + DWC_MODIFY_REG32(&core_if->core_global_regs->gotgctl, 0,
  22851. + gotgctl.d32);
  22852. + /* Set OTG version supported */
  22853. + core_if->otg_ver = core_if->core_params->otg_ver;
  22854. + DWC_PRINTF("OTG VER PARAM: %d, OTG VER FLAG: %d\n",
  22855. + core_if->core_params->otg_ver, core_if->otg_ver);
  22856. + }
  22857. +
  22858. +
  22859. + /* Enable common interrupts */
  22860. + dwc_otg_enable_common_interrupts(core_if);
  22861. +
  22862. + /* Do device or host intialization based on mode during PCD
  22863. + * and HCD initialization */
  22864. + if (dwc_otg_is_host_mode(core_if)) {
  22865. + DWC_DEBUGPL(DBG_ANY, "Host Mode\n");
  22866. + core_if->op_state = A_HOST;
  22867. + } else {
  22868. + DWC_DEBUGPL(DBG_ANY, "Device Mode\n");
  22869. + core_if->op_state = B_PERIPHERAL;
  22870. +#ifdef DWC_DEVICE_ONLY
  22871. + dwc_otg_core_dev_init(core_if);
  22872. +#endif
  22873. + }
  22874. +}
  22875. +
  22876. +/**
  22877. + * This function enables the Device mode interrupts.
  22878. + *
  22879. + * @param core_if Programming view of DWC_otg controller
  22880. + */
  22881. +void dwc_otg_enable_device_interrupts(dwc_otg_core_if_t * core_if)
  22882. +{
  22883. + gintmsk_data_t intr_mask = {.d32 = 0 };
  22884. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  22885. +
  22886. + DWC_DEBUGPL(DBG_CIL, "%s()\n", __func__);
  22887. +
  22888. + /* Disable all interrupts. */
  22889. + DWC_WRITE_REG32(&global_regs->gintmsk, 0);
  22890. +
  22891. + /* Clear any pending interrupts */
  22892. + DWC_WRITE_REG32(&global_regs->gintsts, 0xFFFFFFFF);
  22893. +
  22894. + /* Enable the common interrupts */
  22895. + dwc_otg_enable_common_interrupts(core_if);
  22896. +
  22897. + /* Enable interrupts */
  22898. + intr_mask.b.usbreset = 1;
  22899. + intr_mask.b.enumdone = 1;
  22900. + /* Disable Disconnect interrupt in Device mode */
  22901. + intr_mask.b.disconnect = 0;
  22902. +
  22903. + if (!core_if->multiproc_int_enable) {
  22904. + intr_mask.b.inepintr = 1;
  22905. + intr_mask.b.outepintr = 1;
  22906. + }
  22907. +
  22908. + intr_mask.b.erlysuspend = 1;
  22909. +
  22910. + if (core_if->en_multiple_tx_fifo == 0) {
  22911. + intr_mask.b.epmismatch = 1;
  22912. + }
  22913. +
  22914. + //intr_mask.b.incomplisoout = 1;
  22915. + intr_mask.b.incomplisoin = 1;
  22916. +
  22917. +/* Enable the ignore frame number for ISOC xfers - MAS */
  22918. +/* Disable to support high bandwith ISOC transfers - manukz */
  22919. +#if 0
  22920. +#ifdef DWC_UTE_PER_IO
  22921. + if (core_if->dma_enable) {
  22922. + if (core_if->dma_desc_enable) {
  22923. + dctl_data_t dctl1 = {.d32 = 0 };
  22924. + dctl1.b.ifrmnum = 1;
  22925. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  22926. + dctl, 0, dctl1.d32);
  22927. + DWC_DEBUG("----Enabled Ignore frame number (0x%08x)",
  22928. + DWC_READ_REG32(&core_if->dev_if->
  22929. + dev_global_regs->dctl));
  22930. + }
  22931. + }
  22932. +#endif
  22933. +#endif
  22934. +#ifdef DWC_EN_ISOC
  22935. + if (core_if->dma_enable) {
  22936. + if (core_if->dma_desc_enable == 0) {
  22937. + if (core_if->pti_enh_enable) {
  22938. + dctl_data_t dctl = {.d32 = 0 };
  22939. + dctl.b.ifrmnum = 1;
  22940. + DWC_MODIFY_REG32(&core_if->
  22941. + dev_if->dev_global_regs->dctl,
  22942. + 0, dctl.d32);
  22943. + } else {
  22944. + intr_mask.b.incomplisoin = 1;
  22945. + intr_mask.b.incomplisoout = 1;
  22946. + }
  22947. + }
  22948. + } else {
  22949. + intr_mask.b.incomplisoin = 1;
  22950. + intr_mask.b.incomplisoout = 1;
  22951. + }
  22952. +#endif /* DWC_EN_ISOC */
  22953. +
  22954. + /** @todo NGS: Should this be a module parameter? */
  22955. +#ifdef USE_PERIODIC_EP
  22956. + intr_mask.b.isooutdrop = 1;
  22957. + intr_mask.b.eopframe = 1;
  22958. + intr_mask.b.incomplisoin = 1;
  22959. + intr_mask.b.incomplisoout = 1;
  22960. +#endif
  22961. +
  22962. + DWC_MODIFY_REG32(&global_regs->gintmsk, intr_mask.d32, intr_mask.d32);
  22963. +
  22964. + DWC_DEBUGPL(DBG_CIL, "%s() gintmsk=%0x\n", __func__,
  22965. + DWC_READ_REG32(&global_regs->gintmsk));
  22966. +}
  22967. +
  22968. +/**
  22969. + * This function initializes the DWC_otg controller registers for
  22970. + * device mode.
  22971. + *
  22972. + * @param core_if Programming view of DWC_otg controller
  22973. + *
  22974. + */
  22975. +void dwc_otg_core_dev_init(dwc_otg_core_if_t * core_if)
  22976. +{
  22977. + int i;
  22978. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  22979. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  22980. + dwc_otg_core_params_t *params = core_if->core_params;
  22981. + dcfg_data_t dcfg = {.d32 = 0 };
  22982. + depctl_data_t diepctl = {.d32 = 0 };
  22983. + grstctl_t resetctl = {.d32 = 0 };
  22984. + uint32_t rx_fifo_size;
  22985. + fifosize_data_t nptxfifosize;
  22986. + fifosize_data_t txfifosize;
  22987. + dthrctl_data_t dthrctl;
  22988. + fifosize_data_t ptxfifosize;
  22989. + uint16_t rxfsiz, nptxfsiz;
  22990. + gdfifocfg_data_t gdfifocfg = {.d32 = 0 };
  22991. + hwcfg3_data_t hwcfg3 = {.d32 = 0 };
  22992. +
  22993. + /* Restart the Phy Clock */
  22994. + DWC_WRITE_REG32(core_if->pcgcctl, 0);
  22995. +
  22996. + /* Device configuration register */
  22997. + init_devspd(core_if);
  22998. + dcfg.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dcfg);
  22999. + dcfg.b.descdma = (core_if->dma_desc_enable) ? 1 : 0;
  23000. + dcfg.b.perfrint = DWC_DCFG_FRAME_INTERVAL_80;
  23001. + /* Enable Device OUT NAK in case of DDMA mode*/
  23002. + if (core_if->core_params->dev_out_nak) {
  23003. + dcfg.b.endevoutnak = 1;
  23004. + }
  23005. +
  23006. + if (core_if->core_params->cont_on_bna) {
  23007. + dctl_data_t dctl = {.d32 = 0 };
  23008. + dctl.b.encontonbna = 1;
  23009. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, 0, dctl.d32);
  23010. + }
  23011. +
  23012. +
  23013. + DWC_WRITE_REG32(&dev_if->dev_global_regs->dcfg, dcfg.d32);
  23014. +
  23015. + /* Configure data FIFO sizes */
  23016. + if (core_if->hwcfg2.b.dynamic_fifo && params->enable_dynamic_fifo) {
  23017. + DWC_DEBUGPL(DBG_CIL, "Total FIFO Size=%d\n",
  23018. + core_if->total_fifo_size);
  23019. + DWC_DEBUGPL(DBG_CIL, "Rx FIFO Size=%d\n",
  23020. + params->dev_rx_fifo_size);
  23021. + DWC_DEBUGPL(DBG_CIL, "NP Tx FIFO Size=%d\n",
  23022. + params->dev_nperio_tx_fifo_size);
  23023. +
  23024. + /* Rx FIFO */
  23025. + DWC_DEBUGPL(DBG_CIL, "initial grxfsiz=%08x\n",
  23026. + DWC_READ_REG32(&global_regs->grxfsiz));
  23027. +
  23028. +#ifdef DWC_UTE_CFI
  23029. + core_if->pwron_rxfsiz = DWC_READ_REG32(&global_regs->grxfsiz);
  23030. + core_if->init_rxfsiz = params->dev_rx_fifo_size;
  23031. +#endif
  23032. + rx_fifo_size = params->dev_rx_fifo_size;
  23033. + DWC_WRITE_REG32(&global_regs->grxfsiz, rx_fifo_size);
  23034. +
  23035. + DWC_DEBUGPL(DBG_CIL, "new grxfsiz=%08x\n",
  23036. + DWC_READ_REG32(&global_regs->grxfsiz));
  23037. +
  23038. + /** Set Periodic Tx FIFO Mask all bits 0 */
  23039. + core_if->p_tx_msk = 0;
  23040. +
  23041. + /** Set Tx FIFO Mask all bits 0 */
  23042. + core_if->tx_msk = 0;
  23043. +
  23044. + if (core_if->en_multiple_tx_fifo == 0) {
  23045. + /* Non-periodic Tx FIFO */
  23046. + DWC_DEBUGPL(DBG_CIL, "initial gnptxfsiz=%08x\n",
  23047. + DWC_READ_REG32(&global_regs->gnptxfsiz));
  23048. +
  23049. + nptxfifosize.b.depth = params->dev_nperio_tx_fifo_size;
  23050. + nptxfifosize.b.startaddr = params->dev_rx_fifo_size;
  23051. +
  23052. + DWC_WRITE_REG32(&global_regs->gnptxfsiz,
  23053. + nptxfifosize.d32);
  23054. +
  23055. + DWC_DEBUGPL(DBG_CIL, "new gnptxfsiz=%08x\n",
  23056. + DWC_READ_REG32(&global_regs->gnptxfsiz));
  23057. +
  23058. + /**@todo NGS: Fix Periodic FIFO Sizing! */
  23059. + /*
  23060. + * Periodic Tx FIFOs These FIFOs are numbered from 1 to 15.
  23061. + * Indexes of the FIFO size module parameters in the
  23062. + * dev_perio_tx_fifo_size array and the FIFO size registers in
  23063. + * the dptxfsiz array run from 0 to 14.
  23064. + */
  23065. + /** @todo Finish debug of this */
  23066. + ptxfifosize.b.startaddr =
  23067. + nptxfifosize.b.startaddr + nptxfifosize.b.depth;
  23068. + for (i = 0; i < core_if->hwcfg4.b.num_dev_perio_in_ep; i++) {
  23069. + ptxfifosize.b.depth =
  23070. + params->dev_perio_tx_fifo_size[i];
  23071. + DWC_DEBUGPL(DBG_CIL,
  23072. + "initial dtxfsiz[%d]=%08x\n", i,
  23073. + DWC_READ_REG32(&global_regs->dtxfsiz
  23074. + [i]));
  23075. + DWC_WRITE_REG32(&global_regs->dtxfsiz[i],
  23076. + ptxfifosize.d32);
  23077. + DWC_DEBUGPL(DBG_CIL, "new dtxfsiz[%d]=%08x\n",
  23078. + i,
  23079. + DWC_READ_REG32(&global_regs->dtxfsiz
  23080. + [i]));
  23081. + ptxfifosize.b.startaddr += ptxfifosize.b.depth;
  23082. + }
  23083. + } else {
  23084. + /*
  23085. + * Tx FIFOs These FIFOs are numbered from 1 to 15.
  23086. + * Indexes of the FIFO size module parameters in the
  23087. + * dev_tx_fifo_size array and the FIFO size registers in
  23088. + * the dtxfsiz array run from 0 to 14.
  23089. + */
  23090. +
  23091. + /* Non-periodic Tx FIFO */
  23092. + DWC_DEBUGPL(DBG_CIL, "initial gnptxfsiz=%08x\n",
  23093. + DWC_READ_REG32(&global_regs->gnptxfsiz));
  23094. +
  23095. +#ifdef DWC_UTE_CFI
  23096. + core_if->pwron_gnptxfsiz =
  23097. + (DWC_READ_REG32(&global_regs->gnptxfsiz) >> 16);
  23098. + core_if->init_gnptxfsiz =
  23099. + params->dev_nperio_tx_fifo_size;
  23100. +#endif
  23101. + nptxfifosize.b.depth = params->dev_nperio_tx_fifo_size;
  23102. + nptxfifosize.b.startaddr = params->dev_rx_fifo_size;
  23103. +
  23104. + DWC_WRITE_REG32(&global_regs->gnptxfsiz,
  23105. + nptxfifosize.d32);
  23106. +
  23107. + DWC_DEBUGPL(DBG_CIL, "new gnptxfsiz=%08x\n",
  23108. + DWC_READ_REG32(&global_regs->gnptxfsiz));
  23109. +
  23110. + txfifosize.b.startaddr =
  23111. + nptxfifosize.b.startaddr + nptxfifosize.b.depth;
  23112. +
  23113. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; i++) {
  23114. +
  23115. + txfifosize.b.depth =
  23116. + params->dev_tx_fifo_size[i];
  23117. +
  23118. + DWC_DEBUGPL(DBG_CIL,
  23119. + "initial dtxfsiz[%d]=%08x\n",
  23120. + i,
  23121. + DWC_READ_REG32(&global_regs->dtxfsiz
  23122. + [i]));
  23123. +
  23124. +#ifdef DWC_UTE_CFI
  23125. + core_if->pwron_txfsiz[i] =
  23126. + (DWC_READ_REG32
  23127. + (&global_regs->dtxfsiz[i]) >> 16);
  23128. + core_if->init_txfsiz[i] =
  23129. + params->dev_tx_fifo_size[i];
  23130. +#endif
  23131. + DWC_WRITE_REG32(&global_regs->dtxfsiz[i],
  23132. + txfifosize.d32);
  23133. +
  23134. + DWC_DEBUGPL(DBG_CIL,
  23135. + "new dtxfsiz[%d]=%08x\n",
  23136. + i,
  23137. + DWC_READ_REG32(&global_regs->dtxfsiz
  23138. + [i]));
  23139. +
  23140. + txfifosize.b.startaddr += txfifosize.b.depth;
  23141. + }
  23142. + if (core_if->snpsid <= OTG_CORE_REV_2_94a) {
  23143. + /* Calculating DFIFOCFG for Device mode to include RxFIFO and NPTXFIFO */
  23144. + gdfifocfg.d32 = DWC_READ_REG32(&global_regs->gdfifocfg);
  23145. + hwcfg3.d32 = DWC_READ_REG32(&global_regs->ghwcfg3);
  23146. + gdfifocfg.b.gdfifocfg = (DWC_READ_REG32(&global_regs->ghwcfg3) >> 16);
  23147. + DWC_WRITE_REG32(&global_regs->gdfifocfg, gdfifocfg.d32);
  23148. + rxfsiz = (DWC_READ_REG32(&global_regs->grxfsiz) & 0x0000ffff);
  23149. + nptxfsiz = (DWC_READ_REG32(&global_regs->gnptxfsiz) >> 16);
  23150. + gdfifocfg.b.epinfobase = rxfsiz + nptxfsiz;
  23151. + DWC_WRITE_REG32(&global_regs->gdfifocfg, gdfifocfg.d32);
  23152. + }
  23153. + }
  23154. +
  23155. + /* Flush the FIFOs */
  23156. + dwc_otg_flush_tx_fifo(core_if, 0x10); /* all Tx FIFOs */
  23157. + dwc_otg_flush_rx_fifo(core_if);
  23158. +
  23159. + /* Flush the Learning Queue. */
  23160. + resetctl.b.intknqflsh = 1;
  23161. + DWC_WRITE_REG32(&core_if->core_global_regs->grstctl, resetctl.d32);
  23162. +
  23163. + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable) {
  23164. + core_if->start_predict = 0;
  23165. + for (i = 0; i<= core_if->dev_if->num_in_eps; ++i) {
  23166. + core_if->nextep_seq[i] = 0xff; // 0xff - EP not active
  23167. + }
  23168. + core_if->nextep_seq[0] = 0;
  23169. + core_if->first_in_nextep_seq = 0;
  23170. + diepctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[0]->diepctl);
  23171. + diepctl.b.nextep = 0;
  23172. + DWC_WRITE_REG32(&dev_if->in_ep_regs[0]->diepctl, diepctl.d32);
  23173. +
  23174. + /* Update IN Endpoint Mismatch Count by active IN NP EP count + 1 */
  23175. + dcfg.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dcfg);
  23176. + dcfg.b.epmscnt = 2;
  23177. + DWC_WRITE_REG32(&dev_if->dev_global_regs->dcfg, dcfg.d32);
  23178. +
  23179. + DWC_DEBUGPL(DBG_CILV,"%s first_in_nextep_seq= %2d; nextep_seq[]:\n",
  23180. + __func__, core_if->first_in_nextep_seq);
  23181. + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
  23182. + DWC_DEBUGPL(DBG_CILV, "%2d ", core_if->nextep_seq[i]);
  23183. + }
  23184. + DWC_DEBUGPL(DBG_CILV,"\n");
  23185. + }
  23186. +
  23187. + /* Clear all pending Device Interrupts */
  23188. + /** @todo - if the condition needed to be checked
  23189. + * or in any case all pending interrutps should be cleared?
  23190. + */
  23191. + if (core_if->multiproc_int_enable) {
  23192. + for (i = 0; i < core_if->dev_if->num_in_eps; ++i) {
  23193. + DWC_WRITE_REG32(&dev_if->
  23194. + dev_global_regs->diepeachintmsk[i], 0);
  23195. + }
  23196. + }
  23197. +
  23198. + for (i = 0; i < core_if->dev_if->num_out_eps; ++i) {
  23199. + DWC_WRITE_REG32(&dev_if->
  23200. + dev_global_regs->doepeachintmsk[i], 0);
  23201. + }
  23202. +
  23203. + DWC_WRITE_REG32(&dev_if->dev_global_regs->deachint, 0xFFFFFFFF);
  23204. + DWC_WRITE_REG32(&dev_if->dev_global_regs->deachintmsk, 0);
  23205. + } else {
  23206. + DWC_WRITE_REG32(&dev_if->dev_global_regs->diepmsk, 0);
  23207. + DWC_WRITE_REG32(&dev_if->dev_global_regs->doepmsk, 0);
  23208. + DWC_WRITE_REG32(&dev_if->dev_global_regs->daint, 0xFFFFFFFF);
  23209. + DWC_WRITE_REG32(&dev_if->dev_global_regs->daintmsk, 0);
  23210. + }
  23211. +
  23212. + for (i = 0; i <= dev_if->num_in_eps; i++) {
  23213. + depctl_data_t depctl;
  23214. + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  23215. + if (depctl.b.epena) {
  23216. + depctl.d32 = 0;
  23217. + depctl.b.epdis = 1;
  23218. + depctl.b.snak = 1;
  23219. + } else {
  23220. + depctl.d32 = 0;
  23221. + }
  23222. +
  23223. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepctl, depctl.d32);
  23224. +
  23225. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->dieptsiz, 0);
  23226. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepdma, 0);
  23227. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepint, 0xFF);
  23228. + }
  23229. +
  23230. + for (i = 0; i <= dev_if->num_out_eps; i++) {
  23231. + depctl_data_t depctl;
  23232. + depctl.d32 = DWC_READ_REG32(&dev_if->out_ep_regs[i]->doepctl);
  23233. + if (depctl.b.epena) {
  23234. + dctl_data_t dctl = {.d32 = 0 };
  23235. + gintmsk_data_t gintsts = {.d32 = 0 };
  23236. + doepint_data_t doepint = {.d32 = 0 };
  23237. + dctl.b.sgoutnak = 1;
  23238. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
  23239. + do {
  23240. + dwc_udelay(10);
  23241. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  23242. + } while (!gintsts.b.goutnakeff);
  23243. + gintsts.d32 = 0;
  23244. + gintsts.b.goutnakeff = 1;
  23245. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  23246. +
  23247. + depctl.d32 = 0;
  23248. + depctl.b.epdis = 1;
  23249. + depctl.b.snak = 1;
  23250. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[i]->doepctl, depctl.d32);
  23251. + do {
  23252. + dwc_udelay(10);
  23253. + doepint.d32 = DWC_READ_REG32(&core_if->dev_if->
  23254. + out_ep_regs[i]->doepint);
  23255. + } while (!doepint.b.epdisabled);
  23256. +
  23257. + doepint.b.epdisabled = 1;
  23258. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[i]->doepint, doepint.d32);
  23259. +
  23260. + dctl.d32 = 0;
  23261. + dctl.b.cgoutnak = 1;
  23262. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
  23263. + } else {
  23264. + depctl.d32 = 0;
  23265. + }
  23266. +
  23267. + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doepctl, depctl.d32);
  23268. +
  23269. + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doeptsiz, 0);
  23270. + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doepdma, 0);
  23271. + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doepint, 0xFF);
  23272. + }
  23273. +
  23274. + if (core_if->en_multiple_tx_fifo && core_if->dma_enable) {
  23275. + dev_if->non_iso_tx_thr_en = params->thr_ctl & 0x1;
  23276. + dev_if->iso_tx_thr_en = (params->thr_ctl >> 1) & 0x1;
  23277. + dev_if->rx_thr_en = (params->thr_ctl >> 2) & 0x1;
  23278. +
  23279. + dev_if->rx_thr_length = params->rx_thr_length;
  23280. + dev_if->tx_thr_length = params->tx_thr_length;
  23281. +
  23282. + dev_if->setup_desc_index = 0;
  23283. +
  23284. + dthrctl.d32 = 0;
  23285. + dthrctl.b.non_iso_thr_en = dev_if->non_iso_tx_thr_en;
  23286. + dthrctl.b.iso_thr_en = dev_if->iso_tx_thr_en;
  23287. + dthrctl.b.tx_thr_len = dev_if->tx_thr_length;
  23288. + dthrctl.b.rx_thr_en = dev_if->rx_thr_en;
  23289. + dthrctl.b.rx_thr_len = dev_if->rx_thr_length;
  23290. + dthrctl.b.ahb_thr_ratio = params->ahb_thr_ratio;
  23291. +
  23292. + DWC_WRITE_REG32(&dev_if->dev_global_regs->dtknqr3_dthrctl,
  23293. + dthrctl.d32);
  23294. +
  23295. + DWC_DEBUGPL(DBG_CIL,
  23296. + "Non ISO Tx Thr - %d\nISO Tx Thr - %d\nRx Thr - %d\nTx Thr Len - %d\nRx Thr Len - %d\n",
  23297. + dthrctl.b.non_iso_thr_en, dthrctl.b.iso_thr_en,
  23298. + dthrctl.b.rx_thr_en, dthrctl.b.tx_thr_len,
  23299. + dthrctl.b.rx_thr_len);
  23300. +
  23301. + }
  23302. +
  23303. + dwc_otg_enable_device_interrupts(core_if);
  23304. +
  23305. + {
  23306. + diepmsk_data_t msk = {.d32 = 0 };
  23307. + msk.b.txfifoundrn = 1;
  23308. + if (core_if->multiproc_int_enable) {
  23309. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->
  23310. + diepeachintmsk[0], msk.d32, msk.d32);
  23311. + } else {
  23312. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->diepmsk,
  23313. + msk.d32, msk.d32);
  23314. + }
  23315. + }
  23316. +
  23317. + if (core_if->multiproc_int_enable) {
  23318. + /* Set NAK on Babble */
  23319. + dctl_data_t dctl = {.d32 = 0 };
  23320. + dctl.b.nakonbble = 1;
  23321. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, 0, dctl.d32);
  23322. + }
  23323. +
  23324. + if (core_if->snpsid >= OTG_CORE_REV_2_94a) {
  23325. + dctl_data_t dctl = {.d32 = 0 };
  23326. + dctl.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dctl);
  23327. + dctl.b.sftdiscon = 0;
  23328. + DWC_WRITE_REG32(&dev_if->dev_global_regs->dctl, dctl.d32);
  23329. + }
  23330. +}
  23331. +
  23332. +/**
  23333. + * This function enables the Host mode interrupts.
  23334. + *
  23335. + * @param core_if Programming view of DWC_otg controller
  23336. + */
  23337. +void dwc_otg_enable_host_interrupts(dwc_otg_core_if_t * core_if)
  23338. +{
  23339. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  23340. + gintmsk_data_t intr_mask = {.d32 = 0 };
  23341. +
  23342. + DWC_DEBUGPL(DBG_CIL, "%s(%p)\n", __func__, core_if);
  23343. +
  23344. + /* Disable all interrupts. */
  23345. + DWC_WRITE_REG32(&global_regs->gintmsk, 0);
  23346. +
  23347. + /* Clear any pending interrupts. */
  23348. + DWC_WRITE_REG32(&global_regs->gintsts, 0xFFFFFFFF);
  23349. +
  23350. + /* Enable the common interrupts */
  23351. + dwc_otg_enable_common_interrupts(core_if);
  23352. +
  23353. + /*
  23354. + * Enable host mode interrupts without disturbing common
  23355. + * interrupts.
  23356. + */
  23357. +
  23358. + intr_mask.b.disconnect = 1;
  23359. + intr_mask.b.portintr = 1;
  23360. + intr_mask.b.hcintr = 1;
  23361. +
  23362. + DWC_MODIFY_REG32(&global_regs->gintmsk, intr_mask.d32, intr_mask.d32);
  23363. +}
  23364. +
  23365. +/**
  23366. + * This function disables the Host Mode interrupts.
  23367. + *
  23368. + * @param core_if Programming view of DWC_otg controller
  23369. + */
  23370. +void dwc_otg_disable_host_interrupts(dwc_otg_core_if_t * core_if)
  23371. +{
  23372. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  23373. + gintmsk_data_t intr_mask = {.d32 = 0 };
  23374. +
  23375. + DWC_DEBUGPL(DBG_CILV, "%s()\n", __func__);
  23376. +
  23377. + /*
  23378. + * Disable host mode interrupts without disturbing common
  23379. + * interrupts.
  23380. + */
  23381. + intr_mask.b.sofintr = 1;
  23382. + intr_mask.b.portintr = 1;
  23383. + intr_mask.b.hcintr = 1;
  23384. + intr_mask.b.ptxfempty = 1;
  23385. + intr_mask.b.nptxfempty = 1;
  23386. +
  23387. + DWC_MODIFY_REG32(&global_regs->gintmsk, intr_mask.d32, 0);
  23388. +}
  23389. +
  23390. +/**
  23391. + * This function initializes the DWC_otg controller registers for
  23392. + * host mode.
  23393. + *
  23394. + * This function flushes the Tx and Rx FIFOs and it flushes any entries in the
  23395. + * request queues. Host channels are reset to ensure that they are ready for
  23396. + * performing transfers.
  23397. + *
  23398. + * @param core_if Programming view of DWC_otg controller
  23399. + *
  23400. + */
  23401. +void dwc_otg_core_host_init(dwc_otg_core_if_t * core_if)
  23402. +{
  23403. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  23404. + dwc_otg_host_if_t *host_if = core_if->host_if;
  23405. + dwc_otg_core_params_t *params = core_if->core_params;
  23406. + hprt0_data_t hprt0 = {.d32 = 0 };
  23407. + fifosize_data_t nptxfifosize;
  23408. + fifosize_data_t ptxfifosize;
  23409. + uint16_t rxfsiz, nptxfsiz, hptxfsiz;
  23410. + gdfifocfg_data_t gdfifocfg = {.d32 = 0 };
  23411. + int i;
  23412. + hcchar_data_t hcchar;
  23413. + hcfg_data_t hcfg;
  23414. + hfir_data_t hfir;
  23415. + dwc_otg_hc_regs_t *hc_regs;
  23416. + int num_channels;
  23417. + gotgctl_data_t gotgctl = {.d32 = 0 };
  23418. +
  23419. + DWC_DEBUGPL(DBG_CILV, "%s(%p)\n", __func__, core_if);
  23420. +
  23421. + /* Restart the Phy Clock */
  23422. + DWC_WRITE_REG32(core_if->pcgcctl, 0);
  23423. +
  23424. + /* Initialize Host Configuration Register */
  23425. + init_fslspclksel(core_if);
  23426. + if (core_if->core_params->speed == DWC_SPEED_PARAM_FULL) {
  23427. + hcfg.d32 = DWC_READ_REG32(&host_if->host_global_regs->hcfg);
  23428. + hcfg.b.fslssupp = 1;
  23429. + DWC_WRITE_REG32(&host_if->host_global_regs->hcfg, hcfg.d32);
  23430. +
  23431. + }
  23432. +
  23433. + /* This bit allows dynamic reloading of the HFIR register
  23434. + * during runtime. This bit needs to be programmed during
  23435. + * initial configuration and its value must not be changed
  23436. + * during runtime.*/
  23437. + if (core_if->core_params->reload_ctl == 1) {
  23438. + hfir.d32 = DWC_READ_REG32(&host_if->host_global_regs->hfir);
  23439. + hfir.b.hfirrldctrl = 1;
  23440. + DWC_WRITE_REG32(&host_if->host_global_regs->hfir, hfir.d32);
  23441. + }
  23442. +
  23443. + if (core_if->core_params->dma_desc_enable) {
  23444. + uint8_t op_mode = core_if->hwcfg2.b.op_mode;
  23445. + if (!
  23446. + (core_if->hwcfg4.b.desc_dma
  23447. + && (core_if->snpsid >= OTG_CORE_REV_2_90a)
  23448. + && ((op_mode == DWC_HWCFG2_OP_MODE_HNP_SRP_CAPABLE_OTG)
  23449. + || (op_mode == DWC_HWCFG2_OP_MODE_SRP_ONLY_CAPABLE_OTG)
  23450. + || (op_mode ==
  23451. + DWC_HWCFG2_OP_MODE_NO_HNP_SRP_CAPABLE_OTG)
  23452. + || (op_mode == DWC_HWCFG2_OP_MODE_SRP_CAPABLE_HOST)
  23453. + || (op_mode ==
  23454. + DWC_HWCFG2_OP_MODE_NO_SRP_CAPABLE_HOST)))) {
  23455. +
  23456. + DWC_ERROR("Host can't operate in Descriptor DMA mode.\n"
  23457. + "Either core version is below 2.90a or "
  23458. + "GHWCFG2, GHWCFG4 registers' values do not allow Descriptor DMA in host mode.\n"
  23459. + "To run the driver in Buffer DMA host mode set dma_desc_enable "
  23460. + "module parameter to 0.\n");
  23461. + return;
  23462. + }
  23463. + hcfg.d32 = DWC_READ_REG32(&host_if->host_global_regs->hcfg);
  23464. + hcfg.b.descdma = 1;
  23465. + DWC_WRITE_REG32(&host_if->host_global_regs->hcfg, hcfg.d32);
  23466. + }
  23467. +
  23468. + /* Configure data FIFO sizes */
  23469. + if (core_if->hwcfg2.b.dynamic_fifo && params->enable_dynamic_fifo) {
  23470. + DWC_DEBUGPL(DBG_CIL, "Total FIFO Size=%d\n",
  23471. + core_if->total_fifo_size);
  23472. + DWC_DEBUGPL(DBG_CIL, "Rx FIFO Size=%d\n",
  23473. + params->host_rx_fifo_size);
  23474. + DWC_DEBUGPL(DBG_CIL, "NP Tx FIFO Size=%d\n",
  23475. + params->host_nperio_tx_fifo_size);
  23476. + DWC_DEBUGPL(DBG_CIL, "P Tx FIFO Size=%d\n",
  23477. + params->host_perio_tx_fifo_size);
  23478. +
  23479. + /* Rx FIFO */
  23480. + DWC_DEBUGPL(DBG_CIL, "initial grxfsiz=%08x\n",
  23481. + DWC_READ_REG32(&global_regs->grxfsiz));
  23482. + DWC_WRITE_REG32(&global_regs->grxfsiz,
  23483. + params->host_rx_fifo_size);
  23484. + DWC_DEBUGPL(DBG_CIL, "new grxfsiz=%08x\n",
  23485. + DWC_READ_REG32(&global_regs->grxfsiz));
  23486. +
  23487. + /* Non-periodic Tx FIFO */
  23488. + DWC_DEBUGPL(DBG_CIL, "initial gnptxfsiz=%08x\n",
  23489. + DWC_READ_REG32(&global_regs->gnptxfsiz));
  23490. + nptxfifosize.b.depth = params->host_nperio_tx_fifo_size;
  23491. + nptxfifosize.b.startaddr = params->host_rx_fifo_size;
  23492. + DWC_WRITE_REG32(&global_regs->gnptxfsiz, nptxfifosize.d32);
  23493. + DWC_DEBUGPL(DBG_CIL, "new gnptxfsiz=%08x\n",
  23494. + DWC_READ_REG32(&global_regs->gnptxfsiz));
  23495. +
  23496. + /* Periodic Tx FIFO */
  23497. + DWC_DEBUGPL(DBG_CIL, "initial hptxfsiz=%08x\n",
  23498. + DWC_READ_REG32(&global_regs->hptxfsiz));
  23499. + ptxfifosize.b.depth = params->host_perio_tx_fifo_size;
  23500. + ptxfifosize.b.startaddr =
  23501. + nptxfifosize.b.startaddr + nptxfifosize.b.depth;
  23502. + DWC_WRITE_REG32(&global_regs->hptxfsiz, ptxfifosize.d32);
  23503. + DWC_DEBUGPL(DBG_CIL, "new hptxfsiz=%08x\n",
  23504. + DWC_READ_REG32(&global_regs->hptxfsiz));
  23505. +
  23506. + if (core_if->en_multiple_tx_fifo
  23507. + && core_if->snpsid <= OTG_CORE_REV_2_94a) {
  23508. + /* Global DFIFOCFG calculation for Host mode - include RxFIFO, NPTXFIFO and HPTXFIFO */
  23509. + gdfifocfg.d32 = DWC_READ_REG32(&global_regs->gdfifocfg);
  23510. + rxfsiz = (DWC_READ_REG32(&global_regs->grxfsiz) & 0x0000ffff);
  23511. + nptxfsiz = (DWC_READ_REG32(&global_regs->gnptxfsiz) >> 16);
  23512. + hptxfsiz = (DWC_READ_REG32(&global_regs->hptxfsiz) >> 16);
  23513. + gdfifocfg.b.epinfobase = rxfsiz + nptxfsiz + hptxfsiz;
  23514. + DWC_WRITE_REG32(&global_regs->gdfifocfg, gdfifocfg.d32);
  23515. + }
  23516. + }
  23517. +
  23518. + /* TODO - check this */
  23519. + /* Clear Host Set HNP Enable in the OTG Control Register */
  23520. + gotgctl.b.hstsethnpen = 1;
  23521. + DWC_MODIFY_REG32(&global_regs->gotgctl, gotgctl.d32, 0);
  23522. + /* Make sure the FIFOs are flushed. */
  23523. + dwc_otg_flush_tx_fifo(core_if, 0x10 /* all TX FIFOs */ );
  23524. + dwc_otg_flush_rx_fifo(core_if);
  23525. +
  23526. + /* Clear Host Set HNP Enable in the OTG Control Register */
  23527. + gotgctl.b.hstsethnpen = 1;
  23528. + DWC_MODIFY_REG32(&global_regs->gotgctl, gotgctl.d32, 0);
  23529. +
  23530. + if (!core_if->core_params->dma_desc_enable) {
  23531. + /* Flush out any leftover queued requests. */
  23532. + num_channels = core_if->core_params->host_channels;
  23533. +
  23534. + for (i = 0; i < num_channels; i++) {
  23535. + hc_regs = core_if->host_if->hc_regs[i];
  23536. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  23537. + hcchar.b.chen = 0;
  23538. + hcchar.b.chdis = 1;
  23539. + hcchar.b.epdir = 0;
  23540. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  23541. + }
  23542. +
  23543. + /* Halt all channels to put them into a known state. */
  23544. + for (i = 0; i < num_channels; i++) {
  23545. + int count = 0;
  23546. + hc_regs = core_if->host_if->hc_regs[i];
  23547. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  23548. + hcchar.b.chen = 1;
  23549. + hcchar.b.chdis = 1;
  23550. + hcchar.b.epdir = 0;
  23551. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  23552. + DWC_DEBUGPL(DBG_HCDV, "%s: Halt channel %d regs %p\n", __func__, i, hc_regs);
  23553. + do {
  23554. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  23555. + if (++count > 1000) {
  23556. + DWC_ERROR
  23557. + ("%s: Unable to clear halt on channel %d (timeout HCCHAR 0x%X @%p)\n",
  23558. + __func__, i, hcchar.d32, &hc_regs->hcchar);
  23559. + break;
  23560. + }
  23561. + dwc_udelay(1);
  23562. + } while (hcchar.b.chen);
  23563. + }
  23564. + }
  23565. +
  23566. + /* Turn on the vbus power. */
  23567. + DWC_PRINTF("Init: Port Power? op_state=%d\n", core_if->op_state);
  23568. + if (core_if->op_state == A_HOST) {
  23569. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  23570. + DWC_PRINTF("Init: Power Port (%d)\n", hprt0.b.prtpwr);
  23571. + if (hprt0.b.prtpwr == 0) {
  23572. + hprt0.b.prtpwr = 1;
  23573. + DWC_WRITE_REG32(host_if->hprt0, hprt0.d32);
  23574. + }
  23575. + }
  23576. +
  23577. + dwc_otg_enable_host_interrupts(core_if);
  23578. +}
  23579. +
  23580. +/**
  23581. + * Prepares a host channel for transferring packets to/from a specific
  23582. + * endpoint. The HCCHARn register is set up with the characteristics specified
  23583. + * in _hc. Host channel interrupts that may need to be serviced while this
  23584. + * transfer is in progress are enabled.
  23585. + *
  23586. + * @param core_if Programming view of DWC_otg controller
  23587. + * @param hc Information needed to initialize the host channel
  23588. + */
  23589. +void dwc_otg_hc_init(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
  23590. +{
  23591. + hcintmsk_data_t hc_intr_mask;
  23592. + hcchar_data_t hcchar;
  23593. + hcsplt_data_t hcsplt;
  23594. +
  23595. + uint8_t hc_num = hc->hc_num;
  23596. + dwc_otg_host_if_t *host_if = core_if->host_if;
  23597. + dwc_otg_hc_regs_t *hc_regs = host_if->hc_regs[hc_num];
  23598. +
  23599. + /* Clear old interrupt conditions for this host channel. */
  23600. + hc_intr_mask.d32 = 0xFFFFFFFF;
  23601. + hc_intr_mask.b.reserved14_31 = 0;
  23602. + DWC_WRITE_REG32(&hc_regs->hcint, hc_intr_mask.d32);
  23603. +
  23604. + /* Enable channel interrupts required for this transfer. */
  23605. + hc_intr_mask.d32 = 0;
  23606. + hc_intr_mask.b.chhltd = 1;
  23607. + if (core_if->dma_enable) {
  23608. + /* For Descriptor DMA mode core halts the channel on AHB error. Interrupt is not required */
  23609. + if (!core_if->dma_desc_enable)
  23610. + hc_intr_mask.b.ahberr = 1;
  23611. + else {
  23612. + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC)
  23613. + hc_intr_mask.b.xfercompl = 1;
  23614. + }
  23615. +
  23616. + if (hc->error_state && !hc->do_split &&
  23617. + hc->ep_type != DWC_OTG_EP_TYPE_ISOC) {
  23618. + hc_intr_mask.b.ack = 1;
  23619. + if (hc->ep_is_in) {
  23620. + hc_intr_mask.b.datatglerr = 1;
  23621. + if (hc->ep_type != DWC_OTG_EP_TYPE_INTR) {
  23622. + hc_intr_mask.b.nak = 1;
  23623. + }
  23624. + }
  23625. + }
  23626. + } else {
  23627. + switch (hc->ep_type) {
  23628. + case DWC_OTG_EP_TYPE_CONTROL:
  23629. + case DWC_OTG_EP_TYPE_BULK:
  23630. + hc_intr_mask.b.xfercompl = 1;
  23631. + hc_intr_mask.b.stall = 1;
  23632. + hc_intr_mask.b.xacterr = 1;
  23633. + hc_intr_mask.b.datatglerr = 1;
  23634. + if (hc->ep_is_in) {
  23635. + hc_intr_mask.b.bblerr = 1;
  23636. + } else {
  23637. + hc_intr_mask.b.nak = 1;
  23638. + hc_intr_mask.b.nyet = 1;
  23639. + if (hc->do_ping) {
  23640. + hc_intr_mask.b.ack = 1;
  23641. + }
  23642. + }
  23643. +
  23644. + if (hc->do_split) {
  23645. + hc_intr_mask.b.nak = 1;
  23646. + if (hc->complete_split) {
  23647. + hc_intr_mask.b.nyet = 1;
  23648. + } else {
  23649. + hc_intr_mask.b.ack = 1;
  23650. + }
  23651. + }
  23652. +
  23653. + if (hc->error_state) {
  23654. + hc_intr_mask.b.ack = 1;
  23655. + }
  23656. + break;
  23657. + case DWC_OTG_EP_TYPE_INTR:
  23658. + hc_intr_mask.b.xfercompl = 1;
  23659. + hc_intr_mask.b.nak = 1;
  23660. + hc_intr_mask.b.stall = 1;
  23661. + hc_intr_mask.b.xacterr = 1;
  23662. + hc_intr_mask.b.datatglerr = 1;
  23663. + hc_intr_mask.b.frmovrun = 1;
  23664. +
  23665. + if (hc->ep_is_in) {
  23666. + hc_intr_mask.b.bblerr = 1;
  23667. + }
  23668. + if (hc->error_state) {
  23669. + hc_intr_mask.b.ack = 1;
  23670. + }
  23671. + if (hc->do_split) {
  23672. + if (hc->complete_split) {
  23673. + hc_intr_mask.b.nyet = 1;
  23674. + } else {
  23675. + hc_intr_mask.b.ack = 1;
  23676. + }
  23677. + }
  23678. + break;
  23679. + case DWC_OTG_EP_TYPE_ISOC:
  23680. + hc_intr_mask.b.xfercompl = 1;
  23681. + hc_intr_mask.b.frmovrun = 1;
  23682. + hc_intr_mask.b.ack = 1;
  23683. +
  23684. + if (hc->ep_is_in) {
  23685. + hc_intr_mask.b.xacterr = 1;
  23686. + hc_intr_mask.b.bblerr = 1;
  23687. + }
  23688. + break;
  23689. + }
  23690. + }
  23691. + DWC_WRITE_REG32(&hc_regs->hcintmsk, hc_intr_mask.d32);
  23692. +
  23693. + /*
  23694. + * Program the HCCHARn register with the endpoint characteristics for
  23695. + * the current transfer.
  23696. + */
  23697. + hcchar.d32 = 0;
  23698. + hcchar.b.devaddr = hc->dev_addr;
  23699. + hcchar.b.epnum = hc->ep_num;
  23700. + hcchar.b.epdir = hc->ep_is_in;
  23701. + hcchar.b.lspddev = (hc->speed == DWC_OTG_EP_SPEED_LOW);
  23702. + hcchar.b.eptype = hc->ep_type;
  23703. + hcchar.b.mps = hc->max_packet;
  23704. +
  23705. + DWC_WRITE_REG32(&host_if->hc_regs[hc_num]->hcchar, hcchar.d32);
  23706. +
  23707. + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d, Dev Addr %d, EP #%d\n",
  23708. + __func__, hc->hc_num, hcchar.b.devaddr, hcchar.b.epnum);
  23709. + DWC_DEBUGPL(DBG_HCDV, " Is In %d, Is Low Speed %d, EP Type %d, "
  23710. + "Max Pkt %d, Multi Cnt %d\n",
  23711. + hcchar.b.epdir, hcchar.b.lspddev, hcchar.b.eptype,
  23712. + hcchar.b.mps, hcchar.b.multicnt);
  23713. +
  23714. + /*
  23715. + * Program the HCSPLIT register for SPLITs
  23716. + */
  23717. + hcsplt.d32 = 0;
  23718. + if (hc->do_split) {
  23719. + DWC_DEBUGPL(DBG_HCDV, "Programming HC %d with split --> %s\n",
  23720. + hc->hc_num,
  23721. + hc->complete_split ? "CSPLIT" : "SSPLIT");
  23722. + hcsplt.b.compsplt = hc->complete_split;
  23723. + hcsplt.b.xactpos = hc->xact_pos;
  23724. + hcsplt.b.hubaddr = hc->hub_addr;
  23725. + hcsplt.b.prtaddr = hc->port_addr;
  23726. + DWC_DEBUGPL(DBG_HCDV, "\t comp split %d\n", hc->complete_split);
  23727. + DWC_DEBUGPL(DBG_HCDV, "\t xact pos %d\n", hc->xact_pos);
  23728. + DWC_DEBUGPL(DBG_HCDV, "\t hub addr %d\n", hc->hub_addr);
  23729. + DWC_DEBUGPL(DBG_HCDV, "\t port addr %d\n", hc->port_addr);
  23730. + DWC_DEBUGPL(DBG_HCDV, "\t is_in %d\n", hc->ep_is_in);
  23731. + DWC_DEBUGPL(DBG_HCDV, "\t Max Pkt: %d\n", hcchar.b.mps);
  23732. + DWC_DEBUGPL(DBG_HCDV, "\t xferlen: %d\n", hc->xfer_len);
  23733. + }
  23734. + DWC_WRITE_REG32(&host_if->hc_regs[hc_num]->hcsplt, hcsplt.d32);
  23735. +
  23736. +}
  23737. +
  23738. +/**
  23739. + * Attempts to halt a host channel. This function should only be called in
  23740. + * Slave mode or to abort a transfer in either Slave mode or DMA mode. Under
  23741. + * normal circumstances in DMA mode, the controller halts the channel when the
  23742. + * transfer is complete or a condition occurs that requires application
  23743. + * intervention.
  23744. + *
  23745. + * In slave mode, checks for a free request queue entry, then sets the Channel
  23746. + * Enable and Channel Disable bits of the Host Channel Characteristics
  23747. + * register of the specified channel to intiate the halt. If there is no free
  23748. + * request queue entry, sets only the Channel Disable bit of the HCCHARn
  23749. + * register to flush requests for this channel. In the latter case, sets a
  23750. + * flag to indicate that the host channel needs to be halted when a request
  23751. + * queue slot is open.
  23752. + *
  23753. + * In DMA mode, always sets the Channel Enable and Channel Disable bits of the
  23754. + * HCCHARn register. The controller ensures there is space in the request
  23755. + * queue before submitting the halt request.
  23756. + *
  23757. + * Some time may elapse before the core flushes any posted requests for this
  23758. + * host channel and halts. The Channel Halted interrupt handler completes the
  23759. + * deactivation of the host channel.
  23760. + *
  23761. + * @param core_if Controller register interface.
  23762. + * @param hc Host channel to halt.
  23763. + * @param halt_status Reason for halting the channel.
  23764. + */
  23765. +void dwc_otg_hc_halt(dwc_otg_core_if_t * core_if,
  23766. + dwc_hc_t * hc, dwc_otg_halt_status_e halt_status)
  23767. +{
  23768. + gnptxsts_data_t nptxsts;
  23769. + hptxsts_data_t hptxsts;
  23770. + hcchar_data_t hcchar;
  23771. + dwc_otg_hc_regs_t *hc_regs;
  23772. + dwc_otg_core_global_regs_t *global_regs;
  23773. + dwc_otg_host_global_regs_t *host_global_regs;
  23774. +
  23775. + hc_regs = core_if->host_if->hc_regs[hc->hc_num];
  23776. + global_regs = core_if->core_global_regs;
  23777. + host_global_regs = core_if->host_if->host_global_regs;
  23778. +
  23779. + DWC_ASSERT(!(halt_status == DWC_OTG_HC_XFER_NO_HALT_STATUS),
  23780. + "halt_status = %d\n", halt_status);
  23781. +
  23782. + if (halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE ||
  23783. + halt_status == DWC_OTG_HC_XFER_AHB_ERR) {
  23784. + /*
  23785. + * Disable all channel interrupts except Ch Halted. The QTD
  23786. + * and QH state associated with this transfer has been cleared
  23787. + * (in the case of URB_DEQUEUE), so the channel needs to be
  23788. + * shut down carefully to prevent crashes.
  23789. + */
  23790. + hcintmsk_data_t hcintmsk;
  23791. + hcintmsk.d32 = 0;
  23792. + hcintmsk.b.chhltd = 1;
  23793. + DWC_WRITE_REG32(&hc_regs->hcintmsk, hcintmsk.d32);
  23794. +
  23795. + /*
  23796. + * Make sure no other interrupts besides halt are currently
  23797. + * pending. Handling another interrupt could cause a crash due
  23798. + * to the QTD and QH state.
  23799. + */
  23800. + DWC_WRITE_REG32(&hc_regs->hcint, ~hcintmsk.d32);
  23801. +
  23802. + /*
  23803. + * Make sure the halt status is set to URB_DEQUEUE or AHB_ERR
  23804. + * even if the channel was already halted for some other
  23805. + * reason.
  23806. + */
  23807. + hc->halt_status = halt_status;
  23808. +
  23809. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  23810. + if (hcchar.b.chen == 0) {
  23811. + /*
  23812. + * The channel is either already halted or it hasn't
  23813. + * started yet. In DMA mode, the transfer may halt if
  23814. + * it finishes normally or a condition occurs that
  23815. + * requires driver intervention. Don't want to halt
  23816. + * the channel again. In either Slave or DMA mode,
  23817. + * it's possible that the transfer has been assigned
  23818. + * to a channel, but not started yet when an URB is
  23819. + * dequeued. Don't want to halt a channel that hasn't
  23820. + * started yet.
  23821. + */
  23822. + return;
  23823. + }
  23824. + }
  23825. + if (hc->halt_pending) {
  23826. + /*
  23827. + * A halt has already been issued for this channel. This might
  23828. + * happen when a transfer is aborted by a higher level in
  23829. + * the stack.
  23830. + */
  23831. +#ifdef DEBUG
  23832. + DWC_PRINTF
  23833. + ("*** %s: Channel %d, _hc->halt_pending already set ***\n",
  23834. + __func__, hc->hc_num);
  23835. +
  23836. +#endif
  23837. + return;
  23838. + }
  23839. +
  23840. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  23841. +
  23842. + /* No need to set the bit in DDMA for disabling the channel */
  23843. + //TODO check it everywhere channel is disabled
  23844. + if (!core_if->core_params->dma_desc_enable)
  23845. + hcchar.b.chen = 1;
  23846. + hcchar.b.chdis = 1;
  23847. +
  23848. + if (!core_if->dma_enable) {
  23849. + /* Check for space in the request queue to issue the halt. */
  23850. + if (hc->ep_type == DWC_OTG_EP_TYPE_CONTROL ||
  23851. + hc->ep_type == DWC_OTG_EP_TYPE_BULK) {
  23852. + nptxsts.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
  23853. + if (nptxsts.b.nptxqspcavail == 0) {
  23854. + hcchar.b.chen = 0;
  23855. + }
  23856. + } else {
  23857. + hptxsts.d32 =
  23858. + DWC_READ_REG32(&host_global_regs->hptxsts);
  23859. + if ((hptxsts.b.ptxqspcavail == 0)
  23860. + || (core_if->queuing_high_bandwidth)) {
  23861. + hcchar.b.chen = 0;
  23862. + }
  23863. + }
  23864. + }
  23865. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  23866. +
  23867. + hc->halt_status = halt_status;
  23868. +
  23869. + if (hcchar.b.chen) {
  23870. + hc->halt_pending = 1;
  23871. + hc->halt_on_queue = 0;
  23872. + } else {
  23873. + hc->halt_on_queue = 1;
  23874. + }
  23875. +
  23876. + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d\n", __func__, hc->hc_num);
  23877. + DWC_DEBUGPL(DBG_HCDV, " hcchar: 0x%08x\n", hcchar.d32);
  23878. + DWC_DEBUGPL(DBG_HCDV, " halt_pending: %d\n", hc->halt_pending);
  23879. + DWC_DEBUGPL(DBG_HCDV, " halt_on_queue: %d\n", hc->halt_on_queue);
  23880. + DWC_DEBUGPL(DBG_HCDV, " halt_status: %d\n", hc->halt_status);
  23881. +
  23882. + return;
  23883. +}
  23884. +
  23885. +/**
  23886. + * Clears the transfer state for a host channel. This function is normally
  23887. + * called after a transfer is done and the host channel is being released.
  23888. + *
  23889. + * @param core_if Programming view of DWC_otg controller.
  23890. + * @param hc Identifies the host channel to clean up.
  23891. + */
  23892. +void dwc_otg_hc_cleanup(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
  23893. +{
  23894. + dwc_otg_hc_regs_t *hc_regs;
  23895. +
  23896. + hc->xfer_started = 0;
  23897. +
  23898. + /*
  23899. + * Clear channel interrupt enables and any unhandled channel interrupt
  23900. + * conditions.
  23901. + */
  23902. + hc_regs = core_if->host_if->hc_regs[hc->hc_num];
  23903. + DWC_WRITE_REG32(&hc_regs->hcintmsk, 0);
  23904. + DWC_WRITE_REG32(&hc_regs->hcint, 0xFFFFFFFF);
  23905. +#ifdef DEBUG
  23906. + DWC_TIMER_CANCEL(core_if->hc_xfer_timer[hc->hc_num]);
  23907. +#endif
  23908. +}
  23909. +
  23910. +/**
  23911. + * Sets the channel property that indicates in which frame a periodic transfer
  23912. + * should occur. This is always set to the _next_ frame. This function has no
  23913. + * effect on non-periodic transfers.
  23914. + *
  23915. + * @param core_if Programming view of DWC_otg controller.
  23916. + * @param hc Identifies the host channel to set up and its properties.
  23917. + * @param hcchar Current value of the HCCHAR register for the specified host
  23918. + * channel.
  23919. + */
  23920. +static inline void hc_set_even_odd_frame(dwc_otg_core_if_t * core_if,
  23921. + dwc_hc_t * hc, hcchar_data_t * hcchar)
  23922. +{
  23923. + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
  23924. + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
  23925. + hfnum_data_t hfnum;
  23926. + hfnum.d32 =
  23927. + DWC_READ_REG32(&core_if->host_if->host_global_regs->hfnum);
  23928. +
  23929. + /* 1 if _next_ frame is odd, 0 if it's even */
  23930. + hcchar->b.oddfrm = (hfnum.b.frnum & 0x1) ? 0 : 1;
  23931. +#ifdef DEBUG
  23932. + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR && hc->do_split
  23933. + && !hc->complete_split) {
  23934. + switch (hfnum.b.frnum & 0x7) {
  23935. + case 7:
  23936. + core_if->hfnum_7_samples++;
  23937. + core_if->hfnum_7_frrem_accum += hfnum.b.frrem;
  23938. + break;
  23939. + case 0:
  23940. + core_if->hfnum_0_samples++;
  23941. + core_if->hfnum_0_frrem_accum += hfnum.b.frrem;
  23942. + break;
  23943. + default:
  23944. + core_if->hfnum_other_samples++;
  23945. + core_if->hfnum_other_frrem_accum +=
  23946. + hfnum.b.frrem;
  23947. + break;
  23948. + }
  23949. + }
  23950. +#endif
  23951. + }
  23952. +}
  23953. +
  23954. +#ifdef DEBUG
  23955. +void hc_xfer_timeout(void *ptr)
  23956. +{
  23957. + hc_xfer_info_t *xfer_info = NULL;
  23958. + int hc_num = 0;
  23959. +
  23960. + if (ptr)
  23961. + xfer_info = (hc_xfer_info_t *) ptr;
  23962. +
  23963. + if (!xfer_info->hc) {
  23964. + DWC_ERROR("xfer_info->hc = %p\n", xfer_info->hc);
  23965. + return;
  23966. + }
  23967. +
  23968. + hc_num = xfer_info->hc->hc_num;
  23969. + DWC_WARN("%s: timeout on channel %d\n", __func__, hc_num);
  23970. + DWC_WARN(" start_hcchar_val 0x%08x\n",
  23971. + xfer_info->core_if->start_hcchar_val[hc_num]);
  23972. +}
  23973. +#endif
  23974. +
  23975. +void ep_xfer_timeout(void *ptr)
  23976. +{
  23977. + ep_xfer_info_t *xfer_info = NULL;
  23978. + int ep_num = 0;
  23979. + dctl_data_t dctl = {.d32 = 0 };
  23980. + gintsts_data_t gintsts = {.d32 = 0 };
  23981. + gintmsk_data_t gintmsk = {.d32 = 0 };
  23982. +
  23983. + if (ptr)
  23984. + xfer_info = (ep_xfer_info_t *) ptr;
  23985. +
  23986. + if (!xfer_info->ep) {
  23987. + DWC_ERROR("xfer_info->ep = %p\n", xfer_info->ep);
  23988. + return;
  23989. + }
  23990. +
  23991. + ep_num = xfer_info->ep->num;
  23992. + DWC_WARN("%s: timeout on endpoit %d\n", __func__, ep_num);
  23993. + /* Put the sate to 2 as it was time outed */
  23994. + xfer_info->state = 2;
  23995. +
  23996. + dctl.d32 =
  23997. + DWC_READ_REG32(&xfer_info->core_if->dev_if->dev_global_regs->dctl);
  23998. + gintsts.d32 =
  23999. + DWC_READ_REG32(&xfer_info->core_if->core_global_regs->gintsts);
  24000. + gintmsk.d32 =
  24001. + DWC_READ_REG32(&xfer_info->core_if->core_global_regs->gintmsk);
  24002. +
  24003. + if (!gintmsk.b.goutnakeff) {
  24004. + /* Unmask it */
  24005. + gintmsk.b.goutnakeff = 1;
  24006. + DWC_WRITE_REG32(&xfer_info->core_if->core_global_regs->gintmsk,
  24007. + gintmsk.d32);
  24008. +
  24009. + }
  24010. +
  24011. + if (!gintsts.b.goutnakeff) {
  24012. + dctl.b.sgoutnak = 1;
  24013. + }
  24014. + DWC_WRITE_REG32(&xfer_info->core_if->dev_if->dev_global_regs->dctl,
  24015. + dctl.d32);
  24016. +
  24017. +}
  24018. +
  24019. +void set_pid_isoc(dwc_hc_t * hc)
  24020. +{
  24021. + /* Set up the initial PID for the transfer. */
  24022. + if (hc->speed == DWC_OTG_EP_SPEED_HIGH) {
  24023. + if (hc->ep_is_in) {
  24024. + if (hc->multi_count == 1) {
  24025. + hc->data_pid_start = DWC_OTG_HC_PID_DATA0;
  24026. + } else if (hc->multi_count == 2) {
  24027. + hc->data_pid_start = DWC_OTG_HC_PID_DATA1;
  24028. + } else {
  24029. + hc->data_pid_start = DWC_OTG_HC_PID_DATA2;
  24030. + }
  24031. + } else {
  24032. + if (hc->multi_count == 1) {
  24033. + hc->data_pid_start = DWC_OTG_HC_PID_DATA0;
  24034. + } else {
  24035. + hc->data_pid_start = DWC_OTG_HC_PID_MDATA;
  24036. + }
  24037. + }
  24038. + } else {
  24039. + hc->data_pid_start = DWC_OTG_HC_PID_DATA0;
  24040. + }
  24041. +}
  24042. +
  24043. +/**
  24044. + * This function does the setup for a data transfer for a host channel and
  24045. + * starts the transfer. May be called in either Slave mode or DMA mode. In
  24046. + * Slave mode, the caller must ensure that there is sufficient space in the
  24047. + * request queue and Tx Data FIFO.
  24048. + *
  24049. + * For an OUT transfer in Slave mode, it loads a data packet into the
  24050. + * appropriate FIFO. If necessary, additional data packets will be loaded in
  24051. + * the Host ISR.
  24052. + *
  24053. + * For an IN transfer in Slave mode, a data packet is requested. The data
  24054. + * packets are unloaded from the Rx FIFO in the Host ISR. If necessary,
  24055. + * additional data packets are requested in the Host ISR.
  24056. + *
  24057. + * For a PING transfer in Slave mode, the Do Ping bit is set in the HCTSIZ
  24058. + * register along with a packet count of 1 and the channel is enabled. This
  24059. + * causes a single PING transaction to occur. Other fields in HCTSIZ are
  24060. + * simply set to 0 since no data transfer occurs in this case.
  24061. + *
  24062. + * For a PING transfer in DMA mode, the HCTSIZ register is initialized with
  24063. + * all the information required to perform the subsequent data transfer. In
  24064. + * addition, the Do Ping bit is set in the HCTSIZ register. In this case, the
  24065. + * controller performs the entire PING protocol, then starts the data
  24066. + * transfer.
  24067. + *
  24068. + * @param core_if Programming view of DWC_otg controller.
  24069. + * @param hc Information needed to initialize the host channel. The xfer_len
  24070. + * value may be reduced to accommodate the max widths of the XferSize and
  24071. + * PktCnt fields in the HCTSIZn register. The multi_count value may be changed
  24072. + * to reflect the final xfer_len value.
  24073. + */
  24074. +void dwc_otg_hc_start_transfer(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
  24075. +{
  24076. + hcchar_data_t hcchar;
  24077. + hctsiz_data_t hctsiz;
  24078. + uint16_t num_packets;
  24079. + uint32_t max_hc_xfer_size = core_if->core_params->max_transfer_size;
  24080. + uint16_t max_hc_pkt_count = core_if->core_params->max_packet_count;
  24081. + dwc_otg_hc_regs_t *hc_regs = core_if->host_if->hc_regs[hc->hc_num];
  24082. +
  24083. + hctsiz.d32 = 0;
  24084. +
  24085. + if (hc->do_ping) {
  24086. + if (!core_if->dma_enable) {
  24087. + dwc_otg_hc_do_ping(core_if, hc);
  24088. + hc->xfer_started = 1;
  24089. + return;
  24090. + } else {
  24091. + hctsiz.b.dopng = 1;
  24092. + }
  24093. + }
  24094. +
  24095. + if (hc->do_split) {
  24096. + num_packets = 1;
  24097. +
  24098. + if (hc->complete_split && !hc->ep_is_in) {
  24099. + /* For CSPLIT OUT Transfer, set the size to 0 so the
  24100. + * core doesn't expect any data written to the FIFO */
  24101. + hc->xfer_len = 0;
  24102. + } else if (hc->ep_is_in || (hc->xfer_len > hc->max_packet)) {
  24103. + hc->xfer_len = hc->max_packet;
  24104. + } else if (!hc->ep_is_in && (hc->xfer_len > 188)) {
  24105. + hc->xfer_len = 188;
  24106. + }
  24107. +
  24108. + hctsiz.b.xfersize = hc->xfer_len;
  24109. + } else {
  24110. + /*
  24111. + * Ensure that the transfer length and packet count will fit
  24112. + * in the widths allocated for them in the HCTSIZn register.
  24113. + */
  24114. + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
  24115. + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
  24116. + /*
  24117. + * Make sure the transfer size is no larger than one
  24118. + * (micro)frame's worth of data. (A check was done
  24119. + * when the periodic transfer was accepted to ensure
  24120. + * that a (micro)frame's worth of data can be
  24121. + * programmed into a channel.)
  24122. + */
  24123. + uint32_t max_periodic_len =
  24124. + hc->multi_count * hc->max_packet;
  24125. + if (hc->xfer_len > max_periodic_len) {
  24126. + hc->xfer_len = max_periodic_len;
  24127. + } else {
  24128. + }
  24129. + } else if (hc->xfer_len > max_hc_xfer_size) {
  24130. + /* Make sure that xfer_len is a multiple of max packet size. */
  24131. + hc->xfer_len = max_hc_xfer_size - hc->max_packet + 1;
  24132. + }
  24133. +
  24134. + if (hc->xfer_len > 0) {
  24135. + num_packets =
  24136. + (hc->xfer_len + hc->max_packet -
  24137. + 1) / hc->max_packet;
  24138. + if (num_packets > max_hc_pkt_count) {
  24139. + num_packets = max_hc_pkt_count;
  24140. + hc->xfer_len = num_packets * hc->max_packet;
  24141. + }
  24142. + } else {
  24143. + /* Need 1 packet for transfer length of 0. */
  24144. + num_packets = 1;
  24145. + }
  24146. +
  24147. + if (hc->ep_is_in) {
  24148. + /* Always program an integral # of max packets for IN transfers. */
  24149. + hc->xfer_len = num_packets * hc->max_packet;
  24150. + }
  24151. +
  24152. + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
  24153. + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
  24154. + /*
  24155. + * Make sure that the multi_count field matches the
  24156. + * actual transfer length.
  24157. + */
  24158. + hc->multi_count = num_packets;
  24159. + }
  24160. +
  24161. + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC)
  24162. + set_pid_isoc(hc);
  24163. +
  24164. + hctsiz.b.xfersize = hc->xfer_len;
  24165. + }
  24166. +
  24167. + hc->start_pkt_count = num_packets;
  24168. + hctsiz.b.pktcnt = num_packets;
  24169. + hctsiz.b.pid = hc->data_pid_start;
  24170. + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
  24171. +
  24172. + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d\n", __func__, hc->hc_num);
  24173. + DWC_DEBUGPL(DBG_HCDV, " Xfer Size: %d\n", hctsiz.b.xfersize);
  24174. + DWC_DEBUGPL(DBG_HCDV, " Num Pkts: %d\n", hctsiz.b.pktcnt);
  24175. + DWC_DEBUGPL(DBG_HCDV, " Start PID: %d\n", hctsiz.b.pid);
  24176. +
  24177. + if (core_if->dma_enable) {
  24178. + dwc_dma_t dma_addr;
  24179. + if (hc->align_buff) {
  24180. + dma_addr = hc->align_buff;
  24181. + } else {
  24182. + dma_addr = ((unsigned long)hc->xfer_buff & 0xffffffff);
  24183. + }
  24184. + DWC_WRITE_REG32(&hc_regs->hcdma, dma_addr);
  24185. + }
  24186. +
  24187. + /* Start the split */
  24188. + if (hc->do_split) {
  24189. + hcsplt_data_t hcsplt;
  24190. + hcsplt.d32 = DWC_READ_REG32(&hc_regs->hcsplt);
  24191. + hcsplt.b.spltena = 1;
  24192. + DWC_WRITE_REG32(&hc_regs->hcsplt, hcsplt.d32);
  24193. + }
  24194. +
  24195. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  24196. + hcchar.b.multicnt = hc->multi_count;
  24197. + hc_set_even_odd_frame(core_if, hc, &hcchar);
  24198. +#ifdef DEBUG
  24199. + core_if->start_hcchar_val[hc->hc_num] = hcchar.d32;
  24200. + if (hcchar.b.chdis) {
  24201. + DWC_WARN("%s: chdis set, channel %d, hcchar 0x%08x\n",
  24202. + __func__, hc->hc_num, hcchar.d32);
  24203. + }
  24204. +#endif
  24205. +
  24206. + /* Set host channel enable after all other setup is complete. */
  24207. + hcchar.b.chen = 1;
  24208. + hcchar.b.chdis = 0;
  24209. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  24210. +
  24211. + hc->xfer_started = 1;
  24212. + hc->requests++;
  24213. +
  24214. + if (!core_if->dma_enable && !hc->ep_is_in && hc->xfer_len > 0) {
  24215. + /* Load OUT packet into the appropriate Tx FIFO. */
  24216. + dwc_otg_hc_write_packet(core_if, hc);
  24217. + }
  24218. +#ifdef DEBUG
  24219. + if (hc->ep_type != DWC_OTG_EP_TYPE_INTR) {
  24220. + DWC_DEBUGPL(DBG_HCDV, "transfer %d from core_if %p\n",
  24221. + hc->hc_num, core_if);//GRAYG
  24222. + core_if->hc_xfer_info[hc->hc_num].core_if = core_if;
  24223. + core_if->hc_xfer_info[hc->hc_num].hc = hc;
  24224. +
  24225. + /* Start a timer for this transfer. */
  24226. + DWC_TIMER_SCHEDULE(core_if->hc_xfer_timer[hc->hc_num], 10000);
  24227. + }
  24228. +#endif
  24229. +}
  24230. +
  24231. +/**
  24232. + * This function does the setup for a data transfer for a host channel
  24233. + * and starts the transfer in Descriptor DMA mode.
  24234. + *
  24235. + * Initializes HCTSIZ register. For a PING transfer the Do Ping bit is set.
  24236. + * Sets PID and NTD values. For periodic transfers
  24237. + * initializes SCHED_INFO field with micro-frame bitmap.
  24238. + *
  24239. + * Initializes HCDMA register with descriptor list address and CTD value
  24240. + * then starts the transfer via enabling the channel.
  24241. + *
  24242. + * @param core_if Programming view of DWC_otg controller.
  24243. + * @param hc Information needed to initialize the host channel.
  24244. + */
  24245. +void dwc_otg_hc_start_transfer_ddma(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
  24246. +{
  24247. + dwc_otg_hc_regs_t *hc_regs = core_if->host_if->hc_regs[hc->hc_num];
  24248. + hcchar_data_t hcchar;
  24249. + hctsiz_data_t hctsiz;
  24250. + hcdma_data_t hcdma;
  24251. +
  24252. + hctsiz.d32 = 0;
  24253. +
  24254. + if (hc->do_ping)
  24255. + hctsiz.b_ddma.dopng = 1;
  24256. +
  24257. + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC)
  24258. + set_pid_isoc(hc);
  24259. +
  24260. + /* Packet Count and Xfer Size are not used in Descriptor DMA mode */
  24261. + hctsiz.b_ddma.pid = hc->data_pid_start;
  24262. + hctsiz.b_ddma.ntd = hc->ntd - 1; /* 0 - 1 descriptor, 1 - 2 descriptors, etc. */
  24263. + hctsiz.b_ddma.schinfo = hc->schinfo; /* Non-zero only for high-speed interrupt endpoints */
  24264. +
  24265. + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d\n", __func__, hc->hc_num);
  24266. + DWC_DEBUGPL(DBG_HCDV, " Start PID: %d\n", hctsiz.b.pid);
  24267. + DWC_DEBUGPL(DBG_HCDV, " NTD: %d\n", hctsiz.b_ddma.ntd);
  24268. +
  24269. + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
  24270. +
  24271. + hcdma.d32 = 0;
  24272. + hcdma.b.dma_addr = ((uint32_t) hc->desc_list_addr) >> 11;
  24273. +
  24274. + /* Always start from first descriptor. */
  24275. + hcdma.b.ctd = 0;
  24276. + DWC_WRITE_REG32(&hc_regs->hcdma, hcdma.d32);
  24277. +
  24278. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  24279. + hcchar.b.multicnt = hc->multi_count;
  24280. +
  24281. +#ifdef DEBUG
  24282. + core_if->start_hcchar_val[hc->hc_num] = hcchar.d32;
  24283. + if (hcchar.b.chdis) {
  24284. + DWC_WARN("%s: chdis set, channel %d, hcchar 0x%08x\n",
  24285. + __func__, hc->hc_num, hcchar.d32);
  24286. + }
  24287. +#endif
  24288. +
  24289. + /* Set host channel enable after all other setup is complete. */
  24290. + hcchar.b.chen = 1;
  24291. + hcchar.b.chdis = 0;
  24292. +
  24293. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  24294. +
  24295. + hc->xfer_started = 1;
  24296. + hc->requests++;
  24297. +
  24298. +#ifdef DEBUG
  24299. + if ((hc->ep_type != DWC_OTG_EP_TYPE_INTR)
  24300. + && (hc->ep_type != DWC_OTG_EP_TYPE_ISOC)) {
  24301. + DWC_DEBUGPL(DBG_HCDV, "DMA transfer %d from core_if %p\n",
  24302. + hc->hc_num, core_if);//GRAYG
  24303. + core_if->hc_xfer_info[hc->hc_num].core_if = core_if;
  24304. + core_if->hc_xfer_info[hc->hc_num].hc = hc;
  24305. + /* Start a timer for this transfer. */
  24306. + DWC_TIMER_SCHEDULE(core_if->hc_xfer_timer[hc->hc_num], 10000);
  24307. + }
  24308. +#endif
  24309. +
  24310. +}
  24311. +
  24312. +/**
  24313. + * This function continues a data transfer that was started by previous call
  24314. + * to <code>dwc_otg_hc_start_transfer</code>. The caller must ensure there is
  24315. + * sufficient space in the request queue and Tx Data FIFO. This function
  24316. + * should only be called in Slave mode. In DMA mode, the controller acts
  24317. + * autonomously to complete transfers programmed to a host channel.
  24318. + *
  24319. + * For an OUT transfer, a new data packet is loaded into the appropriate FIFO
  24320. + * if there is any data remaining to be queued. For an IN transfer, another
  24321. + * data packet is always requested. For the SETUP phase of a control transfer,
  24322. + * this function does nothing.
  24323. + *
  24324. + * @return 1 if a new request is queued, 0 if no more requests are required
  24325. + * for this transfer.
  24326. + */
  24327. +int dwc_otg_hc_continue_transfer(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
  24328. +{
  24329. + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d\n", __func__, hc->hc_num);
  24330. +
  24331. + if (hc->do_split) {
  24332. + /* SPLITs always queue just once per channel */
  24333. + return 0;
  24334. + } else if (hc->data_pid_start == DWC_OTG_HC_PID_SETUP) {
  24335. + /* SETUPs are queued only once since they can't be NAKed. */
  24336. + return 0;
  24337. + } else if (hc->ep_is_in) {
  24338. + /*
  24339. + * Always queue another request for other IN transfers. If
  24340. + * back-to-back INs are issued and NAKs are received for both,
  24341. + * the driver may still be processing the first NAK when the
  24342. + * second NAK is received. When the interrupt handler clears
  24343. + * the NAK interrupt for the first NAK, the second NAK will
  24344. + * not be seen. So we can't depend on the NAK interrupt
  24345. + * handler to requeue a NAKed request. Instead, IN requests
  24346. + * are issued each time this function is called. When the
  24347. + * transfer completes, the extra requests for the channel will
  24348. + * be flushed.
  24349. + */
  24350. + hcchar_data_t hcchar;
  24351. + dwc_otg_hc_regs_t *hc_regs =
  24352. + core_if->host_if->hc_regs[hc->hc_num];
  24353. +
  24354. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  24355. + hc_set_even_odd_frame(core_if, hc, &hcchar);
  24356. + hcchar.b.chen = 1;
  24357. + hcchar.b.chdis = 0;
  24358. + DWC_DEBUGPL(DBG_HCDV, " IN xfer: hcchar = 0x%08x\n",
  24359. + hcchar.d32);
  24360. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  24361. + hc->requests++;
  24362. + return 1;
  24363. + } else {
  24364. + /* OUT transfers. */
  24365. + if (hc->xfer_count < hc->xfer_len) {
  24366. + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
  24367. + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
  24368. + hcchar_data_t hcchar;
  24369. + dwc_otg_hc_regs_t *hc_regs;
  24370. + hc_regs = core_if->host_if->hc_regs[hc->hc_num];
  24371. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  24372. + hc_set_even_odd_frame(core_if, hc, &hcchar);
  24373. + }
  24374. +
  24375. + /* Load OUT packet into the appropriate Tx FIFO. */
  24376. + dwc_otg_hc_write_packet(core_if, hc);
  24377. + hc->requests++;
  24378. + return 1;
  24379. + } else {
  24380. + return 0;
  24381. + }
  24382. + }
  24383. +}
  24384. +
  24385. +/**
  24386. + * Starts a PING transfer. This function should only be called in Slave mode.
  24387. + * The Do Ping bit is set in the HCTSIZ register, then the channel is enabled.
  24388. + */
  24389. +void dwc_otg_hc_do_ping(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
  24390. +{
  24391. + hcchar_data_t hcchar;
  24392. + hctsiz_data_t hctsiz;
  24393. + dwc_otg_hc_regs_t *hc_regs = core_if->host_if->hc_regs[hc->hc_num];
  24394. +
  24395. + DWC_DEBUGPL(DBG_HCDV, "%s: Channel %d\n", __func__, hc->hc_num);
  24396. +
  24397. + hctsiz.d32 = 0;
  24398. + hctsiz.b.dopng = 1;
  24399. + hctsiz.b.pktcnt = 1;
  24400. + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
  24401. +
  24402. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  24403. + hcchar.b.chen = 1;
  24404. + hcchar.b.chdis = 0;
  24405. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  24406. +}
  24407. +
  24408. +/*
  24409. + * This function writes a packet into the Tx FIFO associated with the Host
  24410. + * Channel. For a channel associated with a non-periodic EP, the non-periodic
  24411. + * Tx FIFO is written. For a channel associated with a periodic EP, the
  24412. + * periodic Tx FIFO is written. This function should only be called in Slave
  24413. + * mode.
  24414. + *
  24415. + * Upon return the xfer_buff and xfer_count fields in _hc are incremented by
  24416. + * then number of bytes written to the Tx FIFO.
  24417. + */
  24418. +void dwc_otg_hc_write_packet(dwc_otg_core_if_t * core_if, dwc_hc_t * hc)
  24419. +{
  24420. + uint32_t i;
  24421. + uint32_t remaining_count;
  24422. + uint32_t byte_count;
  24423. + uint32_t dword_count;
  24424. +
  24425. + uint32_t *data_buff = (uint32_t *) (hc->xfer_buff);
  24426. + uint32_t *data_fifo = core_if->data_fifo[hc->hc_num];
  24427. +
  24428. + remaining_count = hc->xfer_len - hc->xfer_count;
  24429. + if (remaining_count > hc->max_packet) {
  24430. + byte_count = hc->max_packet;
  24431. + } else {
  24432. + byte_count = remaining_count;
  24433. + }
  24434. +
  24435. + dword_count = (byte_count + 3) / 4;
  24436. +
  24437. + if ((((unsigned long)data_buff) & 0x3) == 0) {
  24438. + /* xfer_buff is DWORD aligned. */
  24439. + for (i = 0; i < dword_count; i++, data_buff++) {
  24440. + DWC_WRITE_REG32(data_fifo, *data_buff);
  24441. + }
  24442. + } else {
  24443. + /* xfer_buff is not DWORD aligned. */
  24444. + for (i = 0; i < dword_count; i++, data_buff++) {
  24445. + uint32_t data;
  24446. + data =
  24447. + (data_buff[0] | data_buff[1] << 8 | data_buff[2] <<
  24448. + 16 | data_buff[3] << 24);
  24449. + DWC_WRITE_REG32(data_fifo, data);
  24450. + }
  24451. + }
  24452. +
  24453. + hc->xfer_count += byte_count;
  24454. + hc->xfer_buff += byte_count;
  24455. +}
  24456. +
  24457. +/**
  24458. + * Gets the current USB frame number. This is the frame number from the last
  24459. + * SOF packet.
  24460. + */
  24461. +uint32_t dwc_otg_get_frame_number(dwc_otg_core_if_t * core_if)
  24462. +{
  24463. + dsts_data_t dsts;
  24464. + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  24465. +
  24466. + /* read current frame/microframe number from DSTS register */
  24467. + return dsts.b.soffn;
  24468. +}
  24469. +
  24470. +/**
  24471. + * Calculates and gets the frame Interval value of HFIR register according PHY
  24472. + * type and speed.The application can modify a value of HFIR register only after
  24473. + * the Port Enable bit of the Host Port Control and Status register
  24474. + * (HPRT.PrtEnaPort) has been set.
  24475. +*/
  24476. +
  24477. +uint32_t calc_frame_interval(dwc_otg_core_if_t * core_if)
  24478. +{
  24479. + gusbcfg_data_t usbcfg;
  24480. + hwcfg2_data_t hwcfg2;
  24481. + hprt0_data_t hprt0;
  24482. + int clock = 60; // default value
  24483. + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  24484. + hwcfg2.d32 = DWC_READ_REG32(&core_if->core_global_regs->ghwcfg2);
  24485. + hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
  24486. + if (!usbcfg.b.physel && usbcfg.b.ulpi_utmi_sel && !usbcfg.b.phyif)
  24487. + clock = 60;
  24488. + if (usbcfg.b.physel && hwcfg2.b.fs_phy_type == 3)
  24489. + clock = 48;
  24490. + if (!usbcfg.b.phylpwrclksel && !usbcfg.b.physel &&
  24491. + !usbcfg.b.ulpi_utmi_sel && usbcfg.b.phyif)
  24492. + clock = 30;
  24493. + if (!usbcfg.b.phylpwrclksel && !usbcfg.b.physel &&
  24494. + !usbcfg.b.ulpi_utmi_sel && !usbcfg.b.phyif)
  24495. + clock = 60;
  24496. + if (usbcfg.b.phylpwrclksel && !usbcfg.b.physel &&
  24497. + !usbcfg.b.ulpi_utmi_sel && usbcfg.b.phyif)
  24498. + clock = 48;
  24499. + if (usbcfg.b.physel && !usbcfg.b.phyif && hwcfg2.b.fs_phy_type == 2)
  24500. + clock = 48;
  24501. + if (usbcfg.b.physel && hwcfg2.b.fs_phy_type == 1)
  24502. + clock = 48;
  24503. + if (hprt0.b.prtspd == 0)
  24504. + /* High speed case */
  24505. + return 125 * clock - 1;
  24506. + else
  24507. + /* FS/LS case */
  24508. + return 1000 * clock - 1;
  24509. +}
  24510. +
  24511. +/**
  24512. + * This function reads a setup packet from the Rx FIFO into the destination
  24513. + * buffer. This function is called from the Rx Status Queue Level (RxStsQLvl)
  24514. + * Interrupt routine when a SETUP packet has been received in Slave mode.
  24515. + *
  24516. + * @param core_if Programming view of DWC_otg controller.
  24517. + * @param dest Destination buffer for packet data.
  24518. + */
  24519. +void dwc_otg_read_setup_packet(dwc_otg_core_if_t * core_if, uint32_t * dest)
  24520. +{
  24521. + device_grxsts_data_t status;
  24522. + /* Get the 8 bytes of a setup transaction data */
  24523. +
  24524. + /* Pop 2 DWORDS off the receive data FIFO into memory */
  24525. + dest[0] = DWC_READ_REG32(core_if->data_fifo[0]);
  24526. + dest[1] = DWC_READ_REG32(core_if->data_fifo[0]);
  24527. + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
  24528. + status.d32 =
  24529. + DWC_READ_REG32(&core_if->core_global_regs->grxstsp);
  24530. + DWC_DEBUGPL(DBG_ANY,
  24531. + "EP:%d BCnt:%d " "pktsts:%x Frame:%d(0x%0x)\n",
  24532. + status.b.epnum, status.b.bcnt, status.b.pktsts,
  24533. + status.b.fn, status.b.fn);
  24534. + }
  24535. +}
  24536. +
  24537. +/**
  24538. + * This function enables EP0 OUT to receive SETUP packets and configures EP0
  24539. + * IN for transmitting packets. It is normally called when the
  24540. + * "Enumeration Done" interrupt occurs.
  24541. + *
  24542. + * @param core_if Programming view of DWC_otg controller.
  24543. + * @param ep The EP0 data.
  24544. + */
  24545. +void dwc_otg_ep0_activate(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  24546. +{
  24547. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  24548. + dsts_data_t dsts;
  24549. + depctl_data_t diepctl;
  24550. + depctl_data_t doepctl;
  24551. + dctl_data_t dctl = {.d32 = 0 };
  24552. +
  24553. + ep->stp_rollover = 0;
  24554. + /* Read the Device Status and Endpoint 0 Control registers */
  24555. + dsts.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dsts);
  24556. + diepctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[0]->diepctl);
  24557. + doepctl.d32 = DWC_READ_REG32(&dev_if->out_ep_regs[0]->doepctl);
  24558. +
  24559. + /* Set the MPS of the IN EP based on the enumeration speed */
  24560. + switch (dsts.b.enumspd) {
  24561. + case DWC_DSTS_ENUMSPD_HS_PHY_30MHZ_OR_60MHZ:
  24562. + case DWC_DSTS_ENUMSPD_FS_PHY_30MHZ_OR_60MHZ:
  24563. + case DWC_DSTS_ENUMSPD_FS_PHY_48MHZ:
  24564. + diepctl.b.mps = DWC_DEP0CTL_MPS_64;
  24565. + break;
  24566. + case DWC_DSTS_ENUMSPD_LS_PHY_6MHZ:
  24567. + diepctl.b.mps = DWC_DEP0CTL_MPS_8;
  24568. + break;
  24569. + }
  24570. +
  24571. + DWC_WRITE_REG32(&dev_if->in_ep_regs[0]->diepctl, diepctl.d32);
  24572. +
  24573. + /* Enable OUT EP for receive */
  24574. + if (core_if->snpsid <= OTG_CORE_REV_2_94a) {
  24575. + doepctl.b.epena = 1;
  24576. + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doepctl, doepctl.d32);
  24577. + }
  24578. +#ifdef VERBOSE
  24579. + DWC_DEBUGPL(DBG_PCDV, "doepctl0=%0x\n",
  24580. + DWC_READ_REG32(&dev_if->out_ep_regs[0]->doepctl));
  24581. + DWC_DEBUGPL(DBG_PCDV, "diepctl0=%0x\n",
  24582. + DWC_READ_REG32(&dev_if->in_ep_regs[0]->diepctl));
  24583. +#endif
  24584. + dctl.b.cgnpinnak = 1;
  24585. +
  24586. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, dctl.d32, dctl.d32);
  24587. + DWC_DEBUGPL(DBG_PCDV, "dctl=%0x\n",
  24588. + DWC_READ_REG32(&dev_if->dev_global_regs->dctl));
  24589. +
  24590. +}
  24591. +
  24592. +/**
  24593. + * This function activates an EP. The Device EP control register for
  24594. + * the EP is configured as defined in the ep structure. Note: This
  24595. + * function is not used for EP0.
  24596. + *
  24597. + * @param core_if Programming view of DWC_otg controller.
  24598. + * @param ep The EP to activate.
  24599. + */
  24600. +void dwc_otg_ep_activate(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  24601. +{
  24602. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  24603. + depctl_data_t depctl;
  24604. + volatile uint32_t *addr;
  24605. + daint_data_t daintmsk = {.d32 = 0 };
  24606. + dcfg_data_t dcfg;
  24607. + uint8_t i;
  24608. +
  24609. + DWC_DEBUGPL(DBG_PCDV, "%s() EP%d-%s\n", __func__, ep->num,
  24610. + (ep->is_in ? "IN" : "OUT"));
  24611. +
  24612. +#ifdef DWC_UTE_PER_IO
  24613. + ep->xiso_frame_num = 0xFFFFFFFF;
  24614. + ep->xiso_active_xfers = 0;
  24615. + ep->xiso_queued_xfers = 0;
  24616. +#endif
  24617. + /* Read DEPCTLn register */
  24618. + if (ep->is_in == 1) {
  24619. + addr = &dev_if->in_ep_regs[ep->num]->diepctl;
  24620. + daintmsk.ep.in = 1 << ep->num;
  24621. + } else {
  24622. + addr = &dev_if->out_ep_regs[ep->num]->doepctl;
  24623. + daintmsk.ep.out = 1 << ep->num;
  24624. + }
  24625. +
  24626. + /* If the EP is already active don't change the EP Control
  24627. + * register. */
  24628. + depctl.d32 = DWC_READ_REG32(addr);
  24629. + if (!depctl.b.usbactep) {
  24630. + depctl.b.mps = ep->maxpacket;
  24631. + depctl.b.eptype = ep->type;
  24632. + depctl.b.txfnum = ep->tx_fifo_num;
  24633. +
  24634. + if (ep->type == DWC_OTG_EP_TYPE_ISOC) {
  24635. + depctl.b.setd0pid = 1; // ???
  24636. + } else {
  24637. + depctl.b.setd0pid = 1;
  24638. + }
  24639. + depctl.b.usbactep = 1;
  24640. +
  24641. + /* Update nextep_seq array and EPMSCNT in DCFG*/
  24642. + if (!(depctl.b.eptype & 1) && (ep->is_in == 1)) { // NP IN EP
  24643. + for (i = 0; i <= core_if->dev_if->num_in_eps; i++) {
  24644. + if (core_if->nextep_seq[i] == core_if->first_in_nextep_seq)
  24645. + break;
  24646. + }
  24647. + core_if->nextep_seq[i] = ep->num;
  24648. + core_if->nextep_seq[ep->num] = core_if->first_in_nextep_seq;
  24649. + depctl.b.nextep = core_if->nextep_seq[ep->num];
  24650. + dcfg.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dcfg);
  24651. + dcfg.b.epmscnt++;
  24652. + DWC_WRITE_REG32(&dev_if->dev_global_regs->dcfg, dcfg.d32);
  24653. +
  24654. + DWC_DEBUGPL(DBG_PCDV,
  24655. + "%s first_in_nextep_seq= %2d; nextep_seq[]:\n",
  24656. + __func__, core_if->first_in_nextep_seq);
  24657. + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
  24658. + DWC_DEBUGPL(DBG_PCDV, "%2d\n",
  24659. + core_if->nextep_seq[i]);
  24660. + }
  24661. +
  24662. + }
  24663. +
  24664. +
  24665. + DWC_WRITE_REG32(addr, depctl.d32);
  24666. + DWC_DEBUGPL(DBG_PCDV, "DEPCTL=%08x\n", DWC_READ_REG32(addr));
  24667. + }
  24668. +
  24669. + /* Enable the Interrupt for this EP */
  24670. + if (core_if->multiproc_int_enable) {
  24671. + if (ep->is_in == 1) {
  24672. + diepmsk_data_t diepmsk = {.d32 = 0 };
  24673. + diepmsk.b.xfercompl = 1;
  24674. + diepmsk.b.timeout = 1;
  24675. + diepmsk.b.epdisabled = 1;
  24676. + diepmsk.b.ahberr = 1;
  24677. + diepmsk.b.intknepmis = 1;
  24678. + if (!core_if->en_multiple_tx_fifo && core_if->dma_enable)
  24679. + diepmsk.b.intknepmis = 0;
  24680. + diepmsk.b.txfifoundrn = 1; //?????
  24681. + if (ep->type == DWC_OTG_EP_TYPE_ISOC) {
  24682. + diepmsk.b.nak = 1;
  24683. + }
  24684. +
  24685. +
  24686. +
  24687. +/*
  24688. + if (core_if->dma_desc_enable) {
  24689. + diepmsk.b.bna = 1;
  24690. + }
  24691. +*/
  24692. +/*
  24693. + if (core_if->dma_enable) {
  24694. + doepmsk.b.nak = 1;
  24695. + }
  24696. +*/
  24697. + DWC_WRITE_REG32(&dev_if->dev_global_regs->
  24698. + diepeachintmsk[ep->num], diepmsk.d32);
  24699. +
  24700. + } else {
  24701. + doepmsk_data_t doepmsk = {.d32 = 0 };
  24702. + doepmsk.b.xfercompl = 1;
  24703. + doepmsk.b.ahberr = 1;
  24704. + doepmsk.b.epdisabled = 1;
  24705. + if (ep->type == DWC_OTG_EP_TYPE_ISOC)
  24706. + doepmsk.b.outtknepdis = 1;
  24707. +
  24708. +/*
  24709. +
  24710. + if (core_if->dma_desc_enable) {
  24711. + doepmsk.b.bna = 1;
  24712. + }
  24713. +*/
  24714. +/*
  24715. + doepmsk.b.babble = 1;
  24716. + doepmsk.b.nyet = 1;
  24717. + doepmsk.b.nak = 1;
  24718. +*/
  24719. + DWC_WRITE_REG32(&dev_if->dev_global_regs->
  24720. + doepeachintmsk[ep->num], doepmsk.d32);
  24721. + }
  24722. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->deachintmsk,
  24723. + 0, daintmsk.d32);
  24724. + } else {
  24725. + if (ep->type == DWC_OTG_EP_TYPE_ISOC) {
  24726. + if (ep->is_in) {
  24727. + diepmsk_data_t diepmsk = {.d32 = 0 };
  24728. + diepmsk.b.nak = 1;
  24729. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->diepmsk, 0, diepmsk.d32);
  24730. + } else {
  24731. + doepmsk_data_t doepmsk = {.d32 = 0 };
  24732. + doepmsk.b.outtknepdis = 1;
  24733. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->doepmsk, 0, doepmsk.d32);
  24734. + }
  24735. + }
  24736. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->daintmsk,
  24737. + 0, daintmsk.d32);
  24738. + }
  24739. +
  24740. + DWC_DEBUGPL(DBG_PCDV, "DAINTMSK=%0x\n",
  24741. + DWC_READ_REG32(&dev_if->dev_global_regs->daintmsk));
  24742. +
  24743. + ep->stall_clear_flag = 0;
  24744. +
  24745. + return;
  24746. +}
  24747. +
  24748. +/**
  24749. + * This function deactivates an EP. This is done by clearing the USB Active
  24750. + * EP bit in the Device EP control register. Note: This function is not used
  24751. + * for EP0. EP0 cannot be deactivated.
  24752. + *
  24753. + * @param core_if Programming view of DWC_otg controller.
  24754. + * @param ep The EP to deactivate.
  24755. + */
  24756. +void dwc_otg_ep_deactivate(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  24757. +{
  24758. + depctl_data_t depctl = {.d32 = 0 };
  24759. + volatile uint32_t *addr;
  24760. + daint_data_t daintmsk = {.d32 = 0 };
  24761. + dcfg_data_t dcfg;
  24762. + uint8_t i = 0;
  24763. +
  24764. +#ifdef DWC_UTE_PER_IO
  24765. + ep->xiso_frame_num = 0xFFFFFFFF;
  24766. + ep->xiso_active_xfers = 0;
  24767. + ep->xiso_queued_xfers = 0;
  24768. +#endif
  24769. +
  24770. + /* Read DEPCTLn register */
  24771. + if (ep->is_in == 1) {
  24772. + addr = &core_if->dev_if->in_ep_regs[ep->num]->diepctl;
  24773. + daintmsk.ep.in = 1 << ep->num;
  24774. + } else {
  24775. + addr = &core_if->dev_if->out_ep_regs[ep->num]->doepctl;
  24776. + daintmsk.ep.out = 1 << ep->num;
  24777. + }
  24778. +
  24779. + depctl.d32 = DWC_READ_REG32(addr);
  24780. +
  24781. + depctl.b.usbactep = 0;
  24782. +
  24783. + /* Update nextep_seq array and EPMSCNT in DCFG*/
  24784. + if (!(depctl.b.eptype & 1) && ep->is_in == 1) { // NP EP IN
  24785. + for (i = 0; i <= core_if->dev_if->num_in_eps; i++) {
  24786. + if (core_if->nextep_seq[i] == ep->num)
  24787. + break;
  24788. + }
  24789. + core_if->nextep_seq[i] = core_if->nextep_seq[ep->num];
  24790. + if (core_if->first_in_nextep_seq == ep->num)
  24791. + core_if->first_in_nextep_seq = i;
  24792. + core_if->nextep_seq[ep->num] = 0xff;
  24793. + depctl.b.nextep = 0;
  24794. + dcfg.d32 =
  24795. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  24796. + dcfg.b.epmscnt--;
  24797. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg,
  24798. + dcfg.d32);
  24799. +
  24800. + DWC_DEBUGPL(DBG_PCDV,
  24801. + "%s first_in_nextep_seq= %2d; nextep_seq[]:\n",
  24802. + __func__, core_if->first_in_nextep_seq);
  24803. + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
  24804. + DWC_DEBUGPL(DBG_PCDV, "%2d\n", core_if->nextep_seq[i]);
  24805. + }
  24806. + }
  24807. +
  24808. + if (ep->is_in == 1)
  24809. + depctl.b.txfnum = 0;
  24810. +
  24811. + if (core_if->dma_desc_enable)
  24812. + depctl.b.epdis = 1;
  24813. +
  24814. + DWC_WRITE_REG32(addr, depctl.d32);
  24815. + depctl.d32 = DWC_READ_REG32(addr);
  24816. + if (core_if->dma_enable && ep->type == DWC_OTG_EP_TYPE_ISOC
  24817. + && depctl.b.epena) {
  24818. + depctl_data_t depctl = {.d32 = 0};
  24819. + if (ep->is_in) {
  24820. + diepint_data_t diepint = {.d32 = 0};
  24821. +
  24822. + depctl.b.snak = 1;
  24823. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
  24824. + diepctl, depctl.d32);
  24825. + do {
  24826. + dwc_udelay(10);
  24827. + diepint.d32 =
  24828. + DWC_READ_REG32(&core_if->
  24829. + dev_if->in_ep_regs[ep->num]->
  24830. + diepint);
  24831. + } while (!diepint.b.inepnakeff);
  24832. + diepint.b.inepnakeff = 1;
  24833. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
  24834. + diepint, diepint.d32);
  24835. + depctl.d32 = 0;
  24836. + depctl.b.epdis = 1;
  24837. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
  24838. + diepctl, depctl.d32);
  24839. + do {
  24840. + dwc_udelay(10);
  24841. + diepint.d32 =
  24842. + DWC_READ_REG32(&core_if->
  24843. + dev_if->in_ep_regs[ep->num]->
  24844. + diepint);
  24845. + } while (!diepint.b.epdisabled);
  24846. + diepint.b.epdisabled = 1;
  24847. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
  24848. + diepint, diepint.d32);
  24849. + } else {
  24850. + dctl_data_t dctl = {.d32 = 0};
  24851. + gintmsk_data_t gintsts = {.d32 = 0};
  24852. + doepint_data_t doepint = {.d32 = 0};
  24853. + dctl.b.sgoutnak = 1;
  24854. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  24855. + dctl, 0, dctl.d32);
  24856. + do {
  24857. + dwc_udelay(10);
  24858. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  24859. + } while (!gintsts.b.goutnakeff);
  24860. + gintsts.d32 = 0;
  24861. + gintsts.b.goutnakeff = 1;
  24862. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  24863. +
  24864. + depctl.d32 = 0;
  24865. + depctl.b.epdis = 1;
  24866. + depctl.b.snak = 1;
  24867. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[ep->num]->doepctl, depctl.d32);
  24868. + do
  24869. + {
  24870. + dwc_udelay(10);
  24871. + doepint.d32 = DWC_READ_REG32(&core_if->dev_if->
  24872. + out_ep_regs[ep->num]->doepint);
  24873. + } while (!doepint.b.epdisabled);
  24874. +
  24875. + doepint.b.epdisabled = 1;
  24876. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[ep->num]->doepint, doepint.d32);
  24877. +
  24878. + dctl.d32 = 0;
  24879. + dctl.b.cgoutnak = 1;
  24880. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
  24881. + }
  24882. + }
  24883. +
  24884. + /* Disable the Interrupt for this EP */
  24885. + if (core_if->multiproc_int_enable) {
  24886. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->deachintmsk,
  24887. + daintmsk.d32, 0);
  24888. +
  24889. + if (ep->is_in == 1) {
  24890. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->
  24891. + diepeachintmsk[ep->num], 0);
  24892. + } else {
  24893. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->
  24894. + doepeachintmsk[ep->num], 0);
  24895. + }
  24896. + } else {
  24897. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->daintmsk,
  24898. + daintmsk.d32, 0);
  24899. + }
  24900. +
  24901. +}
  24902. +
  24903. +/**
  24904. + * This function initializes dma descriptor chain.
  24905. + *
  24906. + * @param core_if Programming view of DWC_otg controller.
  24907. + * @param ep The EP to start the transfer on.
  24908. + */
  24909. +static void init_dma_desc_chain(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  24910. +{
  24911. + dwc_otg_dev_dma_desc_t *dma_desc;
  24912. + uint32_t offset;
  24913. + uint32_t xfer_est;
  24914. + int i;
  24915. + unsigned maxxfer_local, total_len;
  24916. +
  24917. + if (!ep->is_in && ep->type == DWC_OTG_EP_TYPE_INTR &&
  24918. + (ep->maxpacket%4)) {
  24919. + maxxfer_local = ep->maxpacket;
  24920. + total_len = ep->xfer_len;
  24921. + } else {
  24922. + maxxfer_local = ep->maxxfer;
  24923. + total_len = ep->total_len;
  24924. + }
  24925. +
  24926. + ep->desc_cnt = (total_len / maxxfer_local) +
  24927. + ((total_len % maxxfer_local) ? 1 : 0);
  24928. +
  24929. + if (!ep->desc_cnt)
  24930. + ep->desc_cnt = 1;
  24931. +
  24932. + if (ep->desc_cnt > MAX_DMA_DESC_CNT)
  24933. + ep->desc_cnt = MAX_DMA_DESC_CNT;
  24934. +
  24935. + dma_desc = ep->desc_addr;
  24936. + if (maxxfer_local == ep->maxpacket) {
  24937. + if ((total_len % maxxfer_local) &&
  24938. + (total_len/maxxfer_local < MAX_DMA_DESC_CNT)) {
  24939. + xfer_est = (ep->desc_cnt - 1) * maxxfer_local +
  24940. + (total_len % maxxfer_local);
  24941. + } else
  24942. + xfer_est = ep->desc_cnt * maxxfer_local;
  24943. + } else
  24944. + xfer_est = total_len;
  24945. + offset = 0;
  24946. + for (i = 0; i < ep->desc_cnt; ++i) {
  24947. + /** DMA Descriptor Setup */
  24948. + if (xfer_est > maxxfer_local) {
  24949. + dma_desc->status.b.bs = BS_HOST_BUSY;
  24950. + dma_desc->status.b.l = 0;
  24951. + dma_desc->status.b.ioc = 0;
  24952. + dma_desc->status.b.sp = 0;
  24953. + dma_desc->status.b.bytes = maxxfer_local;
  24954. + dma_desc->buf = ep->dma_addr + offset;
  24955. + dma_desc->status.b.sts = 0;
  24956. + dma_desc->status.b.bs = BS_HOST_READY;
  24957. +
  24958. + xfer_est -= maxxfer_local;
  24959. + offset += maxxfer_local;
  24960. + } else {
  24961. + dma_desc->status.b.bs = BS_HOST_BUSY;
  24962. + dma_desc->status.b.l = 1;
  24963. + dma_desc->status.b.ioc = 1;
  24964. + if (ep->is_in) {
  24965. + dma_desc->status.b.sp =
  24966. + (xfer_est %
  24967. + ep->maxpacket) ? 1 : ((ep->
  24968. + sent_zlp) ? 1 : 0);
  24969. + dma_desc->status.b.bytes = xfer_est;
  24970. + } else {
  24971. + if (maxxfer_local == ep->maxpacket)
  24972. + dma_desc->status.b.bytes = xfer_est;
  24973. + else
  24974. + dma_desc->status.b.bytes =
  24975. + xfer_est + ((4 - (xfer_est & 0x3)) & 0x3);
  24976. + }
  24977. +
  24978. + dma_desc->buf = ep->dma_addr + offset;
  24979. + dma_desc->status.b.sts = 0;
  24980. + dma_desc->status.b.bs = BS_HOST_READY;
  24981. + }
  24982. + dma_desc++;
  24983. + }
  24984. +}
  24985. +/**
  24986. + * This function is called when to write ISOC data into appropriate dedicated
  24987. + * periodic FIFO.
  24988. + */
  24989. +static int32_t write_isoc_tx_fifo(dwc_otg_core_if_t * core_if, dwc_ep_t * dwc_ep)
  24990. +{
  24991. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  24992. + dwc_otg_dev_in_ep_regs_t *ep_regs;
  24993. + dtxfsts_data_t txstatus = {.d32 = 0 };
  24994. + uint32_t len = 0;
  24995. + int epnum = dwc_ep->num;
  24996. + int dwords;
  24997. +
  24998. + DWC_DEBUGPL(DBG_PCD, "Dedicated TxFifo Empty: %d \n", epnum);
  24999. +
  25000. + ep_regs = core_if->dev_if->in_ep_regs[epnum];
  25001. +
  25002. + len = dwc_ep->xfer_len - dwc_ep->xfer_count;
  25003. +
  25004. + if (len > dwc_ep->maxpacket) {
  25005. + len = dwc_ep->maxpacket;
  25006. + }
  25007. +
  25008. + dwords = (len + 3) / 4;
  25009. +
  25010. + /* While there is space in the queue and space in the FIFO and
  25011. + * More data to tranfer, Write packets to the Tx FIFO */
  25012. + txstatus.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts);
  25013. + DWC_DEBUGPL(DBG_PCDV, "b4 dtxfsts[%d]=0x%08x\n", epnum, txstatus.d32);
  25014. +
  25015. + while (txstatus.b.txfspcavail > dwords &&
  25016. + dwc_ep->xfer_count < dwc_ep->xfer_len && dwc_ep->xfer_len != 0) {
  25017. + /* Write the FIFO */
  25018. + dwc_otg_ep_write_packet(core_if, dwc_ep, 0);
  25019. +
  25020. + len = dwc_ep->xfer_len - dwc_ep->xfer_count;
  25021. + if (len > dwc_ep->maxpacket) {
  25022. + len = dwc_ep->maxpacket;
  25023. + }
  25024. +
  25025. + dwords = (len + 3) / 4;
  25026. + txstatus.d32 =
  25027. + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts);
  25028. + DWC_DEBUGPL(DBG_PCDV, "dtxfsts[%d]=0x%08x\n", epnum,
  25029. + txstatus.d32);
  25030. + }
  25031. +
  25032. + DWC_DEBUGPL(DBG_PCDV, "b4 dtxfsts[%d]=0x%08x\n", epnum,
  25033. + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts));
  25034. +
  25035. + return 1;
  25036. +}
  25037. +/**
  25038. + * This function does the setup for a data transfer for an EP and
  25039. + * starts the transfer. For an IN transfer, the packets will be
  25040. + * loaded into the appropriate Tx FIFO in the ISR. For OUT transfers,
  25041. + * the packets are unloaded from the Rx FIFO in the ISR. the ISR.
  25042. + *
  25043. + * @param core_if Programming view of DWC_otg controller.
  25044. + * @param ep The EP to start the transfer on.
  25045. + */
  25046. +
  25047. +void dwc_otg_ep_start_transfer(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  25048. +{
  25049. + depctl_data_t depctl;
  25050. + deptsiz_data_t deptsiz;
  25051. + gintmsk_data_t intr_mask = {.d32 = 0 };
  25052. +
  25053. + DWC_DEBUGPL((DBG_PCDV | DBG_CILV), "%s()\n", __func__);
  25054. + DWC_DEBUGPL(DBG_PCD, "ep%d-%s xfer_len=%d xfer_cnt=%d "
  25055. + "xfer_buff=%p start_xfer_buff=%p, total_len = %d\n",
  25056. + ep->num, (ep->is_in ? "IN" : "OUT"), ep->xfer_len,
  25057. + ep->xfer_count, ep->xfer_buff, ep->start_xfer_buff,
  25058. + ep->total_len);
  25059. + /* IN endpoint */
  25060. + if (ep->is_in == 1) {
  25061. + dwc_otg_dev_in_ep_regs_t *in_regs =
  25062. + core_if->dev_if->in_ep_regs[ep->num];
  25063. +
  25064. + gnptxsts_data_t gtxstatus;
  25065. +
  25066. + gtxstatus.d32 =
  25067. + DWC_READ_REG32(&core_if->core_global_regs->gnptxsts);
  25068. +
  25069. + if (core_if->en_multiple_tx_fifo == 0
  25070. + && gtxstatus.b.nptxqspcavail == 0 && !core_if->dma_enable) {
  25071. +#ifdef DEBUG
  25072. + DWC_PRINTF("TX Queue Full (0x%0x)\n", gtxstatus.d32);
  25073. +#endif
  25074. + return;
  25075. + }
  25076. +
  25077. + depctl.d32 = DWC_READ_REG32(&(in_regs->diepctl));
  25078. + deptsiz.d32 = DWC_READ_REG32(&(in_regs->dieptsiz));
  25079. +
  25080. + if (ep->maxpacket > ep->maxxfer / MAX_PKT_CNT)
  25081. + ep->xfer_len += (ep->maxxfer < (ep->total_len - ep->xfer_len)) ?
  25082. + ep->maxxfer : (ep->total_len - ep->xfer_len);
  25083. + else
  25084. + ep->xfer_len += (MAX_PKT_CNT * ep->maxpacket < (ep->total_len - ep->xfer_len)) ?
  25085. + MAX_PKT_CNT * ep->maxpacket : (ep->total_len - ep->xfer_len);
  25086. +
  25087. +
  25088. + /* Zero Length Packet? */
  25089. + if ((ep->xfer_len - ep->xfer_count) == 0) {
  25090. + deptsiz.b.xfersize = 0;
  25091. + deptsiz.b.pktcnt = 1;
  25092. + } else {
  25093. + /* Program the transfer size and packet count
  25094. + * as follows: xfersize = N * maxpacket +
  25095. + * short_packet pktcnt = N + (short_packet
  25096. + * exist ? 1 : 0)
  25097. + */
  25098. + deptsiz.b.xfersize = ep->xfer_len - ep->xfer_count;
  25099. + deptsiz.b.pktcnt =
  25100. + (ep->xfer_len - ep->xfer_count - 1 +
  25101. + ep->maxpacket) / ep->maxpacket;
  25102. + if (deptsiz.b.pktcnt > MAX_PKT_CNT) {
  25103. + deptsiz.b.pktcnt = MAX_PKT_CNT;
  25104. + deptsiz.b.xfersize = deptsiz.b.pktcnt * ep->maxpacket;
  25105. + }
  25106. + if (ep->type == DWC_OTG_EP_TYPE_ISOC)
  25107. + deptsiz.b.mc = deptsiz.b.pktcnt;
  25108. + }
  25109. +
  25110. + /* Write the DMA register */
  25111. + if (core_if->dma_enable) {
  25112. + if (core_if->dma_desc_enable == 0) {
  25113. + if (ep->type != DWC_OTG_EP_TYPE_ISOC)
  25114. + deptsiz.b.mc = 1;
  25115. + DWC_WRITE_REG32(&in_regs->dieptsiz,
  25116. + deptsiz.d32);
  25117. + DWC_WRITE_REG32(&(in_regs->diepdma),
  25118. + (uint32_t) ep->dma_addr);
  25119. + } else {
  25120. +#ifdef DWC_UTE_CFI
  25121. + /* The descriptor chain should be already initialized by now */
  25122. + if (ep->buff_mode != BM_STANDARD) {
  25123. + DWC_WRITE_REG32(&in_regs->diepdma,
  25124. + ep->descs_dma_addr);
  25125. + } else {
  25126. +#endif
  25127. + init_dma_desc_chain(core_if, ep);
  25128. + /** DIEPDMAn Register write */
  25129. + DWC_WRITE_REG32(&in_regs->diepdma,
  25130. + ep->dma_desc_addr);
  25131. +#ifdef DWC_UTE_CFI
  25132. + }
  25133. +#endif
  25134. + }
  25135. + } else {
  25136. + DWC_WRITE_REG32(&in_regs->dieptsiz, deptsiz.d32);
  25137. + if (ep->type != DWC_OTG_EP_TYPE_ISOC) {
  25138. + /**
  25139. + * Enable the Non-Periodic Tx FIFO empty interrupt,
  25140. + * or the Tx FIFO epmty interrupt in dedicated Tx FIFO mode,
  25141. + * the data will be written into the fifo by the ISR.
  25142. + */
  25143. + if (core_if->en_multiple_tx_fifo == 0) {
  25144. + intr_mask.b.nptxfempty = 1;
  25145. + DWC_MODIFY_REG32
  25146. + (&core_if->core_global_regs->gintmsk,
  25147. + intr_mask.d32, intr_mask.d32);
  25148. + } else {
  25149. + /* Enable the Tx FIFO Empty Interrupt for this EP */
  25150. + if (ep->xfer_len > 0) {
  25151. + uint32_t fifoemptymsk = 0;
  25152. + fifoemptymsk = 1 << ep->num;
  25153. + DWC_MODIFY_REG32
  25154. + (&core_if->dev_if->dev_global_regs->dtknqr4_fifoemptymsk,
  25155. + 0, fifoemptymsk);
  25156. +
  25157. + }
  25158. + }
  25159. + } else {
  25160. + write_isoc_tx_fifo(core_if, ep);
  25161. + }
  25162. + }
  25163. + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable)
  25164. + depctl.b.nextep = core_if->nextep_seq[ep->num];
  25165. +
  25166. + if (ep->type == DWC_OTG_EP_TYPE_ISOC) {
  25167. + dsts_data_t dsts = {.d32 = 0};
  25168. + if (ep->bInterval == 1) {
  25169. + dsts.d32 =
  25170. + DWC_READ_REG32(&core_if->dev_if->
  25171. + dev_global_regs->dsts);
  25172. + ep->frame_num = dsts.b.soffn + ep->bInterval;
  25173. + if (ep->frame_num > 0x3FFF) {
  25174. + ep->frm_overrun = 1;
  25175. + ep->frame_num &= 0x3FFF;
  25176. + } else
  25177. + ep->frm_overrun = 0;
  25178. + if (ep->frame_num & 0x1) {
  25179. + depctl.b.setd1pid = 1;
  25180. + } else {
  25181. + depctl.b.setd0pid = 1;
  25182. + }
  25183. + }
  25184. + }
  25185. + /* EP enable, IN data in FIFO */
  25186. + depctl.b.cnak = 1;
  25187. + depctl.b.epena = 1;
  25188. + DWC_WRITE_REG32(&in_regs->diepctl, depctl.d32);
  25189. +
  25190. + } else {
  25191. + /* OUT endpoint */
  25192. + dwc_otg_dev_out_ep_regs_t *out_regs =
  25193. + core_if->dev_if->out_ep_regs[ep->num];
  25194. +
  25195. + depctl.d32 = DWC_READ_REG32(&(out_regs->doepctl));
  25196. + deptsiz.d32 = DWC_READ_REG32(&(out_regs->doeptsiz));
  25197. +
  25198. + if (!core_if->dma_desc_enable) {
  25199. + if (ep->maxpacket > ep->maxxfer / MAX_PKT_CNT)
  25200. + ep->xfer_len += (ep->maxxfer < (ep->total_len - ep->xfer_len)) ?
  25201. + ep->maxxfer : (ep->total_len - ep->xfer_len);
  25202. + else
  25203. + ep->xfer_len += (MAX_PKT_CNT * ep->maxpacket < (ep->total_len
  25204. + - ep->xfer_len)) ? MAX_PKT_CNT * ep->maxpacket : (ep->total_len - ep->xfer_len);
  25205. + }
  25206. +
  25207. + /* Program the transfer size and packet count as follows:
  25208. + *
  25209. + * pktcnt = N
  25210. + * xfersize = N * maxpacket
  25211. + */
  25212. + if ((ep->xfer_len - ep->xfer_count) == 0) {
  25213. + /* Zero Length Packet */
  25214. + deptsiz.b.xfersize = ep->maxpacket;
  25215. + deptsiz.b.pktcnt = 1;
  25216. + } else {
  25217. + deptsiz.b.pktcnt =
  25218. + (ep->xfer_len - ep->xfer_count +
  25219. + (ep->maxpacket - 1)) / ep->maxpacket;
  25220. + if (deptsiz.b.pktcnt > MAX_PKT_CNT) {
  25221. + deptsiz.b.pktcnt = MAX_PKT_CNT;
  25222. + }
  25223. + if (!core_if->dma_desc_enable) {
  25224. + ep->xfer_len =
  25225. + deptsiz.b.pktcnt * ep->maxpacket + ep->xfer_count;
  25226. + }
  25227. + deptsiz.b.xfersize = ep->xfer_len - ep->xfer_count;
  25228. + }
  25229. +
  25230. + DWC_DEBUGPL(DBG_PCDV, "ep%d xfersize=%d pktcnt=%d\n",
  25231. + ep->num, deptsiz.b.xfersize, deptsiz.b.pktcnt);
  25232. +
  25233. + if (core_if->dma_enable) {
  25234. + if (!core_if->dma_desc_enable) {
  25235. + DWC_WRITE_REG32(&out_regs->doeptsiz,
  25236. + deptsiz.d32);
  25237. +
  25238. + DWC_WRITE_REG32(&(out_regs->doepdma),
  25239. + (uint32_t) ep->dma_addr);
  25240. + } else {
  25241. +#ifdef DWC_UTE_CFI
  25242. + /* The descriptor chain should be already initialized by now */
  25243. + if (ep->buff_mode != BM_STANDARD) {
  25244. + DWC_WRITE_REG32(&out_regs->doepdma,
  25245. + ep->descs_dma_addr);
  25246. + } else {
  25247. +#endif
  25248. + /** This is used for interrupt out transfers*/
  25249. + if (!ep->xfer_len)
  25250. + ep->xfer_len = ep->total_len;
  25251. + init_dma_desc_chain(core_if, ep);
  25252. +
  25253. + if (core_if->core_params->dev_out_nak) {
  25254. + if (ep->type == DWC_OTG_EP_TYPE_BULK) {
  25255. + deptsiz.b.pktcnt = (ep->total_len +
  25256. + (ep->maxpacket - 1)) / ep->maxpacket;
  25257. + deptsiz.b.xfersize = ep->total_len;
  25258. + /* Remember initial value of doeptsiz */
  25259. + core_if->start_doeptsiz_val[ep->num] = deptsiz.d32;
  25260. + DWC_WRITE_REG32(&out_regs->doeptsiz,
  25261. + deptsiz.d32);
  25262. + }
  25263. + }
  25264. + /** DOEPDMAn Register write */
  25265. + DWC_WRITE_REG32(&out_regs->doepdma,
  25266. + ep->dma_desc_addr);
  25267. +#ifdef DWC_UTE_CFI
  25268. + }
  25269. +#endif
  25270. + }
  25271. + } else {
  25272. + DWC_WRITE_REG32(&out_regs->doeptsiz, deptsiz.d32);
  25273. + }
  25274. +
  25275. + if (ep->type == DWC_OTG_EP_TYPE_ISOC) {
  25276. + dsts_data_t dsts = {.d32 = 0};
  25277. + if (ep->bInterval == 1) {
  25278. + dsts.d32 =
  25279. + DWC_READ_REG32(&core_if->dev_if->
  25280. + dev_global_regs->dsts);
  25281. + ep->frame_num = dsts.b.soffn + ep->bInterval;
  25282. + if (ep->frame_num > 0x3FFF) {
  25283. + ep->frm_overrun = 1;
  25284. + ep->frame_num &= 0x3FFF;
  25285. + } else
  25286. + ep->frm_overrun = 0;
  25287. +
  25288. + if (ep->frame_num & 0x1) {
  25289. + depctl.b.setd1pid = 1;
  25290. + } else {
  25291. + depctl.b.setd0pid = 1;
  25292. + }
  25293. + }
  25294. + }
  25295. +
  25296. + /* EP enable */
  25297. + depctl.b.cnak = 1;
  25298. + depctl.b.epena = 1;
  25299. +
  25300. + DWC_WRITE_REG32(&out_regs->doepctl, depctl.d32);
  25301. +
  25302. + DWC_DEBUGPL(DBG_PCD, "DOEPCTL=%08x DOEPTSIZ=%08x\n",
  25303. + DWC_READ_REG32(&out_regs->doepctl),
  25304. + DWC_READ_REG32(&out_regs->doeptsiz));
  25305. + DWC_DEBUGPL(DBG_PCD, "DAINTMSK=%08x GINTMSK=%08x\n",
  25306. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->
  25307. + daintmsk),
  25308. + DWC_READ_REG32(&core_if->core_global_regs->
  25309. + gintmsk));
  25310. +
  25311. + /* Timer is scheduling only for out bulk transfers for
  25312. + * "Device DDMA OUT NAK Enhancement" feature to inform user
  25313. + * about received data payload in case of timeout
  25314. + */
  25315. + if (core_if->core_params->dev_out_nak) {
  25316. + if (ep->type == DWC_OTG_EP_TYPE_BULK) {
  25317. + core_if->ep_xfer_info[ep->num].core_if = core_if;
  25318. + core_if->ep_xfer_info[ep->num].ep = ep;
  25319. + core_if->ep_xfer_info[ep->num].state = 1;
  25320. +
  25321. + /* Start a timer for this transfer. */
  25322. + DWC_TIMER_SCHEDULE(core_if->ep_xfer_timer[ep->num], 10000);
  25323. + }
  25324. + }
  25325. + }
  25326. +}
  25327. +
  25328. +/**
  25329. + * This function setup a zero length transfer in Buffer DMA and
  25330. + * Slave modes for usb requests with zero field set
  25331. + *
  25332. + * @param core_if Programming view of DWC_otg controller.
  25333. + * @param ep The EP to start the transfer on.
  25334. + *
  25335. + */
  25336. +void dwc_otg_ep_start_zl_transfer(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  25337. +{
  25338. +
  25339. + depctl_data_t depctl;
  25340. + deptsiz_data_t deptsiz;
  25341. + gintmsk_data_t intr_mask = {.d32 = 0 };
  25342. +
  25343. + DWC_DEBUGPL((DBG_PCDV | DBG_CILV), "%s()\n", __func__);
  25344. + DWC_PRINTF("zero length transfer is called\n");
  25345. +
  25346. + /* IN endpoint */
  25347. + if (ep->is_in == 1) {
  25348. + dwc_otg_dev_in_ep_regs_t *in_regs =
  25349. + core_if->dev_if->in_ep_regs[ep->num];
  25350. +
  25351. + depctl.d32 = DWC_READ_REG32(&(in_regs->diepctl));
  25352. + deptsiz.d32 = DWC_READ_REG32(&(in_regs->dieptsiz));
  25353. +
  25354. + deptsiz.b.xfersize = 0;
  25355. + deptsiz.b.pktcnt = 1;
  25356. +
  25357. + /* Write the DMA register */
  25358. + if (core_if->dma_enable) {
  25359. + if (core_if->dma_desc_enable == 0) {
  25360. + deptsiz.b.mc = 1;
  25361. + DWC_WRITE_REG32(&in_regs->dieptsiz,
  25362. + deptsiz.d32);
  25363. + DWC_WRITE_REG32(&(in_regs->diepdma),
  25364. + (uint32_t) ep->dma_addr);
  25365. + }
  25366. + } else {
  25367. + DWC_WRITE_REG32(&in_regs->dieptsiz, deptsiz.d32);
  25368. + /**
  25369. + * Enable the Non-Periodic Tx FIFO empty interrupt,
  25370. + * or the Tx FIFO epmty interrupt in dedicated Tx FIFO mode,
  25371. + * the data will be written into the fifo by the ISR.
  25372. + */
  25373. + if (core_if->en_multiple_tx_fifo == 0) {
  25374. + intr_mask.b.nptxfempty = 1;
  25375. + DWC_MODIFY_REG32(&core_if->
  25376. + core_global_regs->gintmsk,
  25377. + intr_mask.d32, intr_mask.d32);
  25378. + } else {
  25379. + /* Enable the Tx FIFO Empty Interrupt for this EP */
  25380. + if (ep->xfer_len > 0) {
  25381. + uint32_t fifoemptymsk = 0;
  25382. + fifoemptymsk = 1 << ep->num;
  25383. + DWC_MODIFY_REG32(&core_if->
  25384. + dev_if->dev_global_regs->dtknqr4_fifoemptymsk,
  25385. + 0, fifoemptymsk);
  25386. + }
  25387. + }
  25388. + }
  25389. +
  25390. + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable)
  25391. + depctl.b.nextep = core_if->nextep_seq[ep->num];
  25392. + /* EP enable, IN data in FIFO */
  25393. + depctl.b.cnak = 1;
  25394. + depctl.b.epena = 1;
  25395. + DWC_WRITE_REG32(&in_regs->diepctl, depctl.d32);
  25396. +
  25397. + } else {
  25398. + /* OUT endpoint */
  25399. + dwc_otg_dev_out_ep_regs_t *out_regs =
  25400. + core_if->dev_if->out_ep_regs[ep->num];
  25401. +
  25402. + depctl.d32 = DWC_READ_REG32(&(out_regs->doepctl));
  25403. + deptsiz.d32 = DWC_READ_REG32(&(out_regs->doeptsiz));
  25404. +
  25405. + /* Zero Length Packet */
  25406. + deptsiz.b.xfersize = ep->maxpacket;
  25407. + deptsiz.b.pktcnt = 1;
  25408. +
  25409. + if (core_if->dma_enable) {
  25410. + if (!core_if->dma_desc_enable) {
  25411. + DWC_WRITE_REG32(&out_regs->doeptsiz,
  25412. + deptsiz.d32);
  25413. +
  25414. + DWC_WRITE_REG32(&(out_regs->doepdma),
  25415. + (uint32_t) ep->dma_addr);
  25416. + }
  25417. + } else {
  25418. + DWC_WRITE_REG32(&out_regs->doeptsiz, deptsiz.d32);
  25419. + }
  25420. +
  25421. + /* EP enable */
  25422. + depctl.b.cnak = 1;
  25423. + depctl.b.epena = 1;
  25424. +
  25425. + DWC_WRITE_REG32(&out_regs->doepctl, depctl.d32);
  25426. +
  25427. + }
  25428. +}
  25429. +
  25430. +/**
  25431. + * This function does the setup for a data transfer for EP0 and starts
  25432. + * the transfer. For an IN transfer, the packets will be loaded into
  25433. + * the appropriate Tx FIFO in the ISR. For OUT transfers, the packets are
  25434. + * unloaded from the Rx FIFO in the ISR.
  25435. + *
  25436. + * @param core_if Programming view of DWC_otg controller.
  25437. + * @param ep The EP0 data.
  25438. + */
  25439. +void dwc_otg_ep0_start_transfer(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  25440. +{
  25441. + depctl_data_t depctl;
  25442. + deptsiz0_data_t deptsiz;
  25443. + gintmsk_data_t intr_mask = {.d32 = 0 };
  25444. + dwc_otg_dev_dma_desc_t *dma_desc;
  25445. +
  25446. + DWC_DEBUGPL(DBG_PCD, "ep%d-%s xfer_len=%d xfer_cnt=%d "
  25447. + "xfer_buff=%p start_xfer_buff=%p \n",
  25448. + ep->num, (ep->is_in ? "IN" : "OUT"), ep->xfer_len,
  25449. + ep->xfer_count, ep->xfer_buff, ep->start_xfer_buff);
  25450. +
  25451. + ep->total_len = ep->xfer_len;
  25452. +
  25453. + /* IN endpoint */
  25454. + if (ep->is_in == 1) {
  25455. + dwc_otg_dev_in_ep_regs_t *in_regs =
  25456. + core_if->dev_if->in_ep_regs[0];
  25457. +
  25458. + gnptxsts_data_t gtxstatus;
  25459. +
  25460. + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
  25461. + depctl.d32 = DWC_READ_REG32(&in_regs->diepctl);
  25462. + if (depctl.b.epena)
  25463. + return;
  25464. + }
  25465. +
  25466. + gtxstatus.d32 =
  25467. + DWC_READ_REG32(&core_if->core_global_regs->gnptxsts);
  25468. +
  25469. + /* If dedicated FIFO every time flush fifo before enable ep*/
  25470. + if (core_if->en_multiple_tx_fifo && core_if->snpsid >= OTG_CORE_REV_3_00a)
  25471. + dwc_otg_flush_tx_fifo(core_if, ep->tx_fifo_num);
  25472. +
  25473. + if (core_if->en_multiple_tx_fifo == 0
  25474. + && gtxstatus.b.nptxqspcavail == 0
  25475. + && !core_if->dma_enable) {
  25476. +#ifdef DEBUG
  25477. + deptsiz.d32 = DWC_READ_REG32(&in_regs->dieptsiz);
  25478. + DWC_DEBUGPL(DBG_PCD, "DIEPCTL0=%0x\n",
  25479. + DWC_READ_REG32(&in_regs->diepctl));
  25480. + DWC_DEBUGPL(DBG_PCD, "DIEPTSIZ0=%0x (sz=%d, pcnt=%d)\n",
  25481. + deptsiz.d32,
  25482. + deptsiz.b.xfersize, deptsiz.b.pktcnt);
  25483. + DWC_PRINTF("TX Queue or FIFO Full (0x%0x)\n",
  25484. + gtxstatus.d32);
  25485. +#endif
  25486. + return;
  25487. + }
  25488. +
  25489. + depctl.d32 = DWC_READ_REG32(&in_regs->diepctl);
  25490. + deptsiz.d32 = DWC_READ_REG32(&in_regs->dieptsiz);
  25491. +
  25492. + /* Zero Length Packet? */
  25493. + if (ep->xfer_len == 0) {
  25494. + deptsiz.b.xfersize = 0;
  25495. + deptsiz.b.pktcnt = 1;
  25496. + } else {
  25497. + /* Program the transfer size and packet count
  25498. + * as follows: xfersize = N * maxpacket +
  25499. + * short_packet pktcnt = N + (short_packet
  25500. + * exist ? 1 : 0)
  25501. + */
  25502. + if (ep->xfer_len > ep->maxpacket) {
  25503. + ep->xfer_len = ep->maxpacket;
  25504. + deptsiz.b.xfersize = ep->maxpacket;
  25505. + } else {
  25506. + deptsiz.b.xfersize = ep->xfer_len;
  25507. + }
  25508. + deptsiz.b.pktcnt = 1;
  25509. +
  25510. + }
  25511. + DWC_DEBUGPL(DBG_PCDV,
  25512. + "IN len=%d xfersize=%d pktcnt=%d [%08x]\n",
  25513. + ep->xfer_len, deptsiz.b.xfersize, deptsiz.b.pktcnt,
  25514. + deptsiz.d32);
  25515. +
  25516. + /* Write the DMA register */
  25517. + if (core_if->dma_enable) {
  25518. + if (core_if->dma_desc_enable == 0) {
  25519. + DWC_WRITE_REG32(&in_regs->dieptsiz,
  25520. + deptsiz.d32);
  25521. +
  25522. + DWC_WRITE_REG32(&(in_regs->diepdma),
  25523. + (uint32_t) ep->dma_addr);
  25524. + } else {
  25525. + dma_desc = core_if->dev_if->in_desc_addr;
  25526. +
  25527. + /** DMA Descriptor Setup */
  25528. + dma_desc->status.b.bs = BS_HOST_BUSY;
  25529. + dma_desc->status.b.l = 1;
  25530. + dma_desc->status.b.ioc = 1;
  25531. + dma_desc->status.b.sp =
  25532. + (ep->xfer_len == ep->maxpacket) ? 0 : 1;
  25533. + dma_desc->status.b.bytes = ep->xfer_len;
  25534. + dma_desc->buf = ep->dma_addr;
  25535. + dma_desc->status.b.sts = 0;
  25536. + dma_desc->status.b.bs = BS_HOST_READY;
  25537. +
  25538. + /** DIEPDMA0 Register write */
  25539. + DWC_WRITE_REG32(&in_regs->diepdma,
  25540. + core_if->
  25541. + dev_if->dma_in_desc_addr);
  25542. + }
  25543. + } else {
  25544. + DWC_WRITE_REG32(&in_regs->dieptsiz, deptsiz.d32);
  25545. + }
  25546. +
  25547. + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable)
  25548. + depctl.b.nextep = core_if->nextep_seq[ep->num];
  25549. + /* EP enable, IN data in FIFO */
  25550. + depctl.b.cnak = 1;
  25551. + depctl.b.epena = 1;
  25552. + DWC_WRITE_REG32(&in_regs->diepctl, depctl.d32);
  25553. +
  25554. + /**
  25555. + * Enable the Non-Periodic Tx FIFO empty interrupt, the
  25556. + * data will be written into the fifo by the ISR.
  25557. + */
  25558. + if (!core_if->dma_enable) {
  25559. + if (core_if->en_multiple_tx_fifo == 0) {
  25560. + intr_mask.b.nptxfempty = 1;
  25561. + DWC_MODIFY_REG32(&core_if->
  25562. + core_global_regs->gintmsk,
  25563. + intr_mask.d32, intr_mask.d32);
  25564. + } else {
  25565. + /* Enable the Tx FIFO Empty Interrupt for this EP */
  25566. + if (ep->xfer_len > 0) {
  25567. + uint32_t fifoemptymsk = 0;
  25568. + fifoemptymsk |= 1 << ep->num;
  25569. + DWC_MODIFY_REG32(&core_if->
  25570. + dev_if->dev_global_regs->dtknqr4_fifoemptymsk,
  25571. + 0, fifoemptymsk);
  25572. + }
  25573. + }
  25574. + }
  25575. + } else {
  25576. + /* OUT endpoint */
  25577. + dwc_otg_dev_out_ep_regs_t *out_regs =
  25578. + core_if->dev_if->out_ep_regs[0];
  25579. +
  25580. + depctl.d32 = DWC_READ_REG32(&out_regs->doepctl);
  25581. + deptsiz.d32 = DWC_READ_REG32(&out_regs->doeptsiz);
  25582. +
  25583. + /* Program the transfer size and packet count as follows:
  25584. + * xfersize = N * (maxpacket + 4 - (maxpacket % 4))
  25585. + * pktcnt = N */
  25586. + /* Zero Length Packet */
  25587. + deptsiz.b.xfersize = ep->maxpacket;
  25588. + deptsiz.b.pktcnt = 1;
  25589. + if (core_if->snpsid >= OTG_CORE_REV_3_00a)
  25590. + deptsiz.b.supcnt = 3;
  25591. +
  25592. + DWC_DEBUGPL(DBG_PCDV, "len=%d xfersize=%d pktcnt=%d\n",
  25593. + ep->xfer_len, deptsiz.b.xfersize, deptsiz.b.pktcnt);
  25594. +
  25595. + if (core_if->dma_enable) {
  25596. + if (!core_if->dma_desc_enable) {
  25597. + DWC_WRITE_REG32(&out_regs->doeptsiz,
  25598. + deptsiz.d32);
  25599. +
  25600. + DWC_WRITE_REG32(&(out_regs->doepdma),
  25601. + (uint32_t) ep->dma_addr);
  25602. + } else {
  25603. + dma_desc = core_if->dev_if->out_desc_addr;
  25604. +
  25605. + /** DMA Descriptor Setup */
  25606. + dma_desc->status.b.bs = BS_HOST_BUSY;
  25607. + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
  25608. + dma_desc->status.b.mtrf = 0;
  25609. + dma_desc->status.b.sr = 0;
  25610. + }
  25611. + dma_desc->status.b.l = 1;
  25612. + dma_desc->status.b.ioc = 1;
  25613. + dma_desc->status.b.bytes = ep->maxpacket;
  25614. + dma_desc->buf = ep->dma_addr;
  25615. + dma_desc->status.b.sts = 0;
  25616. + dma_desc->status.b.bs = BS_HOST_READY;
  25617. +
  25618. + /** DOEPDMA0 Register write */
  25619. + DWC_WRITE_REG32(&out_regs->doepdma,
  25620. + core_if->dev_if->
  25621. + dma_out_desc_addr);
  25622. + }
  25623. + } else {
  25624. + DWC_WRITE_REG32(&out_regs->doeptsiz, deptsiz.d32);
  25625. + }
  25626. +
  25627. + /* EP enable */
  25628. + depctl.b.cnak = 1;
  25629. + depctl.b.epena = 1;
  25630. + DWC_WRITE_REG32(&(out_regs->doepctl), depctl.d32);
  25631. + }
  25632. +}
  25633. +
  25634. +/**
  25635. + * This function continues control IN transfers started by
  25636. + * dwc_otg_ep0_start_transfer, when the transfer does not fit in a
  25637. + * single packet. NOTE: The DIEPCTL0/DOEPCTL0 registers only have one
  25638. + * bit for the packet count.
  25639. + *
  25640. + * @param core_if Programming view of DWC_otg controller.
  25641. + * @param ep The EP0 data.
  25642. + */
  25643. +void dwc_otg_ep0_continue_transfer(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  25644. +{
  25645. + depctl_data_t depctl;
  25646. + deptsiz0_data_t deptsiz;
  25647. + gintmsk_data_t intr_mask = {.d32 = 0 };
  25648. + dwc_otg_dev_dma_desc_t *dma_desc;
  25649. +
  25650. + if (ep->is_in == 1) {
  25651. + dwc_otg_dev_in_ep_regs_t *in_regs =
  25652. + core_if->dev_if->in_ep_regs[0];
  25653. + gnptxsts_data_t tx_status = {.d32 = 0 };
  25654. +
  25655. + tx_status.d32 =
  25656. + DWC_READ_REG32(&core_if->core_global_regs->gnptxsts);
  25657. + /** @todo Should there be check for room in the Tx
  25658. + * Status Queue. If not remove the code above this comment. */
  25659. +
  25660. + depctl.d32 = DWC_READ_REG32(&in_regs->diepctl);
  25661. + deptsiz.d32 = DWC_READ_REG32(&in_regs->dieptsiz);
  25662. +
  25663. + /* Program the transfer size and packet count
  25664. + * as follows: xfersize = N * maxpacket +
  25665. + * short_packet pktcnt = N + (short_packet
  25666. + * exist ? 1 : 0)
  25667. + */
  25668. +
  25669. + if (core_if->dma_desc_enable == 0) {
  25670. + deptsiz.b.xfersize =
  25671. + (ep->total_len - ep->xfer_count) >
  25672. + ep->maxpacket ? ep->maxpacket : (ep->total_len -
  25673. + ep->xfer_count);
  25674. + deptsiz.b.pktcnt = 1;
  25675. + if (core_if->dma_enable == 0) {
  25676. + ep->xfer_len += deptsiz.b.xfersize;
  25677. + } else {
  25678. + ep->xfer_len = deptsiz.b.xfersize;
  25679. + }
  25680. + DWC_WRITE_REG32(&in_regs->dieptsiz, deptsiz.d32);
  25681. + } else {
  25682. + ep->xfer_len =
  25683. + (ep->total_len - ep->xfer_count) >
  25684. + ep->maxpacket ? ep->maxpacket : (ep->total_len -
  25685. + ep->xfer_count);
  25686. +
  25687. + dma_desc = core_if->dev_if->in_desc_addr;
  25688. +
  25689. + /** DMA Descriptor Setup */
  25690. + dma_desc->status.b.bs = BS_HOST_BUSY;
  25691. + dma_desc->status.b.l = 1;
  25692. + dma_desc->status.b.ioc = 1;
  25693. + dma_desc->status.b.sp =
  25694. + (ep->xfer_len == ep->maxpacket) ? 0 : 1;
  25695. + dma_desc->status.b.bytes = ep->xfer_len;
  25696. + dma_desc->buf = ep->dma_addr;
  25697. + dma_desc->status.b.sts = 0;
  25698. + dma_desc->status.b.bs = BS_HOST_READY;
  25699. +
  25700. + /** DIEPDMA0 Register write */
  25701. + DWC_WRITE_REG32(&in_regs->diepdma,
  25702. + core_if->dev_if->dma_in_desc_addr);
  25703. + }
  25704. +
  25705. + DWC_DEBUGPL(DBG_PCDV,
  25706. + "IN len=%d xfersize=%d pktcnt=%d [%08x]\n",
  25707. + ep->xfer_len, deptsiz.b.xfersize, deptsiz.b.pktcnt,
  25708. + deptsiz.d32);
  25709. +
  25710. + /* Write the DMA register */
  25711. + if (core_if->hwcfg2.b.architecture == DWC_INT_DMA_ARCH) {
  25712. + if (core_if->dma_desc_enable == 0)
  25713. + DWC_WRITE_REG32(&(in_regs->diepdma),
  25714. + (uint32_t) ep->dma_addr);
  25715. + }
  25716. + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable)
  25717. + depctl.b.nextep = core_if->nextep_seq[ep->num];
  25718. + /* EP enable, IN data in FIFO */
  25719. + depctl.b.cnak = 1;
  25720. + depctl.b.epena = 1;
  25721. + DWC_WRITE_REG32(&in_regs->diepctl, depctl.d32);
  25722. +
  25723. + /**
  25724. + * Enable the Non-Periodic Tx FIFO empty interrupt, the
  25725. + * data will be written into the fifo by the ISR.
  25726. + */
  25727. + if (!core_if->dma_enable) {
  25728. + if (core_if->en_multiple_tx_fifo == 0) {
  25729. + /* First clear it from GINTSTS */
  25730. + intr_mask.b.nptxfempty = 1;
  25731. + DWC_MODIFY_REG32(&core_if->
  25732. + core_global_regs->gintmsk,
  25733. + intr_mask.d32, intr_mask.d32);
  25734. +
  25735. + } else {
  25736. + /* Enable the Tx FIFO Empty Interrupt for this EP */
  25737. + if (ep->xfer_len > 0) {
  25738. + uint32_t fifoemptymsk = 0;
  25739. + fifoemptymsk |= 1 << ep->num;
  25740. + DWC_MODIFY_REG32(&core_if->
  25741. + dev_if->dev_global_regs->dtknqr4_fifoemptymsk,
  25742. + 0, fifoemptymsk);
  25743. + }
  25744. + }
  25745. + }
  25746. + } else {
  25747. + dwc_otg_dev_out_ep_regs_t *out_regs =
  25748. + core_if->dev_if->out_ep_regs[0];
  25749. +
  25750. + depctl.d32 = DWC_READ_REG32(&out_regs->doepctl);
  25751. + deptsiz.d32 = DWC_READ_REG32(&out_regs->doeptsiz);
  25752. +
  25753. + /* Program the transfer size and packet count
  25754. + * as follows: xfersize = N * maxpacket +
  25755. + * short_packet pktcnt = N + (short_packet
  25756. + * exist ? 1 : 0)
  25757. + */
  25758. + deptsiz.b.xfersize = ep->maxpacket;
  25759. + deptsiz.b.pktcnt = 1;
  25760. +
  25761. + if (core_if->dma_desc_enable == 0) {
  25762. + DWC_WRITE_REG32(&out_regs->doeptsiz, deptsiz.d32);
  25763. + } else {
  25764. + dma_desc = core_if->dev_if->out_desc_addr;
  25765. +
  25766. + /** DMA Descriptor Setup */
  25767. + dma_desc->status.b.bs = BS_HOST_BUSY;
  25768. + dma_desc->status.b.l = 1;
  25769. + dma_desc->status.b.ioc = 1;
  25770. + dma_desc->status.b.bytes = ep->maxpacket;
  25771. + dma_desc->buf = ep->dma_addr;
  25772. + dma_desc->status.b.sts = 0;
  25773. + dma_desc->status.b.bs = BS_HOST_READY;
  25774. +
  25775. + /** DOEPDMA0 Register write */
  25776. + DWC_WRITE_REG32(&out_regs->doepdma,
  25777. + core_if->dev_if->dma_out_desc_addr);
  25778. + }
  25779. +
  25780. + DWC_DEBUGPL(DBG_PCDV,
  25781. + "IN len=%d xfersize=%d pktcnt=%d [%08x]\n",
  25782. + ep->xfer_len, deptsiz.b.xfersize, deptsiz.b.pktcnt,
  25783. + deptsiz.d32);
  25784. +
  25785. + /* Write the DMA register */
  25786. + if (core_if->hwcfg2.b.architecture == DWC_INT_DMA_ARCH) {
  25787. + if (core_if->dma_desc_enable == 0)
  25788. + DWC_WRITE_REG32(&(out_regs->doepdma),
  25789. + (uint32_t) ep->dma_addr);
  25790. +
  25791. + }
  25792. +
  25793. + /* EP enable, IN data in FIFO */
  25794. + depctl.b.cnak = 1;
  25795. + depctl.b.epena = 1;
  25796. + DWC_WRITE_REG32(&out_regs->doepctl, depctl.d32);
  25797. +
  25798. + }
  25799. +}
  25800. +
  25801. +#ifdef DEBUG
  25802. +void dump_msg(const u8 * buf, unsigned int length)
  25803. +{
  25804. + unsigned int start, num, i;
  25805. + char line[52], *p;
  25806. +
  25807. + if (length >= 512)
  25808. + return;
  25809. + start = 0;
  25810. + while (length > 0) {
  25811. + num = length < 16u ? length : 16u;
  25812. + p = line;
  25813. + for (i = 0; i < num; ++i) {
  25814. + if (i == 8)
  25815. + *p++ = ' ';
  25816. + DWC_SPRINTF(p, " %02x", buf[i]);
  25817. + p += 3;
  25818. + }
  25819. + *p = 0;
  25820. + DWC_PRINTF("%6x: %s\n", start, line);
  25821. + buf += num;
  25822. + start += num;
  25823. + length -= num;
  25824. + }
  25825. +}
  25826. +#else
  25827. +static inline void dump_msg(const u8 * buf, unsigned int length)
  25828. +{
  25829. +}
  25830. +#endif
  25831. +
  25832. +/**
  25833. + * This function writes a packet into the Tx FIFO associated with the
  25834. + * EP. For non-periodic EPs the non-periodic Tx FIFO is written. For
  25835. + * periodic EPs the periodic Tx FIFO associated with the EP is written
  25836. + * with all packets for the next micro-frame.
  25837. + *
  25838. + * @param core_if Programming view of DWC_otg controller.
  25839. + * @param ep The EP to write packet for.
  25840. + * @param dma Indicates if DMA is being used.
  25841. + */
  25842. +void dwc_otg_ep_write_packet(dwc_otg_core_if_t * core_if, dwc_ep_t * ep,
  25843. + int dma)
  25844. +{
  25845. + /**
  25846. + * The buffer is padded to DWORD on a per packet basis in
  25847. + * slave/dma mode if the MPS is not DWORD aligned. The last
  25848. + * packet, if short, is also padded to a multiple of DWORD.
  25849. + *
  25850. + * ep->xfer_buff always starts DWORD aligned in memory and is a
  25851. + * multiple of DWORD in length
  25852. + *
  25853. + * ep->xfer_len can be any number of bytes
  25854. + *
  25855. + * ep->xfer_count is a multiple of ep->maxpacket until the last
  25856. + * packet
  25857. + *
  25858. + * FIFO access is DWORD */
  25859. +
  25860. + uint32_t i;
  25861. + uint32_t byte_count;
  25862. + uint32_t dword_count;
  25863. + uint32_t *fifo;
  25864. + uint32_t *data_buff = (uint32_t *) ep->xfer_buff;
  25865. +
  25866. + DWC_DEBUGPL((DBG_PCDV | DBG_CILV), "%s(%p,%p)\n", __func__, core_if,
  25867. + ep);
  25868. + if (ep->xfer_count >= ep->xfer_len) {
  25869. + DWC_WARN("%s() No data for EP%d!!!\n", __func__, ep->num);
  25870. + return;
  25871. + }
  25872. +
  25873. + /* Find the byte length of the packet either short packet or MPS */
  25874. + if ((ep->xfer_len - ep->xfer_count) < ep->maxpacket) {
  25875. + byte_count = ep->xfer_len - ep->xfer_count;
  25876. + } else {
  25877. + byte_count = ep->maxpacket;
  25878. + }
  25879. +
  25880. + /* Find the DWORD length, padded by extra bytes as neccessary if MPS
  25881. + * is not a multiple of DWORD */
  25882. + dword_count = (byte_count + 3) / 4;
  25883. +
  25884. +#ifdef VERBOSE
  25885. + dump_msg(ep->xfer_buff, byte_count);
  25886. +#endif
  25887. +
  25888. + /**@todo NGS Where are the Periodic Tx FIFO addresses
  25889. + * intialized? What should this be? */
  25890. +
  25891. + fifo = core_if->data_fifo[ep->num];
  25892. +
  25893. + DWC_DEBUGPL((DBG_PCDV | DBG_CILV), "fifo=%p buff=%p *p=%08x bc=%d\n",
  25894. + fifo, data_buff, *data_buff, byte_count);
  25895. +
  25896. + if (!dma) {
  25897. + for (i = 0; i < dword_count; i++, data_buff++) {
  25898. + DWC_WRITE_REG32(fifo, *data_buff);
  25899. + }
  25900. + }
  25901. +
  25902. + ep->xfer_count += byte_count;
  25903. + ep->xfer_buff += byte_count;
  25904. + ep->dma_addr += byte_count;
  25905. +}
  25906. +
  25907. +/**
  25908. + * Set the EP STALL.
  25909. + *
  25910. + * @param core_if Programming view of DWC_otg controller.
  25911. + * @param ep The EP to set the stall on.
  25912. + */
  25913. +void dwc_otg_ep_set_stall(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  25914. +{
  25915. + depctl_data_t depctl;
  25916. + volatile uint32_t *depctl_addr;
  25917. +
  25918. + DWC_DEBUGPL(DBG_PCD, "%s ep%d-%s\n", __func__, ep->num,
  25919. + (ep->is_in ? "IN" : "OUT"));
  25920. +
  25921. + if (ep->is_in == 1) {
  25922. + depctl_addr = &(core_if->dev_if->in_ep_regs[ep->num]->diepctl);
  25923. + depctl.d32 = DWC_READ_REG32(depctl_addr);
  25924. +
  25925. + /* set the disable and stall bits */
  25926. + if (depctl.b.epena) {
  25927. + depctl.b.epdis = 1;
  25928. + }
  25929. + depctl.b.stall = 1;
  25930. + DWC_WRITE_REG32(depctl_addr, depctl.d32);
  25931. + } else {
  25932. + depctl_addr = &(core_if->dev_if->out_ep_regs[ep->num]->doepctl);
  25933. + depctl.d32 = DWC_READ_REG32(depctl_addr);
  25934. +
  25935. + /* set the stall bit */
  25936. + depctl.b.stall = 1;
  25937. + DWC_WRITE_REG32(depctl_addr, depctl.d32);
  25938. + }
  25939. +
  25940. + DWC_DEBUGPL(DBG_PCD, "DEPCTL=%0x\n", DWC_READ_REG32(depctl_addr));
  25941. +
  25942. + return;
  25943. +}
  25944. +
  25945. +/**
  25946. + * Clear the EP STALL.
  25947. + *
  25948. + * @param core_if Programming view of DWC_otg controller.
  25949. + * @param ep The EP to clear stall from.
  25950. + */
  25951. +void dwc_otg_ep_clear_stall(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  25952. +{
  25953. + depctl_data_t depctl;
  25954. + volatile uint32_t *depctl_addr;
  25955. +
  25956. + DWC_DEBUGPL(DBG_PCD, "%s ep%d-%s\n", __func__, ep->num,
  25957. + (ep->is_in ? "IN" : "OUT"));
  25958. +
  25959. + if (ep->is_in == 1) {
  25960. + depctl_addr = &(core_if->dev_if->in_ep_regs[ep->num]->diepctl);
  25961. + } else {
  25962. + depctl_addr = &(core_if->dev_if->out_ep_regs[ep->num]->doepctl);
  25963. + }
  25964. +
  25965. + depctl.d32 = DWC_READ_REG32(depctl_addr);
  25966. +
  25967. + /* clear the stall bits */
  25968. + depctl.b.stall = 0;
  25969. +
  25970. + /*
  25971. + * USB Spec 9.4.5: For endpoints using data toggle, regardless
  25972. + * of whether an endpoint has the Halt feature set, a
  25973. + * ClearFeature(ENDPOINT_HALT) request always results in the
  25974. + * data toggle being reinitialized to DATA0.
  25975. + */
  25976. + if (ep->type == DWC_OTG_EP_TYPE_INTR ||
  25977. + ep->type == DWC_OTG_EP_TYPE_BULK) {
  25978. + depctl.b.setd0pid = 1; /* DATA0 */
  25979. + }
  25980. +
  25981. + DWC_WRITE_REG32(depctl_addr, depctl.d32);
  25982. + DWC_DEBUGPL(DBG_PCD, "DEPCTL=%0x\n", DWC_READ_REG32(depctl_addr));
  25983. + return;
  25984. +}
  25985. +
  25986. +/**
  25987. + * This function reads a packet from the Rx FIFO into the destination
  25988. + * buffer. To read SETUP data use dwc_otg_read_setup_packet.
  25989. + *
  25990. + * @param core_if Programming view of DWC_otg controller.
  25991. + * @param dest Destination buffer for the packet.
  25992. + * @param bytes Number of bytes to copy to the destination.
  25993. + */
  25994. +void dwc_otg_read_packet(dwc_otg_core_if_t * core_if,
  25995. + uint8_t * dest, uint16_t bytes)
  25996. +{
  25997. + int i;
  25998. + int word_count = (bytes + 3) / 4;
  25999. +
  26000. + volatile uint32_t *fifo = core_if->data_fifo[0];
  26001. + uint32_t *data_buff = (uint32_t *) dest;
  26002. +
  26003. + /**
  26004. + * @todo Account for the case where _dest is not dword aligned. This
  26005. + * requires reading data from the FIFO into a uint32_t temp buffer,
  26006. + * then moving it into the data buffer.
  26007. + */
  26008. +
  26009. + DWC_DEBUGPL((DBG_PCDV | DBG_CILV), "%s(%p,%p,%d)\n", __func__,
  26010. + core_if, dest, bytes);
  26011. +
  26012. + for (i = 0; i < word_count; i++, data_buff++) {
  26013. + *data_buff = DWC_READ_REG32(fifo);
  26014. + }
  26015. +
  26016. + return;
  26017. +}
  26018. +
  26019. +/**
  26020. + * This functions reads the device registers and prints them
  26021. + *
  26022. + * @param core_if Programming view of DWC_otg controller.
  26023. + */
  26024. +void dwc_otg_dump_dev_registers(dwc_otg_core_if_t * core_if)
  26025. +{
  26026. + int i;
  26027. + volatile uint32_t *addr;
  26028. +
  26029. + DWC_PRINTF("Device Global Registers\n");
  26030. + addr = &core_if->dev_if->dev_global_regs->dcfg;
  26031. + DWC_PRINTF("DCFG @0x%08lX : 0x%08X\n",
  26032. + (unsigned long)addr, DWC_READ_REG32(addr));
  26033. + addr = &core_if->dev_if->dev_global_regs->dctl;
  26034. + DWC_PRINTF("DCTL @0x%08lX : 0x%08X\n",
  26035. + (unsigned long)addr, DWC_READ_REG32(addr));
  26036. + addr = &core_if->dev_if->dev_global_regs->dsts;
  26037. + DWC_PRINTF("DSTS @0x%08lX : 0x%08X\n",
  26038. + (unsigned long)addr, DWC_READ_REG32(addr));
  26039. + addr = &core_if->dev_if->dev_global_regs->diepmsk;
  26040. + DWC_PRINTF("DIEPMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26041. + DWC_READ_REG32(addr));
  26042. + addr = &core_if->dev_if->dev_global_regs->doepmsk;
  26043. + DWC_PRINTF("DOEPMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26044. + DWC_READ_REG32(addr));
  26045. + addr = &core_if->dev_if->dev_global_regs->daint;
  26046. + DWC_PRINTF("DAINT @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26047. + DWC_READ_REG32(addr));
  26048. + addr = &core_if->dev_if->dev_global_regs->daintmsk;
  26049. + DWC_PRINTF("DAINTMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26050. + DWC_READ_REG32(addr));
  26051. + addr = &core_if->dev_if->dev_global_regs->dtknqr1;
  26052. + DWC_PRINTF("DTKNQR1 @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26053. + DWC_READ_REG32(addr));
  26054. + if (core_if->hwcfg2.b.dev_token_q_depth > 6) {
  26055. + addr = &core_if->dev_if->dev_global_regs->dtknqr2;
  26056. + DWC_PRINTF("DTKNQR2 @0x%08lX : 0x%08X\n",
  26057. + (unsigned long)addr, DWC_READ_REG32(addr));
  26058. + }
  26059. +
  26060. + addr = &core_if->dev_if->dev_global_regs->dvbusdis;
  26061. + DWC_PRINTF("DVBUSID @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26062. + DWC_READ_REG32(addr));
  26063. +
  26064. + addr = &core_if->dev_if->dev_global_regs->dvbuspulse;
  26065. + DWC_PRINTF("DVBUSPULSE @0x%08lX : 0x%08X\n",
  26066. + (unsigned long)addr, DWC_READ_REG32(addr));
  26067. +
  26068. + addr = &core_if->dev_if->dev_global_regs->dtknqr3_dthrctl;
  26069. + DWC_PRINTF("DTKNQR3_DTHRCTL @0x%08lX : 0x%08X\n",
  26070. + (unsigned long)addr, DWC_READ_REG32(addr));
  26071. +
  26072. + if (core_if->hwcfg2.b.dev_token_q_depth > 22) {
  26073. + addr = &core_if->dev_if->dev_global_regs->dtknqr4_fifoemptymsk;
  26074. + DWC_PRINTF("DTKNQR4 @0x%08lX : 0x%08X\n",
  26075. + (unsigned long)addr, DWC_READ_REG32(addr));
  26076. + }
  26077. +
  26078. + addr = &core_if->dev_if->dev_global_regs->dtknqr4_fifoemptymsk;
  26079. + DWC_PRINTF("FIFOEMPMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26080. + DWC_READ_REG32(addr));
  26081. +
  26082. + if (core_if->hwcfg2.b.multi_proc_int) {
  26083. +
  26084. + addr = &core_if->dev_if->dev_global_regs->deachint;
  26085. + DWC_PRINTF("DEACHINT @0x%08lX : 0x%08X\n",
  26086. + (unsigned long)addr, DWC_READ_REG32(addr));
  26087. + addr = &core_if->dev_if->dev_global_regs->deachintmsk;
  26088. + DWC_PRINTF("DEACHINTMSK @0x%08lX : 0x%08X\n",
  26089. + (unsigned long)addr, DWC_READ_REG32(addr));
  26090. +
  26091. + for (i = 0; i <= core_if->dev_if->num_in_eps; i++) {
  26092. + addr =
  26093. + &core_if->dev_if->
  26094. + dev_global_regs->diepeachintmsk[i];
  26095. + DWC_PRINTF("DIEPEACHINTMSK[%d] @0x%08lX : 0x%08X\n",
  26096. + i, (unsigned long)addr,
  26097. + DWC_READ_REG32(addr));
  26098. + }
  26099. +
  26100. + for (i = 0; i <= core_if->dev_if->num_out_eps; i++) {
  26101. + addr =
  26102. + &core_if->dev_if->
  26103. + dev_global_regs->doepeachintmsk[i];
  26104. + DWC_PRINTF("DOEPEACHINTMSK[%d] @0x%08lX : 0x%08X\n",
  26105. + i, (unsigned long)addr,
  26106. + DWC_READ_REG32(addr));
  26107. + }
  26108. + }
  26109. +
  26110. + for (i = 0; i <= core_if->dev_if->num_in_eps; i++) {
  26111. + DWC_PRINTF("Device IN EP %d Registers\n", i);
  26112. + addr = &core_if->dev_if->in_ep_regs[i]->diepctl;
  26113. + DWC_PRINTF("DIEPCTL @0x%08lX : 0x%08X\n",
  26114. + (unsigned long)addr, DWC_READ_REG32(addr));
  26115. + addr = &core_if->dev_if->in_ep_regs[i]->diepint;
  26116. + DWC_PRINTF("DIEPINT @0x%08lX : 0x%08X\n",
  26117. + (unsigned long)addr, DWC_READ_REG32(addr));
  26118. + addr = &core_if->dev_if->in_ep_regs[i]->dieptsiz;
  26119. + DWC_PRINTF("DIETSIZ @0x%08lX : 0x%08X\n",
  26120. + (unsigned long)addr, DWC_READ_REG32(addr));
  26121. + addr = &core_if->dev_if->in_ep_regs[i]->diepdma;
  26122. + DWC_PRINTF("DIEPDMA @0x%08lX : 0x%08X\n",
  26123. + (unsigned long)addr, DWC_READ_REG32(addr));
  26124. + addr = &core_if->dev_if->in_ep_regs[i]->dtxfsts;
  26125. + DWC_PRINTF("DTXFSTS @0x%08lX : 0x%08X\n",
  26126. + (unsigned long)addr, DWC_READ_REG32(addr));
  26127. + addr = &core_if->dev_if->in_ep_regs[i]->diepdmab;
  26128. + DWC_PRINTF("DIEPDMAB @0x%08lX : 0x%08X\n",
  26129. + (unsigned long)addr, 0 /*DWC_READ_REG32(addr) */ );
  26130. + }
  26131. +
  26132. + for (i = 0; i <= core_if->dev_if->num_out_eps; i++) {
  26133. + DWC_PRINTF("Device OUT EP %d Registers\n", i);
  26134. + addr = &core_if->dev_if->out_ep_regs[i]->doepctl;
  26135. + DWC_PRINTF("DOEPCTL @0x%08lX : 0x%08X\n",
  26136. + (unsigned long)addr, DWC_READ_REG32(addr));
  26137. + addr = &core_if->dev_if->out_ep_regs[i]->doepint;
  26138. + DWC_PRINTF("DOEPINT @0x%08lX : 0x%08X\n",
  26139. + (unsigned long)addr, DWC_READ_REG32(addr));
  26140. + addr = &core_if->dev_if->out_ep_regs[i]->doeptsiz;
  26141. + DWC_PRINTF("DOETSIZ @0x%08lX : 0x%08X\n",
  26142. + (unsigned long)addr, DWC_READ_REG32(addr));
  26143. + addr = &core_if->dev_if->out_ep_regs[i]->doepdma;
  26144. + DWC_PRINTF("DOEPDMA @0x%08lX : 0x%08X\n",
  26145. + (unsigned long)addr, DWC_READ_REG32(addr));
  26146. + if (core_if->dma_enable) { /* Don't access this register in SLAVE mode */
  26147. + addr = &core_if->dev_if->out_ep_regs[i]->doepdmab;
  26148. + DWC_PRINTF("DOEPDMAB @0x%08lX : 0x%08X\n",
  26149. + (unsigned long)addr, DWC_READ_REG32(addr));
  26150. + }
  26151. +
  26152. + }
  26153. +}
  26154. +
  26155. +/**
  26156. + * This functions reads the SPRAM and prints its content
  26157. + *
  26158. + * @param core_if Programming view of DWC_otg controller.
  26159. + */
  26160. +void dwc_otg_dump_spram(dwc_otg_core_if_t * core_if)
  26161. +{
  26162. + volatile uint8_t *addr, *start_addr, *end_addr;
  26163. +
  26164. + DWC_PRINTF("SPRAM Data:\n");
  26165. + start_addr = (void *)core_if->core_global_regs;
  26166. + DWC_PRINTF("Base Address: 0x%8lX\n", (unsigned long)start_addr);
  26167. + start_addr += 0x00028000;
  26168. + end_addr = (void *)core_if->core_global_regs;
  26169. + end_addr += 0x000280e0;
  26170. +
  26171. + for (addr = start_addr; addr < end_addr; addr += 16) {
  26172. + DWC_PRINTF
  26173. + ("0x%8lX:\t%2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X %2X\n",
  26174. + (unsigned long)addr, addr[0], addr[1], addr[2], addr[3],
  26175. + addr[4], addr[5], addr[6], addr[7], addr[8], addr[9],
  26176. + addr[10], addr[11], addr[12], addr[13], addr[14], addr[15]
  26177. + );
  26178. + }
  26179. +
  26180. + return;
  26181. +}
  26182. +
  26183. +/**
  26184. + * This function reads the host registers and prints them
  26185. + *
  26186. + * @param core_if Programming view of DWC_otg controller.
  26187. + */
  26188. +void dwc_otg_dump_host_registers(dwc_otg_core_if_t * core_if)
  26189. +{
  26190. + int i;
  26191. + volatile uint32_t *addr;
  26192. +
  26193. + DWC_PRINTF("Host Global Registers\n");
  26194. + addr = &core_if->host_if->host_global_regs->hcfg;
  26195. + DWC_PRINTF("HCFG @0x%08lX : 0x%08X\n",
  26196. + (unsigned long)addr, DWC_READ_REG32(addr));
  26197. + addr = &core_if->host_if->host_global_regs->hfir;
  26198. + DWC_PRINTF("HFIR @0x%08lX : 0x%08X\n",
  26199. + (unsigned long)addr, DWC_READ_REG32(addr));
  26200. + addr = &core_if->host_if->host_global_regs->hfnum;
  26201. + DWC_PRINTF("HFNUM @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26202. + DWC_READ_REG32(addr));
  26203. + addr = &core_if->host_if->host_global_regs->hptxsts;
  26204. + DWC_PRINTF("HPTXSTS @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26205. + DWC_READ_REG32(addr));
  26206. + addr = &core_if->host_if->host_global_regs->haint;
  26207. + DWC_PRINTF("HAINT @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26208. + DWC_READ_REG32(addr));
  26209. + addr = &core_if->host_if->host_global_regs->haintmsk;
  26210. + DWC_PRINTF("HAINTMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26211. + DWC_READ_REG32(addr));
  26212. + if (core_if->dma_desc_enable) {
  26213. + addr = &core_if->host_if->host_global_regs->hflbaddr;
  26214. + DWC_PRINTF("HFLBADDR @0x%08lX : 0x%08X\n",
  26215. + (unsigned long)addr, DWC_READ_REG32(addr));
  26216. + }
  26217. +
  26218. + addr = core_if->host_if->hprt0;
  26219. + DWC_PRINTF("HPRT0 @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26220. + DWC_READ_REG32(addr));
  26221. +
  26222. + for (i = 0; i < core_if->core_params->host_channels; i++) {
  26223. + DWC_PRINTF("Host Channel %d Specific Registers\n", i);
  26224. + addr = &core_if->host_if->hc_regs[i]->hcchar;
  26225. + DWC_PRINTF("HCCHAR @0x%08lX : 0x%08X\n",
  26226. + (unsigned long)addr, DWC_READ_REG32(addr));
  26227. + addr = &core_if->host_if->hc_regs[i]->hcsplt;
  26228. + DWC_PRINTF("HCSPLT @0x%08lX : 0x%08X\n",
  26229. + (unsigned long)addr, DWC_READ_REG32(addr));
  26230. + addr = &core_if->host_if->hc_regs[i]->hcint;
  26231. + DWC_PRINTF("HCINT @0x%08lX : 0x%08X\n",
  26232. + (unsigned long)addr, DWC_READ_REG32(addr));
  26233. + addr = &core_if->host_if->hc_regs[i]->hcintmsk;
  26234. + DWC_PRINTF("HCINTMSK @0x%08lX : 0x%08X\n",
  26235. + (unsigned long)addr, DWC_READ_REG32(addr));
  26236. + addr = &core_if->host_if->hc_regs[i]->hctsiz;
  26237. + DWC_PRINTF("HCTSIZ @0x%08lX : 0x%08X\n",
  26238. + (unsigned long)addr, DWC_READ_REG32(addr));
  26239. + addr = &core_if->host_if->hc_regs[i]->hcdma;
  26240. + DWC_PRINTF("HCDMA @0x%08lX : 0x%08X\n",
  26241. + (unsigned long)addr, DWC_READ_REG32(addr));
  26242. + if (core_if->dma_desc_enable) {
  26243. + addr = &core_if->host_if->hc_regs[i]->hcdmab;
  26244. + DWC_PRINTF("HCDMAB @0x%08lX : 0x%08X\n",
  26245. + (unsigned long)addr, DWC_READ_REG32(addr));
  26246. + }
  26247. +
  26248. + }
  26249. + return;
  26250. +}
  26251. +
  26252. +/**
  26253. + * This function reads the core global registers and prints them
  26254. + *
  26255. + * @param core_if Programming view of DWC_otg controller.
  26256. + */
  26257. +void dwc_otg_dump_global_registers(dwc_otg_core_if_t * core_if)
  26258. +{
  26259. + int i, ep_num;
  26260. + volatile uint32_t *addr;
  26261. + char *txfsiz;
  26262. +
  26263. + DWC_PRINTF("Core Global Registers\n");
  26264. + addr = &core_if->core_global_regs->gotgctl;
  26265. + DWC_PRINTF("GOTGCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26266. + DWC_READ_REG32(addr));
  26267. + addr = &core_if->core_global_regs->gotgint;
  26268. + DWC_PRINTF("GOTGINT @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26269. + DWC_READ_REG32(addr));
  26270. + addr = &core_if->core_global_regs->gahbcfg;
  26271. + DWC_PRINTF("GAHBCFG @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26272. + DWC_READ_REG32(addr));
  26273. + addr = &core_if->core_global_regs->gusbcfg;
  26274. + DWC_PRINTF("GUSBCFG @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26275. + DWC_READ_REG32(addr));
  26276. + addr = &core_if->core_global_regs->grstctl;
  26277. + DWC_PRINTF("GRSTCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26278. + DWC_READ_REG32(addr));
  26279. + addr = &core_if->core_global_regs->gintsts;
  26280. + DWC_PRINTF("GINTSTS @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26281. + DWC_READ_REG32(addr));
  26282. + addr = &core_if->core_global_regs->gintmsk;
  26283. + DWC_PRINTF("GINTMSK @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26284. + DWC_READ_REG32(addr));
  26285. + addr = &core_if->core_global_regs->grxstsr;
  26286. + DWC_PRINTF("GRXSTSR @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26287. + DWC_READ_REG32(addr));
  26288. + addr = &core_if->core_global_regs->grxfsiz;
  26289. + DWC_PRINTF("GRXFSIZ @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26290. + DWC_READ_REG32(addr));
  26291. + addr = &core_if->core_global_regs->gnptxfsiz;
  26292. + DWC_PRINTF("GNPTXFSIZ @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26293. + DWC_READ_REG32(addr));
  26294. + addr = &core_if->core_global_regs->gnptxsts;
  26295. + DWC_PRINTF("GNPTXSTS @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26296. + DWC_READ_REG32(addr));
  26297. + addr = &core_if->core_global_regs->gi2cctl;
  26298. + DWC_PRINTF("GI2CCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26299. + DWC_READ_REG32(addr));
  26300. + addr = &core_if->core_global_regs->gpvndctl;
  26301. + DWC_PRINTF("GPVNDCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26302. + DWC_READ_REG32(addr));
  26303. + addr = &core_if->core_global_regs->ggpio;
  26304. + DWC_PRINTF("GGPIO @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26305. + DWC_READ_REG32(addr));
  26306. + addr = &core_if->core_global_regs->guid;
  26307. + DWC_PRINTF("GUID @0x%08lX : 0x%08X\n",
  26308. + (unsigned long)addr, DWC_READ_REG32(addr));
  26309. + addr = &core_if->core_global_regs->gsnpsid;
  26310. + DWC_PRINTF("GSNPSID @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26311. + DWC_READ_REG32(addr));
  26312. + addr = &core_if->core_global_regs->ghwcfg1;
  26313. + DWC_PRINTF("GHWCFG1 @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26314. + DWC_READ_REG32(addr));
  26315. + addr = &core_if->core_global_regs->ghwcfg2;
  26316. + DWC_PRINTF("GHWCFG2 @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26317. + DWC_READ_REG32(addr));
  26318. + addr = &core_if->core_global_regs->ghwcfg3;
  26319. + DWC_PRINTF("GHWCFG3 @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26320. + DWC_READ_REG32(addr));
  26321. + addr = &core_if->core_global_regs->ghwcfg4;
  26322. + DWC_PRINTF("GHWCFG4 @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26323. + DWC_READ_REG32(addr));
  26324. + addr = &core_if->core_global_regs->glpmcfg;
  26325. + DWC_PRINTF("GLPMCFG @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26326. + DWC_READ_REG32(addr));
  26327. + addr = &core_if->core_global_regs->gpwrdn;
  26328. + DWC_PRINTF("GPWRDN @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26329. + DWC_READ_REG32(addr));
  26330. + addr = &core_if->core_global_regs->gdfifocfg;
  26331. + DWC_PRINTF("GDFIFOCFG @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26332. + DWC_READ_REG32(addr));
  26333. + addr = &core_if->core_global_regs->adpctl;
  26334. + DWC_PRINTF("ADPCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26335. + dwc_otg_adp_read_reg(core_if));
  26336. + addr = &core_if->core_global_regs->hptxfsiz;
  26337. + DWC_PRINTF("HPTXFSIZ @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26338. + DWC_READ_REG32(addr));
  26339. +
  26340. + if (core_if->en_multiple_tx_fifo == 0) {
  26341. + ep_num = core_if->hwcfg4.b.num_dev_perio_in_ep;
  26342. + txfsiz = "DPTXFSIZ";
  26343. + } else {
  26344. + ep_num = core_if->hwcfg4.b.num_in_eps;
  26345. + txfsiz = "DIENPTXF";
  26346. + }
  26347. + for (i = 0; i < ep_num; i++) {
  26348. + addr = &core_if->core_global_regs->dtxfsiz[i];
  26349. + DWC_PRINTF("%s[%d] @0x%08lX : 0x%08X\n", txfsiz, i + 1,
  26350. + (unsigned long)addr, DWC_READ_REG32(addr));
  26351. + }
  26352. + addr = core_if->pcgcctl;
  26353. + DWC_PRINTF("PCGCCTL @0x%08lX : 0x%08X\n", (unsigned long)addr,
  26354. + DWC_READ_REG32(addr));
  26355. +}
  26356. +
  26357. +/**
  26358. + * Flush a Tx FIFO.
  26359. + *
  26360. + * @param core_if Programming view of DWC_otg controller.
  26361. + * @param num Tx FIFO to flush.
  26362. + */
  26363. +void dwc_otg_flush_tx_fifo(dwc_otg_core_if_t * core_if, const int num)
  26364. +{
  26365. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  26366. + volatile grstctl_t greset = {.d32 = 0 };
  26367. + int count = 0;
  26368. +
  26369. + DWC_DEBUGPL((DBG_CIL | DBG_PCDV), "Flush Tx FIFO %d\n", num);
  26370. +
  26371. + greset.b.txfflsh = 1;
  26372. + greset.b.txfnum = num;
  26373. + DWC_WRITE_REG32(&global_regs->grstctl, greset.d32);
  26374. +
  26375. + do {
  26376. + greset.d32 = DWC_READ_REG32(&global_regs->grstctl);
  26377. + if (++count > 10000) {
  26378. + DWC_WARN("%s() HANG! GRSTCTL=%0x GNPTXSTS=0x%08x\n",
  26379. + __func__, greset.d32,
  26380. + DWC_READ_REG32(&global_regs->gnptxsts));
  26381. + break;
  26382. + }
  26383. + dwc_udelay(1);
  26384. + } while (greset.b.txfflsh == 1);
  26385. +
  26386. + /* Wait for 3 PHY Clocks */
  26387. + dwc_udelay(1);
  26388. +}
  26389. +
  26390. +/**
  26391. + * Flush Rx FIFO.
  26392. + *
  26393. + * @param core_if Programming view of DWC_otg controller.
  26394. + */
  26395. +void dwc_otg_flush_rx_fifo(dwc_otg_core_if_t * core_if)
  26396. +{
  26397. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  26398. + volatile grstctl_t greset = {.d32 = 0 };
  26399. + int count = 0;
  26400. +
  26401. + DWC_DEBUGPL((DBG_CIL | DBG_PCDV), "%s\n", __func__);
  26402. + /*
  26403. + *
  26404. + */
  26405. + greset.b.rxfflsh = 1;
  26406. + DWC_WRITE_REG32(&global_regs->grstctl, greset.d32);
  26407. +
  26408. + do {
  26409. + greset.d32 = DWC_READ_REG32(&global_regs->grstctl);
  26410. + if (++count > 10000) {
  26411. + DWC_WARN("%s() HANG! GRSTCTL=%0x\n", __func__,
  26412. + greset.d32);
  26413. + break;
  26414. + }
  26415. + dwc_udelay(1);
  26416. + } while (greset.b.rxfflsh == 1);
  26417. +
  26418. + /* Wait for 3 PHY Clocks */
  26419. + dwc_udelay(1);
  26420. +}
  26421. +
  26422. +/**
  26423. + * Do core a soft reset of the core. Be careful with this because it
  26424. + * resets all the internal state machines of the core.
  26425. + */
  26426. +void dwc_otg_core_reset(dwc_otg_core_if_t * core_if)
  26427. +{
  26428. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  26429. + volatile grstctl_t greset = {.d32 = 0 };
  26430. + int count = 0;
  26431. +
  26432. + DWC_DEBUGPL(DBG_CILV, "%s\n", __func__);
  26433. + /* Wait for AHB master IDLE state. */
  26434. + do {
  26435. + dwc_udelay(10);
  26436. + greset.d32 = DWC_READ_REG32(&global_regs->grstctl);
  26437. + if (++count > 100000) {
  26438. + DWC_WARN("%s() HANG! AHB Idle GRSTCTL=%0x\n", __func__,
  26439. + greset.d32);
  26440. + return;
  26441. + }
  26442. + }
  26443. + while (greset.b.ahbidle == 0);
  26444. +
  26445. + /* Core Soft Reset */
  26446. + count = 0;
  26447. + greset.b.csftrst = 1;
  26448. + DWC_WRITE_REG32(&global_regs->grstctl, greset.d32);
  26449. + do {
  26450. + greset.d32 = DWC_READ_REG32(&global_regs->grstctl);
  26451. + if (++count > 10000) {
  26452. + DWC_WARN("%s() HANG! Soft Reset GRSTCTL=%0x\n",
  26453. + __func__, greset.d32);
  26454. + break;
  26455. + }
  26456. + dwc_udelay(1);
  26457. + }
  26458. + while (greset.b.csftrst == 1);
  26459. +
  26460. + /* Wait for 3 PHY Clocks */
  26461. + dwc_mdelay(100);
  26462. +}
  26463. +
  26464. +uint8_t dwc_otg_is_device_mode(dwc_otg_core_if_t * _core_if)
  26465. +{
  26466. + return (dwc_otg_mode(_core_if) != DWC_HOST_MODE);
  26467. +}
  26468. +
  26469. +uint8_t dwc_otg_is_host_mode(dwc_otg_core_if_t * _core_if)
  26470. +{
  26471. + return (dwc_otg_mode(_core_if) == DWC_HOST_MODE);
  26472. +}
  26473. +
  26474. +/**
  26475. + * Register HCD callbacks. The callbacks are used to start and stop
  26476. + * the HCD for interrupt processing.
  26477. + *
  26478. + * @param core_if Programming view of DWC_otg controller.
  26479. + * @param cb the HCD callback structure.
  26480. + * @param p pointer to be passed to callback function (usb_hcd*).
  26481. + */
  26482. +void dwc_otg_cil_register_hcd_callbacks(dwc_otg_core_if_t * core_if,
  26483. + dwc_otg_cil_callbacks_t * cb, void *p)
  26484. +{
  26485. + core_if->hcd_cb = cb;
  26486. + cb->p = p;
  26487. +}
  26488. +
  26489. +/**
  26490. + * Register PCD callbacks. The callbacks are used to start and stop
  26491. + * the PCD for interrupt processing.
  26492. + *
  26493. + * @param core_if Programming view of DWC_otg controller.
  26494. + * @param cb the PCD callback structure.
  26495. + * @param p pointer to be passed to callback function (pcd*).
  26496. + */
  26497. +void dwc_otg_cil_register_pcd_callbacks(dwc_otg_core_if_t * core_if,
  26498. + dwc_otg_cil_callbacks_t * cb, void *p)
  26499. +{
  26500. + core_if->pcd_cb = cb;
  26501. + cb->p = p;
  26502. +}
  26503. +
  26504. +#ifdef DWC_EN_ISOC
  26505. +
  26506. +/**
  26507. + * This function writes isoc data per 1 (micro)frame into tx fifo
  26508. + *
  26509. + * @param core_if Programming view of DWC_otg controller.
  26510. + * @param ep The EP to start the transfer on.
  26511. + *
  26512. + */
  26513. +void write_isoc_frame_data(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  26514. +{
  26515. + dwc_otg_dev_in_ep_regs_t *ep_regs;
  26516. + dtxfsts_data_t txstatus = {.d32 = 0 };
  26517. + uint32_t len = 0;
  26518. + uint32_t dwords;
  26519. +
  26520. + ep->xfer_len = ep->data_per_frame;
  26521. + ep->xfer_count = 0;
  26522. +
  26523. + ep_regs = core_if->dev_if->in_ep_regs[ep->num];
  26524. +
  26525. + len = ep->xfer_len - ep->xfer_count;
  26526. +
  26527. + if (len > ep->maxpacket) {
  26528. + len = ep->maxpacket;
  26529. + }
  26530. +
  26531. + dwords = (len + 3) / 4;
  26532. +
  26533. + /* While there is space in the queue and space in the FIFO and
  26534. + * More data to tranfer, Write packets to the Tx FIFO */
  26535. + txstatus.d32 =
  26536. + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[ep->num]->dtxfsts);
  26537. + DWC_DEBUGPL(DBG_PCDV, "b4 dtxfsts[%d]=0x%08x\n", ep->num, txstatus.d32);
  26538. +
  26539. + while (txstatus.b.txfspcavail > dwords &&
  26540. + ep->xfer_count < ep->xfer_len && ep->xfer_len != 0) {
  26541. + /* Write the FIFO */
  26542. + dwc_otg_ep_write_packet(core_if, ep, 0);
  26543. +
  26544. + len = ep->xfer_len - ep->xfer_count;
  26545. + if (len > ep->maxpacket) {
  26546. + len = ep->maxpacket;
  26547. + }
  26548. +
  26549. + dwords = (len + 3) / 4;
  26550. + txstatus.d32 =
  26551. + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
  26552. + dtxfsts);
  26553. + DWC_DEBUGPL(DBG_PCDV, "dtxfsts[%d]=0x%08x\n", ep->num,
  26554. + txstatus.d32);
  26555. + }
  26556. +}
  26557. +
  26558. +/**
  26559. + * This function initializes a descriptor chain for Isochronous transfer
  26560. + *
  26561. + * @param core_if Programming view of DWC_otg controller.
  26562. + * @param ep The EP to start the transfer on.
  26563. + *
  26564. + */
  26565. +void dwc_otg_iso_ep_start_frm_transfer(dwc_otg_core_if_t * core_if,
  26566. + dwc_ep_t * ep)
  26567. +{
  26568. + deptsiz_data_t deptsiz = {.d32 = 0 };
  26569. + depctl_data_t depctl = {.d32 = 0 };
  26570. + dsts_data_t dsts = {.d32 = 0 };
  26571. + volatile uint32_t *addr;
  26572. +
  26573. + if (ep->is_in) {
  26574. + addr = &core_if->dev_if->in_ep_regs[ep->num]->diepctl;
  26575. + } else {
  26576. + addr = &core_if->dev_if->out_ep_regs[ep->num]->doepctl;
  26577. + }
  26578. +
  26579. + ep->xfer_len = ep->data_per_frame;
  26580. + ep->xfer_count = 0;
  26581. + ep->xfer_buff = ep->cur_pkt_addr;
  26582. + ep->dma_addr = ep->cur_pkt_dma_addr;
  26583. +
  26584. + if (ep->is_in) {
  26585. + /* Program the transfer size and packet count
  26586. + * as follows: xfersize = N * maxpacket +
  26587. + * short_packet pktcnt = N + (short_packet
  26588. + * exist ? 1 : 0)
  26589. + */
  26590. + deptsiz.b.xfersize = ep->xfer_len;
  26591. + deptsiz.b.pktcnt =
  26592. + (ep->xfer_len - 1 + ep->maxpacket) / ep->maxpacket;
  26593. + deptsiz.b.mc = deptsiz.b.pktcnt;
  26594. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->dieptsiz,
  26595. + deptsiz.d32);
  26596. +
  26597. + /* Write the DMA register */
  26598. + if (core_if->dma_enable) {
  26599. + DWC_WRITE_REG32(&
  26600. + (core_if->dev_if->in_ep_regs[ep->num]->
  26601. + diepdma), (uint32_t) ep->dma_addr);
  26602. + }
  26603. + } else {
  26604. + deptsiz.b.pktcnt =
  26605. + (ep->xfer_len + (ep->maxpacket - 1)) / ep->maxpacket;
  26606. + deptsiz.b.xfersize = deptsiz.b.pktcnt * ep->maxpacket;
  26607. +
  26608. + DWC_WRITE_REG32(&core_if->dev_if->
  26609. + out_ep_regs[ep->num]->doeptsiz, deptsiz.d32);
  26610. +
  26611. + if (core_if->dma_enable) {
  26612. + DWC_WRITE_REG32(&
  26613. + (core_if->dev_if->
  26614. + out_ep_regs[ep->num]->doepdma),
  26615. + (uint32_t) ep->dma_addr);
  26616. + }
  26617. + }
  26618. +
  26619. + /** Enable endpoint, clear nak */
  26620. +
  26621. + depctl.d32 = 0;
  26622. + if (ep->bInterval == 1) {
  26623. + dsts.d32 =
  26624. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  26625. + ep->next_frame = dsts.b.soffn + ep->bInterval;
  26626. +
  26627. + if (ep->next_frame & 0x1) {
  26628. + depctl.b.setd1pid = 1;
  26629. + } else {
  26630. + depctl.b.setd0pid = 1;
  26631. + }
  26632. + } else {
  26633. + ep->next_frame += ep->bInterval;
  26634. +
  26635. + if (ep->next_frame & 0x1) {
  26636. + depctl.b.setd1pid = 1;
  26637. + } else {
  26638. + depctl.b.setd0pid = 1;
  26639. + }
  26640. + }
  26641. + depctl.b.epena = 1;
  26642. + depctl.b.cnak = 1;
  26643. +
  26644. + DWC_MODIFY_REG32(addr, 0, depctl.d32);
  26645. + depctl.d32 = DWC_READ_REG32(addr);
  26646. +
  26647. + if (ep->is_in && core_if->dma_enable == 0) {
  26648. + write_isoc_frame_data(core_if, ep);
  26649. + }
  26650. +
  26651. +}
  26652. +#endif /* DWC_EN_ISOC */
  26653. +
  26654. +static void dwc_otg_set_uninitialized(int32_t * p, int size)
  26655. +{
  26656. + int i;
  26657. + for (i = 0; i < size; i++) {
  26658. + p[i] = -1;
  26659. + }
  26660. +}
  26661. +
  26662. +static int dwc_otg_param_initialized(int32_t val)
  26663. +{
  26664. + return val != -1;
  26665. +}
  26666. +
  26667. +static int dwc_otg_setup_params(dwc_otg_core_if_t * core_if)
  26668. +{
  26669. + int i;
  26670. + core_if->core_params = DWC_ALLOC(sizeof(*core_if->core_params));
  26671. + if (!core_if->core_params) {
  26672. + return -DWC_E_NO_MEMORY;
  26673. + }
  26674. + dwc_otg_set_uninitialized((int32_t *) core_if->core_params,
  26675. + sizeof(*core_if->core_params) /
  26676. + sizeof(int32_t));
  26677. + DWC_PRINTF("Setting default values for core params\n");
  26678. + dwc_otg_set_param_otg_cap(core_if, dwc_param_otg_cap_default);
  26679. + dwc_otg_set_param_dma_enable(core_if, dwc_param_dma_enable_default);
  26680. + dwc_otg_set_param_dma_desc_enable(core_if,
  26681. + dwc_param_dma_desc_enable_default);
  26682. + dwc_otg_set_param_opt(core_if, dwc_param_opt_default);
  26683. + dwc_otg_set_param_dma_burst_size(core_if,
  26684. + dwc_param_dma_burst_size_default);
  26685. + dwc_otg_set_param_host_support_fs_ls_low_power(core_if,
  26686. + dwc_param_host_support_fs_ls_low_power_default);
  26687. + dwc_otg_set_param_enable_dynamic_fifo(core_if,
  26688. + dwc_param_enable_dynamic_fifo_default);
  26689. + dwc_otg_set_param_data_fifo_size(core_if,
  26690. + dwc_param_data_fifo_size_default);
  26691. + dwc_otg_set_param_dev_rx_fifo_size(core_if,
  26692. + dwc_param_dev_rx_fifo_size_default);
  26693. + dwc_otg_set_param_dev_nperio_tx_fifo_size(core_if,
  26694. + dwc_param_dev_nperio_tx_fifo_size_default);
  26695. + dwc_otg_set_param_host_rx_fifo_size(core_if,
  26696. + dwc_param_host_rx_fifo_size_default);
  26697. + dwc_otg_set_param_host_nperio_tx_fifo_size(core_if,
  26698. + dwc_param_host_nperio_tx_fifo_size_default);
  26699. + dwc_otg_set_param_host_perio_tx_fifo_size(core_if,
  26700. + dwc_param_host_perio_tx_fifo_size_default);
  26701. + dwc_otg_set_param_max_transfer_size(core_if,
  26702. + dwc_param_max_transfer_size_default);
  26703. + dwc_otg_set_param_max_packet_count(core_if,
  26704. + dwc_param_max_packet_count_default);
  26705. + dwc_otg_set_param_host_channels(core_if,
  26706. + dwc_param_host_channels_default);
  26707. + dwc_otg_set_param_dev_endpoints(core_if,
  26708. + dwc_param_dev_endpoints_default);
  26709. + dwc_otg_set_param_phy_type(core_if, dwc_param_phy_type_default);
  26710. + dwc_otg_set_param_speed(core_if, dwc_param_speed_default);
  26711. + dwc_otg_set_param_host_ls_low_power_phy_clk(core_if,
  26712. + dwc_param_host_ls_low_power_phy_clk_default);
  26713. + dwc_otg_set_param_phy_ulpi_ddr(core_if, dwc_param_phy_ulpi_ddr_default);
  26714. + dwc_otg_set_param_phy_ulpi_ext_vbus(core_if,
  26715. + dwc_param_phy_ulpi_ext_vbus_default);
  26716. + dwc_otg_set_param_phy_utmi_width(core_if,
  26717. + dwc_param_phy_utmi_width_default);
  26718. + dwc_otg_set_param_ts_dline(core_if, dwc_param_ts_dline_default);
  26719. + dwc_otg_set_param_i2c_enable(core_if, dwc_param_i2c_enable_default);
  26720. + dwc_otg_set_param_ulpi_fs_ls(core_if, dwc_param_ulpi_fs_ls_default);
  26721. + dwc_otg_set_param_en_multiple_tx_fifo(core_if,
  26722. + dwc_param_en_multiple_tx_fifo_default);
  26723. + for (i = 0; i < 15; i++) {
  26724. + dwc_otg_set_param_dev_perio_tx_fifo_size(core_if,
  26725. + dwc_param_dev_perio_tx_fifo_size_default,
  26726. + i);
  26727. + }
  26728. +
  26729. + for (i = 0; i < 15; i++) {
  26730. + dwc_otg_set_param_dev_tx_fifo_size(core_if,
  26731. + dwc_param_dev_tx_fifo_size_default,
  26732. + i);
  26733. + }
  26734. + dwc_otg_set_param_thr_ctl(core_if, dwc_param_thr_ctl_default);
  26735. + dwc_otg_set_param_mpi_enable(core_if, dwc_param_mpi_enable_default);
  26736. + dwc_otg_set_param_pti_enable(core_if, dwc_param_pti_enable_default);
  26737. + dwc_otg_set_param_lpm_enable(core_if, dwc_param_lpm_enable_default);
  26738. + dwc_otg_set_param_ic_usb_cap(core_if, dwc_param_ic_usb_cap_default);
  26739. + dwc_otg_set_param_tx_thr_length(core_if,
  26740. + dwc_param_tx_thr_length_default);
  26741. + dwc_otg_set_param_rx_thr_length(core_if,
  26742. + dwc_param_rx_thr_length_default);
  26743. + dwc_otg_set_param_ahb_thr_ratio(core_if,
  26744. + dwc_param_ahb_thr_ratio_default);
  26745. + dwc_otg_set_param_power_down(core_if, dwc_param_power_down_default);
  26746. + dwc_otg_set_param_reload_ctl(core_if, dwc_param_reload_ctl_default);
  26747. + dwc_otg_set_param_dev_out_nak(core_if, dwc_param_dev_out_nak_default);
  26748. + dwc_otg_set_param_cont_on_bna(core_if, dwc_param_cont_on_bna_default);
  26749. + dwc_otg_set_param_ahb_single(core_if, dwc_param_ahb_single_default);
  26750. + dwc_otg_set_param_otg_ver(core_if, dwc_param_otg_ver_default);
  26751. + dwc_otg_set_param_adp_enable(core_if, dwc_param_adp_enable_default);
  26752. + DWC_PRINTF("Finished setting default values for core params\n");
  26753. +
  26754. + return 0;
  26755. +}
  26756. +
  26757. +uint8_t dwc_otg_is_dma_enable(dwc_otg_core_if_t * core_if)
  26758. +{
  26759. + return core_if->dma_enable;
  26760. +}
  26761. +
  26762. +/* Checks if the parameter is outside of its valid range of values */
  26763. +#define DWC_OTG_PARAM_TEST(_param_, _low_, _high_) \
  26764. + (((_param_) < (_low_)) || \
  26765. + ((_param_) > (_high_)))
  26766. +
  26767. +/* Parameter access functions */
  26768. +int dwc_otg_set_param_otg_cap(dwc_otg_core_if_t * core_if, int32_t val)
  26769. +{
  26770. + int valid;
  26771. + int retval = 0;
  26772. + if (DWC_OTG_PARAM_TEST(val, 0, 2)) {
  26773. + DWC_WARN("Wrong value for otg_cap parameter\n");
  26774. + DWC_WARN("otg_cap parameter must be 0,1 or 2\n");
  26775. + retval = -DWC_E_INVALID;
  26776. + goto out;
  26777. + }
  26778. +
  26779. + valid = 1;
  26780. + switch (val) {
  26781. + case DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE:
  26782. + if (core_if->hwcfg2.b.op_mode !=
  26783. + DWC_HWCFG2_OP_MODE_HNP_SRP_CAPABLE_OTG)
  26784. + valid = 0;
  26785. + break;
  26786. + case DWC_OTG_CAP_PARAM_SRP_ONLY_CAPABLE:
  26787. + if ((core_if->hwcfg2.b.op_mode !=
  26788. + DWC_HWCFG2_OP_MODE_HNP_SRP_CAPABLE_OTG)
  26789. + && (core_if->hwcfg2.b.op_mode !=
  26790. + DWC_HWCFG2_OP_MODE_SRP_ONLY_CAPABLE_OTG)
  26791. + && (core_if->hwcfg2.b.op_mode !=
  26792. + DWC_HWCFG2_OP_MODE_SRP_CAPABLE_DEVICE)
  26793. + && (core_if->hwcfg2.b.op_mode !=
  26794. + DWC_HWCFG2_OP_MODE_SRP_CAPABLE_HOST)) {
  26795. + valid = 0;
  26796. + }
  26797. + break;
  26798. + case DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE:
  26799. + /* always valid */
  26800. + break;
  26801. + }
  26802. + if (!valid) {
  26803. + if (dwc_otg_param_initialized(core_if->core_params->otg_cap)) {
  26804. + DWC_ERROR
  26805. + ("%d invalid for otg_cap paremter. Check HW configuration.\n",
  26806. + val);
  26807. + }
  26808. + val =
  26809. + (((core_if->hwcfg2.b.op_mode ==
  26810. + DWC_HWCFG2_OP_MODE_HNP_SRP_CAPABLE_OTG)
  26811. + || (core_if->hwcfg2.b.op_mode ==
  26812. + DWC_HWCFG2_OP_MODE_SRP_ONLY_CAPABLE_OTG)
  26813. + || (core_if->hwcfg2.b.op_mode ==
  26814. + DWC_HWCFG2_OP_MODE_SRP_CAPABLE_DEVICE)
  26815. + || (core_if->hwcfg2.b.op_mode ==
  26816. + DWC_HWCFG2_OP_MODE_SRP_CAPABLE_HOST)) ?
  26817. + DWC_OTG_CAP_PARAM_SRP_ONLY_CAPABLE :
  26818. + DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE);
  26819. + retval = -DWC_E_INVALID;
  26820. + }
  26821. +
  26822. + core_if->core_params->otg_cap = val;
  26823. +out:
  26824. + return retval;
  26825. +}
  26826. +
  26827. +int32_t dwc_otg_get_param_otg_cap(dwc_otg_core_if_t * core_if)
  26828. +{
  26829. + return core_if->core_params->otg_cap;
  26830. +}
  26831. +
  26832. +int dwc_otg_set_param_opt(dwc_otg_core_if_t * core_if, int32_t val)
  26833. +{
  26834. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  26835. + DWC_WARN("Wrong value for opt parameter\n");
  26836. + return -DWC_E_INVALID;
  26837. + }
  26838. + core_if->core_params->opt = val;
  26839. + return 0;
  26840. +}
  26841. +
  26842. +int32_t dwc_otg_get_param_opt(dwc_otg_core_if_t * core_if)
  26843. +{
  26844. + return core_if->core_params->opt;
  26845. +}
  26846. +
  26847. +int dwc_otg_set_param_dma_enable(dwc_otg_core_if_t * core_if, int32_t val)
  26848. +{
  26849. + int retval = 0;
  26850. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  26851. + DWC_WARN("Wrong value for dma enable\n");
  26852. + return -DWC_E_INVALID;
  26853. + }
  26854. +
  26855. + if ((val == 1) && (core_if->hwcfg2.b.architecture == 0)) {
  26856. + if (dwc_otg_param_initialized(core_if->core_params->dma_enable)) {
  26857. + DWC_ERROR
  26858. + ("%d invalid for dma_enable paremter. Check HW configuration.\n",
  26859. + val);
  26860. + }
  26861. + val = 0;
  26862. + retval = -DWC_E_INVALID;
  26863. + }
  26864. +
  26865. + core_if->core_params->dma_enable = val;
  26866. + if (val == 0) {
  26867. + dwc_otg_set_param_dma_desc_enable(core_if, 0);
  26868. + }
  26869. + return retval;
  26870. +}
  26871. +
  26872. +int32_t dwc_otg_get_param_dma_enable(dwc_otg_core_if_t * core_if)
  26873. +{
  26874. + return core_if->core_params->dma_enable;
  26875. +}
  26876. +
  26877. +int dwc_otg_set_param_dma_desc_enable(dwc_otg_core_if_t * core_if, int32_t val)
  26878. +{
  26879. + int retval = 0;
  26880. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  26881. + DWC_WARN("Wrong value for dma_enable\n");
  26882. + DWC_WARN("dma_desc_enable must be 0 or 1\n");
  26883. + return -DWC_E_INVALID;
  26884. + }
  26885. +
  26886. + if ((val == 1)
  26887. + && ((dwc_otg_get_param_dma_enable(core_if) == 0)
  26888. + || (core_if->hwcfg4.b.desc_dma == 0))) {
  26889. + if (dwc_otg_param_initialized
  26890. + (core_if->core_params->dma_desc_enable)) {
  26891. + DWC_ERROR
  26892. + ("%d invalid for dma_desc_enable paremter. Check HW configuration.\n",
  26893. + val);
  26894. + }
  26895. + val = 0;
  26896. + retval = -DWC_E_INVALID;
  26897. + }
  26898. + core_if->core_params->dma_desc_enable = val;
  26899. + return retval;
  26900. +}
  26901. +
  26902. +int32_t dwc_otg_get_param_dma_desc_enable(dwc_otg_core_if_t * core_if)
  26903. +{
  26904. + return core_if->core_params->dma_desc_enable;
  26905. +}
  26906. +
  26907. +int dwc_otg_set_param_host_support_fs_ls_low_power(dwc_otg_core_if_t * core_if,
  26908. + int32_t val)
  26909. +{
  26910. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  26911. + DWC_WARN("Wrong value for host_support_fs_low_power\n");
  26912. + DWC_WARN("host_support_fs_low_power must be 0 or 1\n");
  26913. + return -DWC_E_INVALID;
  26914. + }
  26915. + core_if->core_params->host_support_fs_ls_low_power = val;
  26916. + return 0;
  26917. +}
  26918. +
  26919. +int32_t dwc_otg_get_param_host_support_fs_ls_low_power(dwc_otg_core_if_t *
  26920. + core_if)
  26921. +{
  26922. + return core_if->core_params->host_support_fs_ls_low_power;
  26923. +}
  26924. +
  26925. +int dwc_otg_set_param_enable_dynamic_fifo(dwc_otg_core_if_t * core_if,
  26926. + int32_t val)
  26927. +{
  26928. + int retval = 0;
  26929. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  26930. + DWC_WARN("Wrong value for enable_dynamic_fifo\n");
  26931. + DWC_WARN("enable_dynamic_fifo must be 0 or 1\n");
  26932. + return -DWC_E_INVALID;
  26933. + }
  26934. +
  26935. + if ((val == 1) && (core_if->hwcfg2.b.dynamic_fifo == 0)) {
  26936. + if (dwc_otg_param_initialized
  26937. + (core_if->core_params->enable_dynamic_fifo)) {
  26938. + DWC_ERROR
  26939. + ("%d invalid for enable_dynamic_fifo paremter. Check HW configuration.\n",
  26940. + val);
  26941. + }
  26942. + val = 0;
  26943. + retval = -DWC_E_INVALID;
  26944. + }
  26945. + core_if->core_params->enable_dynamic_fifo = val;
  26946. + return retval;
  26947. +}
  26948. +
  26949. +int32_t dwc_otg_get_param_enable_dynamic_fifo(dwc_otg_core_if_t * core_if)
  26950. +{
  26951. + return core_if->core_params->enable_dynamic_fifo;
  26952. +}
  26953. +
  26954. +int dwc_otg_set_param_data_fifo_size(dwc_otg_core_if_t * core_if, int32_t val)
  26955. +{
  26956. + int retval = 0;
  26957. + if (DWC_OTG_PARAM_TEST(val, 32, 32768)) {
  26958. + DWC_WARN("Wrong value for data_fifo_size\n");
  26959. + DWC_WARN("data_fifo_size must be 32-32768\n");
  26960. + return -DWC_E_INVALID;
  26961. + }
  26962. +
  26963. + if (val > core_if->hwcfg3.b.dfifo_depth) {
  26964. + if (dwc_otg_param_initialized
  26965. + (core_if->core_params->data_fifo_size)) {
  26966. + DWC_ERROR
  26967. + ("%d invalid for data_fifo_size parameter. Check HW configuration.\n",
  26968. + val);
  26969. + }
  26970. + val = core_if->hwcfg3.b.dfifo_depth;
  26971. + retval = -DWC_E_INVALID;
  26972. + }
  26973. +
  26974. + core_if->core_params->data_fifo_size = val;
  26975. + return retval;
  26976. +}
  26977. +
  26978. +int32_t dwc_otg_get_param_data_fifo_size(dwc_otg_core_if_t * core_if)
  26979. +{
  26980. + return core_if->core_params->data_fifo_size;
  26981. +}
  26982. +
  26983. +int dwc_otg_set_param_dev_rx_fifo_size(dwc_otg_core_if_t * core_if, int32_t val)
  26984. +{
  26985. + int retval = 0;
  26986. + if (DWC_OTG_PARAM_TEST(val, 16, 32768)) {
  26987. + DWC_WARN("Wrong value for dev_rx_fifo_size\n");
  26988. + DWC_WARN("dev_rx_fifo_size must be 16-32768\n");
  26989. + return -DWC_E_INVALID;
  26990. + }
  26991. +
  26992. + if (val > DWC_READ_REG32(&core_if->core_global_regs->grxfsiz)) {
  26993. + if (dwc_otg_param_initialized(core_if->core_params->dev_rx_fifo_size)) {
  26994. + DWC_WARN("%d invalid for dev_rx_fifo_size parameter\n", val);
  26995. + }
  26996. + val = DWC_READ_REG32(&core_if->core_global_regs->grxfsiz);
  26997. + retval = -DWC_E_INVALID;
  26998. + }
  26999. +
  27000. + core_if->core_params->dev_rx_fifo_size = val;
  27001. + return retval;
  27002. +}
  27003. +
  27004. +int32_t dwc_otg_get_param_dev_rx_fifo_size(dwc_otg_core_if_t * core_if)
  27005. +{
  27006. + return core_if->core_params->dev_rx_fifo_size;
  27007. +}
  27008. +
  27009. +int dwc_otg_set_param_dev_nperio_tx_fifo_size(dwc_otg_core_if_t * core_if,
  27010. + int32_t val)
  27011. +{
  27012. + int retval = 0;
  27013. +
  27014. + if (DWC_OTG_PARAM_TEST(val, 16, 32768)) {
  27015. + DWC_WARN("Wrong value for dev_nperio_tx_fifo\n");
  27016. + DWC_WARN("dev_nperio_tx_fifo must be 16-32768\n");
  27017. + return -DWC_E_INVALID;
  27018. + }
  27019. +
  27020. + if (val > (DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz) >> 16)) {
  27021. + if (dwc_otg_param_initialized
  27022. + (core_if->core_params->dev_nperio_tx_fifo_size)) {
  27023. + DWC_ERROR
  27024. + ("%d invalid for dev_nperio_tx_fifo_size. Check HW configuration.\n",
  27025. + val);
  27026. + }
  27027. + val =
  27028. + (DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz) >>
  27029. + 16);
  27030. + retval = -DWC_E_INVALID;
  27031. + }
  27032. +
  27033. + core_if->core_params->dev_nperio_tx_fifo_size = val;
  27034. + return retval;
  27035. +}
  27036. +
  27037. +int32_t dwc_otg_get_param_dev_nperio_tx_fifo_size(dwc_otg_core_if_t * core_if)
  27038. +{
  27039. + return core_if->core_params->dev_nperio_tx_fifo_size;
  27040. +}
  27041. +
  27042. +int dwc_otg_set_param_host_rx_fifo_size(dwc_otg_core_if_t * core_if,
  27043. + int32_t val)
  27044. +{
  27045. + int retval = 0;
  27046. +
  27047. + if (DWC_OTG_PARAM_TEST(val, 16, 32768)) {
  27048. + DWC_WARN("Wrong value for host_rx_fifo_size\n");
  27049. + DWC_WARN("host_rx_fifo_size must be 16-32768\n");
  27050. + return -DWC_E_INVALID;
  27051. + }
  27052. +
  27053. + if (val > DWC_READ_REG32(&core_if->core_global_regs->grxfsiz)) {
  27054. + if (dwc_otg_param_initialized
  27055. + (core_if->core_params->host_rx_fifo_size)) {
  27056. + DWC_ERROR
  27057. + ("%d invalid for host_rx_fifo_size. Check HW configuration.\n",
  27058. + val);
  27059. + }
  27060. + val = DWC_READ_REG32(&core_if->core_global_regs->grxfsiz);
  27061. + retval = -DWC_E_INVALID;
  27062. + }
  27063. +
  27064. + core_if->core_params->host_rx_fifo_size = val;
  27065. + return retval;
  27066. +
  27067. +}
  27068. +
  27069. +int32_t dwc_otg_get_param_host_rx_fifo_size(dwc_otg_core_if_t * core_if)
  27070. +{
  27071. + return core_if->core_params->host_rx_fifo_size;
  27072. +}
  27073. +
  27074. +int dwc_otg_set_param_host_nperio_tx_fifo_size(dwc_otg_core_if_t * core_if,
  27075. + int32_t val)
  27076. +{
  27077. + int retval = 0;
  27078. +
  27079. + if (DWC_OTG_PARAM_TEST(val, 16, 32768)) {
  27080. + DWC_WARN("Wrong value for host_nperio_tx_fifo_size\n");
  27081. + DWC_WARN("host_nperio_tx_fifo_size must be 16-32768\n");
  27082. + return -DWC_E_INVALID;
  27083. + }
  27084. +
  27085. + if (val > (DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz) >> 16)) {
  27086. + if (dwc_otg_param_initialized
  27087. + (core_if->core_params->host_nperio_tx_fifo_size)) {
  27088. + DWC_ERROR
  27089. + ("%d invalid for host_nperio_tx_fifo_size. Check HW configuration.\n",
  27090. + val);
  27091. + }
  27092. + val =
  27093. + (DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz) >>
  27094. + 16);
  27095. + retval = -DWC_E_INVALID;
  27096. + }
  27097. +
  27098. + core_if->core_params->host_nperio_tx_fifo_size = val;
  27099. + return retval;
  27100. +}
  27101. +
  27102. +int32_t dwc_otg_get_param_host_nperio_tx_fifo_size(dwc_otg_core_if_t * core_if)
  27103. +{
  27104. + return core_if->core_params->host_nperio_tx_fifo_size;
  27105. +}
  27106. +
  27107. +int dwc_otg_set_param_host_perio_tx_fifo_size(dwc_otg_core_if_t * core_if,
  27108. + int32_t val)
  27109. +{
  27110. + int retval = 0;
  27111. + if (DWC_OTG_PARAM_TEST(val, 16, 32768)) {
  27112. + DWC_WARN("Wrong value for host_perio_tx_fifo_size\n");
  27113. + DWC_WARN("host_perio_tx_fifo_size must be 16-32768\n");
  27114. + return -DWC_E_INVALID;
  27115. + }
  27116. +
  27117. + if (val > ((core_if->hptxfsiz.d32) >> 16)) {
  27118. + if (dwc_otg_param_initialized
  27119. + (core_if->core_params->host_perio_tx_fifo_size)) {
  27120. + DWC_ERROR
  27121. + ("%d invalid for host_perio_tx_fifo_size. Check HW configuration.\n",
  27122. + val);
  27123. + }
  27124. + val = (core_if->hptxfsiz.d32) >> 16;
  27125. + retval = -DWC_E_INVALID;
  27126. + }
  27127. +
  27128. + core_if->core_params->host_perio_tx_fifo_size = val;
  27129. + return retval;
  27130. +}
  27131. +
  27132. +int32_t dwc_otg_get_param_host_perio_tx_fifo_size(dwc_otg_core_if_t * core_if)
  27133. +{
  27134. + return core_if->core_params->host_perio_tx_fifo_size;
  27135. +}
  27136. +
  27137. +int dwc_otg_set_param_max_transfer_size(dwc_otg_core_if_t * core_if,
  27138. + int32_t val)
  27139. +{
  27140. + int retval = 0;
  27141. +
  27142. + if (DWC_OTG_PARAM_TEST(val, 2047, 524288)) {
  27143. + DWC_WARN("Wrong value for max_transfer_size\n");
  27144. + DWC_WARN("max_transfer_size must be 2047-524288\n");
  27145. + return -DWC_E_INVALID;
  27146. + }
  27147. +
  27148. + if (val >= (1 << (core_if->hwcfg3.b.xfer_size_cntr_width + 11))) {
  27149. + if (dwc_otg_param_initialized
  27150. + (core_if->core_params->max_transfer_size)) {
  27151. + DWC_ERROR
  27152. + ("%d invalid for max_transfer_size. Check HW configuration.\n",
  27153. + val);
  27154. + }
  27155. + val =
  27156. + ((1 << (core_if->hwcfg3.b.packet_size_cntr_width + 11)) -
  27157. + 1);
  27158. + retval = -DWC_E_INVALID;
  27159. + }
  27160. +
  27161. + core_if->core_params->max_transfer_size = val;
  27162. + return retval;
  27163. +}
  27164. +
  27165. +int32_t dwc_otg_get_param_max_transfer_size(dwc_otg_core_if_t * core_if)
  27166. +{
  27167. + return core_if->core_params->max_transfer_size;
  27168. +}
  27169. +
  27170. +int dwc_otg_set_param_max_packet_count(dwc_otg_core_if_t * core_if, int32_t val)
  27171. +{
  27172. + int retval = 0;
  27173. +
  27174. + if (DWC_OTG_PARAM_TEST(val, 15, 511)) {
  27175. + DWC_WARN("Wrong value for max_packet_count\n");
  27176. + DWC_WARN("max_packet_count must be 15-511\n");
  27177. + return -DWC_E_INVALID;
  27178. + }
  27179. +
  27180. + if (val > (1 << (core_if->hwcfg3.b.packet_size_cntr_width + 4))) {
  27181. + if (dwc_otg_param_initialized
  27182. + (core_if->core_params->max_packet_count)) {
  27183. + DWC_ERROR
  27184. + ("%d invalid for max_packet_count. Check HW configuration.\n",
  27185. + val);
  27186. + }
  27187. + val =
  27188. + ((1 << (core_if->hwcfg3.b.packet_size_cntr_width + 4)) - 1);
  27189. + retval = -DWC_E_INVALID;
  27190. + }
  27191. +
  27192. + core_if->core_params->max_packet_count = val;
  27193. + return retval;
  27194. +}
  27195. +
  27196. +int32_t dwc_otg_get_param_max_packet_count(dwc_otg_core_if_t * core_if)
  27197. +{
  27198. + return core_if->core_params->max_packet_count;
  27199. +}
  27200. +
  27201. +int dwc_otg_set_param_host_channels(dwc_otg_core_if_t * core_if, int32_t val)
  27202. +{
  27203. + int retval = 0;
  27204. +
  27205. + if (DWC_OTG_PARAM_TEST(val, 1, 16)) {
  27206. + DWC_WARN("Wrong value for host_channels\n");
  27207. + DWC_WARN("host_channels must be 1-16\n");
  27208. + return -DWC_E_INVALID;
  27209. + }
  27210. +
  27211. + if (val > (core_if->hwcfg2.b.num_host_chan + 1)) {
  27212. + if (dwc_otg_param_initialized
  27213. + (core_if->core_params->host_channels)) {
  27214. + DWC_ERROR
  27215. + ("%d invalid for host_channels. Check HW configurations.\n",
  27216. + val);
  27217. + }
  27218. + val = (core_if->hwcfg2.b.num_host_chan + 1);
  27219. + retval = -DWC_E_INVALID;
  27220. + }
  27221. +
  27222. + core_if->core_params->host_channels = val;
  27223. + return retval;
  27224. +}
  27225. +
  27226. +int32_t dwc_otg_get_param_host_channels(dwc_otg_core_if_t * core_if)
  27227. +{
  27228. + return core_if->core_params->host_channels;
  27229. +}
  27230. +
  27231. +int dwc_otg_set_param_dev_endpoints(dwc_otg_core_if_t * core_if, int32_t val)
  27232. +{
  27233. + int retval = 0;
  27234. +
  27235. + if (DWC_OTG_PARAM_TEST(val, 1, 15)) {
  27236. + DWC_WARN("Wrong value for dev_endpoints\n");
  27237. + DWC_WARN("dev_endpoints must be 1-15\n");
  27238. + return -DWC_E_INVALID;
  27239. + }
  27240. +
  27241. + if (val > (core_if->hwcfg2.b.num_dev_ep)) {
  27242. + if (dwc_otg_param_initialized
  27243. + (core_if->core_params->dev_endpoints)) {
  27244. + DWC_ERROR
  27245. + ("%d invalid for dev_endpoints. Check HW configurations.\n",
  27246. + val);
  27247. + }
  27248. + val = core_if->hwcfg2.b.num_dev_ep;
  27249. + retval = -DWC_E_INVALID;
  27250. + }
  27251. +
  27252. + core_if->core_params->dev_endpoints = val;
  27253. + return retval;
  27254. +}
  27255. +
  27256. +int32_t dwc_otg_get_param_dev_endpoints(dwc_otg_core_if_t * core_if)
  27257. +{
  27258. + return core_if->core_params->dev_endpoints;
  27259. +}
  27260. +
  27261. +int dwc_otg_set_param_phy_type(dwc_otg_core_if_t * core_if, int32_t val)
  27262. +{
  27263. + int retval = 0;
  27264. + int valid = 0;
  27265. +
  27266. + if (DWC_OTG_PARAM_TEST(val, 0, 2)) {
  27267. + DWC_WARN("Wrong value for phy_type\n");
  27268. + DWC_WARN("phy_type must be 0,1 or 2\n");
  27269. + return -DWC_E_INVALID;
  27270. + }
  27271. +#ifndef NO_FS_PHY_HW_CHECKS
  27272. + if ((val == DWC_PHY_TYPE_PARAM_UTMI) &&
  27273. + ((core_if->hwcfg2.b.hs_phy_type == 1) ||
  27274. + (core_if->hwcfg2.b.hs_phy_type == 3))) {
  27275. + valid = 1;
  27276. + } else if ((val == DWC_PHY_TYPE_PARAM_ULPI) &&
  27277. + ((core_if->hwcfg2.b.hs_phy_type == 2) ||
  27278. + (core_if->hwcfg2.b.hs_phy_type == 3))) {
  27279. + valid = 1;
  27280. + } else if ((val == DWC_PHY_TYPE_PARAM_FS) &&
  27281. + (core_if->hwcfg2.b.fs_phy_type == 1)) {
  27282. + valid = 1;
  27283. + }
  27284. + if (!valid) {
  27285. + if (dwc_otg_param_initialized(core_if->core_params->phy_type)) {
  27286. + DWC_ERROR
  27287. + ("%d invalid for phy_type. Check HW configurations.\n",
  27288. + val);
  27289. + }
  27290. + if (core_if->hwcfg2.b.hs_phy_type) {
  27291. + if ((core_if->hwcfg2.b.hs_phy_type == 3) ||
  27292. + (core_if->hwcfg2.b.hs_phy_type == 1)) {
  27293. + val = DWC_PHY_TYPE_PARAM_UTMI;
  27294. + } else {
  27295. + val = DWC_PHY_TYPE_PARAM_ULPI;
  27296. + }
  27297. + }
  27298. + retval = -DWC_E_INVALID;
  27299. + }
  27300. +#endif
  27301. + core_if->core_params->phy_type = val;
  27302. + return retval;
  27303. +}
  27304. +
  27305. +int32_t dwc_otg_get_param_phy_type(dwc_otg_core_if_t * core_if)
  27306. +{
  27307. + return core_if->core_params->phy_type;
  27308. +}
  27309. +
  27310. +int dwc_otg_set_param_speed(dwc_otg_core_if_t * core_if, int32_t val)
  27311. +{
  27312. + int retval = 0;
  27313. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  27314. + DWC_WARN("Wrong value for speed parameter\n");
  27315. + DWC_WARN("max_speed parameter must be 0 or 1\n");
  27316. + return -DWC_E_INVALID;
  27317. + }
  27318. + if ((val == 0)
  27319. + && dwc_otg_get_param_phy_type(core_if) == DWC_PHY_TYPE_PARAM_FS) {
  27320. + if (dwc_otg_param_initialized(core_if->core_params->speed)) {
  27321. + DWC_ERROR
  27322. + ("%d invalid for speed paremter. Check HW configuration.\n",
  27323. + val);
  27324. + }
  27325. + val =
  27326. + (dwc_otg_get_param_phy_type(core_if) ==
  27327. + DWC_PHY_TYPE_PARAM_FS ? 1 : 0);
  27328. + retval = -DWC_E_INVALID;
  27329. + }
  27330. + core_if->core_params->speed = val;
  27331. + return retval;
  27332. +}
  27333. +
  27334. +int32_t dwc_otg_get_param_speed(dwc_otg_core_if_t * core_if)
  27335. +{
  27336. + return core_if->core_params->speed;
  27337. +}
  27338. +
  27339. +int dwc_otg_set_param_host_ls_low_power_phy_clk(dwc_otg_core_if_t * core_if,
  27340. + int32_t val)
  27341. +{
  27342. + int retval = 0;
  27343. +
  27344. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  27345. + DWC_WARN
  27346. + ("Wrong value for host_ls_low_power_phy_clk parameter\n");
  27347. + DWC_WARN("host_ls_low_power_phy_clk must be 0 or 1\n");
  27348. + return -DWC_E_INVALID;
  27349. + }
  27350. +
  27351. + if ((val == DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ)
  27352. + && (dwc_otg_get_param_phy_type(core_if) == DWC_PHY_TYPE_PARAM_FS)) {
  27353. + if (dwc_otg_param_initialized
  27354. + (core_if->core_params->host_ls_low_power_phy_clk)) {
  27355. + DWC_ERROR
  27356. + ("%d invalid for host_ls_low_power_phy_clk. Check HW configuration.\n",
  27357. + val);
  27358. + }
  27359. + val =
  27360. + (dwc_otg_get_param_phy_type(core_if) ==
  27361. + DWC_PHY_TYPE_PARAM_FS) ?
  27362. + DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_6MHZ :
  27363. + DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ;
  27364. + retval = -DWC_E_INVALID;
  27365. + }
  27366. +
  27367. + core_if->core_params->host_ls_low_power_phy_clk = val;
  27368. + return retval;
  27369. +}
  27370. +
  27371. +int32_t dwc_otg_get_param_host_ls_low_power_phy_clk(dwc_otg_core_if_t * core_if)
  27372. +{
  27373. + return core_if->core_params->host_ls_low_power_phy_clk;
  27374. +}
  27375. +
  27376. +int dwc_otg_set_param_phy_ulpi_ddr(dwc_otg_core_if_t * core_if, int32_t val)
  27377. +{
  27378. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  27379. + DWC_WARN("Wrong value for phy_ulpi_ddr\n");
  27380. + DWC_WARN("phy_upli_ddr must be 0 or 1\n");
  27381. + return -DWC_E_INVALID;
  27382. + }
  27383. +
  27384. + core_if->core_params->phy_ulpi_ddr = val;
  27385. + return 0;
  27386. +}
  27387. +
  27388. +int32_t dwc_otg_get_param_phy_ulpi_ddr(dwc_otg_core_if_t * core_if)
  27389. +{
  27390. + return core_if->core_params->phy_ulpi_ddr;
  27391. +}
  27392. +
  27393. +int dwc_otg_set_param_phy_ulpi_ext_vbus(dwc_otg_core_if_t * core_if,
  27394. + int32_t val)
  27395. +{
  27396. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  27397. + DWC_WARN("Wrong valaue for phy_ulpi_ext_vbus\n");
  27398. + DWC_WARN("phy_ulpi_ext_vbus must be 0 or 1\n");
  27399. + return -DWC_E_INVALID;
  27400. + }
  27401. +
  27402. + core_if->core_params->phy_ulpi_ext_vbus = val;
  27403. + return 0;
  27404. +}
  27405. +
  27406. +int32_t dwc_otg_get_param_phy_ulpi_ext_vbus(dwc_otg_core_if_t * core_if)
  27407. +{
  27408. + return core_if->core_params->phy_ulpi_ext_vbus;
  27409. +}
  27410. +
  27411. +int dwc_otg_set_param_phy_utmi_width(dwc_otg_core_if_t * core_if, int32_t val)
  27412. +{
  27413. + if (DWC_OTG_PARAM_TEST(val, 8, 8) && DWC_OTG_PARAM_TEST(val, 16, 16)) {
  27414. + DWC_WARN("Wrong valaue for phy_utmi_width\n");
  27415. + DWC_WARN("phy_utmi_width must be 8 or 16\n");
  27416. + return -DWC_E_INVALID;
  27417. + }
  27418. +
  27419. + core_if->core_params->phy_utmi_width = val;
  27420. + return 0;
  27421. +}
  27422. +
  27423. +int32_t dwc_otg_get_param_phy_utmi_width(dwc_otg_core_if_t * core_if)
  27424. +{
  27425. + return core_if->core_params->phy_utmi_width;
  27426. +}
  27427. +
  27428. +int dwc_otg_set_param_ulpi_fs_ls(dwc_otg_core_if_t * core_if, int32_t val)
  27429. +{
  27430. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  27431. + DWC_WARN("Wrong valaue for ulpi_fs_ls\n");
  27432. + DWC_WARN("ulpi_fs_ls must be 0 or 1\n");
  27433. + return -DWC_E_INVALID;
  27434. + }
  27435. +
  27436. + core_if->core_params->ulpi_fs_ls = val;
  27437. + return 0;
  27438. +}
  27439. +
  27440. +int32_t dwc_otg_get_param_ulpi_fs_ls(dwc_otg_core_if_t * core_if)
  27441. +{
  27442. + return core_if->core_params->ulpi_fs_ls;
  27443. +}
  27444. +
  27445. +int dwc_otg_set_param_ts_dline(dwc_otg_core_if_t * core_if, int32_t val)
  27446. +{
  27447. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  27448. + DWC_WARN("Wrong valaue for ts_dline\n");
  27449. + DWC_WARN("ts_dline must be 0 or 1\n");
  27450. + return -DWC_E_INVALID;
  27451. + }
  27452. +
  27453. + core_if->core_params->ts_dline = val;
  27454. + return 0;
  27455. +}
  27456. +
  27457. +int32_t dwc_otg_get_param_ts_dline(dwc_otg_core_if_t * core_if)
  27458. +{
  27459. + return core_if->core_params->ts_dline;
  27460. +}
  27461. +
  27462. +int dwc_otg_set_param_i2c_enable(dwc_otg_core_if_t * core_if, int32_t val)
  27463. +{
  27464. + int retval = 0;
  27465. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  27466. + DWC_WARN("Wrong valaue for i2c_enable\n");
  27467. + DWC_WARN("i2c_enable must be 0 or 1\n");
  27468. + return -DWC_E_INVALID;
  27469. + }
  27470. +#ifndef NO_FS_PHY_HW_CHECK
  27471. + if (val == 1 && core_if->hwcfg3.b.i2c == 0) {
  27472. + if (dwc_otg_param_initialized(core_if->core_params->i2c_enable)) {
  27473. + DWC_ERROR
  27474. + ("%d invalid for i2c_enable. Check HW configuration.\n",
  27475. + val);
  27476. + }
  27477. + val = 0;
  27478. + retval = -DWC_E_INVALID;
  27479. + }
  27480. +#endif
  27481. +
  27482. + core_if->core_params->i2c_enable = val;
  27483. + return retval;
  27484. +}
  27485. +
  27486. +int32_t dwc_otg_get_param_i2c_enable(dwc_otg_core_if_t * core_if)
  27487. +{
  27488. + return core_if->core_params->i2c_enable;
  27489. +}
  27490. +
  27491. +int dwc_otg_set_param_dev_perio_tx_fifo_size(dwc_otg_core_if_t * core_if,
  27492. + int32_t val, int fifo_num)
  27493. +{
  27494. + int retval = 0;
  27495. +
  27496. + if (DWC_OTG_PARAM_TEST(val, 4, 768)) {
  27497. + DWC_WARN("Wrong value for dev_perio_tx_fifo_size\n");
  27498. + DWC_WARN("dev_perio_tx_fifo_size must be 4-768\n");
  27499. + return -DWC_E_INVALID;
  27500. + }
  27501. +
  27502. + if (val >
  27503. + (DWC_READ_REG32(&core_if->core_global_regs->dtxfsiz[fifo_num]))) {
  27504. + if (dwc_otg_param_initialized
  27505. + (core_if->core_params->dev_perio_tx_fifo_size[fifo_num])) {
  27506. + DWC_ERROR
  27507. + ("`%d' invalid for parameter `dev_perio_fifo_size_%d'. Check HW configuration.\n",
  27508. + val, fifo_num);
  27509. + }
  27510. + val = (DWC_READ_REG32(&core_if->core_global_regs->dtxfsiz[fifo_num]));
  27511. + retval = -DWC_E_INVALID;
  27512. + }
  27513. +
  27514. + core_if->core_params->dev_perio_tx_fifo_size[fifo_num] = val;
  27515. + return retval;
  27516. +}
  27517. +
  27518. +int32_t dwc_otg_get_param_dev_perio_tx_fifo_size(dwc_otg_core_if_t * core_if,
  27519. + int fifo_num)
  27520. +{
  27521. + return core_if->core_params->dev_perio_tx_fifo_size[fifo_num];
  27522. +}
  27523. +
  27524. +int dwc_otg_set_param_en_multiple_tx_fifo(dwc_otg_core_if_t * core_if,
  27525. + int32_t val)
  27526. +{
  27527. + int retval = 0;
  27528. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  27529. + DWC_WARN("Wrong valaue for en_multiple_tx_fifo,\n");
  27530. + DWC_WARN("en_multiple_tx_fifo must be 0 or 1\n");
  27531. + return -DWC_E_INVALID;
  27532. + }
  27533. +
  27534. + if (val == 1 && core_if->hwcfg4.b.ded_fifo_en == 0) {
  27535. + if (dwc_otg_param_initialized
  27536. + (core_if->core_params->en_multiple_tx_fifo)) {
  27537. + DWC_ERROR
  27538. + ("%d invalid for parameter en_multiple_tx_fifo. Check HW configuration.\n",
  27539. + val);
  27540. + }
  27541. + val = 0;
  27542. + retval = -DWC_E_INVALID;
  27543. + }
  27544. +
  27545. + core_if->core_params->en_multiple_tx_fifo = val;
  27546. + return retval;
  27547. +}
  27548. +
  27549. +int32_t dwc_otg_get_param_en_multiple_tx_fifo(dwc_otg_core_if_t * core_if)
  27550. +{
  27551. + return core_if->core_params->en_multiple_tx_fifo;
  27552. +}
  27553. +
  27554. +int dwc_otg_set_param_dev_tx_fifo_size(dwc_otg_core_if_t * core_if, int32_t val,
  27555. + int fifo_num)
  27556. +{
  27557. + int retval = 0;
  27558. +
  27559. + if (DWC_OTG_PARAM_TEST(val, 4, 768)) {
  27560. + DWC_WARN("Wrong value for dev_tx_fifo_size\n");
  27561. + DWC_WARN("dev_tx_fifo_size must be 4-768\n");
  27562. + return -DWC_E_INVALID;
  27563. + }
  27564. +
  27565. + if (val >
  27566. + (DWC_READ_REG32(&core_if->core_global_regs->dtxfsiz[fifo_num]))) {
  27567. + if (dwc_otg_param_initialized
  27568. + (core_if->core_params->dev_tx_fifo_size[fifo_num])) {
  27569. + DWC_ERROR
  27570. + ("`%d' invalid for parameter `dev_tx_fifo_size_%d'. Check HW configuration.\n",
  27571. + val, fifo_num);
  27572. + }
  27573. + val = (DWC_READ_REG32(&core_if->core_global_regs->dtxfsiz[fifo_num]));
  27574. + retval = -DWC_E_INVALID;
  27575. + }
  27576. +
  27577. + core_if->core_params->dev_tx_fifo_size[fifo_num] = val;
  27578. + return retval;
  27579. +}
  27580. +
  27581. +int32_t dwc_otg_get_param_dev_tx_fifo_size(dwc_otg_core_if_t * core_if,
  27582. + int fifo_num)
  27583. +{
  27584. + return core_if->core_params->dev_tx_fifo_size[fifo_num];
  27585. +}
  27586. +
  27587. +int dwc_otg_set_param_thr_ctl(dwc_otg_core_if_t * core_if, int32_t val)
  27588. +{
  27589. + int retval = 0;
  27590. +
  27591. + if (DWC_OTG_PARAM_TEST(val, 0, 7)) {
  27592. + DWC_WARN("Wrong value for thr_ctl\n");
  27593. + DWC_WARN("thr_ctl must be 0-7\n");
  27594. + return -DWC_E_INVALID;
  27595. + }
  27596. +
  27597. + if ((val != 0) &&
  27598. + (!dwc_otg_get_param_dma_enable(core_if) ||
  27599. + !core_if->hwcfg4.b.ded_fifo_en)) {
  27600. + if (dwc_otg_param_initialized(core_if->core_params->thr_ctl)) {
  27601. + DWC_ERROR
  27602. + ("%d invalid for parameter thr_ctl. Check HW configuration.\n",
  27603. + val);
  27604. + }
  27605. + val = 0;
  27606. + retval = -DWC_E_INVALID;
  27607. + }
  27608. +
  27609. + core_if->core_params->thr_ctl = val;
  27610. + return retval;
  27611. +}
  27612. +
  27613. +int32_t dwc_otg_get_param_thr_ctl(dwc_otg_core_if_t * core_if)
  27614. +{
  27615. + return core_if->core_params->thr_ctl;
  27616. +}
  27617. +
  27618. +int dwc_otg_set_param_lpm_enable(dwc_otg_core_if_t * core_if, int32_t val)
  27619. +{
  27620. + int retval = 0;
  27621. +
  27622. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  27623. + DWC_WARN("Wrong value for lpm_enable\n");
  27624. + DWC_WARN("lpm_enable must be 0 or 1\n");
  27625. + return -DWC_E_INVALID;
  27626. + }
  27627. +
  27628. + if (val && !core_if->hwcfg3.b.otg_lpm_en) {
  27629. + if (dwc_otg_param_initialized(core_if->core_params->lpm_enable)) {
  27630. + DWC_ERROR
  27631. + ("%d invalid for parameter lpm_enable. Check HW configuration.\n",
  27632. + val);
  27633. + }
  27634. + val = 0;
  27635. + retval = -DWC_E_INVALID;
  27636. + }
  27637. +
  27638. + core_if->core_params->lpm_enable = val;
  27639. + return retval;
  27640. +}
  27641. +
  27642. +int32_t dwc_otg_get_param_lpm_enable(dwc_otg_core_if_t * core_if)
  27643. +{
  27644. + return core_if->core_params->lpm_enable;
  27645. +}
  27646. +
  27647. +int dwc_otg_set_param_tx_thr_length(dwc_otg_core_if_t * core_if, int32_t val)
  27648. +{
  27649. + if (DWC_OTG_PARAM_TEST(val, 8, 128)) {
  27650. + DWC_WARN("Wrong valaue for tx_thr_length\n");
  27651. + DWC_WARN("tx_thr_length must be 8 - 128\n");
  27652. + return -DWC_E_INVALID;
  27653. + }
  27654. +
  27655. + core_if->core_params->tx_thr_length = val;
  27656. + return 0;
  27657. +}
  27658. +
  27659. +int32_t dwc_otg_get_param_tx_thr_length(dwc_otg_core_if_t * core_if)
  27660. +{
  27661. + return core_if->core_params->tx_thr_length;
  27662. +}
  27663. +
  27664. +int dwc_otg_set_param_rx_thr_length(dwc_otg_core_if_t * core_if, int32_t val)
  27665. +{
  27666. + if (DWC_OTG_PARAM_TEST(val, 8, 128)) {
  27667. + DWC_WARN("Wrong valaue for rx_thr_length\n");
  27668. + DWC_WARN("rx_thr_length must be 8 - 128\n");
  27669. + return -DWC_E_INVALID;
  27670. + }
  27671. +
  27672. + core_if->core_params->rx_thr_length = val;
  27673. + return 0;
  27674. +}
  27675. +
  27676. +int32_t dwc_otg_get_param_rx_thr_length(dwc_otg_core_if_t * core_if)
  27677. +{
  27678. + return core_if->core_params->rx_thr_length;
  27679. +}
  27680. +
  27681. +int dwc_otg_set_param_dma_burst_size(dwc_otg_core_if_t * core_if, int32_t val)
  27682. +{
  27683. + if (DWC_OTG_PARAM_TEST(val, 1, 1) &&
  27684. + DWC_OTG_PARAM_TEST(val, 4, 4) &&
  27685. + DWC_OTG_PARAM_TEST(val, 8, 8) &&
  27686. + DWC_OTG_PARAM_TEST(val, 16, 16) &&
  27687. + DWC_OTG_PARAM_TEST(val, 32, 32) &&
  27688. + DWC_OTG_PARAM_TEST(val, 64, 64) &&
  27689. + DWC_OTG_PARAM_TEST(val, 128, 128) &&
  27690. + DWC_OTG_PARAM_TEST(val, 256, 256)) {
  27691. + DWC_WARN("`%d' invalid for parameter `dma_burst_size'\n", val);
  27692. + return -DWC_E_INVALID;
  27693. + }
  27694. + core_if->core_params->dma_burst_size = val;
  27695. + return 0;
  27696. +}
  27697. +
  27698. +int32_t dwc_otg_get_param_dma_burst_size(dwc_otg_core_if_t * core_if)
  27699. +{
  27700. + return core_if->core_params->dma_burst_size;
  27701. +}
  27702. +
  27703. +int dwc_otg_set_param_pti_enable(dwc_otg_core_if_t * core_if, int32_t val)
  27704. +{
  27705. + int retval = 0;
  27706. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  27707. + DWC_WARN("`%d' invalid for parameter `pti_enable'\n", val);
  27708. + return -DWC_E_INVALID;
  27709. + }
  27710. + if (val && (core_if->snpsid < OTG_CORE_REV_2_72a)) {
  27711. + if (dwc_otg_param_initialized(core_if->core_params->pti_enable)) {
  27712. + DWC_ERROR
  27713. + ("%d invalid for parameter pti_enable. Check HW configuration.\n",
  27714. + val);
  27715. + }
  27716. + retval = -DWC_E_INVALID;
  27717. + val = 0;
  27718. + }
  27719. + core_if->core_params->pti_enable = val;
  27720. + return retval;
  27721. +}
  27722. +
  27723. +int32_t dwc_otg_get_param_pti_enable(dwc_otg_core_if_t * core_if)
  27724. +{
  27725. + return core_if->core_params->pti_enable;
  27726. +}
  27727. +
  27728. +int dwc_otg_set_param_mpi_enable(dwc_otg_core_if_t * core_if, int32_t val)
  27729. +{
  27730. + int retval = 0;
  27731. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  27732. + DWC_WARN("`%d' invalid for parameter `mpi_enable'\n", val);
  27733. + return -DWC_E_INVALID;
  27734. + }
  27735. + if (val && (core_if->hwcfg2.b.multi_proc_int == 0)) {
  27736. + if (dwc_otg_param_initialized(core_if->core_params->mpi_enable)) {
  27737. + DWC_ERROR
  27738. + ("%d invalid for parameter mpi_enable. Check HW configuration.\n",
  27739. + val);
  27740. + }
  27741. + retval = -DWC_E_INVALID;
  27742. + val = 0;
  27743. + }
  27744. + core_if->core_params->mpi_enable = val;
  27745. + return retval;
  27746. +}
  27747. +
  27748. +int32_t dwc_otg_get_param_mpi_enable(dwc_otg_core_if_t * core_if)
  27749. +{
  27750. + return core_if->core_params->mpi_enable;
  27751. +}
  27752. +
  27753. +int dwc_otg_set_param_adp_enable(dwc_otg_core_if_t * core_if, int32_t val)
  27754. +{
  27755. + int retval = 0;
  27756. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  27757. + DWC_WARN("`%d' invalid for parameter `adp_enable'\n", val);
  27758. + return -DWC_E_INVALID;
  27759. + }
  27760. + if (val && (core_if->hwcfg3.b.adp_supp == 0)) {
  27761. + if (dwc_otg_param_initialized
  27762. + (core_if->core_params->adp_supp_enable)) {
  27763. + DWC_ERROR
  27764. + ("%d invalid for parameter adp_enable. Check HW configuration.\n",
  27765. + val);
  27766. + }
  27767. + retval = -DWC_E_INVALID;
  27768. + val = 0;
  27769. + }
  27770. + core_if->core_params->adp_supp_enable = val;
  27771. + /*Set OTG version 2.0 in case of enabling ADP*/
  27772. + if (val)
  27773. + dwc_otg_set_param_otg_ver(core_if, 1);
  27774. +
  27775. + return retval;
  27776. +}
  27777. +
  27778. +int32_t dwc_otg_get_param_adp_enable(dwc_otg_core_if_t * core_if)
  27779. +{
  27780. + return core_if->core_params->adp_supp_enable;
  27781. +}
  27782. +
  27783. +int dwc_otg_set_param_ic_usb_cap(dwc_otg_core_if_t * core_if, int32_t val)
  27784. +{
  27785. + int retval = 0;
  27786. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  27787. + DWC_WARN("`%d' invalid for parameter `ic_usb_cap'\n", val);
  27788. + DWC_WARN("ic_usb_cap must be 0 or 1\n");
  27789. + return -DWC_E_INVALID;
  27790. + }
  27791. +
  27792. + if (val && (core_if->hwcfg2.b.otg_enable_ic_usb == 0)) {
  27793. + if (dwc_otg_param_initialized(core_if->core_params->ic_usb_cap)) {
  27794. + DWC_ERROR
  27795. + ("%d invalid for parameter ic_usb_cap. Check HW configuration.\n",
  27796. + val);
  27797. + }
  27798. + retval = -DWC_E_INVALID;
  27799. + val = 0;
  27800. + }
  27801. + core_if->core_params->ic_usb_cap = val;
  27802. + return retval;
  27803. +}
  27804. +
  27805. +int32_t dwc_otg_get_param_ic_usb_cap(dwc_otg_core_if_t * core_if)
  27806. +{
  27807. + return core_if->core_params->ic_usb_cap;
  27808. +}
  27809. +
  27810. +int dwc_otg_set_param_ahb_thr_ratio(dwc_otg_core_if_t * core_if, int32_t val)
  27811. +{
  27812. + int retval = 0;
  27813. + int valid = 1;
  27814. +
  27815. + if (DWC_OTG_PARAM_TEST(val, 0, 3)) {
  27816. + DWC_WARN("`%d' invalid for parameter `ahb_thr_ratio'\n", val);
  27817. + DWC_WARN("ahb_thr_ratio must be 0 - 3\n");
  27818. + return -DWC_E_INVALID;
  27819. + }
  27820. +
  27821. + if (val
  27822. + && (core_if->snpsid < OTG_CORE_REV_2_81a
  27823. + || !dwc_otg_get_param_thr_ctl(core_if))) {
  27824. + valid = 0;
  27825. + } else if (val
  27826. + && ((dwc_otg_get_param_tx_thr_length(core_if) / (1 << val)) <
  27827. + 4)) {
  27828. + valid = 0;
  27829. + }
  27830. + if (valid == 0) {
  27831. + if (dwc_otg_param_initialized
  27832. + (core_if->core_params->ahb_thr_ratio)) {
  27833. + DWC_ERROR
  27834. + ("%d invalid for parameter ahb_thr_ratio. Check HW configuration.\n",
  27835. + val);
  27836. + }
  27837. + retval = -DWC_E_INVALID;
  27838. + val = 0;
  27839. + }
  27840. +
  27841. + core_if->core_params->ahb_thr_ratio = val;
  27842. + return retval;
  27843. +}
  27844. +
  27845. +int32_t dwc_otg_get_param_ahb_thr_ratio(dwc_otg_core_if_t * core_if)
  27846. +{
  27847. + return core_if->core_params->ahb_thr_ratio;
  27848. +}
  27849. +
  27850. +int dwc_otg_set_param_power_down(dwc_otg_core_if_t * core_if, int32_t val)
  27851. +{
  27852. + int retval = 0;
  27853. + int valid = 1;
  27854. + hwcfg4_data_t hwcfg4 = {.d32 = 0 };
  27855. + hwcfg4.d32 = DWC_READ_REG32(&core_if->core_global_regs->ghwcfg4);
  27856. +
  27857. + if (DWC_OTG_PARAM_TEST(val, 0, 3)) {
  27858. + DWC_WARN("`%d' invalid for parameter `power_down'\n", val);
  27859. + DWC_WARN("power_down must be 0 - 2\n");
  27860. + return -DWC_E_INVALID;
  27861. + }
  27862. +
  27863. + if ((val == 2) && (core_if->snpsid < OTG_CORE_REV_2_91a)) {
  27864. + valid = 0;
  27865. + }
  27866. + if ((val == 3)
  27867. + && ((core_if->snpsid < OTG_CORE_REV_3_00a)
  27868. + || (hwcfg4.b.xhiber == 0))) {
  27869. + valid = 0;
  27870. + }
  27871. + if (valid == 0) {
  27872. + if (dwc_otg_param_initialized(core_if->core_params->power_down)) {
  27873. + DWC_ERROR
  27874. + ("%d invalid for parameter power_down. Check HW configuration.\n",
  27875. + val);
  27876. + }
  27877. + retval = -DWC_E_INVALID;
  27878. + val = 0;
  27879. + }
  27880. + core_if->core_params->power_down = val;
  27881. + return retval;
  27882. +}
  27883. +
  27884. +int32_t dwc_otg_get_param_power_down(dwc_otg_core_if_t * core_if)
  27885. +{
  27886. + return core_if->core_params->power_down;
  27887. +}
  27888. +
  27889. +int dwc_otg_set_param_reload_ctl(dwc_otg_core_if_t * core_if, int32_t val)
  27890. +{
  27891. + int retval = 0;
  27892. + int valid = 1;
  27893. +
  27894. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  27895. + DWC_WARN("`%d' invalid for parameter `reload_ctl'\n", val);
  27896. + DWC_WARN("reload_ctl must be 0 or 1\n");
  27897. + return -DWC_E_INVALID;
  27898. + }
  27899. +
  27900. + if ((val == 1) && (core_if->snpsid < OTG_CORE_REV_2_92a)) {
  27901. + valid = 0;
  27902. + }
  27903. + if (valid == 0) {
  27904. + if (dwc_otg_param_initialized(core_if->core_params->reload_ctl)) {
  27905. + DWC_ERROR("%d invalid for parameter reload_ctl."
  27906. + "Check HW configuration.\n", val);
  27907. + }
  27908. + retval = -DWC_E_INVALID;
  27909. + val = 0;
  27910. + }
  27911. + core_if->core_params->reload_ctl = val;
  27912. + return retval;
  27913. +}
  27914. +
  27915. +int32_t dwc_otg_get_param_reload_ctl(dwc_otg_core_if_t * core_if)
  27916. +{
  27917. + return core_if->core_params->reload_ctl;
  27918. +}
  27919. +
  27920. +int dwc_otg_set_param_dev_out_nak(dwc_otg_core_if_t * core_if, int32_t val)
  27921. +{
  27922. + int retval = 0;
  27923. + int valid = 1;
  27924. +
  27925. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  27926. + DWC_WARN("`%d' invalid for parameter `dev_out_nak'\n", val);
  27927. + DWC_WARN("dev_out_nak must be 0 or 1\n");
  27928. + return -DWC_E_INVALID;
  27929. + }
  27930. +
  27931. + if ((val == 1) && ((core_if->snpsid < OTG_CORE_REV_2_93a) ||
  27932. + !(core_if->core_params->dma_desc_enable))) {
  27933. + valid = 0;
  27934. + }
  27935. + if (valid == 0) {
  27936. + if (dwc_otg_param_initialized(core_if->core_params->dev_out_nak)) {
  27937. + DWC_ERROR("%d invalid for parameter dev_out_nak."
  27938. + "Check HW configuration.\n", val);
  27939. + }
  27940. + retval = -DWC_E_INVALID;
  27941. + val = 0;
  27942. + }
  27943. + core_if->core_params->dev_out_nak = val;
  27944. + return retval;
  27945. +}
  27946. +
  27947. +int32_t dwc_otg_get_param_dev_out_nak(dwc_otg_core_if_t * core_if)
  27948. +{
  27949. + return core_if->core_params->dev_out_nak;
  27950. +}
  27951. +
  27952. +int dwc_otg_set_param_cont_on_bna(dwc_otg_core_if_t * core_if, int32_t val)
  27953. +{
  27954. + int retval = 0;
  27955. + int valid = 1;
  27956. +
  27957. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  27958. + DWC_WARN("`%d' invalid for parameter `cont_on_bna'\n", val);
  27959. + DWC_WARN("cont_on_bna must be 0 or 1\n");
  27960. + return -DWC_E_INVALID;
  27961. + }
  27962. +
  27963. + if ((val == 1) && ((core_if->snpsid < OTG_CORE_REV_2_94a) ||
  27964. + !(core_if->core_params->dma_desc_enable))) {
  27965. + valid = 0;
  27966. + }
  27967. + if (valid == 0) {
  27968. + if (dwc_otg_param_initialized(core_if->core_params->cont_on_bna)) {
  27969. + DWC_ERROR("%d invalid for parameter cont_on_bna."
  27970. + "Check HW configuration.\n", val);
  27971. + }
  27972. + retval = -DWC_E_INVALID;
  27973. + val = 0;
  27974. + }
  27975. + core_if->core_params->cont_on_bna = val;
  27976. + return retval;
  27977. +}
  27978. +
  27979. +int32_t dwc_otg_get_param_cont_on_bna(dwc_otg_core_if_t * core_if)
  27980. +{
  27981. + return core_if->core_params->cont_on_bna;
  27982. +}
  27983. +
  27984. +int dwc_otg_set_param_ahb_single(dwc_otg_core_if_t * core_if, int32_t val)
  27985. +{
  27986. + int retval = 0;
  27987. + int valid = 1;
  27988. +
  27989. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  27990. + DWC_WARN("`%d' invalid for parameter `ahb_single'\n", val);
  27991. + DWC_WARN("ahb_single must be 0 or 1\n");
  27992. + return -DWC_E_INVALID;
  27993. + }
  27994. +
  27995. + if ((val == 1) && (core_if->snpsid < OTG_CORE_REV_2_94a)) {
  27996. + valid = 0;
  27997. + }
  27998. + if (valid == 0) {
  27999. + if (dwc_otg_param_initialized(core_if->core_params->ahb_single)) {
  28000. + DWC_ERROR("%d invalid for parameter ahb_single."
  28001. + "Check HW configuration.\n", val);
  28002. + }
  28003. + retval = -DWC_E_INVALID;
  28004. + val = 0;
  28005. + }
  28006. + core_if->core_params->ahb_single = val;
  28007. + return retval;
  28008. +}
  28009. +
  28010. +int32_t dwc_otg_get_param_ahb_single(dwc_otg_core_if_t * core_if)
  28011. +{
  28012. + return core_if->core_params->ahb_single;
  28013. +}
  28014. +
  28015. +int dwc_otg_set_param_otg_ver(dwc_otg_core_if_t * core_if, int32_t val)
  28016. +{
  28017. + int retval = 0;
  28018. +
  28019. + if (DWC_OTG_PARAM_TEST(val, 0, 1)) {
  28020. + DWC_WARN("`%d' invalid for parameter `otg_ver'\n", val);
  28021. + DWC_WARN
  28022. + ("otg_ver must be 0(for OTG 1.3 support) or 1(for OTG 2.0 support)\n");
  28023. + return -DWC_E_INVALID;
  28024. + }
  28025. +
  28026. + core_if->core_params->otg_ver = val;
  28027. + return retval;
  28028. +}
  28029. +
  28030. +int32_t dwc_otg_get_param_otg_ver(dwc_otg_core_if_t * core_if)
  28031. +{
  28032. + return core_if->core_params->otg_ver;
  28033. +}
  28034. +
  28035. +uint32_t dwc_otg_get_hnpstatus(dwc_otg_core_if_t * core_if)
  28036. +{
  28037. + gotgctl_data_t otgctl;
  28038. + otgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  28039. + return otgctl.b.hstnegscs;
  28040. +}
  28041. +
  28042. +uint32_t dwc_otg_get_srpstatus(dwc_otg_core_if_t * core_if)
  28043. +{
  28044. + gotgctl_data_t otgctl;
  28045. + otgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  28046. + return otgctl.b.sesreqscs;
  28047. +}
  28048. +
  28049. +void dwc_otg_set_hnpreq(dwc_otg_core_if_t * core_if, uint32_t val)
  28050. +{
  28051. + if(core_if->otg_ver == 0) {
  28052. + gotgctl_data_t otgctl;
  28053. + otgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  28054. + otgctl.b.hnpreq = val;
  28055. + DWC_WRITE_REG32(&core_if->core_global_regs->gotgctl, otgctl.d32);
  28056. + } else {
  28057. + core_if->otg_sts = val;
  28058. + }
  28059. +}
  28060. +
  28061. +uint32_t dwc_otg_get_gsnpsid(dwc_otg_core_if_t * core_if)
  28062. +{
  28063. + return core_if->snpsid;
  28064. +}
  28065. +
  28066. +uint32_t dwc_otg_get_mode(dwc_otg_core_if_t * core_if)
  28067. +{
  28068. + gintsts_data_t gintsts;
  28069. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  28070. + return gintsts.b.curmode;
  28071. +}
  28072. +
  28073. +uint32_t dwc_otg_get_hnpcapable(dwc_otg_core_if_t * core_if)
  28074. +{
  28075. + gusbcfg_data_t usbcfg;
  28076. + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  28077. + return usbcfg.b.hnpcap;
  28078. +}
  28079. +
  28080. +void dwc_otg_set_hnpcapable(dwc_otg_core_if_t * core_if, uint32_t val)
  28081. +{
  28082. + gusbcfg_data_t usbcfg;
  28083. + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  28084. + usbcfg.b.hnpcap = val;
  28085. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, usbcfg.d32);
  28086. +}
  28087. +
  28088. +uint32_t dwc_otg_get_srpcapable(dwc_otg_core_if_t * core_if)
  28089. +{
  28090. + gusbcfg_data_t usbcfg;
  28091. + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  28092. + return usbcfg.b.srpcap;
  28093. +}
  28094. +
  28095. +void dwc_otg_set_srpcapable(dwc_otg_core_if_t * core_if, uint32_t val)
  28096. +{
  28097. + gusbcfg_data_t usbcfg;
  28098. + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  28099. + usbcfg.b.srpcap = val;
  28100. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, usbcfg.d32);
  28101. +}
  28102. +
  28103. +uint32_t dwc_otg_get_devspeed(dwc_otg_core_if_t * core_if)
  28104. +{
  28105. + dcfg_data_t dcfg;
  28106. + /* originally: dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg); */
  28107. +
  28108. + dcfg.d32 = -1; //GRAYG
  28109. + DWC_DEBUGPL(DBG_CILV, "%s - core_if(%p)\n", __func__, core_if);
  28110. + if (NULL == core_if)
  28111. + DWC_ERROR("reg request with NULL core_if\n");
  28112. + DWC_DEBUGPL(DBG_CILV, "%s - core_if(%p)->dev_if(%p)\n", __func__,
  28113. + core_if, core_if->dev_if);
  28114. + if (NULL == core_if->dev_if)
  28115. + DWC_ERROR("reg request with NULL dev_if\n");
  28116. + DWC_DEBUGPL(DBG_CILV, "%s - core_if(%p)->dev_if(%p)->"
  28117. + "dev_global_regs(%p)\n", __func__,
  28118. + core_if, core_if->dev_if,
  28119. + core_if->dev_if->dev_global_regs);
  28120. + if (NULL == core_if->dev_if->dev_global_regs)
  28121. + DWC_ERROR("reg request with NULL dev_global_regs\n");
  28122. + else {
  28123. + DWC_DEBUGPL(DBG_CILV, "%s - &core_if(%p)->dev_if(%p)->"
  28124. + "dev_global_regs(%p)->dcfg = %p\n", __func__,
  28125. + core_if, core_if->dev_if,
  28126. + core_if->dev_if->dev_global_regs,
  28127. + &core_if->dev_if->dev_global_regs->dcfg);
  28128. + dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  28129. + }
  28130. + return dcfg.b.devspd;
  28131. +}
  28132. +
  28133. +void dwc_otg_set_devspeed(dwc_otg_core_if_t * core_if, uint32_t val)
  28134. +{
  28135. + dcfg_data_t dcfg;
  28136. + dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  28137. + dcfg.b.devspd = val;
  28138. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg, dcfg.d32);
  28139. +}
  28140. +
  28141. +uint32_t dwc_otg_get_busconnected(dwc_otg_core_if_t * core_if)
  28142. +{
  28143. + hprt0_data_t hprt0;
  28144. + hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
  28145. + return hprt0.b.prtconnsts;
  28146. +}
  28147. +
  28148. +uint32_t dwc_otg_get_enumspeed(dwc_otg_core_if_t * core_if)
  28149. +{
  28150. + dsts_data_t dsts;
  28151. + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  28152. + return dsts.b.enumspd;
  28153. +}
  28154. +
  28155. +uint32_t dwc_otg_get_prtpower(dwc_otg_core_if_t * core_if)
  28156. +{
  28157. + hprt0_data_t hprt0;
  28158. + hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
  28159. + return hprt0.b.prtpwr;
  28160. +
  28161. +}
  28162. +
  28163. +uint32_t dwc_otg_get_core_state(dwc_otg_core_if_t * core_if)
  28164. +{
  28165. + return core_if->hibernation_suspend;
  28166. +}
  28167. +
  28168. +void dwc_otg_set_prtpower(dwc_otg_core_if_t * core_if, uint32_t val)
  28169. +{
  28170. + hprt0_data_t hprt0;
  28171. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  28172. + hprt0.b.prtpwr = val;
  28173. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  28174. +}
  28175. +
  28176. +uint32_t dwc_otg_get_prtsuspend(dwc_otg_core_if_t * core_if)
  28177. +{
  28178. + hprt0_data_t hprt0;
  28179. + hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
  28180. + return hprt0.b.prtsusp;
  28181. +
  28182. +}
  28183. +
  28184. +void dwc_otg_set_prtsuspend(dwc_otg_core_if_t * core_if, uint32_t val)
  28185. +{
  28186. + hprt0_data_t hprt0;
  28187. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  28188. + hprt0.b.prtsusp = val;
  28189. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  28190. +}
  28191. +
  28192. +uint32_t dwc_otg_get_fr_interval(dwc_otg_core_if_t * core_if)
  28193. +{
  28194. + hfir_data_t hfir;
  28195. + hfir.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->hfir);
  28196. + return hfir.b.frint;
  28197. +
  28198. +}
  28199. +
  28200. +void dwc_otg_set_fr_interval(dwc_otg_core_if_t * core_if, uint32_t val)
  28201. +{
  28202. + hfir_data_t hfir;
  28203. + uint32_t fram_int;
  28204. + fram_int = calc_frame_interval(core_if);
  28205. + hfir.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->hfir);
  28206. + if (!core_if->core_params->reload_ctl) {
  28207. + DWC_WARN("\nCannot reload HFIR register.HFIR.HFIRRldCtrl bit is"
  28208. + "not set to 1.\nShould load driver with reload_ctl=1"
  28209. + " module parameter\n");
  28210. + return;
  28211. + }
  28212. + switch (fram_int) {
  28213. + case 3750:
  28214. + if ((val < 3350) || (val > 4150)) {
  28215. + DWC_WARN("HFIR interval for HS core and 30 MHz"
  28216. + "clock freq should be from 3350 to 4150\n");
  28217. + return;
  28218. + }
  28219. + break;
  28220. + case 30000:
  28221. + if ((val < 26820) || (val > 33180)) {
  28222. + DWC_WARN("HFIR interval for FS/LS core and 30 MHz"
  28223. + "clock freq should be from 26820 to 33180\n");
  28224. + return;
  28225. + }
  28226. + break;
  28227. + case 6000:
  28228. + if ((val < 5360) || (val > 6640)) {
  28229. + DWC_WARN("HFIR interval for HS core and 48 MHz"
  28230. + "clock freq should be from 5360 to 6640\n");
  28231. + return;
  28232. + }
  28233. + break;
  28234. + case 48000:
  28235. + if ((val < 42912) || (val > 53088)) {
  28236. + DWC_WARN("HFIR interval for FS/LS core and 48 MHz"
  28237. + "clock freq should be from 42912 to 53088\n");
  28238. + return;
  28239. + }
  28240. + break;
  28241. + case 7500:
  28242. + if ((val < 6700) || (val > 8300)) {
  28243. + DWC_WARN("HFIR interval for HS core and 60 MHz"
  28244. + "clock freq should be from 6700 to 8300\n");
  28245. + return;
  28246. + }
  28247. + break;
  28248. + case 60000:
  28249. + if ((val < 53640) || (val > 65536)) {
  28250. + DWC_WARN("HFIR interval for FS/LS core and 60 MHz"
  28251. + "clock freq should be from 53640 to 65536\n");
  28252. + return;
  28253. + }
  28254. + break;
  28255. + default:
  28256. + DWC_WARN("Unknown frame interval\n");
  28257. + return;
  28258. + break;
  28259. +
  28260. + }
  28261. + hfir.b.frint = val;
  28262. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hfir, hfir.d32);
  28263. +}
  28264. +
  28265. +uint32_t dwc_otg_get_mode_ch_tim(dwc_otg_core_if_t * core_if)
  28266. +{
  28267. + hcfg_data_t hcfg;
  28268. + hcfg.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->hcfg);
  28269. + return hcfg.b.modechtimen;
  28270. +
  28271. +}
  28272. +
  28273. +void dwc_otg_set_mode_ch_tim(dwc_otg_core_if_t * core_if, uint32_t val)
  28274. +{
  28275. + hcfg_data_t hcfg;
  28276. + hcfg.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->hcfg);
  28277. + hcfg.b.modechtimen = val;
  28278. + DWC_WRITE_REG32(&core_if->host_if->host_global_regs->hcfg, hcfg.d32);
  28279. +}
  28280. +
  28281. +void dwc_otg_set_prtresume(dwc_otg_core_if_t * core_if, uint32_t val)
  28282. +{
  28283. + hprt0_data_t hprt0;
  28284. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  28285. + hprt0.b.prtres = val;
  28286. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  28287. +}
  28288. +
  28289. +uint32_t dwc_otg_get_remotewakesig(dwc_otg_core_if_t * core_if)
  28290. +{
  28291. + dctl_data_t dctl;
  28292. + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dctl);
  28293. + return dctl.b.rmtwkupsig;
  28294. +}
  28295. +
  28296. +uint32_t dwc_otg_get_lpm_portsleepstatus(dwc_otg_core_if_t * core_if)
  28297. +{
  28298. + glpmcfg_data_t lpmcfg;
  28299. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  28300. +
  28301. + DWC_ASSERT(!
  28302. + ((core_if->lx_state == DWC_OTG_L1) ^ lpmcfg.b.prt_sleep_sts),
  28303. + "lx_state = %d, lmpcfg.prt_sleep_sts = %d\n",
  28304. + core_if->lx_state, lpmcfg.b.prt_sleep_sts);
  28305. +
  28306. + return lpmcfg.b.prt_sleep_sts;
  28307. +}
  28308. +
  28309. +uint32_t dwc_otg_get_lpm_remotewakeenabled(dwc_otg_core_if_t * core_if)
  28310. +{
  28311. + glpmcfg_data_t lpmcfg;
  28312. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  28313. + return lpmcfg.b.rem_wkup_en;
  28314. +}
  28315. +
  28316. +uint32_t dwc_otg_get_lpmresponse(dwc_otg_core_if_t * core_if)
  28317. +{
  28318. + glpmcfg_data_t lpmcfg;
  28319. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  28320. + return lpmcfg.b.appl_resp;
  28321. +}
  28322. +
  28323. +void dwc_otg_set_lpmresponse(dwc_otg_core_if_t * core_if, uint32_t val)
  28324. +{
  28325. + glpmcfg_data_t lpmcfg;
  28326. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  28327. + lpmcfg.b.appl_resp = val;
  28328. + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg, lpmcfg.d32);
  28329. +}
  28330. +
  28331. +uint32_t dwc_otg_get_hsic_connect(dwc_otg_core_if_t * core_if)
  28332. +{
  28333. + glpmcfg_data_t lpmcfg;
  28334. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  28335. + return lpmcfg.b.hsic_connect;
  28336. +}
  28337. +
  28338. +void dwc_otg_set_hsic_connect(dwc_otg_core_if_t * core_if, uint32_t val)
  28339. +{
  28340. + glpmcfg_data_t lpmcfg;
  28341. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  28342. + lpmcfg.b.hsic_connect = val;
  28343. + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg, lpmcfg.d32);
  28344. +}
  28345. +
  28346. +uint32_t dwc_otg_get_inv_sel_hsic(dwc_otg_core_if_t * core_if)
  28347. +{
  28348. + glpmcfg_data_t lpmcfg;
  28349. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  28350. + return lpmcfg.b.inv_sel_hsic;
  28351. +
  28352. +}
  28353. +
  28354. +void dwc_otg_set_inv_sel_hsic(dwc_otg_core_if_t * core_if, uint32_t val)
  28355. +{
  28356. + glpmcfg_data_t lpmcfg;
  28357. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  28358. + lpmcfg.b.inv_sel_hsic = val;
  28359. + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg, lpmcfg.d32);
  28360. +}
  28361. +
  28362. +uint32_t dwc_otg_get_gotgctl(dwc_otg_core_if_t * core_if)
  28363. +{
  28364. + return DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  28365. +}
  28366. +
  28367. +void dwc_otg_set_gotgctl(dwc_otg_core_if_t * core_if, uint32_t val)
  28368. +{
  28369. + DWC_WRITE_REG32(&core_if->core_global_regs->gotgctl, val);
  28370. +}
  28371. +
  28372. +uint32_t dwc_otg_get_gusbcfg(dwc_otg_core_if_t * core_if)
  28373. +{
  28374. + return DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  28375. +}
  28376. +
  28377. +void dwc_otg_set_gusbcfg(dwc_otg_core_if_t * core_if, uint32_t val)
  28378. +{
  28379. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, val);
  28380. +}
  28381. +
  28382. +uint32_t dwc_otg_get_grxfsiz(dwc_otg_core_if_t * core_if)
  28383. +{
  28384. + return DWC_READ_REG32(&core_if->core_global_regs->grxfsiz);
  28385. +}
  28386. +
  28387. +void dwc_otg_set_grxfsiz(dwc_otg_core_if_t * core_if, uint32_t val)
  28388. +{
  28389. + DWC_WRITE_REG32(&core_if->core_global_regs->grxfsiz, val);
  28390. +}
  28391. +
  28392. +uint32_t dwc_otg_get_gnptxfsiz(dwc_otg_core_if_t * core_if)
  28393. +{
  28394. + return DWC_READ_REG32(&core_if->core_global_regs->gnptxfsiz);
  28395. +}
  28396. +
  28397. +void dwc_otg_set_gnptxfsiz(dwc_otg_core_if_t * core_if, uint32_t val)
  28398. +{
  28399. + DWC_WRITE_REG32(&core_if->core_global_regs->gnptxfsiz, val);
  28400. +}
  28401. +
  28402. +uint32_t dwc_otg_get_gpvndctl(dwc_otg_core_if_t * core_if)
  28403. +{
  28404. + return DWC_READ_REG32(&core_if->core_global_regs->gpvndctl);
  28405. +}
  28406. +
  28407. +void dwc_otg_set_gpvndctl(dwc_otg_core_if_t * core_if, uint32_t val)
  28408. +{
  28409. + DWC_WRITE_REG32(&core_if->core_global_regs->gpvndctl, val);
  28410. +}
  28411. +
  28412. +uint32_t dwc_otg_get_ggpio(dwc_otg_core_if_t * core_if)
  28413. +{
  28414. + return DWC_READ_REG32(&core_if->core_global_regs->ggpio);
  28415. +}
  28416. +
  28417. +void dwc_otg_set_ggpio(dwc_otg_core_if_t * core_if, uint32_t val)
  28418. +{
  28419. + DWC_WRITE_REG32(&core_if->core_global_regs->ggpio, val);
  28420. +}
  28421. +
  28422. +uint32_t dwc_otg_get_hprt0(dwc_otg_core_if_t * core_if)
  28423. +{
  28424. + return DWC_READ_REG32(core_if->host_if->hprt0);
  28425. +
  28426. +}
  28427. +
  28428. +void dwc_otg_set_hprt0(dwc_otg_core_if_t * core_if, uint32_t val)
  28429. +{
  28430. + DWC_WRITE_REG32(core_if->host_if->hprt0, val);
  28431. +}
  28432. +
  28433. +uint32_t dwc_otg_get_guid(dwc_otg_core_if_t * core_if)
  28434. +{
  28435. + return DWC_READ_REG32(&core_if->core_global_regs->guid);
  28436. +}
  28437. +
  28438. +void dwc_otg_set_guid(dwc_otg_core_if_t * core_if, uint32_t val)
  28439. +{
  28440. + DWC_WRITE_REG32(&core_if->core_global_regs->guid, val);
  28441. +}
  28442. +
  28443. +uint32_t dwc_otg_get_hptxfsiz(dwc_otg_core_if_t * core_if)
  28444. +{
  28445. + return DWC_READ_REG32(&core_if->core_global_regs->hptxfsiz);
  28446. +}
  28447. +
  28448. +uint16_t dwc_otg_get_otg_version(dwc_otg_core_if_t * core_if)
  28449. +{
  28450. + return ((core_if->otg_ver == 1) ? (uint16_t)0x0200 : (uint16_t)0x0103);
  28451. +}
  28452. +
  28453. +/**
  28454. + * Start the SRP timer to detect when the SRP does not complete within
  28455. + * 6 seconds.
  28456. + *
  28457. + * @param core_if the pointer to core_if strucure.
  28458. + */
  28459. +void dwc_otg_pcd_start_srp_timer(dwc_otg_core_if_t * core_if)
  28460. +{
  28461. + core_if->srp_timer_started = 1;
  28462. + DWC_TIMER_SCHEDULE(core_if->srp_timer, 6000 /* 6 secs */ );
  28463. +}
  28464. +
  28465. +void dwc_otg_initiate_srp(dwc_otg_core_if_t * core_if)
  28466. +{
  28467. + uint32_t *addr = (uint32_t *) & (core_if->core_global_regs->gotgctl);
  28468. + gotgctl_data_t mem;
  28469. + gotgctl_data_t val;
  28470. +
  28471. + val.d32 = DWC_READ_REG32(addr);
  28472. + if (val.b.sesreq) {
  28473. + DWC_ERROR("Session Request Already active!\n");
  28474. + return;
  28475. + }
  28476. +
  28477. + DWC_INFO("Session Request Initated\n"); //NOTICE
  28478. + mem.d32 = DWC_READ_REG32(addr);
  28479. + mem.b.sesreq = 1;
  28480. + DWC_WRITE_REG32(addr, mem.d32);
  28481. +
  28482. + /* Start the SRP timer */
  28483. + dwc_otg_pcd_start_srp_timer(core_if);
  28484. + return;
  28485. +}
  28486. --- /dev/null
  28487. +++ b/drivers/usb/host/dwc_otg/dwc_otg_cil.h
  28488. @@ -0,0 +1,1464 @@
  28489. +/* ==========================================================================
  28490. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_cil.h $
  28491. + * $Revision: #123 $
  28492. + * $Date: 2012/08/10 $
  28493. + * $Change: 2047372 $
  28494. + *
  28495. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  28496. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  28497. + * otherwise expressly agreed to in writing between Synopsys and you.
  28498. + *
  28499. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  28500. + * any End User Software License Agreement or Agreement for Licensed Product
  28501. + * with Synopsys or any supplement thereto. You are permitted to use and
  28502. + * redistribute this Software in source and binary forms, with or without
  28503. + * modification, provided that redistributions of source code must retain this
  28504. + * notice. You may not view, use, disclose, copy or distribute this file or
  28505. + * any information contained herein except pursuant to this license grant from
  28506. + * Synopsys. If you do not agree with this notice, including the disclaimer
  28507. + * below, then you are not authorized to use the Software.
  28508. + *
  28509. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  28510. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28511. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28512. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  28513. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  28514. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  28515. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  28516. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  28517. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  28518. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  28519. + * DAMAGE.
  28520. + * ========================================================================== */
  28521. +
  28522. +#if !defined(__DWC_CIL_H__)
  28523. +#define __DWC_CIL_H__
  28524. +
  28525. +#include "dwc_list.h"
  28526. +#include "dwc_otg_dbg.h"
  28527. +#include "dwc_otg_regs.h"
  28528. +
  28529. +#include "dwc_otg_core_if.h"
  28530. +#include "dwc_otg_adp.h"
  28531. +
  28532. +/**
  28533. + * @file
  28534. + * This file contains the interface to the Core Interface Layer.
  28535. + */
  28536. +
  28537. +#ifdef DWC_UTE_CFI
  28538. +
  28539. +#define MAX_DMA_DESCS_PER_EP 256
  28540. +
  28541. +/**
  28542. + * Enumeration for the data buffer mode
  28543. + */
  28544. +typedef enum _data_buffer_mode {
  28545. + BM_STANDARD = 0, /* data buffer is in normal mode */
  28546. + BM_SG = 1, /* data buffer uses the scatter/gather mode */
  28547. + BM_CONCAT = 2, /* data buffer uses the concatenation mode */
  28548. + BM_CIRCULAR = 3, /* data buffer uses the circular DMA mode */
  28549. + BM_ALIGN = 4 /* data buffer is in buffer alignment mode */
  28550. +} data_buffer_mode_e;
  28551. +#endif //DWC_UTE_CFI
  28552. +
  28553. +/** Macros defined for DWC OTG HW Release version */
  28554. +
  28555. +#define OTG_CORE_REV_2_60a 0x4F54260A
  28556. +#define OTG_CORE_REV_2_71a 0x4F54271A
  28557. +#define OTG_CORE_REV_2_72a 0x4F54272A
  28558. +#define OTG_CORE_REV_2_80a 0x4F54280A
  28559. +#define OTG_CORE_REV_2_81a 0x4F54281A
  28560. +#define OTG_CORE_REV_2_90a 0x4F54290A
  28561. +#define OTG_CORE_REV_2_91a 0x4F54291A
  28562. +#define OTG_CORE_REV_2_92a 0x4F54292A
  28563. +#define OTG_CORE_REV_2_93a 0x4F54293A
  28564. +#define OTG_CORE_REV_2_94a 0x4F54294A
  28565. +#define OTG_CORE_REV_3_00a 0x4F54300A
  28566. +
  28567. +/**
  28568. + * Information for each ISOC packet.
  28569. + */
  28570. +typedef struct iso_pkt_info {
  28571. + uint32_t offset;
  28572. + uint32_t length;
  28573. + int32_t status;
  28574. +} iso_pkt_info_t;
  28575. +
  28576. +/**
  28577. + * The <code>dwc_ep</code> structure represents the state of a single
  28578. + * endpoint when acting in device mode. It contains the data items
  28579. + * needed for an endpoint to be activated and transfer packets.
  28580. + */
  28581. +typedef struct dwc_ep {
  28582. + /** EP number used for register address lookup */
  28583. + uint8_t num;
  28584. + /** EP direction 0 = OUT */
  28585. + unsigned is_in:1;
  28586. + /** EP active. */
  28587. + unsigned active:1;
  28588. +
  28589. + /**
  28590. + * Periodic Tx FIFO # for IN EPs For INTR EP set to 0 to use non-periodic
  28591. + * Tx FIFO. If dedicated Tx FIFOs are enabled Tx FIFO # FOR IN EPs*/
  28592. + unsigned tx_fifo_num:4;
  28593. + /** EP type: 0 - Control, 1 - ISOC, 2 - BULK, 3 - INTR */
  28594. + unsigned type:2;
  28595. +#define DWC_OTG_EP_TYPE_CONTROL 0
  28596. +#define DWC_OTG_EP_TYPE_ISOC 1
  28597. +#define DWC_OTG_EP_TYPE_BULK 2
  28598. +#define DWC_OTG_EP_TYPE_INTR 3
  28599. +
  28600. + /** DATA start PID for INTR and BULK EP */
  28601. + unsigned data_pid_start:1;
  28602. + /** Frame (even/odd) for ISOC EP */
  28603. + unsigned even_odd_frame:1;
  28604. + /** Max Packet bytes */
  28605. + unsigned maxpacket:11;
  28606. +
  28607. + /** Max Transfer size */
  28608. + uint32_t maxxfer;
  28609. +
  28610. + /** @name Transfer state */
  28611. + /** @{ */
  28612. +
  28613. + /**
  28614. + * Pointer to the beginning of the transfer buffer -- do not modify
  28615. + * during transfer.
  28616. + */
  28617. +
  28618. + dwc_dma_t dma_addr;
  28619. +
  28620. + dwc_dma_t dma_desc_addr;
  28621. + dwc_otg_dev_dma_desc_t *desc_addr;
  28622. +
  28623. + uint8_t *start_xfer_buff;
  28624. + /** pointer to the transfer buffer */
  28625. + uint8_t *xfer_buff;
  28626. + /** Number of bytes to transfer */
  28627. + unsigned xfer_len:19;
  28628. + /** Number of bytes transferred. */
  28629. + unsigned xfer_count:19;
  28630. + /** Sent ZLP */
  28631. + unsigned sent_zlp:1;
  28632. + /** Total len for control transfer */
  28633. + unsigned total_len:19;
  28634. +
  28635. + /** stall clear flag */
  28636. + unsigned stall_clear_flag:1;
  28637. +
  28638. + /** SETUP pkt cnt rollover flag for EP0 out*/
  28639. + unsigned stp_rollover;
  28640. +
  28641. +#ifdef DWC_UTE_CFI
  28642. + /* The buffer mode */
  28643. + data_buffer_mode_e buff_mode;
  28644. +
  28645. + /* The chain of DMA descriptors.
  28646. + * MAX_DMA_DESCS_PER_EP will be allocated for each active EP.
  28647. + */
  28648. + dwc_otg_dma_desc_t *descs;
  28649. +
  28650. + /* The DMA address of the descriptors chain start */
  28651. + dma_addr_t descs_dma_addr;
  28652. + /** This variable stores the length of the last enqueued request */
  28653. + uint32_t cfi_req_len;
  28654. +#endif //DWC_UTE_CFI
  28655. +
  28656. +/** Max DMA Descriptor count for any EP */
  28657. +#define MAX_DMA_DESC_CNT 256
  28658. + /** Allocated DMA Desc count */
  28659. + uint32_t desc_cnt;
  28660. +
  28661. + /** bInterval */
  28662. + uint32_t bInterval;
  28663. + /** Next frame num to setup next ISOC transfer */
  28664. + uint32_t frame_num;
  28665. + /** Indicates SOF number overrun in DSTS */
  28666. + uint8_t frm_overrun;
  28667. +
  28668. +#ifdef DWC_UTE_PER_IO
  28669. + /** Next frame num for which will be setup DMA Desc */
  28670. + uint32_t xiso_frame_num;
  28671. + /** bInterval */
  28672. + uint32_t xiso_bInterval;
  28673. + /** Count of currently active transfers - shall be either 0 or 1 */
  28674. + int xiso_active_xfers;
  28675. + int xiso_queued_xfers;
  28676. +#endif
  28677. +#ifdef DWC_EN_ISOC
  28678. + /**
  28679. + * Variables specific for ISOC EPs
  28680. + *
  28681. + */
  28682. + /** DMA addresses of ISOC buffers */
  28683. + dwc_dma_t dma_addr0;
  28684. + dwc_dma_t dma_addr1;
  28685. +
  28686. + dwc_dma_t iso_dma_desc_addr;
  28687. + dwc_otg_dev_dma_desc_t *iso_desc_addr;
  28688. +
  28689. + /** pointer to the transfer buffers */
  28690. + uint8_t *xfer_buff0;
  28691. + uint8_t *xfer_buff1;
  28692. +
  28693. + /** number of ISOC Buffer is processing */
  28694. + uint32_t proc_buf_num;
  28695. + /** Interval of ISOC Buffer processing */
  28696. + uint32_t buf_proc_intrvl;
  28697. + /** Data size for regular frame */
  28698. + uint32_t data_per_frame;
  28699. +
  28700. + /* todo - pattern data support is to be implemented in the future */
  28701. + /** Data size for pattern frame */
  28702. + uint32_t data_pattern_frame;
  28703. + /** Frame number of pattern data */
  28704. + uint32_t sync_frame;
  28705. +
  28706. + /** bInterval */
  28707. + uint32_t bInterval;
  28708. + /** ISO Packet number per frame */
  28709. + uint32_t pkt_per_frm;
  28710. + /** Next frame num for which will be setup DMA Desc */
  28711. + uint32_t next_frame;
  28712. + /** Number of packets per buffer processing */
  28713. + uint32_t pkt_cnt;
  28714. + /** Info for all isoc packets */
  28715. + iso_pkt_info_t *pkt_info;
  28716. + /** current pkt number */
  28717. + uint32_t cur_pkt;
  28718. + /** current pkt number */
  28719. + uint8_t *cur_pkt_addr;
  28720. + /** current pkt number */
  28721. + uint32_t cur_pkt_dma_addr;
  28722. +#endif /* DWC_EN_ISOC */
  28723. +
  28724. +/** @} */
  28725. +} dwc_ep_t;
  28726. +
  28727. +/*
  28728. + * Reasons for halting a host channel.
  28729. + */
  28730. +typedef enum dwc_otg_halt_status {
  28731. + DWC_OTG_HC_XFER_NO_HALT_STATUS,
  28732. + DWC_OTG_HC_XFER_COMPLETE,
  28733. + DWC_OTG_HC_XFER_URB_COMPLETE,
  28734. + DWC_OTG_HC_XFER_ACK,
  28735. + DWC_OTG_HC_XFER_NAK,
  28736. + DWC_OTG_HC_XFER_NYET,
  28737. + DWC_OTG_HC_XFER_STALL,
  28738. + DWC_OTG_HC_XFER_XACT_ERR,
  28739. + DWC_OTG_HC_XFER_FRAME_OVERRUN,
  28740. + DWC_OTG_HC_XFER_BABBLE_ERR,
  28741. + DWC_OTG_HC_XFER_DATA_TOGGLE_ERR,
  28742. + DWC_OTG_HC_XFER_AHB_ERR,
  28743. + DWC_OTG_HC_XFER_PERIODIC_INCOMPLETE,
  28744. + DWC_OTG_HC_XFER_URB_DEQUEUE
  28745. +} dwc_otg_halt_status_e;
  28746. +
  28747. +/**
  28748. + * Host channel descriptor. This structure represents the state of a single
  28749. + * host channel when acting in host mode. It contains the data items needed to
  28750. + * transfer packets to an endpoint via a host channel.
  28751. + */
  28752. +typedef struct dwc_hc {
  28753. + /** Host channel number used for register address lookup */
  28754. + uint8_t hc_num;
  28755. +
  28756. + /** Device to access */
  28757. + unsigned dev_addr:7;
  28758. +
  28759. + /** EP to access */
  28760. + unsigned ep_num:4;
  28761. +
  28762. + /** EP direction. 0: OUT, 1: IN */
  28763. + unsigned ep_is_in:1;
  28764. +
  28765. + /**
  28766. + * EP speed.
  28767. + * One of the following values:
  28768. + * - DWC_OTG_EP_SPEED_LOW
  28769. + * - DWC_OTG_EP_SPEED_FULL
  28770. + * - DWC_OTG_EP_SPEED_HIGH
  28771. + */
  28772. + unsigned speed:2;
  28773. +#define DWC_OTG_EP_SPEED_LOW 0
  28774. +#define DWC_OTG_EP_SPEED_FULL 1
  28775. +#define DWC_OTG_EP_SPEED_HIGH 2
  28776. +
  28777. + /**
  28778. + * Endpoint type.
  28779. + * One of the following values:
  28780. + * - DWC_OTG_EP_TYPE_CONTROL: 0
  28781. + * - DWC_OTG_EP_TYPE_ISOC: 1
  28782. + * - DWC_OTG_EP_TYPE_BULK: 2
  28783. + * - DWC_OTG_EP_TYPE_INTR: 3
  28784. + */
  28785. + unsigned ep_type:2;
  28786. +
  28787. + /** Max packet size in bytes */
  28788. + unsigned max_packet:11;
  28789. +
  28790. + /**
  28791. + * PID for initial transaction.
  28792. + * 0: DATA0,<br>
  28793. + * 1: DATA2,<br>
  28794. + * 2: DATA1,<br>
  28795. + * 3: MDATA (non-Control EP),
  28796. + * SETUP (Control EP)
  28797. + */
  28798. + unsigned data_pid_start:2;
  28799. +#define DWC_OTG_HC_PID_DATA0 0
  28800. +#define DWC_OTG_HC_PID_DATA2 1
  28801. +#define DWC_OTG_HC_PID_DATA1 2
  28802. +#define DWC_OTG_HC_PID_MDATA 3
  28803. +#define DWC_OTG_HC_PID_SETUP 3
  28804. +
  28805. + /** Number of periodic transactions per (micro)frame */
  28806. + unsigned multi_count:2;
  28807. +
  28808. + /** @name Transfer State */
  28809. + /** @{ */
  28810. +
  28811. + /** Pointer to the current transfer buffer position. */
  28812. + uint8_t *xfer_buff;
  28813. + /**
  28814. + * In Buffer DMA mode this buffer will be used
  28815. + * if xfer_buff is not DWORD aligned.
  28816. + */
  28817. + dwc_dma_t align_buff;
  28818. + /** Total number of bytes to transfer. */
  28819. + uint32_t xfer_len;
  28820. + /** Number of bytes transferred so far. */
  28821. + uint32_t xfer_count;
  28822. + /** Packet count at start of transfer.*/
  28823. + uint16_t start_pkt_count;
  28824. +
  28825. + /**
  28826. + * Flag to indicate whether the transfer has been started. Set to 1 if
  28827. + * it has been started, 0 otherwise.
  28828. + */
  28829. + uint8_t xfer_started;
  28830. +
  28831. + /**
  28832. + * Set to 1 to indicate that a PING request should be issued on this
  28833. + * channel. If 0, process normally.
  28834. + */
  28835. + uint8_t do_ping;
  28836. +
  28837. + /**
  28838. + * Set to 1 to indicate that the error count for this transaction is
  28839. + * non-zero. Set to 0 if the error count is 0.
  28840. + */
  28841. + uint8_t error_state;
  28842. +
  28843. + /**
  28844. + * Set to 1 to indicate that this channel should be halted the next
  28845. + * time a request is queued for the channel. This is necessary in
  28846. + * slave mode if no request queue space is available when an attempt
  28847. + * is made to halt the channel.
  28848. + */
  28849. + uint8_t halt_on_queue;
  28850. +
  28851. + /**
  28852. + * Set to 1 if the host channel has been halted, but the core is not
  28853. + * finished flushing queued requests. Otherwise 0.
  28854. + */
  28855. + uint8_t halt_pending;
  28856. +
  28857. + /**
  28858. + * Reason for halting the host channel.
  28859. + */
  28860. + dwc_otg_halt_status_e halt_status;
  28861. +
  28862. + /*
  28863. + * Split settings for the host channel
  28864. + */
  28865. + uint8_t do_split; /**< Enable split for the channel */
  28866. + uint8_t complete_split; /**< Enable complete split */
  28867. + uint8_t hub_addr; /**< Address of high speed hub */
  28868. +
  28869. + uint8_t port_addr; /**< Port of the low/full speed device */
  28870. + /** Split transaction position
  28871. + * One of the following values:
  28872. + * - DWC_HCSPLIT_XACTPOS_MID
  28873. + * - DWC_HCSPLIT_XACTPOS_BEGIN
  28874. + * - DWC_HCSPLIT_XACTPOS_END
  28875. + * - DWC_HCSPLIT_XACTPOS_ALL */
  28876. + uint8_t xact_pos;
  28877. +
  28878. + /** Set when the host channel does a short read. */
  28879. + uint8_t short_read;
  28880. +
  28881. + /**
  28882. + * Number of requests issued for this channel since it was assigned to
  28883. + * the current transfer (not counting PINGs).
  28884. + */
  28885. + uint8_t requests;
  28886. +
  28887. + /**
  28888. + * Queue Head for the transfer being processed by this channel.
  28889. + */
  28890. + struct dwc_otg_qh *qh;
  28891. +
  28892. + /** @} */
  28893. +
  28894. + /** Entry in list of host channels. */
  28895. + DWC_CIRCLEQ_ENTRY(dwc_hc) hc_list_entry;
  28896. +
  28897. + /** @name Descriptor DMA support */
  28898. + /** @{ */
  28899. +
  28900. + /** Number of Transfer Descriptors */
  28901. + uint16_t ntd;
  28902. +
  28903. + /** Descriptor List DMA address */
  28904. + dwc_dma_t desc_list_addr;
  28905. +
  28906. + /** Scheduling micro-frame bitmap. */
  28907. + uint8_t schinfo;
  28908. +
  28909. + /** @} */
  28910. +} dwc_hc_t;
  28911. +
  28912. +/**
  28913. + * The following parameters may be specified when starting the module. These
  28914. + * parameters define how the DWC_otg controller should be configured.
  28915. + */
  28916. +typedef struct dwc_otg_core_params {
  28917. + int32_t opt;
  28918. +
  28919. + /**
  28920. + * Specifies the OTG capabilities. The driver will automatically
  28921. + * detect the value for this parameter if none is specified.
  28922. + * 0 - HNP and SRP capable (default)
  28923. + * 1 - SRP Only capable
  28924. + * 2 - No HNP/SRP capable
  28925. + */
  28926. + int32_t otg_cap;
  28927. +
  28928. + /**
  28929. + * Specifies whether to use slave or DMA mode for accessing the data
  28930. + * FIFOs. The driver will automatically detect the value for this
  28931. + * parameter if none is specified.
  28932. + * 0 - Slave
  28933. + * 1 - DMA (default, if available)
  28934. + */
  28935. + int32_t dma_enable;
  28936. +
  28937. + /**
  28938. + * When DMA mode is enabled specifies whether to use address DMA or DMA
  28939. + * Descriptor mode for accessing the data FIFOs in device mode. The driver
  28940. + * will automatically detect the value for this if none is specified.
  28941. + * 0 - address DMA
  28942. + * 1 - DMA Descriptor(default, if available)
  28943. + */
  28944. + int32_t dma_desc_enable;
  28945. + /** The DMA Burst size (applicable only for External DMA
  28946. + * Mode). 1, 4, 8 16, 32, 64, 128, 256 (default 32)
  28947. + */
  28948. + int32_t dma_burst_size; /* Translate this to GAHBCFG values */
  28949. +
  28950. + /**
  28951. + * Specifies the maximum speed of operation in host and device mode.
  28952. + * The actual speed depends on the speed of the attached device and
  28953. + * the value of phy_type. The actual speed depends on the speed of the
  28954. + * attached device.
  28955. + * 0 - High Speed (default)
  28956. + * 1 - Full Speed
  28957. + */
  28958. + int32_t speed;
  28959. + /** Specifies whether low power mode is supported when attached
  28960. + * to a Full Speed or Low Speed device in host mode.
  28961. + * 0 - Don't support low power mode (default)
  28962. + * 1 - Support low power mode
  28963. + */
  28964. + int32_t host_support_fs_ls_low_power;
  28965. +
  28966. + /** Specifies the PHY clock rate in low power mode when connected to a
  28967. + * Low Speed device in host mode. This parameter is applicable only if
  28968. + * HOST_SUPPORT_FS_LS_LOW_POWER is enabled. If PHY_TYPE is set to FS
  28969. + * then defaults to 6 MHZ otherwise 48 MHZ.
  28970. + *
  28971. + * 0 - 48 MHz
  28972. + * 1 - 6 MHz
  28973. + */
  28974. + int32_t host_ls_low_power_phy_clk;
  28975. +
  28976. + /**
  28977. + * 0 - Use cC FIFO size parameters
  28978. + * 1 - Allow dynamic FIFO sizing (default)
  28979. + */
  28980. + int32_t enable_dynamic_fifo;
  28981. +
  28982. + /** Total number of 4-byte words in the data FIFO memory. This
  28983. + * memory includes the Rx FIFO, non-periodic Tx FIFO, and periodic
  28984. + * Tx FIFOs.
  28985. + * 32 to 32768 (default 8192)
  28986. + * Note: The total FIFO memory depth in the FPGA configuration is 8192.
  28987. + */
  28988. + int32_t data_fifo_size;
  28989. +
  28990. + /** Number of 4-byte words in the Rx FIFO in device mode when dynamic
  28991. + * FIFO sizing is enabled.
  28992. + * 16 to 32768 (default 1064)
  28993. + */
  28994. + int32_t dev_rx_fifo_size;
  28995. +
  28996. + /** Number of 4-byte words in the non-periodic Tx FIFO in device mode
  28997. + * when dynamic FIFO sizing is enabled.
  28998. + * 16 to 32768 (default 1024)
  28999. + */
  29000. + int32_t dev_nperio_tx_fifo_size;
  29001. +
  29002. + /** Number of 4-byte words in each of the periodic Tx FIFOs in device
  29003. + * mode when dynamic FIFO sizing is enabled.
  29004. + * 4 to 768 (default 256)
  29005. + */
  29006. + uint32_t dev_perio_tx_fifo_size[MAX_PERIO_FIFOS];
  29007. +
  29008. + /** Number of 4-byte words in the Rx FIFO in host mode when dynamic
  29009. + * FIFO sizing is enabled.
  29010. + * 16 to 32768 (default 1024)
  29011. + */
  29012. + int32_t host_rx_fifo_size;
  29013. +
  29014. + /** Number of 4-byte words in the non-periodic Tx FIFO in host mode
  29015. + * when Dynamic FIFO sizing is enabled in the core.
  29016. + * 16 to 32768 (default 1024)
  29017. + */
  29018. + int32_t host_nperio_tx_fifo_size;
  29019. +
  29020. + /** Number of 4-byte words in the host periodic Tx FIFO when dynamic
  29021. + * FIFO sizing is enabled.
  29022. + * 16 to 32768 (default 1024)
  29023. + */
  29024. + int32_t host_perio_tx_fifo_size;
  29025. +
  29026. + /** The maximum transfer size supported in bytes.
  29027. + * 2047 to 65,535 (default 65,535)
  29028. + */
  29029. + int32_t max_transfer_size;
  29030. +
  29031. + /** The maximum number of packets in a transfer.
  29032. + * 15 to 511 (default 511)
  29033. + */
  29034. + int32_t max_packet_count;
  29035. +
  29036. + /** The number of host channel registers to use.
  29037. + * 1 to 16 (default 12)
  29038. + * Note: The FPGA configuration supports a maximum of 12 host channels.
  29039. + */
  29040. + int32_t host_channels;
  29041. +
  29042. + /** The number of endpoints in addition to EP0 available for device
  29043. + * mode operations.
  29044. + * 1 to 15 (default 6 IN and OUT)
  29045. + * Note: The FPGA configuration supports a maximum of 6 IN and OUT
  29046. + * endpoints in addition to EP0.
  29047. + */
  29048. + int32_t dev_endpoints;
  29049. +
  29050. + /**
  29051. + * Specifies the type of PHY interface to use. By default, the driver
  29052. + * will automatically detect the phy_type.
  29053. + *
  29054. + * 0 - Full Speed PHY
  29055. + * 1 - UTMI+ (default)
  29056. + * 2 - ULPI
  29057. + */
  29058. + int32_t phy_type;
  29059. +
  29060. + /**
  29061. + * Specifies the UTMI+ Data Width. This parameter is
  29062. + * applicable for a PHY_TYPE of UTMI+ or ULPI. (For a ULPI
  29063. + * PHY_TYPE, this parameter indicates the data width between
  29064. + * the MAC and the ULPI Wrapper.) Also, this parameter is
  29065. + * applicable only if the OTG_HSPHY_WIDTH cC parameter was set
  29066. + * to "8 and 16 bits", meaning that the core has been
  29067. + * configured to work at either data path width.
  29068. + *
  29069. + * 8 or 16 bits (default 16)
  29070. + */
  29071. + int32_t phy_utmi_width;
  29072. +
  29073. + /**
  29074. + * Specifies whether the ULPI operates at double or single
  29075. + * data rate. This parameter is only applicable if PHY_TYPE is
  29076. + * ULPI.
  29077. + *
  29078. + * 0 - single data rate ULPI interface with 8 bit wide data
  29079. + * bus (default)
  29080. + * 1 - double data rate ULPI interface with 4 bit wide data
  29081. + * bus
  29082. + */
  29083. + int32_t phy_ulpi_ddr;
  29084. +
  29085. + /**
  29086. + * Specifies whether to use the internal or external supply to
  29087. + * drive the vbus with a ULPI phy.
  29088. + */
  29089. + int32_t phy_ulpi_ext_vbus;
  29090. +
  29091. + /**
  29092. + * Specifies whether to use the I2Cinterface for full speed PHY. This
  29093. + * parameter is only applicable if PHY_TYPE is FS.
  29094. + * 0 - No (default)
  29095. + * 1 - Yes
  29096. + */
  29097. + int32_t i2c_enable;
  29098. +
  29099. + int32_t ulpi_fs_ls;
  29100. +
  29101. + int32_t ts_dline;
  29102. +
  29103. + /**
  29104. + * Specifies whether dedicated transmit FIFOs are
  29105. + * enabled for non periodic IN endpoints in device mode
  29106. + * 0 - No
  29107. + * 1 - Yes
  29108. + */
  29109. + int32_t en_multiple_tx_fifo;
  29110. +
  29111. + /** Number of 4-byte words in each of the Tx FIFOs in device
  29112. + * mode when dynamic FIFO sizing is enabled.
  29113. + * 4 to 768 (default 256)
  29114. + */
  29115. + uint32_t dev_tx_fifo_size[MAX_TX_FIFOS];
  29116. +
  29117. + /** Thresholding enable flag-
  29118. + * bit 0 - enable non-ISO Tx thresholding
  29119. + * bit 1 - enable ISO Tx thresholding
  29120. + * bit 2 - enable Rx thresholding
  29121. + */
  29122. + uint32_t thr_ctl;
  29123. +
  29124. + /** Thresholding length for Tx
  29125. + * FIFOs in 32 bit DWORDs
  29126. + */
  29127. + uint32_t tx_thr_length;
  29128. +
  29129. + /** Thresholding length for Rx
  29130. + * FIFOs in 32 bit DWORDs
  29131. + */
  29132. + uint32_t rx_thr_length;
  29133. +
  29134. + /**
  29135. + * Specifies whether LPM (Link Power Management) support is enabled
  29136. + */
  29137. + int32_t lpm_enable;
  29138. +
  29139. + /** Per Transfer Interrupt
  29140. + * mode enable flag
  29141. + * 1 - Enabled
  29142. + * 0 - Disabled
  29143. + */
  29144. + int32_t pti_enable;
  29145. +
  29146. + /** Multi Processor Interrupt
  29147. + * mode enable flag
  29148. + * 1 - Enabled
  29149. + * 0 - Disabled
  29150. + */
  29151. + int32_t mpi_enable;
  29152. +
  29153. + /** IS_USB Capability
  29154. + * 1 - Enabled
  29155. + * 0 - Disabled
  29156. + */
  29157. + int32_t ic_usb_cap;
  29158. +
  29159. + /** AHB Threshold Ratio
  29160. + * 2'b00 AHB Threshold = MAC Threshold
  29161. + * 2'b01 AHB Threshold = 1/2 MAC Threshold
  29162. + * 2'b10 AHB Threshold = 1/4 MAC Threshold
  29163. + * 2'b11 AHB Threshold = 1/8 MAC Threshold
  29164. + */
  29165. + int32_t ahb_thr_ratio;
  29166. +
  29167. + /** ADP Support
  29168. + * 1 - Enabled
  29169. + * 0 - Disabled
  29170. + */
  29171. + int32_t adp_supp_enable;
  29172. +
  29173. + /** HFIR Reload Control
  29174. + * 0 - The HFIR cannot be reloaded dynamically.
  29175. + * 1 - Allow dynamic reloading of the HFIR register during runtime.
  29176. + */
  29177. + int32_t reload_ctl;
  29178. +
  29179. + /** DCFG: Enable device Out NAK
  29180. + * 0 - The core does not set NAK after Bulk Out transfer complete.
  29181. + * 1 - The core sets NAK after Bulk OUT transfer complete.
  29182. + */
  29183. + int32_t dev_out_nak;
  29184. +
  29185. + /** DCFG: Enable Continue on BNA
  29186. + * After receiving BNA interrupt the core disables the endpoint,when the
  29187. + * endpoint is re-enabled by the application the core starts processing
  29188. + * 0 - from the DOEPDMA descriptor
  29189. + * 1 - from the descriptor which received the BNA.
  29190. + */
  29191. + int32_t cont_on_bna;
  29192. +
  29193. + /** GAHBCFG: AHB Single Support
  29194. + * This bit when programmed supports SINGLE transfers for remainder
  29195. + * data in a transfer for DMA mode of operation.
  29196. + * 0 - in this case the remainder data will be sent using INCR burst size.
  29197. + * 1 - in this case the remainder data will be sent using SINGLE burst size.
  29198. + */
  29199. + int32_t ahb_single;
  29200. +
  29201. + /** Core Power down mode
  29202. + * 0 - No Power Down is enabled
  29203. + * 1 - Reserved
  29204. + * 2 - Complete Power Down (Hibernation)
  29205. + */
  29206. + int32_t power_down;
  29207. +
  29208. + /** OTG revision supported
  29209. + * 0 - OTG 1.3 revision
  29210. + * 1 - OTG 2.0 revision
  29211. + */
  29212. + int32_t otg_ver;
  29213. +
  29214. +} dwc_otg_core_params_t;
  29215. +
  29216. +#ifdef DEBUG
  29217. +struct dwc_otg_core_if;
  29218. +typedef struct hc_xfer_info {
  29219. + struct dwc_otg_core_if *core_if;
  29220. + dwc_hc_t *hc;
  29221. +} hc_xfer_info_t;
  29222. +#endif
  29223. +
  29224. +typedef struct ep_xfer_info {
  29225. + struct dwc_otg_core_if *core_if;
  29226. + dwc_ep_t *ep;
  29227. + uint8_t state;
  29228. +} ep_xfer_info_t;
  29229. +/*
  29230. + * Device States
  29231. + */
  29232. +typedef enum dwc_otg_lx_state {
  29233. + /** On state */
  29234. + DWC_OTG_L0,
  29235. + /** LPM sleep state*/
  29236. + DWC_OTG_L1,
  29237. + /** USB suspend state*/
  29238. + DWC_OTG_L2,
  29239. + /** Off state*/
  29240. + DWC_OTG_L3
  29241. +} dwc_otg_lx_state_e;
  29242. +
  29243. +struct dwc_otg_global_regs_backup {
  29244. + uint32_t gotgctl_local;
  29245. + uint32_t gintmsk_local;
  29246. + uint32_t gahbcfg_local;
  29247. + uint32_t gusbcfg_local;
  29248. + uint32_t grxfsiz_local;
  29249. + uint32_t gnptxfsiz_local;
  29250. +#ifdef CONFIG_USB_DWC_OTG_LPM
  29251. + uint32_t glpmcfg_local;
  29252. +#endif
  29253. + uint32_t gi2cctl_local;
  29254. + uint32_t hptxfsiz_local;
  29255. + uint32_t pcgcctl_local;
  29256. + uint32_t gdfifocfg_local;
  29257. + uint32_t dtxfsiz_local[MAX_EPS_CHANNELS];
  29258. + uint32_t gpwrdn_local;
  29259. + uint32_t xhib_pcgcctl;
  29260. + uint32_t xhib_gpwrdn;
  29261. +};
  29262. +
  29263. +struct dwc_otg_host_regs_backup {
  29264. + uint32_t hcfg_local;
  29265. + uint32_t haintmsk_local;
  29266. + uint32_t hcintmsk_local[MAX_EPS_CHANNELS];
  29267. + uint32_t hprt0_local;
  29268. + uint32_t hfir_local;
  29269. +};
  29270. +
  29271. +struct dwc_otg_dev_regs_backup {
  29272. + uint32_t dcfg;
  29273. + uint32_t dctl;
  29274. + uint32_t daintmsk;
  29275. + uint32_t diepmsk;
  29276. + uint32_t doepmsk;
  29277. + uint32_t diepctl[MAX_EPS_CHANNELS];
  29278. + uint32_t dieptsiz[MAX_EPS_CHANNELS];
  29279. + uint32_t diepdma[MAX_EPS_CHANNELS];
  29280. +};
  29281. +/**
  29282. + * The <code>dwc_otg_core_if</code> structure contains information needed to manage
  29283. + * the DWC_otg controller acting in either host or device mode. It
  29284. + * represents the programming view of the controller as a whole.
  29285. + */
  29286. +struct dwc_otg_core_if {
  29287. + /** Parameters that define how the core should be configured.*/
  29288. + dwc_otg_core_params_t *core_params;
  29289. +
  29290. + /** Core Global registers starting at offset 000h. */
  29291. + dwc_otg_core_global_regs_t *core_global_regs;
  29292. +
  29293. + /** Device-specific information */
  29294. + dwc_otg_dev_if_t *dev_if;
  29295. + /** Host-specific information */
  29296. + dwc_otg_host_if_t *host_if;
  29297. +
  29298. + /** Value from SNPSID register */
  29299. + uint32_t snpsid;
  29300. +
  29301. + /*
  29302. + * Set to 1 if the core PHY interface bits in USBCFG have been
  29303. + * initialized.
  29304. + */
  29305. + uint8_t phy_init_done;
  29306. +
  29307. + /*
  29308. + * SRP Success flag, set by srp success interrupt in FS I2C mode
  29309. + */
  29310. + uint8_t srp_success;
  29311. + uint8_t srp_timer_started;
  29312. + /** Timer for SRP. If it expires before SRP is successful
  29313. + * clear the SRP. */
  29314. + dwc_timer_t *srp_timer;
  29315. +
  29316. +#ifdef DWC_DEV_SRPCAP
  29317. + /* This timer is needed to power on the hibernated host core if SRP is not
  29318. + * initiated on connected SRP capable device for limited period of time
  29319. + */
  29320. + uint8_t pwron_timer_started;
  29321. + dwc_timer_t *pwron_timer;
  29322. +#endif
  29323. + /* Common configuration information */
  29324. + /** Power and Clock Gating Control Register */
  29325. + volatile uint32_t *pcgcctl;
  29326. +#define DWC_OTG_PCGCCTL_OFFSET 0xE00
  29327. +
  29328. + /** Push/pop addresses for endpoints or host channels.*/
  29329. + uint32_t *data_fifo[MAX_EPS_CHANNELS];
  29330. +#define DWC_OTG_DATA_FIFO_OFFSET 0x1000
  29331. +#define DWC_OTG_DATA_FIFO_SIZE 0x1000
  29332. +
  29333. + /** Total RAM for FIFOs (Bytes) */
  29334. + uint16_t total_fifo_size;
  29335. + /** Size of Rx FIFO (Bytes) */
  29336. + uint16_t rx_fifo_size;
  29337. + /** Size of Non-periodic Tx FIFO (Bytes) */
  29338. + uint16_t nperio_tx_fifo_size;
  29339. +
  29340. + /** 1 if DMA is enabled, 0 otherwise. */
  29341. + uint8_t dma_enable;
  29342. +
  29343. + /** 1 if DMA descriptor is enabled, 0 otherwise. */
  29344. + uint8_t dma_desc_enable;
  29345. +
  29346. + /** 1 if PTI Enhancement mode is enabled, 0 otherwise. */
  29347. + uint8_t pti_enh_enable;
  29348. +
  29349. + /** 1 if MPI Enhancement mode is enabled, 0 otherwise. */
  29350. + uint8_t multiproc_int_enable;
  29351. +
  29352. + /** 1 if dedicated Tx FIFOs are enabled, 0 otherwise. */
  29353. + uint8_t en_multiple_tx_fifo;
  29354. +
  29355. + /** Set to 1 if multiple packets of a high-bandwidth transfer is in
  29356. + * process of being queued */
  29357. + uint8_t queuing_high_bandwidth;
  29358. +
  29359. + /** Hardware Configuration -- stored here for convenience.*/
  29360. + hwcfg1_data_t hwcfg1;
  29361. + hwcfg2_data_t hwcfg2;
  29362. + hwcfg3_data_t hwcfg3;
  29363. + hwcfg4_data_t hwcfg4;
  29364. + fifosize_data_t hptxfsiz;
  29365. +
  29366. + /** Host and Device Configuration -- stored here for convenience.*/
  29367. + hcfg_data_t hcfg;
  29368. + dcfg_data_t dcfg;
  29369. +
  29370. + /** The operational State, during transations
  29371. + * (a_host>>a_peripherial and b_device=>b_host) this may not
  29372. + * match the core but allows the software to determine
  29373. + * transitions.
  29374. + */
  29375. + uint8_t op_state;
  29376. +
  29377. + /**
  29378. + * Set to 1 if the HCD needs to be restarted on a session request
  29379. + * interrupt. This is required if no connector ID status change has
  29380. + * occurred since the HCD was last disconnected.
  29381. + */
  29382. + uint8_t restart_hcd_on_session_req;
  29383. +
  29384. + /** HCD callbacks */
  29385. + /** A-Device is a_host */
  29386. +#define A_HOST (1)
  29387. + /** A-Device is a_suspend */
  29388. +#define A_SUSPEND (2)
  29389. + /** A-Device is a_peripherial */
  29390. +#define A_PERIPHERAL (3)
  29391. + /** B-Device is operating as a Peripheral. */
  29392. +#define B_PERIPHERAL (4)
  29393. + /** B-Device is operating as a Host. */
  29394. +#define B_HOST (5)
  29395. +
  29396. + /** HCD callbacks */
  29397. + struct dwc_otg_cil_callbacks *hcd_cb;
  29398. + /** PCD callbacks */
  29399. + struct dwc_otg_cil_callbacks *pcd_cb;
  29400. +
  29401. + /** Device mode Periodic Tx FIFO Mask */
  29402. + uint32_t p_tx_msk;
  29403. + /** Device mode Periodic Tx FIFO Mask */
  29404. + uint32_t tx_msk;
  29405. +
  29406. + /** Workqueue object used for handling several interrupts */
  29407. + dwc_workq_t *wq_otg;
  29408. +
  29409. + /** Timer object used for handling "Wakeup Detected" Interrupt */
  29410. + dwc_timer_t *wkp_timer;
  29411. + /** This arrays used for debug purposes for DEV OUT NAK enhancement */
  29412. + uint32_t start_doeptsiz_val[MAX_EPS_CHANNELS];
  29413. + ep_xfer_info_t ep_xfer_info[MAX_EPS_CHANNELS];
  29414. + dwc_timer_t *ep_xfer_timer[MAX_EPS_CHANNELS];
  29415. +#ifdef DEBUG
  29416. + uint32_t start_hcchar_val[MAX_EPS_CHANNELS];
  29417. +
  29418. + hc_xfer_info_t hc_xfer_info[MAX_EPS_CHANNELS];
  29419. + dwc_timer_t *hc_xfer_timer[MAX_EPS_CHANNELS];
  29420. +
  29421. + uint32_t hfnum_7_samples;
  29422. + uint64_t hfnum_7_frrem_accum;
  29423. + uint32_t hfnum_0_samples;
  29424. + uint64_t hfnum_0_frrem_accum;
  29425. + uint32_t hfnum_other_samples;
  29426. + uint64_t hfnum_other_frrem_accum;
  29427. +#endif
  29428. +
  29429. +#ifdef DWC_UTE_CFI
  29430. + uint16_t pwron_rxfsiz;
  29431. + uint16_t pwron_gnptxfsiz;
  29432. + uint16_t pwron_txfsiz[15];
  29433. +
  29434. + uint16_t init_rxfsiz;
  29435. + uint16_t init_gnptxfsiz;
  29436. + uint16_t init_txfsiz[15];
  29437. +#endif
  29438. +
  29439. + /** Lx state of device */
  29440. + dwc_otg_lx_state_e lx_state;
  29441. +
  29442. + /** Saved Core Global registers */
  29443. + struct dwc_otg_global_regs_backup *gr_backup;
  29444. + /** Saved Host registers */
  29445. + struct dwc_otg_host_regs_backup *hr_backup;
  29446. + /** Saved Device registers */
  29447. + struct dwc_otg_dev_regs_backup *dr_backup;
  29448. +
  29449. + /** Power Down Enable */
  29450. + uint32_t power_down;
  29451. +
  29452. + /** ADP support Enable */
  29453. + uint32_t adp_enable;
  29454. +
  29455. + /** ADP structure object */
  29456. + dwc_otg_adp_t adp;
  29457. +
  29458. + /** hibernation/suspend flag */
  29459. + int hibernation_suspend;
  29460. +
  29461. + /** Device mode extended hibernation flag */
  29462. + int xhib;
  29463. +
  29464. + /** OTG revision supported */
  29465. + uint32_t otg_ver;
  29466. +
  29467. + /** OTG status flag used for HNP polling */
  29468. + uint8_t otg_sts;
  29469. +
  29470. + /** Pointer to either hcd->lock or pcd->lock */
  29471. + dwc_spinlock_t *lock;
  29472. +
  29473. + /** Start predict NextEP based on Learning Queue if equal 1,
  29474. + * also used as counter of disabled NP IN EP's */
  29475. + uint8_t start_predict;
  29476. +
  29477. + /** NextEp sequence, including EP0: nextep_seq[] = EP if non-periodic and
  29478. + * active, 0xff otherwise */
  29479. + uint8_t nextep_seq[MAX_EPS_CHANNELS];
  29480. +
  29481. + /** Index of fisrt EP in nextep_seq array which should be re-enabled **/
  29482. + uint8_t first_in_nextep_seq;
  29483. +
  29484. + /** Frame number while entering to ISR - needed for ISOCs **/
  29485. + uint32_t frame_num;
  29486. +
  29487. +};
  29488. +
  29489. +#ifdef DEBUG
  29490. +/*
  29491. + * This function is called when transfer is timed out.
  29492. + */
  29493. +extern void hc_xfer_timeout(void *ptr);
  29494. +#endif
  29495. +
  29496. +/*
  29497. + * This function is called when transfer is timed out on endpoint.
  29498. + */
  29499. +extern void ep_xfer_timeout(void *ptr);
  29500. +
  29501. +/*
  29502. + * The following functions are functions for works
  29503. + * using during handling some interrupts
  29504. + */
  29505. +extern void w_conn_id_status_change(void *p);
  29506. +
  29507. +extern void w_wakeup_detected(void *p);
  29508. +
  29509. +/** Saves global register values into system memory. */
  29510. +extern int dwc_otg_save_global_regs(dwc_otg_core_if_t * core_if);
  29511. +/** Saves device register values into system memory. */
  29512. +extern int dwc_otg_save_dev_regs(dwc_otg_core_if_t * core_if);
  29513. +/** Saves host register values into system memory. */
  29514. +extern int dwc_otg_save_host_regs(dwc_otg_core_if_t * core_if);
  29515. +/** Restore global register values. */
  29516. +extern int dwc_otg_restore_global_regs(dwc_otg_core_if_t * core_if);
  29517. +/** Restore host register values. */
  29518. +extern int dwc_otg_restore_host_regs(dwc_otg_core_if_t * core_if, int reset);
  29519. +/** Restore device register values. */
  29520. +extern int dwc_otg_restore_dev_regs(dwc_otg_core_if_t * core_if,
  29521. + int rem_wakeup);
  29522. +extern int restore_lpm_i2c_regs(dwc_otg_core_if_t * core_if);
  29523. +extern int restore_essential_regs(dwc_otg_core_if_t * core_if, int rmode,
  29524. + int is_host);
  29525. +
  29526. +extern int dwc_otg_host_hibernation_restore(dwc_otg_core_if_t * core_if,
  29527. + int restore_mode, int reset);
  29528. +extern int dwc_otg_device_hibernation_restore(dwc_otg_core_if_t * core_if,
  29529. + int rem_wakeup, int reset);
  29530. +
  29531. +/*
  29532. + * The following functions support initialization of the CIL driver component
  29533. + * and the DWC_otg controller.
  29534. + */
  29535. +extern void dwc_otg_core_host_init(dwc_otg_core_if_t * _core_if);
  29536. +extern void dwc_otg_core_dev_init(dwc_otg_core_if_t * _core_if);
  29537. +
  29538. +/** @name Device CIL Functions
  29539. + * The following functions support managing the DWC_otg controller in device
  29540. + * mode.
  29541. + */
  29542. +/**@{*/
  29543. +extern void dwc_otg_wakeup(dwc_otg_core_if_t * _core_if);
  29544. +extern void dwc_otg_read_setup_packet(dwc_otg_core_if_t * _core_if,
  29545. + uint32_t * _dest);
  29546. +extern uint32_t dwc_otg_get_frame_number(dwc_otg_core_if_t * _core_if);
  29547. +extern void dwc_otg_ep0_activate(dwc_otg_core_if_t * _core_if, dwc_ep_t * _ep);
  29548. +extern void dwc_otg_ep_activate(dwc_otg_core_if_t * _core_if, dwc_ep_t * _ep);
  29549. +extern void dwc_otg_ep_deactivate(dwc_otg_core_if_t * _core_if, dwc_ep_t * _ep);
  29550. +extern void dwc_otg_ep_start_transfer(dwc_otg_core_if_t * _core_if,
  29551. + dwc_ep_t * _ep);
  29552. +extern void dwc_otg_ep_start_zl_transfer(dwc_otg_core_if_t * _core_if,
  29553. + dwc_ep_t * _ep);
  29554. +extern void dwc_otg_ep0_start_transfer(dwc_otg_core_if_t * _core_if,
  29555. + dwc_ep_t * _ep);
  29556. +extern void dwc_otg_ep0_continue_transfer(dwc_otg_core_if_t * _core_if,
  29557. + dwc_ep_t * _ep);
  29558. +extern void dwc_otg_ep_write_packet(dwc_otg_core_if_t * _core_if,
  29559. + dwc_ep_t * _ep, int _dma);
  29560. +extern void dwc_otg_ep_set_stall(dwc_otg_core_if_t * _core_if, dwc_ep_t * _ep);
  29561. +extern void dwc_otg_ep_clear_stall(dwc_otg_core_if_t * _core_if,
  29562. + dwc_ep_t * _ep);
  29563. +extern void dwc_otg_enable_device_interrupts(dwc_otg_core_if_t * _core_if);
  29564. +
  29565. +#ifdef DWC_EN_ISOC
  29566. +extern void dwc_otg_iso_ep_start_frm_transfer(dwc_otg_core_if_t * core_if,
  29567. + dwc_ep_t * ep);
  29568. +extern void dwc_otg_iso_ep_start_buf_transfer(dwc_otg_core_if_t * core_if,
  29569. + dwc_ep_t * ep);
  29570. +#endif /* DWC_EN_ISOC */
  29571. +/**@}*/
  29572. +
  29573. +/** @name Host CIL Functions
  29574. + * The following functions support managing the DWC_otg controller in host
  29575. + * mode.
  29576. + */
  29577. +/**@{*/
  29578. +extern void dwc_otg_hc_init(dwc_otg_core_if_t * _core_if, dwc_hc_t * _hc);
  29579. +extern void dwc_otg_hc_halt(dwc_otg_core_if_t * _core_if,
  29580. + dwc_hc_t * _hc, dwc_otg_halt_status_e _halt_status);
  29581. +extern void dwc_otg_hc_cleanup(dwc_otg_core_if_t * _core_if, dwc_hc_t * _hc);
  29582. +extern void dwc_otg_hc_start_transfer(dwc_otg_core_if_t * _core_if,
  29583. + dwc_hc_t * _hc);
  29584. +extern int dwc_otg_hc_continue_transfer(dwc_otg_core_if_t * _core_if,
  29585. + dwc_hc_t * _hc);
  29586. +extern void dwc_otg_hc_do_ping(dwc_otg_core_if_t * _core_if, dwc_hc_t * _hc);
  29587. +extern void dwc_otg_hc_write_packet(dwc_otg_core_if_t * _core_if,
  29588. + dwc_hc_t * _hc);
  29589. +extern void dwc_otg_enable_host_interrupts(dwc_otg_core_if_t * _core_if);
  29590. +extern void dwc_otg_disable_host_interrupts(dwc_otg_core_if_t * _core_if);
  29591. +
  29592. +extern void dwc_otg_hc_start_transfer_ddma(dwc_otg_core_if_t * core_if,
  29593. + dwc_hc_t * hc);
  29594. +
  29595. +extern uint32_t calc_frame_interval(dwc_otg_core_if_t * core_if);
  29596. +
  29597. +/* Macro used to clear one channel interrupt */
  29598. +#define clear_hc_int(_hc_regs_, _intr_) \
  29599. +do { \
  29600. + hcint_data_t hcint_clear = {.d32 = 0}; \
  29601. + hcint_clear.b._intr_ = 1; \
  29602. + DWC_WRITE_REG32(&(_hc_regs_)->hcint, hcint_clear.d32); \
  29603. +} while (0)
  29604. +
  29605. +/*
  29606. + * Macro used to disable one channel interrupt. Channel interrupts are
  29607. + * disabled when the channel is halted or released by the interrupt handler.
  29608. + * There is no need to handle further interrupts of that type until the
  29609. + * channel is re-assigned. In fact, subsequent handling may cause crashes
  29610. + * because the channel structures are cleaned up when the channel is released.
  29611. + */
  29612. +#define disable_hc_int(_hc_regs_, _intr_) \
  29613. +do { \
  29614. + hcintmsk_data_t hcintmsk = {.d32 = 0}; \
  29615. + hcintmsk.b._intr_ = 1; \
  29616. + DWC_MODIFY_REG32(&(_hc_regs_)->hcintmsk, hcintmsk.d32, 0); \
  29617. +} while (0)
  29618. +
  29619. +/**
  29620. + * This function Reads HPRT0 in preparation to modify. It keeps the
  29621. + * WC bits 0 so that if they are read as 1, they won't clear when you
  29622. + * write it back
  29623. + */
  29624. +static inline uint32_t dwc_otg_read_hprt0(dwc_otg_core_if_t * _core_if)
  29625. +{
  29626. + hprt0_data_t hprt0;
  29627. + hprt0.d32 = DWC_READ_REG32(_core_if->host_if->hprt0);
  29628. + hprt0.b.prtena = 0;
  29629. + hprt0.b.prtconndet = 0;
  29630. + hprt0.b.prtenchng = 0;
  29631. + hprt0.b.prtovrcurrchng = 0;
  29632. + return hprt0.d32;
  29633. +}
  29634. +
  29635. +/**@}*/
  29636. +
  29637. +/** @name Common CIL Functions
  29638. + * The following functions support managing the DWC_otg controller in either
  29639. + * device or host mode.
  29640. + */
  29641. +/**@{*/
  29642. +
  29643. +extern void dwc_otg_read_packet(dwc_otg_core_if_t * core_if,
  29644. + uint8_t * dest, uint16_t bytes);
  29645. +
  29646. +extern void dwc_otg_flush_tx_fifo(dwc_otg_core_if_t * _core_if, const int _num);
  29647. +extern void dwc_otg_flush_rx_fifo(dwc_otg_core_if_t * _core_if);
  29648. +extern void dwc_otg_core_reset(dwc_otg_core_if_t * _core_if);
  29649. +
  29650. +/**
  29651. + * This function returns the Core Interrupt register.
  29652. + */
  29653. +static inline uint32_t dwc_otg_read_core_intr(dwc_otg_core_if_t * core_if)
  29654. +{
  29655. + return (DWC_READ_REG32(&core_if->core_global_regs->gintsts) &
  29656. + DWC_READ_REG32(&core_if->core_global_regs->gintmsk));
  29657. +}
  29658. +
  29659. +/**
  29660. + * This function returns the OTG Interrupt register.
  29661. + */
  29662. +static inline uint32_t dwc_otg_read_otg_intr(dwc_otg_core_if_t * core_if)
  29663. +{
  29664. + return (DWC_READ_REG32(&core_if->core_global_regs->gotgint));
  29665. +}
  29666. +
  29667. +/**
  29668. + * This function reads the Device All Endpoints Interrupt register and
  29669. + * returns the IN endpoint interrupt bits.
  29670. + */
  29671. +static inline uint32_t dwc_otg_read_dev_all_in_ep_intr(dwc_otg_core_if_t *
  29672. + core_if)
  29673. +{
  29674. +
  29675. + uint32_t v;
  29676. +
  29677. + if (core_if->multiproc_int_enable) {
  29678. + v = DWC_READ_REG32(&core_if->dev_if->
  29679. + dev_global_regs->deachint) &
  29680. + DWC_READ_REG32(&core_if->
  29681. + dev_if->dev_global_regs->deachintmsk);
  29682. + } else {
  29683. + v = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->daint) &
  29684. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->daintmsk);
  29685. + }
  29686. + return (v & 0xffff);
  29687. +}
  29688. +
  29689. +/**
  29690. + * This function reads the Device All Endpoints Interrupt register and
  29691. + * returns the OUT endpoint interrupt bits.
  29692. + */
  29693. +static inline uint32_t dwc_otg_read_dev_all_out_ep_intr(dwc_otg_core_if_t *
  29694. + core_if)
  29695. +{
  29696. + uint32_t v;
  29697. +
  29698. + if (core_if->multiproc_int_enable) {
  29699. + v = DWC_READ_REG32(&core_if->dev_if->
  29700. + dev_global_regs->deachint) &
  29701. + DWC_READ_REG32(&core_if->
  29702. + dev_if->dev_global_regs->deachintmsk);
  29703. + } else {
  29704. + v = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->daint) &
  29705. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->daintmsk);
  29706. + }
  29707. +
  29708. + return ((v & 0xffff0000) >> 16);
  29709. +}
  29710. +
  29711. +/**
  29712. + * This function returns the Device IN EP Interrupt register
  29713. + */
  29714. +static inline uint32_t dwc_otg_read_dev_in_ep_intr(dwc_otg_core_if_t * core_if,
  29715. + dwc_ep_t * ep)
  29716. +{
  29717. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  29718. + uint32_t v, msk, emp;
  29719. +
  29720. + if (core_if->multiproc_int_enable) {
  29721. + msk =
  29722. + DWC_READ_REG32(&dev_if->
  29723. + dev_global_regs->diepeachintmsk[ep->num]);
  29724. + emp =
  29725. + DWC_READ_REG32(&dev_if->
  29726. + dev_global_regs->dtknqr4_fifoemptymsk);
  29727. + msk |= ((emp >> ep->num) & 0x1) << 7;
  29728. + v = DWC_READ_REG32(&dev_if->in_ep_regs[ep->num]->diepint) & msk;
  29729. + } else {
  29730. + msk = DWC_READ_REG32(&dev_if->dev_global_regs->diepmsk);
  29731. + emp =
  29732. + DWC_READ_REG32(&dev_if->
  29733. + dev_global_regs->dtknqr4_fifoemptymsk);
  29734. + msk |= ((emp >> ep->num) & 0x1) << 7;
  29735. + v = DWC_READ_REG32(&dev_if->in_ep_regs[ep->num]->diepint) & msk;
  29736. + }
  29737. +
  29738. + return v;
  29739. +}
  29740. +
  29741. +/**
  29742. + * This function returns the Device OUT EP Interrupt register
  29743. + */
  29744. +static inline uint32_t dwc_otg_read_dev_out_ep_intr(dwc_otg_core_if_t *
  29745. + _core_if, dwc_ep_t * _ep)
  29746. +{
  29747. + dwc_otg_dev_if_t *dev_if = _core_if->dev_if;
  29748. + uint32_t v;
  29749. + doepmsk_data_t msk = {.d32 = 0 };
  29750. +
  29751. + if (_core_if->multiproc_int_enable) {
  29752. + msk.d32 =
  29753. + DWC_READ_REG32(&dev_if->
  29754. + dev_global_regs->doepeachintmsk[_ep->num]);
  29755. + if (_core_if->pti_enh_enable) {
  29756. + msk.b.pktdrpsts = 1;
  29757. + }
  29758. + v = DWC_READ_REG32(&dev_if->
  29759. + out_ep_regs[_ep->num]->doepint) & msk.d32;
  29760. + } else {
  29761. + msk.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->doepmsk);
  29762. + if (_core_if->pti_enh_enable) {
  29763. + msk.b.pktdrpsts = 1;
  29764. + }
  29765. + v = DWC_READ_REG32(&dev_if->
  29766. + out_ep_regs[_ep->num]->doepint) & msk.d32;
  29767. + }
  29768. + return v;
  29769. +}
  29770. +
  29771. +/**
  29772. + * This function returns the Host All Channel Interrupt register
  29773. + */
  29774. +static inline uint32_t dwc_otg_read_host_all_channels_intr(dwc_otg_core_if_t *
  29775. + _core_if)
  29776. +{
  29777. + return (DWC_READ_REG32(&_core_if->host_if->host_global_regs->haint));
  29778. +}
  29779. +
  29780. +static inline uint32_t dwc_otg_read_host_channel_intr(dwc_otg_core_if_t *
  29781. + _core_if, dwc_hc_t * _hc)
  29782. +{
  29783. + return (DWC_READ_REG32
  29784. + (&_core_if->host_if->hc_regs[_hc->hc_num]->hcint));
  29785. +}
  29786. +
  29787. +/**
  29788. + * This function returns the mode of the operation, host or device.
  29789. + *
  29790. + * @return 0 - Device Mode, 1 - Host Mode
  29791. + */
  29792. +static inline uint32_t dwc_otg_mode(dwc_otg_core_if_t * _core_if)
  29793. +{
  29794. + return (DWC_READ_REG32(&_core_if->core_global_regs->gintsts) & 0x1);
  29795. +}
  29796. +
  29797. +/**@}*/
  29798. +
  29799. +/**
  29800. + * DWC_otg CIL callback structure. This structure allows the HCD and
  29801. + * PCD to register functions used for starting and stopping the PCD
  29802. + * and HCD for role change on for a DRD.
  29803. + */
  29804. +typedef struct dwc_otg_cil_callbacks {
  29805. + /** Start function for role change */
  29806. + int (*start) (void *_p);
  29807. + /** Stop Function for role change */
  29808. + int (*stop) (void *_p);
  29809. + /** Disconnect Function for role change */
  29810. + int (*disconnect) (void *_p);
  29811. + /** Resume/Remote wakeup Function */
  29812. + int (*resume_wakeup) (void *_p);
  29813. + /** Suspend function */
  29814. + int (*suspend) (void *_p);
  29815. + /** Session Start (SRP) */
  29816. + int (*session_start) (void *_p);
  29817. +#ifdef CONFIG_USB_DWC_OTG_LPM
  29818. + /** Sleep (switch to L0 state) */
  29819. + int (*sleep) (void *_p);
  29820. +#endif
  29821. + /** Pointer passed to start() and stop() */
  29822. + void *p;
  29823. +} dwc_otg_cil_callbacks_t;
  29824. +
  29825. +extern void dwc_otg_cil_register_pcd_callbacks(dwc_otg_core_if_t * _core_if,
  29826. + dwc_otg_cil_callbacks_t * _cb,
  29827. + void *_p);
  29828. +extern void dwc_otg_cil_register_hcd_callbacks(dwc_otg_core_if_t * _core_if,
  29829. + dwc_otg_cil_callbacks_t * _cb,
  29830. + void *_p);
  29831. +
  29832. +void dwc_otg_initiate_srp(dwc_otg_core_if_t * core_if);
  29833. +
  29834. +//////////////////////////////////////////////////////////////////////
  29835. +/** Start the HCD. Helper function for using the HCD callbacks.
  29836. + *
  29837. + * @param core_if Programming view of DWC_otg controller.
  29838. + */
  29839. +static inline void cil_hcd_start(dwc_otg_core_if_t * core_if)
  29840. +{
  29841. + if (core_if->hcd_cb && core_if->hcd_cb->start) {
  29842. + core_if->hcd_cb->start(core_if->hcd_cb->p);
  29843. + }
  29844. +}
  29845. +
  29846. +/** Stop the HCD. Helper function for using the HCD callbacks.
  29847. + *
  29848. + * @param core_if Programming view of DWC_otg controller.
  29849. + */
  29850. +static inline void cil_hcd_stop(dwc_otg_core_if_t * core_if)
  29851. +{
  29852. + if (core_if->hcd_cb && core_if->hcd_cb->stop) {
  29853. + core_if->hcd_cb->stop(core_if->hcd_cb->p);
  29854. + }
  29855. +}
  29856. +
  29857. +/** Disconnect the HCD. Helper function for using the HCD callbacks.
  29858. + *
  29859. + * @param core_if Programming view of DWC_otg controller.
  29860. + */
  29861. +static inline void cil_hcd_disconnect(dwc_otg_core_if_t * core_if)
  29862. +{
  29863. + if (core_if->hcd_cb && core_if->hcd_cb->disconnect) {
  29864. + core_if->hcd_cb->disconnect(core_if->hcd_cb->p);
  29865. + }
  29866. +}
  29867. +
  29868. +/** Inform the HCD the a New Session has begun. Helper function for
  29869. + * using the HCD callbacks.
  29870. + *
  29871. + * @param core_if Programming view of DWC_otg controller.
  29872. + */
  29873. +static inline void cil_hcd_session_start(dwc_otg_core_if_t * core_if)
  29874. +{
  29875. + if (core_if->hcd_cb && core_if->hcd_cb->session_start) {
  29876. + core_if->hcd_cb->session_start(core_if->hcd_cb->p);
  29877. + }
  29878. +}
  29879. +
  29880. +#ifdef CONFIG_USB_DWC_OTG_LPM
  29881. +/**
  29882. + * Inform the HCD about LPM sleep.
  29883. + * Helper function for using the HCD callbacks.
  29884. + *
  29885. + * @param core_if Programming view of DWC_otg controller.
  29886. + */
  29887. +static inline void cil_hcd_sleep(dwc_otg_core_if_t * core_if)
  29888. +{
  29889. + if (core_if->hcd_cb && core_if->hcd_cb->sleep) {
  29890. + core_if->hcd_cb->sleep(core_if->hcd_cb->p);
  29891. + }
  29892. +}
  29893. +#endif
  29894. +
  29895. +/** Resume the HCD. Helper function for using the HCD callbacks.
  29896. + *
  29897. + * @param core_if Programming view of DWC_otg controller.
  29898. + */
  29899. +static inline void cil_hcd_resume(dwc_otg_core_if_t * core_if)
  29900. +{
  29901. + if (core_if->hcd_cb && core_if->hcd_cb->resume_wakeup) {
  29902. + core_if->hcd_cb->resume_wakeup(core_if->hcd_cb->p);
  29903. + }
  29904. +}
  29905. +
  29906. +/** Start the PCD. Helper function for using the PCD callbacks.
  29907. + *
  29908. + * @param core_if Programming view of DWC_otg controller.
  29909. + */
  29910. +static inline void cil_pcd_start(dwc_otg_core_if_t * core_if)
  29911. +{
  29912. + if (core_if->pcd_cb && core_if->pcd_cb->start) {
  29913. + core_if->pcd_cb->start(core_if->pcd_cb->p);
  29914. + }
  29915. +}
  29916. +
  29917. +/** Stop the PCD. Helper function for using the PCD callbacks.
  29918. + *
  29919. + * @param core_if Programming view of DWC_otg controller.
  29920. + */
  29921. +static inline void cil_pcd_stop(dwc_otg_core_if_t * core_if)
  29922. +{
  29923. + if (core_if->pcd_cb && core_if->pcd_cb->stop) {
  29924. + core_if->pcd_cb->stop(core_if->pcd_cb->p);
  29925. + }
  29926. +}
  29927. +
  29928. +/** Suspend the PCD. Helper function for using the PCD callbacks.
  29929. + *
  29930. + * @param core_if Programming view of DWC_otg controller.
  29931. + */
  29932. +static inline void cil_pcd_suspend(dwc_otg_core_if_t * core_if)
  29933. +{
  29934. + if (core_if->pcd_cb && core_if->pcd_cb->suspend) {
  29935. + core_if->pcd_cb->suspend(core_if->pcd_cb->p);
  29936. + }
  29937. +}
  29938. +
  29939. +/** Resume the PCD. Helper function for using the PCD callbacks.
  29940. + *
  29941. + * @param core_if Programming view of DWC_otg controller.
  29942. + */
  29943. +static inline void cil_pcd_resume(dwc_otg_core_if_t * core_if)
  29944. +{
  29945. + if (core_if->pcd_cb && core_if->pcd_cb->resume_wakeup) {
  29946. + core_if->pcd_cb->resume_wakeup(core_if->pcd_cb->p);
  29947. + }
  29948. +}
  29949. +
  29950. +//////////////////////////////////////////////////////////////////////
  29951. +
  29952. +#endif
  29953. --- /dev/null
  29954. +++ b/drivers/usb/host/dwc_otg/dwc_otg_cil_intr.c
  29955. @@ -0,0 +1,1594 @@
  29956. +/* ==========================================================================
  29957. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_cil_intr.c $
  29958. + * $Revision: #32 $
  29959. + * $Date: 2012/08/10 $
  29960. + * $Change: 2047372 $
  29961. + *
  29962. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  29963. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  29964. + * otherwise expressly agreed to in writing between Synopsys and you.
  29965. + *
  29966. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  29967. + * any End User Software License Agreement or Agreement for Licensed Product
  29968. + * with Synopsys or any supplement thereto. You are permitted to use and
  29969. + * redistribute this Software in source and binary forms, with or without
  29970. + * modification, provided that redistributions of source code must retain this
  29971. + * notice. You may not view, use, disclose, copy or distribute this file or
  29972. + * any information contained herein except pursuant to this license grant from
  29973. + * Synopsys. If you do not agree with this notice, including the disclaimer
  29974. + * below, then you are not authorized to use the Software.
  29975. + *
  29976. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  29977. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  29978. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29979. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  29980. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  29981. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  29982. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  29983. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29984. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  29985. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  29986. + * DAMAGE.
  29987. + * ========================================================================== */
  29988. +
  29989. +/** @file
  29990. + *
  29991. + * The Core Interface Layer provides basic services for accessing and
  29992. + * managing the DWC_otg hardware. These services are used by both the
  29993. + * Host Controller Driver and the Peripheral Controller Driver.
  29994. + *
  29995. + * This file contains the Common Interrupt handlers.
  29996. + */
  29997. +#include "dwc_os.h"
  29998. +#include "dwc_otg_regs.h"
  29999. +#include "dwc_otg_cil.h"
  30000. +#include "dwc_otg_driver.h"
  30001. +#include "dwc_otg_pcd.h"
  30002. +#include "dwc_otg_hcd.h"
  30003. +
  30004. +#ifdef DEBUG
  30005. +inline const char *op_state_str(dwc_otg_core_if_t * core_if)
  30006. +{
  30007. + return (core_if->op_state == A_HOST ? "a_host" :
  30008. + (core_if->op_state == A_SUSPEND ? "a_suspend" :
  30009. + (core_if->op_state == A_PERIPHERAL ? "a_peripheral" :
  30010. + (core_if->op_state == B_PERIPHERAL ? "b_peripheral" :
  30011. + (core_if->op_state == B_HOST ? "b_host" : "unknown")))));
  30012. +}
  30013. +#endif
  30014. +
  30015. +/** This function will log a debug message
  30016. + *
  30017. + * @param core_if Programming view of DWC_otg controller.
  30018. + */
  30019. +int32_t dwc_otg_handle_mode_mismatch_intr(dwc_otg_core_if_t * core_if)
  30020. +{
  30021. + gintsts_data_t gintsts;
  30022. + DWC_WARN("Mode Mismatch Interrupt: currently in %s mode\n",
  30023. + dwc_otg_mode(core_if) ? "Host" : "Device");
  30024. +
  30025. + /* Clear interrupt */
  30026. + gintsts.d32 = 0;
  30027. + gintsts.b.modemismatch = 1;
  30028. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  30029. + return 1;
  30030. +}
  30031. +
  30032. +/**
  30033. + * This function handles the OTG Interrupts. It reads the OTG
  30034. + * Interrupt Register (GOTGINT) to determine what interrupt has
  30035. + * occurred.
  30036. + *
  30037. + * @param core_if Programming view of DWC_otg controller.
  30038. + */
  30039. +int32_t dwc_otg_handle_otg_intr(dwc_otg_core_if_t * core_if)
  30040. +{
  30041. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  30042. + gotgint_data_t gotgint;
  30043. + gotgctl_data_t gotgctl;
  30044. + gintmsk_data_t gintmsk;
  30045. + gpwrdn_data_t gpwrdn;
  30046. +
  30047. + gotgint.d32 = DWC_READ_REG32(&global_regs->gotgint);
  30048. + gotgctl.d32 = DWC_READ_REG32(&global_regs->gotgctl);
  30049. + DWC_DEBUGPL(DBG_CIL, "++OTG Interrupt gotgint=%0x [%s]\n", gotgint.d32,
  30050. + op_state_str(core_if));
  30051. +
  30052. + if (gotgint.b.sesenddet) {
  30053. + DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: "
  30054. + "Session End Detected++ (%s)\n",
  30055. + op_state_str(core_if));
  30056. + gotgctl.d32 = DWC_READ_REG32(&global_regs->gotgctl);
  30057. +
  30058. + if (core_if->op_state == B_HOST) {
  30059. + cil_pcd_start(core_if);
  30060. + core_if->op_state = B_PERIPHERAL;
  30061. + } else {
  30062. + /* If not B_HOST and Device HNP still set. HNP
  30063. + * Did not succeed!*/
  30064. + if (gotgctl.b.devhnpen) {
  30065. + DWC_DEBUGPL(DBG_ANY, "Session End Detected\n");
  30066. + __DWC_ERROR("Device Not Connected/Responding!\n");
  30067. + }
  30068. +
  30069. + /* If Session End Detected the B-Cable has
  30070. + * been disconnected. */
  30071. + /* Reset PCD and Gadget driver to a
  30072. + * clean state. */
  30073. + core_if->lx_state = DWC_OTG_L0;
  30074. + DWC_SPINUNLOCK(core_if->lock);
  30075. + cil_pcd_stop(core_if);
  30076. + DWC_SPINLOCK(core_if->lock);
  30077. +
  30078. + if (core_if->adp_enable) {
  30079. + if (core_if->power_down == 2) {
  30080. + gpwrdn.d32 = 0;
  30081. + gpwrdn.b.pwrdnswtch = 1;
  30082. + DWC_MODIFY_REG32(&core_if->
  30083. + core_global_regs->
  30084. + gpwrdn, gpwrdn.d32, 0);
  30085. + }
  30086. +
  30087. + gpwrdn.d32 = 0;
  30088. + gpwrdn.b.pmuintsel = 1;
  30089. + gpwrdn.b.pmuactv = 1;
  30090. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  30091. + gpwrdn, 0, gpwrdn.d32);
  30092. +
  30093. + dwc_otg_adp_sense_start(core_if);
  30094. + }
  30095. + }
  30096. +
  30097. + gotgctl.d32 = 0;
  30098. + gotgctl.b.devhnpen = 1;
  30099. + DWC_MODIFY_REG32(&global_regs->gotgctl, gotgctl.d32, 0);
  30100. + }
  30101. + if (gotgint.b.sesreqsucstschng) {
  30102. + DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: "
  30103. + "Session Reqeust Success Status Change++\n");
  30104. + gotgctl.d32 = DWC_READ_REG32(&global_regs->gotgctl);
  30105. + if (gotgctl.b.sesreqscs) {
  30106. +
  30107. + if ((core_if->core_params->phy_type ==
  30108. + DWC_PHY_TYPE_PARAM_FS) && (core_if->core_params->i2c_enable)) {
  30109. + core_if->srp_success = 1;
  30110. + } else {
  30111. + DWC_SPINUNLOCK(core_if->lock);
  30112. + cil_pcd_resume(core_if);
  30113. + DWC_SPINLOCK(core_if->lock);
  30114. + /* Clear Session Request */
  30115. + gotgctl.d32 = 0;
  30116. + gotgctl.b.sesreq = 1;
  30117. + DWC_MODIFY_REG32(&global_regs->gotgctl,
  30118. + gotgctl.d32, 0);
  30119. + }
  30120. + }
  30121. + }
  30122. + if (gotgint.b.hstnegsucstschng) {
  30123. + /* Print statements during the HNP interrupt handling
  30124. + * can cause it to fail.*/
  30125. + gotgctl.d32 = DWC_READ_REG32(&global_regs->gotgctl);
  30126. + /* WA for 3.00a- HW is not setting cur_mode, even sometimes
  30127. + * this does not help*/
  30128. + if (core_if->snpsid >= OTG_CORE_REV_3_00a)
  30129. + dwc_udelay(100);
  30130. + if (gotgctl.b.hstnegscs) {
  30131. + if (dwc_otg_is_host_mode(core_if)) {
  30132. + core_if->op_state = B_HOST;
  30133. + /*
  30134. + * Need to disable SOF interrupt immediately.
  30135. + * When switching from device to host, the PCD
  30136. + * interrupt handler won't handle the
  30137. + * interrupt if host mode is already set. The
  30138. + * HCD interrupt handler won't get called if
  30139. + * the HCD state is HALT. This means that the
  30140. + * interrupt does not get handled and Linux
  30141. + * complains loudly.
  30142. + */
  30143. + gintmsk.d32 = 0;
  30144. + gintmsk.b.sofintr = 1;
  30145. + DWC_MODIFY_REG32(&global_regs->gintmsk,
  30146. + gintmsk.d32, 0);
  30147. + /* Call callback function with spin lock released */
  30148. + DWC_SPINUNLOCK(core_if->lock);
  30149. + cil_pcd_stop(core_if);
  30150. + /*
  30151. + * Initialize the Core for Host mode.
  30152. + */
  30153. + cil_hcd_start(core_if);
  30154. + DWC_SPINLOCK(core_if->lock);
  30155. + core_if->op_state = B_HOST;
  30156. + }
  30157. + } else {
  30158. + gotgctl.d32 = 0;
  30159. + gotgctl.b.hnpreq = 1;
  30160. + gotgctl.b.devhnpen = 1;
  30161. + DWC_MODIFY_REG32(&global_regs->gotgctl, gotgctl.d32, 0);
  30162. + DWC_DEBUGPL(DBG_ANY, "HNP Failed\n");
  30163. + __DWC_ERROR("Device Not Connected/Responding\n");
  30164. + }
  30165. + }
  30166. + if (gotgint.b.hstnegdet) {
  30167. + /* The disconnect interrupt is set at the same time as
  30168. + * Host Negotiation Detected. During the mode
  30169. + * switch all interrupts are cleared so the disconnect
  30170. + * interrupt handler will not get executed.
  30171. + */
  30172. + DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: "
  30173. + "Host Negotiation Detected++ (%s)\n",
  30174. + (dwc_otg_is_host_mode(core_if) ? "Host" :
  30175. + "Device"));
  30176. + if (dwc_otg_is_device_mode(core_if)) {
  30177. + DWC_DEBUGPL(DBG_ANY, "a_suspend->a_peripheral (%d)\n",
  30178. + core_if->op_state);
  30179. + DWC_SPINUNLOCK(core_if->lock);
  30180. + cil_hcd_disconnect(core_if);
  30181. + cil_pcd_start(core_if);
  30182. + DWC_SPINLOCK(core_if->lock);
  30183. + core_if->op_state = A_PERIPHERAL;
  30184. + } else {
  30185. + /*
  30186. + * Need to disable SOF interrupt immediately. When
  30187. + * switching from device to host, the PCD interrupt
  30188. + * handler won't handle the interrupt if host mode is
  30189. + * already set. The HCD interrupt handler won't get
  30190. + * called if the HCD state is HALT. This means that
  30191. + * the interrupt does not get handled and Linux
  30192. + * complains loudly.
  30193. + */
  30194. + gintmsk.d32 = 0;
  30195. + gintmsk.b.sofintr = 1;
  30196. + DWC_MODIFY_REG32(&global_regs->gintmsk, gintmsk.d32, 0);
  30197. + DWC_SPINUNLOCK(core_if->lock);
  30198. + cil_pcd_stop(core_if);
  30199. + cil_hcd_start(core_if);
  30200. + DWC_SPINLOCK(core_if->lock);
  30201. + core_if->op_state = A_HOST;
  30202. + }
  30203. + }
  30204. + if (gotgint.b.adevtoutchng) {
  30205. + DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: "
  30206. + "A-Device Timeout Change++\n");
  30207. + }
  30208. + if (gotgint.b.debdone) {
  30209. + DWC_DEBUGPL(DBG_ANY, " ++OTG Interrupt: " "Debounce Done++\n");
  30210. + }
  30211. +
  30212. + /* Clear GOTGINT */
  30213. + DWC_WRITE_REG32(&core_if->core_global_regs->gotgint, gotgint.d32);
  30214. +
  30215. + return 1;
  30216. +}
  30217. +
  30218. +void w_conn_id_status_change(void *p)
  30219. +{
  30220. + dwc_otg_core_if_t *core_if = p;
  30221. + uint32_t count = 0;
  30222. + gotgctl_data_t gotgctl = {.d32 = 0 };
  30223. +
  30224. + gotgctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  30225. + DWC_DEBUGPL(DBG_CIL, "gotgctl=%0x\n", gotgctl.d32);
  30226. + DWC_DEBUGPL(DBG_CIL, "gotgctl.b.conidsts=%d\n", gotgctl.b.conidsts);
  30227. +
  30228. + /* B-Device connector (Device Mode) */
  30229. + if (gotgctl.b.conidsts) {
  30230. + /* Wait for switch to device mode. */
  30231. + while (!dwc_otg_is_device_mode(core_if)) {
  30232. + DWC_PRINTF("Waiting for Peripheral Mode, Mode=%s\n",
  30233. + (dwc_otg_is_host_mode(core_if) ? "Host" :
  30234. + "Peripheral"));
  30235. + dwc_mdelay(100);
  30236. + if (++count > 10000)
  30237. + break;
  30238. + }
  30239. + DWC_ASSERT(++count < 10000,
  30240. + "Connection id status change timed out");
  30241. + core_if->op_state = B_PERIPHERAL;
  30242. + dwc_otg_core_init(core_if);
  30243. + dwc_otg_enable_global_interrupts(core_if);
  30244. + cil_pcd_start(core_if);
  30245. + } else {
  30246. + /* A-Device connector (Host Mode) */
  30247. + while (!dwc_otg_is_host_mode(core_if)) {
  30248. + DWC_PRINTF("Waiting for Host Mode, Mode=%s\n",
  30249. + (dwc_otg_is_host_mode(core_if) ? "Host" :
  30250. + "Peripheral"));
  30251. + dwc_mdelay(100);
  30252. + if (++count > 10000)
  30253. + break;
  30254. + }
  30255. + DWC_ASSERT(++count < 10000,
  30256. + "Connection id status change timed out");
  30257. + core_if->op_state = A_HOST;
  30258. + /*
  30259. + * Initialize the Core for Host mode.
  30260. + */
  30261. + dwc_otg_core_init(core_if);
  30262. + dwc_otg_enable_global_interrupts(core_if);
  30263. + cil_hcd_start(core_if);
  30264. + }
  30265. +}
  30266. +
  30267. +/**
  30268. + * This function handles the Connector ID Status Change Interrupt. It
  30269. + * reads the OTG Interrupt Register (GOTCTL) to determine whether this
  30270. + * is a Device to Host Mode transition or a Host Mode to Device
  30271. + * Transition.
  30272. + *
  30273. + * This only occurs when the cable is connected/removed from the PHY
  30274. + * connector.
  30275. + *
  30276. + * @param core_if Programming view of DWC_otg controller.
  30277. + */
  30278. +int32_t dwc_otg_handle_conn_id_status_change_intr(dwc_otg_core_if_t * core_if)
  30279. +{
  30280. +
  30281. + /*
  30282. + * Need to disable SOF interrupt immediately. If switching from device
  30283. + * to host, the PCD interrupt handler won't handle the interrupt if
  30284. + * host mode is already set. The HCD interrupt handler won't get
  30285. + * called if the HCD state is HALT. This means that the interrupt does
  30286. + * not get handled and Linux complains loudly.
  30287. + */
  30288. + gintmsk_data_t gintmsk = {.d32 = 0 };
  30289. + gintsts_data_t gintsts = {.d32 = 0 };
  30290. +
  30291. + gintmsk.b.sofintr = 1;
  30292. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, gintmsk.d32, 0);
  30293. +
  30294. + DWC_DEBUGPL(DBG_CIL,
  30295. + " ++Connector ID Status Change Interrupt++ (%s)\n",
  30296. + (dwc_otg_is_host_mode(core_if) ? "Host" : "Device"));
  30297. +
  30298. + DWC_SPINUNLOCK(core_if->lock);
  30299. +
  30300. + /*
  30301. + * Need to schedule a work, as there are possible DELAY function calls
  30302. + * Release lock before scheduling workq as it holds spinlock during scheduling
  30303. + */
  30304. +
  30305. + DWC_WORKQ_SCHEDULE(core_if->wq_otg, w_conn_id_status_change,
  30306. + core_if, "connection id status change");
  30307. + DWC_SPINLOCK(core_if->lock);
  30308. +
  30309. + /* Set flag and clear interrupt */
  30310. + gintsts.b.conidstschng = 1;
  30311. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  30312. +
  30313. + return 1;
  30314. +}
  30315. +
  30316. +/**
  30317. + * This interrupt indicates that a device is initiating the Session
  30318. + * Request Protocol to request the host to turn on bus power so a new
  30319. + * session can begin. The handler responds by turning on bus power. If
  30320. + * the DWC_otg controller is in low power mode, the handler brings the
  30321. + * controller out of low power mode before turning on bus power.
  30322. + *
  30323. + * @param core_if Programming view of DWC_otg controller.
  30324. + */
  30325. +int32_t dwc_otg_handle_session_req_intr(dwc_otg_core_if_t * core_if)
  30326. +{
  30327. + gintsts_data_t gintsts;
  30328. +
  30329. +#ifndef DWC_HOST_ONLY
  30330. + DWC_DEBUGPL(DBG_ANY, "++Session Request Interrupt++\n");
  30331. +
  30332. + if (dwc_otg_is_device_mode(core_if)) {
  30333. + DWC_PRINTF("SRP: Device mode\n");
  30334. + } else {
  30335. + hprt0_data_t hprt0;
  30336. + DWC_PRINTF("SRP: Host mode\n");
  30337. +
  30338. + /* Turn on the port power bit. */
  30339. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  30340. + hprt0.b.prtpwr = 1;
  30341. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  30342. +
  30343. + /* Start the Connection timer. So a message can be displayed
  30344. + * if connect does not occur within 10 seconds. */
  30345. + cil_hcd_session_start(core_if);
  30346. + }
  30347. +#endif
  30348. +
  30349. + /* Clear interrupt */
  30350. + gintsts.d32 = 0;
  30351. + gintsts.b.sessreqintr = 1;
  30352. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  30353. +
  30354. + return 1;
  30355. +}
  30356. +
  30357. +void w_wakeup_detected(void *p)
  30358. +{
  30359. + dwc_otg_core_if_t *core_if = (dwc_otg_core_if_t *) p;
  30360. + /*
  30361. + * Clear the Resume after 70ms. (Need 20 ms minimum. Use 70 ms
  30362. + * so that OPT tests pass with all PHYs).
  30363. + */
  30364. + hprt0_data_t hprt0 = {.d32 = 0 };
  30365. +#if 0
  30366. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  30367. + /* Restart the Phy Clock */
  30368. + pcgcctl.b.stoppclk = 1;
  30369. + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
  30370. + dwc_udelay(10);
  30371. +#endif //0
  30372. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  30373. + DWC_DEBUGPL(DBG_ANY, "Resume: HPRT0=%0x\n", hprt0.d32);
  30374. +// dwc_mdelay(70);
  30375. + hprt0.b.prtres = 0; /* Resume */
  30376. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  30377. + DWC_DEBUGPL(DBG_ANY, "Clear Resume: HPRT0=%0x\n",
  30378. + DWC_READ_REG32(core_if->host_if->hprt0));
  30379. +
  30380. + cil_hcd_resume(core_if);
  30381. +
  30382. + /** Change to L0 state*/
  30383. + core_if->lx_state = DWC_OTG_L0;
  30384. +}
  30385. +
  30386. +/**
  30387. + * This interrupt indicates that the DWC_otg controller has detected a
  30388. + * resume or remote wakeup sequence. If the DWC_otg controller is in
  30389. + * low power mode, the handler must brings the controller out of low
  30390. + * power mode. The controller automatically begins resume
  30391. + * signaling. The handler schedules a time to stop resume signaling.
  30392. + */
  30393. +int32_t dwc_otg_handle_wakeup_detected_intr(dwc_otg_core_if_t * core_if)
  30394. +{
  30395. + gintsts_data_t gintsts;
  30396. +
  30397. + DWC_DEBUGPL(DBG_ANY,
  30398. + "++Resume and Remote Wakeup Detected Interrupt++\n");
  30399. +
  30400. + DWC_PRINTF("%s lxstate = %d\n", __func__, core_if->lx_state);
  30401. +
  30402. + if (dwc_otg_is_device_mode(core_if)) {
  30403. + dctl_data_t dctl = {.d32 = 0 };
  30404. + DWC_DEBUGPL(DBG_PCD, "DSTS=0x%0x\n",
  30405. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->
  30406. + dsts));
  30407. + if (core_if->lx_state == DWC_OTG_L2) {
  30408. +#ifdef PARTIAL_POWER_DOWN
  30409. + if (core_if->hwcfg4.b.power_optimiz) {
  30410. + pcgcctl_data_t power = {.d32 = 0 };
  30411. +
  30412. + power.d32 = DWC_READ_REG32(core_if->pcgcctl);
  30413. + DWC_DEBUGPL(DBG_CIL, "PCGCCTL=%0x\n",
  30414. + power.d32);
  30415. +
  30416. + power.b.stoppclk = 0;
  30417. + DWC_WRITE_REG32(core_if->pcgcctl, power.d32);
  30418. +
  30419. + power.b.pwrclmp = 0;
  30420. + DWC_WRITE_REG32(core_if->pcgcctl, power.d32);
  30421. +
  30422. + power.b.rstpdwnmodule = 0;
  30423. + DWC_WRITE_REG32(core_if->pcgcctl, power.d32);
  30424. + }
  30425. +#endif
  30426. + /* Clear the Remote Wakeup Signaling */
  30427. + dctl.b.rmtwkupsig = 1;
  30428. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  30429. + dctl, dctl.d32, 0);
  30430. +
  30431. + DWC_SPINUNLOCK(core_if->lock);
  30432. + if (core_if->pcd_cb && core_if->pcd_cb->resume_wakeup) {
  30433. + core_if->pcd_cb->resume_wakeup(core_if->pcd_cb->p);
  30434. + }
  30435. + DWC_SPINLOCK(core_if->lock);
  30436. + } else {
  30437. + glpmcfg_data_t lpmcfg;
  30438. + lpmcfg.d32 =
  30439. + DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  30440. + lpmcfg.b.hird_thres &= (~(1 << 4));
  30441. + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg,
  30442. + lpmcfg.d32);
  30443. + }
  30444. + /** Change to L0 state*/
  30445. + core_if->lx_state = DWC_OTG_L0;
  30446. + } else {
  30447. + if (core_if->lx_state != DWC_OTG_L1) {
  30448. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  30449. +
  30450. + /* Restart the Phy Clock */
  30451. + pcgcctl.b.stoppclk = 1;
  30452. + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
  30453. + DWC_TIMER_SCHEDULE(core_if->wkp_timer, 71);
  30454. + } else {
  30455. + /** Change to L0 state*/
  30456. + core_if->lx_state = DWC_OTG_L0;
  30457. + }
  30458. + }
  30459. +
  30460. + /* Clear interrupt */
  30461. + gintsts.d32 = 0;
  30462. + gintsts.b.wkupintr = 1;
  30463. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  30464. +
  30465. + return 1;
  30466. +}
  30467. +
  30468. +/**
  30469. + * This interrupt indicates that the Wakeup Logic has detected a
  30470. + * Device disconnect.
  30471. + */
  30472. +static int32_t dwc_otg_handle_pwrdn_disconnect_intr(dwc_otg_core_if_t *core_if)
  30473. +{
  30474. + gpwrdn_data_t gpwrdn = { .d32 = 0 };
  30475. + gpwrdn_data_t gpwrdn_temp = { .d32 = 0 };
  30476. + gpwrdn_temp.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  30477. +
  30478. + DWC_PRINTF("%s called\n", __FUNCTION__);
  30479. +
  30480. + if (!core_if->hibernation_suspend) {
  30481. + DWC_PRINTF("Already exited from Hibernation\n");
  30482. + return 1;
  30483. + }
  30484. +
  30485. + /* Switch on the voltage to the core */
  30486. + gpwrdn.b.pwrdnswtch = 1;
  30487. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30488. + dwc_udelay(10);
  30489. +
  30490. + /* Reset the core */
  30491. + gpwrdn.d32 = 0;
  30492. + gpwrdn.b.pwrdnrstn = 1;
  30493. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30494. + dwc_udelay(10);
  30495. +
  30496. + /* Disable power clamps*/
  30497. + gpwrdn.d32 = 0;
  30498. + gpwrdn.b.pwrdnclmp = 1;
  30499. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30500. +
  30501. + /* Remove reset the core signal */
  30502. + gpwrdn.d32 = 0;
  30503. + gpwrdn.b.pwrdnrstn = 1;
  30504. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  30505. + dwc_udelay(10);
  30506. +
  30507. + /* Disable PMU interrupt */
  30508. + gpwrdn.d32 = 0;
  30509. + gpwrdn.b.pmuintsel = 1;
  30510. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30511. +
  30512. + core_if->hibernation_suspend = 0;
  30513. +
  30514. + /* Disable PMU */
  30515. + gpwrdn.d32 = 0;
  30516. + gpwrdn.b.pmuactv = 1;
  30517. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30518. + dwc_udelay(10);
  30519. +
  30520. + if (gpwrdn_temp.b.idsts) {
  30521. + core_if->op_state = B_PERIPHERAL;
  30522. + dwc_otg_core_init(core_if);
  30523. + dwc_otg_enable_global_interrupts(core_if);
  30524. + cil_pcd_start(core_if);
  30525. + } else {
  30526. + core_if->op_state = A_HOST;
  30527. + dwc_otg_core_init(core_if);
  30528. + dwc_otg_enable_global_interrupts(core_if);
  30529. + cil_hcd_start(core_if);
  30530. + }
  30531. +
  30532. + return 1;
  30533. +}
  30534. +
  30535. +/**
  30536. + * This interrupt indicates that the Wakeup Logic has detected a
  30537. + * remote wakeup sequence.
  30538. + */
  30539. +static int32_t dwc_otg_handle_pwrdn_wakeup_detected_intr(dwc_otg_core_if_t * core_if)
  30540. +{
  30541. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  30542. + DWC_DEBUGPL(DBG_ANY,
  30543. + "++Powerdown Remote Wakeup Detected Interrupt++\n");
  30544. +
  30545. + if (!core_if->hibernation_suspend) {
  30546. + DWC_PRINTF("Already exited from Hibernation\n");
  30547. + return 1;
  30548. + }
  30549. +
  30550. + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  30551. + if (gpwrdn.b.idsts) { // Device Mode
  30552. + if ((core_if->power_down == 2)
  30553. + && (core_if->hibernation_suspend == 1)) {
  30554. + dwc_otg_device_hibernation_restore(core_if, 0, 0);
  30555. + }
  30556. + } else {
  30557. + if ((core_if->power_down == 2)
  30558. + && (core_if->hibernation_suspend == 1)) {
  30559. + dwc_otg_host_hibernation_restore(core_if, 1, 0);
  30560. + }
  30561. + }
  30562. + return 1;
  30563. +}
  30564. +
  30565. +static int32_t dwc_otg_handle_pwrdn_idsts_change(dwc_otg_device_t *otg_dev)
  30566. +{
  30567. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  30568. + gpwrdn_data_t gpwrdn_temp = {.d32 = 0 };
  30569. + dwc_otg_core_if_t *core_if = otg_dev->core_if;
  30570. +
  30571. + DWC_DEBUGPL(DBG_ANY, "%s called\n", __FUNCTION__);
  30572. + gpwrdn_temp.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  30573. + if (core_if->power_down == 2) {
  30574. + if (!core_if->hibernation_suspend) {
  30575. + DWC_PRINTF("Already exited from Hibernation\n");
  30576. + return 1;
  30577. + }
  30578. + DWC_DEBUGPL(DBG_ANY, "Exit from hibernation on ID sts change\n");
  30579. + /* Switch on the voltage to the core */
  30580. + gpwrdn.b.pwrdnswtch = 1;
  30581. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30582. + dwc_udelay(10);
  30583. +
  30584. + /* Reset the core */
  30585. + gpwrdn.d32 = 0;
  30586. + gpwrdn.b.pwrdnrstn = 1;
  30587. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30588. + dwc_udelay(10);
  30589. +
  30590. + /* Disable power clamps */
  30591. + gpwrdn.d32 = 0;
  30592. + gpwrdn.b.pwrdnclmp = 1;
  30593. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30594. +
  30595. + /* Remove reset the core signal */
  30596. + gpwrdn.d32 = 0;
  30597. + gpwrdn.b.pwrdnrstn = 1;
  30598. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  30599. + dwc_udelay(10);
  30600. +
  30601. + /* Disable PMU interrupt */
  30602. + gpwrdn.d32 = 0;
  30603. + gpwrdn.b.pmuintsel = 1;
  30604. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30605. +
  30606. + /*Indicates that we are exiting from hibernation */
  30607. + core_if->hibernation_suspend = 0;
  30608. +
  30609. + /* Disable PMU */
  30610. + gpwrdn.d32 = 0;
  30611. + gpwrdn.b.pmuactv = 1;
  30612. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30613. + dwc_udelay(10);
  30614. +
  30615. + gpwrdn.d32 = core_if->gr_backup->gpwrdn_local;
  30616. + if (gpwrdn.b.dis_vbus == 1) {
  30617. + gpwrdn.d32 = 0;
  30618. + gpwrdn.b.dis_vbus = 1;
  30619. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30620. + }
  30621. +
  30622. + if (gpwrdn_temp.b.idsts) {
  30623. + core_if->op_state = B_PERIPHERAL;
  30624. + dwc_otg_core_init(core_if);
  30625. + dwc_otg_enable_global_interrupts(core_if);
  30626. + cil_pcd_start(core_if);
  30627. + } else {
  30628. + core_if->op_state = A_HOST;
  30629. + dwc_otg_core_init(core_if);
  30630. + dwc_otg_enable_global_interrupts(core_if);
  30631. + cil_hcd_start(core_if);
  30632. + }
  30633. + }
  30634. +
  30635. + if (core_if->adp_enable) {
  30636. + uint8_t is_host = 0;
  30637. + DWC_SPINUNLOCK(core_if->lock);
  30638. + /* Change the core_if's lock to hcd/pcd lock depend on mode? */
  30639. +#ifndef DWC_HOST_ONLY
  30640. + if (gpwrdn_temp.b.idsts)
  30641. + core_if->lock = otg_dev->pcd->lock;
  30642. +#endif
  30643. +#ifndef DWC_DEVICE_ONLY
  30644. + if (!gpwrdn_temp.b.idsts) {
  30645. + core_if->lock = otg_dev->hcd->lock;
  30646. + is_host = 1;
  30647. + }
  30648. +#endif
  30649. + DWC_PRINTF("RESTART ADP\n");
  30650. + if (core_if->adp.probe_enabled)
  30651. + dwc_otg_adp_probe_stop(core_if);
  30652. + if (core_if->adp.sense_enabled)
  30653. + dwc_otg_adp_sense_stop(core_if);
  30654. + if (core_if->adp.sense_timer_started)
  30655. + DWC_TIMER_CANCEL(core_if->adp.sense_timer);
  30656. + if (core_if->adp.vbuson_timer_started)
  30657. + DWC_TIMER_CANCEL(core_if->adp.vbuson_timer);
  30658. + core_if->adp.probe_timer_values[0] = -1;
  30659. + core_if->adp.probe_timer_values[1] = -1;
  30660. + core_if->adp.sense_timer_started = 0;
  30661. + core_if->adp.vbuson_timer_started = 0;
  30662. + core_if->adp.probe_counter = 0;
  30663. + core_if->adp.gpwrdn = 0;
  30664. +
  30665. + /* Disable PMU and restart ADP */
  30666. + gpwrdn_temp.d32 = 0;
  30667. + gpwrdn_temp.b.pmuactv = 1;
  30668. + gpwrdn_temp.b.pmuintsel = 1;
  30669. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30670. + DWC_PRINTF("Check point 1\n");
  30671. + dwc_mdelay(110);
  30672. + dwc_otg_adp_start(core_if, is_host);
  30673. + DWC_SPINLOCK(core_if->lock);
  30674. + }
  30675. +
  30676. +
  30677. + return 1;
  30678. +}
  30679. +
  30680. +static int32_t dwc_otg_handle_pwrdn_session_change(dwc_otg_core_if_t * core_if)
  30681. +{
  30682. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  30683. + int32_t otg_cap_param = core_if->core_params->otg_cap;
  30684. + DWC_DEBUGPL(DBG_ANY, "%s called\n", __FUNCTION__);
  30685. +
  30686. + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  30687. + if (core_if->power_down == 2) {
  30688. + if (!core_if->hibernation_suspend) {
  30689. + DWC_PRINTF("Already exited from Hibernation\n");
  30690. + return 1;
  30691. + }
  30692. +
  30693. + if ((otg_cap_param != DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE ||
  30694. + otg_cap_param != DWC_OTG_CAP_PARAM_SRP_ONLY_CAPABLE) &&
  30695. + gpwrdn.b.bsessvld == 0) {
  30696. + /* Save gpwrdn register for further usage if stschng interrupt */
  30697. + core_if->gr_backup->gpwrdn_local =
  30698. + DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  30699. + /*Exit from ISR and wait for stschng interrupt with bsessvld = 1 */
  30700. + return 1;
  30701. + }
  30702. +
  30703. + /* Switch on the voltage to the core */
  30704. + gpwrdn.d32 = 0;
  30705. + gpwrdn.b.pwrdnswtch = 1;
  30706. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30707. + dwc_udelay(10);
  30708. +
  30709. + /* Reset the core */
  30710. + gpwrdn.d32 = 0;
  30711. + gpwrdn.b.pwrdnrstn = 1;
  30712. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30713. + dwc_udelay(10);
  30714. +
  30715. + /* Disable power clamps */
  30716. + gpwrdn.d32 = 0;
  30717. + gpwrdn.b.pwrdnclmp = 1;
  30718. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30719. +
  30720. + /* Remove reset the core signal */
  30721. + gpwrdn.d32 = 0;
  30722. + gpwrdn.b.pwrdnrstn = 1;
  30723. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  30724. + dwc_udelay(10);
  30725. +
  30726. + /* Disable PMU interrupt */
  30727. + gpwrdn.d32 = 0;
  30728. + gpwrdn.b.pmuintsel = 1;
  30729. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30730. + dwc_udelay(10);
  30731. +
  30732. + /*Indicates that we are exiting from hibernation */
  30733. + core_if->hibernation_suspend = 0;
  30734. +
  30735. + /* Disable PMU */
  30736. + gpwrdn.d32 = 0;
  30737. + gpwrdn.b.pmuactv = 1;
  30738. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30739. + dwc_udelay(10);
  30740. +
  30741. + core_if->op_state = B_PERIPHERAL;
  30742. + dwc_otg_core_init(core_if);
  30743. + dwc_otg_enable_global_interrupts(core_if);
  30744. + cil_pcd_start(core_if);
  30745. +
  30746. + if (otg_cap_param == DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE ||
  30747. + otg_cap_param == DWC_OTG_CAP_PARAM_SRP_ONLY_CAPABLE) {
  30748. + /*
  30749. + * Initiate SRP after initial ADP probe.
  30750. + */
  30751. + dwc_otg_initiate_srp(core_if);
  30752. + }
  30753. + }
  30754. +
  30755. + return 1;
  30756. +}
  30757. +/**
  30758. + * This interrupt indicates that the Wakeup Logic has detected a
  30759. + * status change either on IDDIG or BSessVld.
  30760. + */
  30761. +static uint32_t dwc_otg_handle_pwrdn_stschng_intr(dwc_otg_device_t *otg_dev)
  30762. +{
  30763. + int retval;
  30764. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  30765. + gpwrdn_data_t gpwrdn_temp = {.d32 = 0 };
  30766. + dwc_otg_core_if_t *core_if = otg_dev->core_if;
  30767. +
  30768. + DWC_PRINTF("%s called\n", __FUNCTION__);
  30769. +
  30770. + if (core_if->power_down == 2) {
  30771. + if (core_if->hibernation_suspend <= 0) {
  30772. + DWC_PRINTF("Already exited from Hibernation\n");
  30773. + return 1;
  30774. + } else
  30775. + gpwrdn_temp.d32 = core_if->gr_backup->gpwrdn_local;
  30776. +
  30777. + } else {
  30778. + gpwrdn_temp.d32 = core_if->adp.gpwrdn;
  30779. + }
  30780. +
  30781. + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  30782. +
  30783. + if (gpwrdn.b.idsts ^ gpwrdn_temp.b.idsts) {
  30784. + retval = dwc_otg_handle_pwrdn_idsts_change(otg_dev);
  30785. + } else if (gpwrdn.b.bsessvld ^ gpwrdn_temp.b.bsessvld) {
  30786. + retval = dwc_otg_handle_pwrdn_session_change(core_if);
  30787. + }
  30788. +
  30789. + return retval;
  30790. +}
  30791. +
  30792. +/**
  30793. + * This interrupt indicates that the Wakeup Logic has detected a
  30794. + * SRP.
  30795. + */
  30796. +static int32_t dwc_otg_handle_pwrdn_srp_intr(dwc_otg_core_if_t * core_if)
  30797. +{
  30798. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  30799. +
  30800. + DWC_PRINTF("%s called\n", __FUNCTION__);
  30801. +
  30802. + if (!core_if->hibernation_suspend) {
  30803. + DWC_PRINTF("Already exited from Hibernation\n");
  30804. + return 1;
  30805. + }
  30806. +#ifdef DWC_DEV_SRPCAP
  30807. + if (core_if->pwron_timer_started) {
  30808. + core_if->pwron_timer_started = 0;
  30809. + DWC_TIMER_CANCEL(core_if->pwron_timer);
  30810. + }
  30811. +#endif
  30812. +
  30813. + /* Switch on the voltage to the core */
  30814. + gpwrdn.b.pwrdnswtch = 1;
  30815. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30816. + dwc_udelay(10);
  30817. +
  30818. + /* Reset the core */
  30819. + gpwrdn.d32 = 0;
  30820. + gpwrdn.b.pwrdnrstn = 1;
  30821. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30822. + dwc_udelay(10);
  30823. +
  30824. + /* Disable power clamps */
  30825. + gpwrdn.d32 = 0;
  30826. + gpwrdn.b.pwrdnclmp = 1;
  30827. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30828. +
  30829. + /* Remove reset the core signal */
  30830. + gpwrdn.d32 = 0;
  30831. + gpwrdn.b.pwrdnrstn = 1;
  30832. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  30833. + dwc_udelay(10);
  30834. +
  30835. + /* Disable PMU interrupt */
  30836. + gpwrdn.d32 = 0;
  30837. + gpwrdn.b.pmuintsel = 1;
  30838. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30839. +
  30840. + /* Indicates that we are exiting from hibernation */
  30841. + core_if->hibernation_suspend = 0;
  30842. +
  30843. + /* Disable PMU */
  30844. + gpwrdn.d32 = 0;
  30845. + gpwrdn.b.pmuactv = 1;
  30846. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30847. + dwc_udelay(10);
  30848. +
  30849. + /* Programm Disable VBUS to 0 */
  30850. + gpwrdn.d32 = 0;
  30851. + gpwrdn.b.dis_vbus = 1;
  30852. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  30853. +
  30854. + /*Initialize the core as Host */
  30855. + core_if->op_state = A_HOST;
  30856. + dwc_otg_core_init(core_if);
  30857. + dwc_otg_enable_global_interrupts(core_if);
  30858. + cil_hcd_start(core_if);
  30859. +
  30860. + return 1;
  30861. +}
  30862. +
  30863. +/** This interrupt indicates that restore command after Hibernation
  30864. + * was completed by the core. */
  30865. +int32_t dwc_otg_handle_restore_done_intr(dwc_otg_core_if_t * core_if)
  30866. +{
  30867. + pcgcctl_data_t pcgcctl;
  30868. + DWC_DEBUGPL(DBG_ANY, "++Restore Done Interrupt++\n");
  30869. +
  30870. + //TODO De-assert restore signal. 8.a
  30871. + pcgcctl.d32 = DWC_READ_REG32(core_if->pcgcctl);
  30872. + if (pcgcctl.b.restoremode == 1) {
  30873. + gintmsk_data_t gintmsk = {.d32 = 0 };
  30874. + /*
  30875. + * If restore mode is Remote Wakeup,
  30876. + * unmask Remote Wakeup interrupt.
  30877. + */
  30878. + gintmsk.b.wkupintr = 1;
  30879. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk,
  30880. + 0, gintmsk.d32);
  30881. + }
  30882. +
  30883. + return 1;
  30884. +}
  30885. +
  30886. +/**
  30887. + * This interrupt indicates that a device has been disconnected from
  30888. + * the root port.
  30889. + */
  30890. +int32_t dwc_otg_handle_disconnect_intr(dwc_otg_core_if_t * core_if)
  30891. +{
  30892. + gintsts_data_t gintsts;
  30893. +
  30894. + DWC_DEBUGPL(DBG_ANY, "++Disconnect Detected Interrupt++ (%s) %s\n",
  30895. + (dwc_otg_is_host_mode(core_if) ? "Host" : "Device"),
  30896. + op_state_str(core_if));
  30897. +
  30898. +/** @todo Consolidate this if statement. */
  30899. +#ifndef DWC_HOST_ONLY
  30900. + if (core_if->op_state == B_HOST) {
  30901. + /* If in device mode Disconnect and stop the HCD, then
  30902. + * start the PCD. */
  30903. + DWC_SPINUNLOCK(core_if->lock);
  30904. + cil_hcd_disconnect(core_if);
  30905. + cil_pcd_start(core_if);
  30906. + DWC_SPINLOCK(core_if->lock);
  30907. + core_if->op_state = B_PERIPHERAL;
  30908. + } else if (dwc_otg_is_device_mode(core_if)) {
  30909. + gotgctl_data_t gotgctl = {.d32 = 0 };
  30910. + gotgctl.d32 =
  30911. + DWC_READ_REG32(&core_if->core_global_regs->gotgctl);
  30912. + if (gotgctl.b.hstsethnpen == 1) {
  30913. + /* Do nothing, if HNP in process the OTG
  30914. + * interrupt "Host Negotiation Detected"
  30915. + * interrupt will do the mode switch.
  30916. + */
  30917. + } else if (gotgctl.b.devhnpen == 0) {
  30918. + /* If in device mode Disconnect and stop the HCD, then
  30919. + * start the PCD. */
  30920. + DWC_SPINUNLOCK(core_if->lock);
  30921. + cil_hcd_disconnect(core_if);
  30922. + cil_pcd_start(core_if);
  30923. + DWC_SPINLOCK(core_if->lock);
  30924. + core_if->op_state = B_PERIPHERAL;
  30925. + } else {
  30926. + DWC_DEBUGPL(DBG_ANY, "!a_peripheral && !devhnpen\n");
  30927. + }
  30928. + } else {
  30929. + if (core_if->op_state == A_HOST) {
  30930. + /* A-Cable still connected but device disconnected. */
  30931. + cil_hcd_disconnect(core_if);
  30932. + if (core_if->adp_enable) {
  30933. + gpwrdn_data_t gpwrdn = { .d32 = 0 };
  30934. + cil_hcd_stop(core_if);
  30935. + /* Enable Power Down Logic */
  30936. + gpwrdn.b.pmuintsel = 1;
  30937. + gpwrdn.b.pmuactv = 1;
  30938. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  30939. + gpwrdn, 0, gpwrdn.d32);
  30940. + dwc_otg_adp_probe_start(core_if);
  30941. +
  30942. + /* Power off the core */
  30943. + if (core_if->power_down == 2) {
  30944. + gpwrdn.d32 = 0;
  30945. + gpwrdn.b.pwrdnswtch = 1;
  30946. + DWC_MODIFY_REG32
  30947. + (&core_if->core_global_regs->gpwrdn,
  30948. + gpwrdn.d32, 0);
  30949. + }
  30950. + }
  30951. + }
  30952. + }
  30953. +#endif
  30954. + /* Change to L3(OFF) state */
  30955. + core_if->lx_state = DWC_OTG_L3;
  30956. +
  30957. + gintsts.d32 = 0;
  30958. + gintsts.b.disconnect = 1;
  30959. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  30960. + return 1;
  30961. +}
  30962. +
  30963. +/**
  30964. + * This interrupt indicates that SUSPEND state has been detected on
  30965. + * the USB.
  30966. + *
  30967. + * For HNP the USB Suspend interrupt signals the change from
  30968. + * "a_peripheral" to "a_host".
  30969. + *
  30970. + * When power management is enabled the core will be put in low power
  30971. + * mode.
  30972. + */
  30973. +int32_t dwc_otg_handle_usb_suspend_intr(dwc_otg_core_if_t * core_if)
  30974. +{
  30975. + dsts_data_t dsts;
  30976. + gintsts_data_t gintsts;
  30977. + dcfg_data_t dcfg;
  30978. +
  30979. + DWC_DEBUGPL(DBG_ANY, "USB SUSPEND\n");
  30980. +
  30981. + if (dwc_otg_is_device_mode(core_if)) {
  30982. + /* Check the Device status register to determine if the Suspend
  30983. + * state is active. */
  30984. + dsts.d32 =
  30985. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  30986. + DWC_DEBUGPL(DBG_PCD, "DSTS=0x%0x\n", dsts.d32);
  30987. + DWC_DEBUGPL(DBG_PCD, "DSTS.Suspend Status=%d "
  30988. + "HWCFG4.power Optimize=%d\n",
  30989. + dsts.b.suspsts, core_if->hwcfg4.b.power_optimiz);
  30990. +
  30991. +#ifdef PARTIAL_POWER_DOWN
  30992. +/** @todo Add a module parameter for power management. */
  30993. +
  30994. + if (dsts.b.suspsts && core_if->hwcfg4.b.power_optimiz) {
  30995. + pcgcctl_data_t power = {.d32 = 0 };
  30996. + DWC_DEBUGPL(DBG_CIL, "suspend\n");
  30997. +
  30998. + power.b.pwrclmp = 1;
  30999. + DWC_WRITE_REG32(core_if->pcgcctl, power.d32);
  31000. +
  31001. + power.b.rstpdwnmodule = 1;
  31002. + DWC_MODIFY_REG32(core_if->pcgcctl, 0, power.d32);
  31003. +
  31004. + power.b.stoppclk = 1;
  31005. + DWC_MODIFY_REG32(core_if->pcgcctl, 0, power.d32);
  31006. +
  31007. + } else {
  31008. + DWC_DEBUGPL(DBG_ANY, "disconnect?\n");
  31009. + }
  31010. +#endif
  31011. + /* PCD callback for suspend. Release the lock inside of callback function */
  31012. + cil_pcd_suspend(core_if);
  31013. + if (core_if->power_down == 2)
  31014. + {
  31015. + dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  31016. + DWC_DEBUGPL(DBG_ANY,"lx_state = %08x\n",core_if->lx_state);
  31017. + DWC_DEBUGPL(DBG_ANY," device address = %08d\n",dcfg.b.devaddr);
  31018. +
  31019. + if (core_if->lx_state != DWC_OTG_L3 && dcfg.b.devaddr) {
  31020. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  31021. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  31022. + gusbcfg_data_t gusbcfg = {.d32 = 0 };
  31023. +
  31024. + /* Change to L2(suspend) state */
  31025. + core_if->lx_state = DWC_OTG_L2;
  31026. +
  31027. + /* Clear interrupt in gintsts */
  31028. + gintsts.d32 = 0;
  31029. + gintsts.b.usbsuspend = 1;
  31030. + DWC_WRITE_REG32(&core_if->core_global_regs->
  31031. + gintsts, gintsts.d32);
  31032. + DWC_PRINTF("Start of hibernation completed\n");
  31033. + dwc_otg_save_global_regs(core_if);
  31034. + dwc_otg_save_dev_regs(core_if);
  31035. +
  31036. + gusbcfg.d32 =
  31037. + DWC_READ_REG32(&core_if->core_global_regs->
  31038. + gusbcfg);
  31039. + if (gusbcfg.b.ulpi_utmi_sel == 1) {
  31040. + /* ULPI interface */
  31041. + /* Suspend the Phy Clock */
  31042. + pcgcctl.d32 = 0;
  31043. + pcgcctl.b.stoppclk = 1;
  31044. + DWC_MODIFY_REG32(core_if->pcgcctl, 0,
  31045. + pcgcctl.d32);
  31046. + dwc_udelay(10);
  31047. + gpwrdn.b.pmuactv = 1;
  31048. + DWC_MODIFY_REG32(&core_if->
  31049. + core_global_regs->
  31050. + gpwrdn, 0, gpwrdn.d32);
  31051. + } else {
  31052. + /* UTMI+ Interface */
  31053. + gpwrdn.b.pmuactv = 1;
  31054. + DWC_MODIFY_REG32(&core_if->
  31055. + core_global_regs->
  31056. + gpwrdn, 0, gpwrdn.d32);
  31057. + dwc_udelay(10);
  31058. + pcgcctl.b.stoppclk = 1;
  31059. + DWC_MODIFY_REG32(core_if->pcgcctl, 0,
  31060. + pcgcctl.d32);
  31061. + dwc_udelay(10);
  31062. + }
  31063. +
  31064. + /* Set flag to indicate that we are in hibernation */
  31065. + core_if->hibernation_suspend = 1;
  31066. + /* Enable interrupts from wake up logic */
  31067. + gpwrdn.d32 = 0;
  31068. + gpwrdn.b.pmuintsel = 1;
  31069. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  31070. + gpwrdn, 0, gpwrdn.d32);
  31071. + dwc_udelay(10);
  31072. +
  31073. + /* Unmask device mode interrupts in GPWRDN */
  31074. + gpwrdn.d32 = 0;
  31075. + gpwrdn.b.rst_det_msk = 1;
  31076. + gpwrdn.b.lnstchng_msk = 1;
  31077. + gpwrdn.b.sts_chngint_msk = 1;
  31078. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  31079. + gpwrdn, 0, gpwrdn.d32);
  31080. + dwc_udelay(10);
  31081. +
  31082. + /* Enable Power Down Clamp */
  31083. + gpwrdn.d32 = 0;
  31084. + gpwrdn.b.pwrdnclmp = 1;
  31085. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  31086. + gpwrdn, 0, gpwrdn.d32);
  31087. + dwc_udelay(10);
  31088. +
  31089. + /* Switch off VDD */
  31090. + gpwrdn.d32 = 0;
  31091. + gpwrdn.b.pwrdnswtch = 1;
  31092. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  31093. + gpwrdn, 0, gpwrdn.d32);
  31094. +
  31095. + /* Save gpwrdn register for further usage if stschng interrupt */
  31096. + core_if->gr_backup->gpwrdn_local =
  31097. + DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  31098. + DWC_PRINTF("Hibernation completed\n");
  31099. +
  31100. + return 1;
  31101. + }
  31102. + } else if (core_if->power_down == 3) {
  31103. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  31104. + dcfg.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dcfg);
  31105. + DWC_DEBUGPL(DBG_ANY, "lx_state = %08x\n",core_if->lx_state);
  31106. + DWC_DEBUGPL(DBG_ANY, " device address = %08d\n",dcfg.b.devaddr);
  31107. +
  31108. + if (core_if->lx_state != DWC_OTG_L3 && dcfg.b.devaddr) {
  31109. + DWC_DEBUGPL(DBG_ANY, "Start entering to extended hibernation\n");
  31110. + core_if->xhib = 1;
  31111. +
  31112. + /* Clear interrupt in gintsts */
  31113. + gintsts.d32 = 0;
  31114. + gintsts.b.usbsuspend = 1;
  31115. + DWC_WRITE_REG32(&core_if->core_global_regs->
  31116. + gintsts, gintsts.d32);
  31117. +
  31118. + dwc_otg_save_global_regs(core_if);
  31119. + dwc_otg_save_dev_regs(core_if);
  31120. +
  31121. + /* Wait for 10 PHY clocks */
  31122. + dwc_udelay(10);
  31123. +
  31124. + /* Program GPIO register while entering to xHib */
  31125. + DWC_WRITE_REG32(&core_if->core_global_regs->ggpio, 0x1);
  31126. +
  31127. + pcgcctl.b.enbl_extnd_hiber = 1;
  31128. + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
  31129. + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
  31130. +
  31131. + pcgcctl.d32 = 0;
  31132. + pcgcctl.b.extnd_hiber_pwrclmp = 1;
  31133. + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
  31134. +
  31135. + pcgcctl.d32 = 0;
  31136. + pcgcctl.b.extnd_hiber_switch = 1;
  31137. + core_if->gr_backup->xhib_gpwrdn = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  31138. + core_if->gr_backup->xhib_pcgcctl = DWC_READ_REG32(core_if->pcgcctl) | pcgcctl.d32;
  31139. + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
  31140. +
  31141. + DWC_DEBUGPL(DBG_ANY, "Finished entering to extended hibernation\n");
  31142. +
  31143. + return 1;
  31144. + }
  31145. + }
  31146. + } else {
  31147. + if (core_if->op_state == A_PERIPHERAL) {
  31148. + DWC_DEBUGPL(DBG_ANY, "a_peripheral->a_host\n");
  31149. + /* Clear the a_peripheral flag, back to a_host. */
  31150. + DWC_SPINUNLOCK(core_if->lock);
  31151. + cil_pcd_stop(core_if);
  31152. + cil_hcd_start(core_if);
  31153. + DWC_SPINLOCK(core_if->lock);
  31154. + core_if->op_state = A_HOST;
  31155. + }
  31156. + }
  31157. +
  31158. + /* Change to L2(suspend) state */
  31159. + core_if->lx_state = DWC_OTG_L2;
  31160. +
  31161. + /* Clear interrupt */
  31162. + gintsts.d32 = 0;
  31163. + gintsts.b.usbsuspend = 1;
  31164. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  31165. +
  31166. + return 1;
  31167. +}
  31168. +
  31169. +static int32_t dwc_otg_handle_xhib_exit_intr(dwc_otg_core_if_t * core_if)
  31170. +{
  31171. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  31172. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  31173. + gahbcfg_data_t gahbcfg = {.d32 = 0 };
  31174. +
  31175. + dwc_udelay(10);
  31176. +
  31177. + /* Program GPIO register while entering to xHib */
  31178. + DWC_WRITE_REG32(&core_if->core_global_regs->ggpio, 0x0);
  31179. +
  31180. + pcgcctl.d32 = core_if->gr_backup->xhib_pcgcctl;
  31181. + pcgcctl.b.extnd_hiber_pwrclmp = 0;
  31182. + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
  31183. + dwc_udelay(10);
  31184. +
  31185. + gpwrdn.d32 = core_if->gr_backup->xhib_gpwrdn;
  31186. + gpwrdn.b.restore = 1;
  31187. + DWC_WRITE_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32);
  31188. + dwc_udelay(10);
  31189. +
  31190. + restore_lpm_i2c_regs(core_if);
  31191. +
  31192. + pcgcctl.d32 = core_if->gr_backup->pcgcctl_local & (0x3FFFF << 14);
  31193. + pcgcctl.b.max_xcvrselect = 1;
  31194. + pcgcctl.b.ess_reg_restored = 0;
  31195. + pcgcctl.b.extnd_hiber_switch = 0;
  31196. + pcgcctl.b.extnd_hiber_pwrclmp = 0;
  31197. + pcgcctl.b.enbl_extnd_hiber = 1;
  31198. + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
  31199. +
  31200. + gahbcfg.d32 = core_if->gr_backup->gahbcfg_local;
  31201. + gahbcfg.b.glblintrmsk = 1;
  31202. + DWC_WRITE_REG32(&core_if->core_global_regs->gahbcfg, gahbcfg.d32);
  31203. +
  31204. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, 0xFFFFFFFF);
  31205. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, 0x1 << 16);
  31206. +
  31207. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg,
  31208. + core_if->gr_backup->gusbcfg_local);
  31209. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg,
  31210. + core_if->dr_backup->dcfg);
  31211. +
  31212. + pcgcctl.d32 = 0;
  31213. + pcgcctl.d32 = core_if->gr_backup->pcgcctl_local & (0x3FFFF << 14);
  31214. + pcgcctl.b.max_xcvrselect = 1;
  31215. + pcgcctl.d32 |= 0x608;
  31216. + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
  31217. + dwc_udelay(10);
  31218. +
  31219. + pcgcctl.d32 = 0;
  31220. + pcgcctl.d32 = core_if->gr_backup->pcgcctl_local & (0x3FFFF << 14);
  31221. + pcgcctl.b.max_xcvrselect = 1;
  31222. + pcgcctl.b.ess_reg_restored = 1;
  31223. + pcgcctl.b.enbl_extnd_hiber = 1;
  31224. + pcgcctl.b.rstpdwnmodule = 1;
  31225. + pcgcctl.b.restoremode = 1;
  31226. + DWC_WRITE_REG32(core_if->pcgcctl, pcgcctl.d32);
  31227. +
  31228. + DWC_DEBUGPL(DBG_ANY, "%s called\n", __FUNCTION__);
  31229. +
  31230. + return 1;
  31231. +}
  31232. +
  31233. +#ifdef CONFIG_USB_DWC_OTG_LPM
  31234. +/**
  31235. + * This function hadles LPM transaction received interrupt.
  31236. + */
  31237. +static int32_t dwc_otg_handle_lpm_intr(dwc_otg_core_if_t * core_if)
  31238. +{
  31239. + glpmcfg_data_t lpmcfg;
  31240. + gintsts_data_t gintsts;
  31241. +
  31242. + if (!core_if->core_params->lpm_enable) {
  31243. + DWC_PRINTF("Unexpected LPM interrupt\n");
  31244. + }
  31245. +
  31246. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  31247. + DWC_PRINTF("LPM config register = 0x%08x\n", lpmcfg.d32);
  31248. +
  31249. + if (dwc_otg_is_host_mode(core_if)) {
  31250. + cil_hcd_sleep(core_if);
  31251. + } else {
  31252. + lpmcfg.b.hird_thres |= (1 << 4);
  31253. + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg,
  31254. + lpmcfg.d32);
  31255. + }
  31256. +
  31257. + /* Examine prt_sleep_sts after TL1TokenTetry period max (10 us) */
  31258. + dwc_udelay(10);
  31259. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  31260. + if (lpmcfg.b.prt_sleep_sts) {
  31261. + /* Save the current state */
  31262. + core_if->lx_state = DWC_OTG_L1;
  31263. + }
  31264. +
  31265. + /* Clear interrupt */
  31266. + gintsts.d32 = 0;
  31267. + gintsts.b.lpmtranrcvd = 1;
  31268. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  31269. + return 1;
  31270. +}
  31271. +#endif /* CONFIG_USB_DWC_OTG_LPM */
  31272. +
  31273. +/**
  31274. + * This function returns the Core Interrupt register.
  31275. + */
  31276. +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)
  31277. +{
  31278. + gahbcfg_data_t gahbcfg = {.d32 = 0 };
  31279. + gintsts_data_t gintsts;
  31280. + gintmsk_data_t gintmsk;
  31281. + gintmsk_data_t gintmsk_common = {.d32 = 0 };
  31282. + gintmsk_common.b.wkupintr = 1;
  31283. + gintmsk_common.b.sessreqintr = 1;
  31284. + gintmsk_common.b.conidstschng = 1;
  31285. + gintmsk_common.b.otgintr = 1;
  31286. + gintmsk_common.b.modemismatch = 1;
  31287. + gintmsk_common.b.disconnect = 1;
  31288. + gintmsk_common.b.usbsuspend = 1;
  31289. +#ifdef CONFIG_USB_DWC_OTG_LPM
  31290. + gintmsk_common.b.lpmtranrcvd = 1;
  31291. +#endif
  31292. + gintmsk_common.b.restoredone = 1;
  31293. + if(dwc_otg_is_device_mode(core_if))
  31294. + {
  31295. + /** @todo: The port interrupt occurs while in device
  31296. + * mode. Added code to CIL to clear the interrupt for now!
  31297. + */
  31298. + gintmsk_common.b.portintr = 1;
  31299. + }
  31300. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  31301. + gintmsk.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
  31302. + if(fiq_enable) {
  31303. + local_fiq_disable();
  31304. + /* Pull in the interrupts that the FIQ has masked */
  31305. + gintmsk.d32 |= ~(hcd->fiq_state->gintmsk_saved.d32);
  31306. + gintmsk.d32 |= gintmsk_common.d32;
  31307. + /* for the upstairs function to reenable - have to read it here in case FIQ triggers again */
  31308. + reenable_gintmsk->d32 = gintmsk.d32;
  31309. + local_fiq_enable();
  31310. + }
  31311. +
  31312. + gahbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gahbcfg);
  31313. +
  31314. +#ifdef DEBUG
  31315. + /* if any common interrupts set */
  31316. + if (gintsts.d32 & gintmsk_common.d32) {
  31317. + DWC_DEBUGPL(DBG_ANY, "common_intr: gintsts=%08x gintmsk=%08x\n",
  31318. + gintsts.d32, gintmsk.d32);
  31319. + }
  31320. +#endif
  31321. + if (!fiq_enable){
  31322. + if (gahbcfg.b.glblintrmsk)
  31323. + return ((gintsts.d32 & gintmsk.d32) & gintmsk_common.d32);
  31324. + else
  31325. + return 0;
  31326. + } else {
  31327. + /* Our IRQ kicker is no longer the USB hardware, it's the MPHI interface.
  31328. + * Can't trust the global interrupt mask bit in this case.
  31329. + */
  31330. + return ((gintsts.d32 & gintmsk.d32) & gintmsk_common.d32);
  31331. + }
  31332. +
  31333. +}
  31334. +
  31335. +/* MACRO for clearing interupt bits in GPWRDN register */
  31336. +#define CLEAR_GPWRDN_INTR(__core_if,__intr) \
  31337. +do { \
  31338. + gpwrdn_data_t gpwrdn = {.d32=0}; \
  31339. + gpwrdn.b.__intr = 1; \
  31340. + DWC_MODIFY_REG32(&__core_if->core_global_regs->gpwrdn, \
  31341. + 0, gpwrdn.d32); \
  31342. +} while (0)
  31343. +
  31344. +/**
  31345. + * Common interrupt handler.
  31346. + *
  31347. + * The common interrupts are those that occur in both Host and Device mode.
  31348. + * This handler handles the following interrupts:
  31349. + * - Mode Mismatch Interrupt
  31350. + * - Disconnect Interrupt
  31351. + * - OTG Interrupt
  31352. + * - Connector ID Status Change Interrupt
  31353. + * - Session Request Interrupt.
  31354. + * - Resume / Remote Wakeup Detected Interrupt.
  31355. + * - LPM Transaction Received Interrupt
  31356. + * - ADP Transaction Received Interrupt
  31357. + *
  31358. + */
  31359. +int32_t dwc_otg_handle_common_intr(void *dev)
  31360. +{
  31361. + int retval = 0;
  31362. + gintsts_data_t gintsts;
  31363. + gintmsk_data_t gintmsk_reenable = { .d32 = 0 };
  31364. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  31365. + dwc_otg_device_t *otg_dev = dev;
  31366. + dwc_otg_core_if_t *core_if = otg_dev->core_if;
  31367. + gpwrdn.d32 = DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  31368. + if (dwc_otg_is_device_mode(core_if))
  31369. + core_if->frame_num = dwc_otg_get_frame_number(core_if);
  31370. +
  31371. + if (core_if->lock)
  31372. + DWC_SPINLOCK(core_if->lock);
  31373. +
  31374. + if (core_if->power_down == 3 && core_if->xhib == 1) {
  31375. + DWC_DEBUGPL(DBG_ANY, "Exiting from xHIB state\n");
  31376. + retval |= dwc_otg_handle_xhib_exit_intr(core_if);
  31377. + core_if->xhib = 2;
  31378. + if (core_if->lock)
  31379. + DWC_SPINUNLOCK(core_if->lock);
  31380. +
  31381. + return retval;
  31382. + }
  31383. +
  31384. + if (core_if->hibernation_suspend <= 0) {
  31385. + /* read_common will have to poke the FIQ's saved mask. We must then clear this mask at the end
  31386. + * of this handler - god only knows why it's done like this
  31387. + */
  31388. + gintsts.d32 = dwc_otg_read_common_intr(core_if, &gintmsk_reenable, otg_dev->hcd);
  31389. +
  31390. + if (gintsts.b.modemismatch) {
  31391. + retval |= dwc_otg_handle_mode_mismatch_intr(core_if);
  31392. + }
  31393. + if (gintsts.b.otgintr) {
  31394. + retval |= dwc_otg_handle_otg_intr(core_if);
  31395. + }
  31396. + if (gintsts.b.conidstschng) {
  31397. + retval |=
  31398. + dwc_otg_handle_conn_id_status_change_intr(core_if);
  31399. + }
  31400. + if (gintsts.b.disconnect) {
  31401. + retval |= dwc_otg_handle_disconnect_intr(core_if);
  31402. + }
  31403. + if (gintsts.b.sessreqintr) {
  31404. + retval |= dwc_otg_handle_session_req_intr(core_if);
  31405. + }
  31406. + if (gintsts.b.wkupintr) {
  31407. + retval |= dwc_otg_handle_wakeup_detected_intr(core_if);
  31408. + }
  31409. + if (gintsts.b.usbsuspend) {
  31410. + retval |= dwc_otg_handle_usb_suspend_intr(core_if);
  31411. + }
  31412. +#ifdef CONFIG_USB_DWC_OTG_LPM
  31413. + if (gintsts.b.lpmtranrcvd) {
  31414. + retval |= dwc_otg_handle_lpm_intr(core_if);
  31415. + }
  31416. +#endif
  31417. + if (gintsts.b.restoredone) {
  31418. + gintsts.d32 = 0;
  31419. + if (core_if->power_down == 2)
  31420. + core_if->hibernation_suspend = -1;
  31421. + else if (core_if->power_down == 3 && core_if->xhib == 2) {
  31422. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  31423. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  31424. + dctl_data_t dctl = {.d32 = 0 };
  31425. +
  31426. + DWC_WRITE_REG32(&core_if->core_global_regs->
  31427. + gintsts, 0xFFFFFFFF);
  31428. +
  31429. + DWC_DEBUGPL(DBG_ANY,
  31430. + "RESTORE DONE generated\n");
  31431. +
  31432. + gpwrdn.b.restore = 1;
  31433. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  31434. + dwc_udelay(10);
  31435. +
  31436. + pcgcctl.b.rstpdwnmodule = 1;
  31437. + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
  31438. +
  31439. + DWC_WRITE_REG32(&core_if->core_global_regs->gusbcfg, core_if->gr_backup->gusbcfg_local);
  31440. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dcfg, core_if->dr_backup->dcfg);
  31441. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl, core_if->dr_backup->dctl);
  31442. + dwc_udelay(50);
  31443. +
  31444. + dctl.b.pwronprgdone = 1;
  31445. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
  31446. + dwc_udelay(10);
  31447. +
  31448. + dwc_otg_restore_global_regs(core_if);
  31449. + dwc_otg_restore_dev_regs(core_if, 0);
  31450. +
  31451. + dctl.d32 = 0;
  31452. + dctl.b.pwronprgdone = 1;
  31453. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32, 0);
  31454. + dwc_udelay(10);
  31455. +
  31456. + pcgcctl.d32 = 0;
  31457. + pcgcctl.b.enbl_extnd_hiber = 1;
  31458. + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
  31459. +
  31460. + /* The core will be in ON STATE */
  31461. + core_if->lx_state = DWC_OTG_L0;
  31462. + core_if->xhib = 0;
  31463. +
  31464. + DWC_SPINUNLOCK(core_if->lock);
  31465. + if (core_if->pcd_cb && core_if->pcd_cb->resume_wakeup) {
  31466. + core_if->pcd_cb->resume_wakeup(core_if->pcd_cb->p);
  31467. + }
  31468. + DWC_SPINLOCK(core_if->lock);
  31469. +
  31470. + }
  31471. +
  31472. + gintsts.b.restoredone = 1;
  31473. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts,gintsts.d32);
  31474. + DWC_PRINTF(" --Restore done interrupt received-- \n");
  31475. + retval |= 1;
  31476. + }
  31477. + if (gintsts.b.portintr && dwc_otg_is_device_mode(core_if)) {
  31478. + /* The port interrupt occurs while in device mode with HPRT0
  31479. + * Port Enable/Disable.
  31480. + */
  31481. + gintsts.d32 = 0;
  31482. + gintsts.b.portintr = 1;
  31483. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts,gintsts.d32);
  31484. + retval |= 1;
  31485. + gintmsk_reenable.b.portintr = 1;
  31486. +
  31487. + }
  31488. + /* Did we actually handle anything? if so, unmask the interrupt */
  31489. +// fiq_print(FIQDBG_INT, otg_dev->hcd->fiq_state, "CILOUT %1d", retval);
  31490. +// fiq_print(FIQDBG_INT, otg_dev->hcd->fiq_state, "%08x", gintsts.d32);
  31491. +// fiq_print(FIQDBG_INT, otg_dev->hcd->fiq_state, "%08x", gintmsk_reenable.d32);
  31492. + if (retval && fiq_enable) {
  31493. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, gintmsk_reenable.d32);
  31494. + }
  31495. +
  31496. + } else {
  31497. + DWC_DEBUGPL(DBG_ANY, "gpwrdn=%08x\n", gpwrdn.d32);
  31498. +
  31499. + if (gpwrdn.b.disconn_det && gpwrdn.b.disconn_det_msk) {
  31500. + CLEAR_GPWRDN_INTR(core_if, disconn_det);
  31501. + if (gpwrdn.b.linestate == 0) {
  31502. + dwc_otg_handle_pwrdn_disconnect_intr(core_if);
  31503. + } else {
  31504. + DWC_PRINTF("Disconnect detected while linestate is not 0\n");
  31505. + }
  31506. +
  31507. + retval |= 1;
  31508. + }
  31509. + if (gpwrdn.b.lnstschng && gpwrdn.b.lnstchng_msk) {
  31510. + CLEAR_GPWRDN_INTR(core_if, lnstschng);
  31511. + /* remote wakeup from hibernation */
  31512. + if (gpwrdn.b.linestate == 2 || gpwrdn.b.linestate == 1) {
  31513. + dwc_otg_handle_pwrdn_wakeup_detected_intr(core_if);
  31514. + } else {
  31515. + DWC_PRINTF("gpwrdn.linestate = %d\n", gpwrdn.b.linestate);
  31516. + }
  31517. + retval |= 1;
  31518. + }
  31519. + if (gpwrdn.b.rst_det && gpwrdn.b.rst_det_msk) {
  31520. + CLEAR_GPWRDN_INTR(core_if, rst_det);
  31521. + if (gpwrdn.b.linestate == 0) {
  31522. + DWC_PRINTF("Reset detected\n");
  31523. + retval |= dwc_otg_device_hibernation_restore(core_if, 0, 1);
  31524. + }
  31525. + }
  31526. + if (gpwrdn.b.srp_det && gpwrdn.b.srp_det_msk) {
  31527. + CLEAR_GPWRDN_INTR(core_if, srp_det);
  31528. + dwc_otg_handle_pwrdn_srp_intr(core_if);
  31529. + retval |= 1;
  31530. + }
  31531. + }
  31532. + /* Handle ADP interrupt here */
  31533. + if (gpwrdn.b.adp_int) {
  31534. + DWC_PRINTF("ADP interrupt\n");
  31535. + CLEAR_GPWRDN_INTR(core_if, adp_int);
  31536. + dwc_otg_adp_handle_intr(core_if);
  31537. + retval |= 1;
  31538. + }
  31539. + if (gpwrdn.b.sts_chngint && gpwrdn.b.sts_chngint_msk) {
  31540. + DWC_PRINTF("STS CHNG interrupt asserted\n");
  31541. + CLEAR_GPWRDN_INTR(core_if, sts_chngint);
  31542. + dwc_otg_handle_pwrdn_stschng_intr(otg_dev);
  31543. +
  31544. + retval |= 1;
  31545. + }
  31546. + if (core_if->lock)
  31547. + DWC_SPINUNLOCK(core_if->lock);
  31548. + return retval;
  31549. +}
  31550. --- /dev/null
  31551. +++ b/drivers/usb/host/dwc_otg/dwc_otg_core_if.h
  31552. @@ -0,0 +1,705 @@
  31553. +/* ==========================================================================
  31554. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_core_if.h $
  31555. + * $Revision: #13 $
  31556. + * $Date: 2012/08/10 $
  31557. + * $Change: 2047372 $
  31558. + *
  31559. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  31560. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  31561. + * otherwise expressly agreed to in writing between Synopsys and you.
  31562. + *
  31563. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  31564. + * any End User Software License Agreement or Agreement for Licensed Product
  31565. + * with Synopsys or any supplement thereto. You are permitted to use and
  31566. + * redistribute this Software in source and binary forms, with or without
  31567. + * modification, provided that redistributions of source code must retain this
  31568. + * notice. You may not view, use, disclose, copy or distribute this file or
  31569. + * any information contained herein except pursuant to this license grant from
  31570. + * Synopsys. If you do not agree with this notice, including the disclaimer
  31571. + * below, then you are not authorized to use the Software.
  31572. + *
  31573. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  31574. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  31575. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  31576. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  31577. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  31578. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  31579. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  31580. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31581. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  31582. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  31583. + * DAMAGE.
  31584. + * ========================================================================== */
  31585. +#if !defined(__DWC_CORE_IF_H__)
  31586. +#define __DWC_CORE_IF_H__
  31587. +
  31588. +#include "dwc_os.h"
  31589. +
  31590. +/** @file
  31591. + * This file defines DWC_OTG Core API
  31592. + */
  31593. +
  31594. +struct dwc_otg_core_if;
  31595. +typedef struct dwc_otg_core_if dwc_otg_core_if_t;
  31596. +
  31597. +/** Maximum number of Periodic FIFOs */
  31598. +#define MAX_PERIO_FIFOS 15
  31599. +/** Maximum number of Periodic FIFOs */
  31600. +#define MAX_TX_FIFOS 15
  31601. +
  31602. +/** Maximum number of Endpoints/HostChannels */
  31603. +#define MAX_EPS_CHANNELS 16
  31604. +
  31605. +extern dwc_otg_core_if_t *dwc_otg_cil_init(const uint32_t * _reg_base_addr);
  31606. +extern void dwc_otg_core_init(dwc_otg_core_if_t * _core_if);
  31607. +extern void dwc_otg_cil_remove(dwc_otg_core_if_t * _core_if);
  31608. +
  31609. +extern void dwc_otg_enable_global_interrupts(dwc_otg_core_if_t * _core_if);
  31610. +extern void dwc_otg_disable_global_interrupts(dwc_otg_core_if_t * _core_if);
  31611. +
  31612. +extern uint8_t dwc_otg_is_device_mode(dwc_otg_core_if_t * _core_if);
  31613. +extern uint8_t dwc_otg_is_host_mode(dwc_otg_core_if_t * _core_if);
  31614. +
  31615. +extern uint8_t dwc_otg_is_dma_enable(dwc_otg_core_if_t * core_if);
  31616. +
  31617. +/** This function should be called on every hardware interrupt. */
  31618. +extern int32_t dwc_otg_handle_common_intr(void *otg_dev);
  31619. +
  31620. +/** @name OTG Core Parameters */
  31621. +/** @{ */
  31622. +
  31623. +/**
  31624. + * Specifies the OTG capabilities. The driver will automatically
  31625. + * detect the value for this parameter if none is specified.
  31626. + * 0 - HNP and SRP capable (default)
  31627. + * 1 - SRP Only capable
  31628. + * 2 - No HNP/SRP capable
  31629. + */
  31630. +extern int dwc_otg_set_param_otg_cap(dwc_otg_core_if_t * core_if, int32_t val);
  31631. +extern int32_t dwc_otg_get_param_otg_cap(dwc_otg_core_if_t * core_if);
  31632. +#define DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE 0
  31633. +#define DWC_OTG_CAP_PARAM_SRP_ONLY_CAPABLE 1
  31634. +#define DWC_OTG_CAP_PARAM_NO_HNP_SRP_CAPABLE 2
  31635. +#define dwc_param_otg_cap_default DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE
  31636. +
  31637. +extern int dwc_otg_set_param_opt(dwc_otg_core_if_t * core_if, int32_t val);
  31638. +extern int32_t dwc_otg_get_param_opt(dwc_otg_core_if_t * core_if);
  31639. +#define dwc_param_opt_default 1
  31640. +
  31641. +/**
  31642. + * Specifies whether to use slave or DMA mode for accessing the data
  31643. + * FIFOs. The driver will automatically detect the value for this
  31644. + * parameter if none is specified.
  31645. + * 0 - Slave
  31646. + * 1 - DMA (default, if available)
  31647. + */
  31648. +extern int dwc_otg_set_param_dma_enable(dwc_otg_core_if_t * core_if,
  31649. + int32_t val);
  31650. +extern int32_t dwc_otg_get_param_dma_enable(dwc_otg_core_if_t * core_if);
  31651. +#define dwc_param_dma_enable_default 1
  31652. +
  31653. +/**
  31654. + * When DMA mode is enabled specifies whether to use
  31655. + * address DMA or DMA Descritor mode for accessing the data
  31656. + * FIFOs in device mode. The driver will automatically detect
  31657. + * the value for this parameter if none is specified.
  31658. + * 0 - address DMA
  31659. + * 1 - DMA Descriptor(default, if available)
  31660. + */
  31661. +extern int dwc_otg_set_param_dma_desc_enable(dwc_otg_core_if_t * core_if,
  31662. + int32_t val);
  31663. +extern int32_t dwc_otg_get_param_dma_desc_enable(dwc_otg_core_if_t * core_if);
  31664. +//#define dwc_param_dma_desc_enable_default 1
  31665. +#define dwc_param_dma_desc_enable_default 0 // Broadcom BCM2708
  31666. +
  31667. +/** The DMA Burst size (applicable only for External DMA
  31668. + * Mode). 1, 4, 8 16, 32, 64, 128, 256 (default 32)
  31669. + */
  31670. +extern int dwc_otg_set_param_dma_burst_size(dwc_otg_core_if_t * core_if,
  31671. + int32_t val);
  31672. +extern int32_t dwc_otg_get_param_dma_burst_size(dwc_otg_core_if_t * core_if);
  31673. +#define dwc_param_dma_burst_size_default 32
  31674. +
  31675. +/**
  31676. + * Specifies the maximum speed of operation in host and device mode.
  31677. + * The actual speed depends on the speed of the attached device and
  31678. + * the value of phy_type. The actual speed depends on the speed of the
  31679. + * attached device.
  31680. + * 0 - High Speed (default)
  31681. + * 1 - Full Speed
  31682. + */
  31683. +extern int dwc_otg_set_param_speed(dwc_otg_core_if_t * core_if, int32_t val);
  31684. +extern int32_t dwc_otg_get_param_speed(dwc_otg_core_if_t * core_if);
  31685. +#define dwc_param_speed_default 0
  31686. +#define DWC_SPEED_PARAM_HIGH 0
  31687. +#define DWC_SPEED_PARAM_FULL 1
  31688. +
  31689. +/** Specifies whether low power mode is supported when attached
  31690. + * to a Full Speed or Low Speed device in host mode.
  31691. + * 0 - Don't support low power mode (default)
  31692. + * 1 - Support low power mode
  31693. + */
  31694. +extern int dwc_otg_set_param_host_support_fs_ls_low_power(dwc_otg_core_if_t *
  31695. + core_if, int32_t val);
  31696. +extern int32_t dwc_otg_get_param_host_support_fs_ls_low_power(dwc_otg_core_if_t
  31697. + * core_if);
  31698. +#define dwc_param_host_support_fs_ls_low_power_default 0
  31699. +
  31700. +/** Specifies the PHY clock rate in low power mode when connected to a
  31701. + * Low Speed device in host mode. This parameter is applicable only if
  31702. + * HOST_SUPPORT_FS_LS_LOW_POWER is enabled. If PHY_TYPE is set to FS
  31703. + * then defaults to 6 MHZ otherwise 48 MHZ.
  31704. + *
  31705. + * 0 - 48 MHz
  31706. + * 1 - 6 MHz
  31707. + */
  31708. +extern int dwc_otg_set_param_host_ls_low_power_phy_clk(dwc_otg_core_if_t *
  31709. + core_if, int32_t val);
  31710. +extern int32_t dwc_otg_get_param_host_ls_low_power_phy_clk(dwc_otg_core_if_t *
  31711. + core_if);
  31712. +#define dwc_param_host_ls_low_power_phy_clk_default 0
  31713. +#define DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ 0
  31714. +#define DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_6MHZ 1
  31715. +
  31716. +/**
  31717. + * 0 - Use cC FIFO size parameters
  31718. + * 1 - Allow dynamic FIFO sizing (default)
  31719. + */
  31720. +extern int dwc_otg_set_param_enable_dynamic_fifo(dwc_otg_core_if_t * core_if,
  31721. + int32_t val);
  31722. +extern int32_t dwc_otg_get_param_enable_dynamic_fifo(dwc_otg_core_if_t *
  31723. + core_if);
  31724. +#define dwc_param_enable_dynamic_fifo_default 1
  31725. +
  31726. +/** Total number of 4-byte words in the data FIFO memory. This
  31727. + * memory includes the Rx FIFO, non-periodic Tx FIFO, and periodic
  31728. + * Tx FIFOs.
  31729. + * 32 to 32768 (default 8192)
  31730. + * Note: The total FIFO memory depth in the FPGA configuration is 8192.
  31731. + */
  31732. +extern int dwc_otg_set_param_data_fifo_size(dwc_otg_core_if_t * core_if,
  31733. + int32_t val);
  31734. +extern int32_t dwc_otg_get_param_data_fifo_size(dwc_otg_core_if_t * core_if);
  31735. +//#define dwc_param_data_fifo_size_default 8192
  31736. +#define dwc_param_data_fifo_size_default 0xFF0 // Broadcom BCM2708
  31737. +
  31738. +/** Number of 4-byte words in the Rx FIFO in device mode when dynamic
  31739. + * FIFO sizing is enabled.
  31740. + * 16 to 32768 (default 1064)
  31741. + */
  31742. +extern int dwc_otg_set_param_dev_rx_fifo_size(dwc_otg_core_if_t * core_if,
  31743. + int32_t val);
  31744. +extern int32_t dwc_otg_get_param_dev_rx_fifo_size(dwc_otg_core_if_t * core_if);
  31745. +#define dwc_param_dev_rx_fifo_size_default 1064
  31746. +
  31747. +/** Number of 4-byte words in the non-periodic Tx FIFO in device mode
  31748. + * when dynamic FIFO sizing is enabled.
  31749. + * 16 to 32768 (default 1024)
  31750. + */
  31751. +extern int dwc_otg_set_param_dev_nperio_tx_fifo_size(dwc_otg_core_if_t *
  31752. + core_if, int32_t val);
  31753. +extern int32_t dwc_otg_get_param_dev_nperio_tx_fifo_size(dwc_otg_core_if_t *
  31754. + core_if);
  31755. +#define dwc_param_dev_nperio_tx_fifo_size_default 1024
  31756. +
  31757. +/** Number of 4-byte words in each of the periodic Tx FIFOs in device
  31758. + * mode when dynamic FIFO sizing is enabled.
  31759. + * 4 to 768 (default 256)
  31760. + */
  31761. +extern int dwc_otg_set_param_dev_perio_tx_fifo_size(dwc_otg_core_if_t * core_if,
  31762. + int32_t val, int fifo_num);
  31763. +extern int32_t dwc_otg_get_param_dev_perio_tx_fifo_size(dwc_otg_core_if_t *
  31764. + core_if, int fifo_num);
  31765. +#define dwc_param_dev_perio_tx_fifo_size_default 256
  31766. +
  31767. +/** Number of 4-byte words in the Rx FIFO in host mode when dynamic
  31768. + * FIFO sizing is enabled.
  31769. + * 16 to 32768 (default 1024)
  31770. + */
  31771. +extern int dwc_otg_set_param_host_rx_fifo_size(dwc_otg_core_if_t * core_if,
  31772. + int32_t val);
  31773. +extern int32_t dwc_otg_get_param_host_rx_fifo_size(dwc_otg_core_if_t * core_if);
  31774. +//#define dwc_param_host_rx_fifo_size_default 1024
  31775. +#define dwc_param_host_rx_fifo_size_default 774 // Broadcom BCM2708
  31776. +
  31777. +/** Number of 4-byte words in the non-periodic Tx FIFO in host mode
  31778. + * when Dynamic FIFO sizing is enabled in the core.
  31779. + * 16 to 32768 (default 1024)
  31780. + */
  31781. +extern int dwc_otg_set_param_host_nperio_tx_fifo_size(dwc_otg_core_if_t *
  31782. + core_if, int32_t val);
  31783. +extern int32_t dwc_otg_get_param_host_nperio_tx_fifo_size(dwc_otg_core_if_t *
  31784. + core_if);
  31785. +//#define dwc_param_host_nperio_tx_fifo_size_default 1024
  31786. +#define dwc_param_host_nperio_tx_fifo_size_default 0x100 // Broadcom BCM2708
  31787. +
  31788. +/** Number of 4-byte words in the host periodic Tx FIFO when dynamic
  31789. + * FIFO sizing is enabled.
  31790. + * 16 to 32768 (default 1024)
  31791. + */
  31792. +extern int dwc_otg_set_param_host_perio_tx_fifo_size(dwc_otg_core_if_t *
  31793. + core_if, int32_t val);
  31794. +extern int32_t dwc_otg_get_param_host_perio_tx_fifo_size(dwc_otg_core_if_t *
  31795. + core_if);
  31796. +//#define dwc_param_host_perio_tx_fifo_size_default 1024
  31797. +#define dwc_param_host_perio_tx_fifo_size_default 0x200 // Broadcom BCM2708
  31798. +
  31799. +/** The maximum transfer size supported in bytes.
  31800. + * 2047 to 65,535 (default 65,535)
  31801. + */
  31802. +extern int dwc_otg_set_param_max_transfer_size(dwc_otg_core_if_t * core_if,
  31803. + int32_t val);
  31804. +extern int32_t dwc_otg_get_param_max_transfer_size(dwc_otg_core_if_t * core_if);
  31805. +#define dwc_param_max_transfer_size_default 65535
  31806. +
  31807. +/** The maximum number of packets in a transfer.
  31808. + * 15 to 511 (default 511)
  31809. + */
  31810. +extern int dwc_otg_set_param_max_packet_count(dwc_otg_core_if_t * core_if,
  31811. + int32_t val);
  31812. +extern int32_t dwc_otg_get_param_max_packet_count(dwc_otg_core_if_t * core_if);
  31813. +#define dwc_param_max_packet_count_default 511
  31814. +
  31815. +/** The number of host channel registers to use.
  31816. + * 1 to 16 (default 12)
  31817. + * Note: The FPGA configuration supports a maximum of 12 host channels.
  31818. + */
  31819. +extern int dwc_otg_set_param_host_channels(dwc_otg_core_if_t * core_if,
  31820. + int32_t val);
  31821. +extern int32_t dwc_otg_get_param_host_channels(dwc_otg_core_if_t * core_if);
  31822. +//#define dwc_param_host_channels_default 12
  31823. +#define dwc_param_host_channels_default 8 // Broadcom BCM2708
  31824. +
  31825. +/** The number of endpoints in addition to EP0 available for device
  31826. + * mode operations.
  31827. + * 1 to 15 (default 6 IN and OUT)
  31828. + * Note: The FPGA configuration supports a maximum of 6 IN and OUT
  31829. + * endpoints in addition to EP0.
  31830. + */
  31831. +extern int dwc_otg_set_param_dev_endpoints(dwc_otg_core_if_t * core_if,
  31832. + int32_t val);
  31833. +extern int32_t dwc_otg_get_param_dev_endpoints(dwc_otg_core_if_t * core_if);
  31834. +#define dwc_param_dev_endpoints_default 6
  31835. +
  31836. +/**
  31837. + * Specifies the type of PHY interface to use. By default, the driver
  31838. + * will automatically detect the phy_type.
  31839. + *
  31840. + * 0 - Full Speed PHY
  31841. + * 1 - UTMI+ (default)
  31842. + * 2 - ULPI
  31843. + */
  31844. +extern int dwc_otg_set_param_phy_type(dwc_otg_core_if_t * core_if, int32_t val);
  31845. +extern int32_t dwc_otg_get_param_phy_type(dwc_otg_core_if_t * core_if);
  31846. +#define DWC_PHY_TYPE_PARAM_FS 0
  31847. +#define DWC_PHY_TYPE_PARAM_UTMI 1
  31848. +#define DWC_PHY_TYPE_PARAM_ULPI 2
  31849. +#define dwc_param_phy_type_default DWC_PHY_TYPE_PARAM_UTMI
  31850. +
  31851. +/**
  31852. + * Specifies the UTMI+ Data Width. This parameter is
  31853. + * applicable for a PHY_TYPE of UTMI+ or ULPI. (For a ULPI
  31854. + * PHY_TYPE, this parameter indicates the data width between
  31855. + * the MAC and the ULPI Wrapper.) Also, this parameter is
  31856. + * applicable only if the OTG_HSPHY_WIDTH cC parameter was set
  31857. + * to "8 and 16 bits", meaning that the core has been
  31858. + * configured to work at either data path width.
  31859. + *
  31860. + * 8 or 16 bits (default 16)
  31861. + */
  31862. +extern int dwc_otg_set_param_phy_utmi_width(dwc_otg_core_if_t * core_if,
  31863. + int32_t val);
  31864. +extern int32_t dwc_otg_get_param_phy_utmi_width(dwc_otg_core_if_t * core_if);
  31865. +//#define dwc_param_phy_utmi_width_default 16
  31866. +#define dwc_param_phy_utmi_width_default 8 // Broadcom BCM2708
  31867. +
  31868. +/**
  31869. + * Specifies whether the ULPI operates at double or single
  31870. + * data rate. This parameter is only applicable if PHY_TYPE is
  31871. + * ULPI.
  31872. + *
  31873. + * 0 - single data rate ULPI interface with 8 bit wide data
  31874. + * bus (default)
  31875. + * 1 - double data rate ULPI interface with 4 bit wide data
  31876. + * bus
  31877. + */
  31878. +extern int dwc_otg_set_param_phy_ulpi_ddr(dwc_otg_core_if_t * core_if,
  31879. + int32_t val);
  31880. +extern int32_t dwc_otg_get_param_phy_ulpi_ddr(dwc_otg_core_if_t * core_if);
  31881. +#define dwc_param_phy_ulpi_ddr_default 0
  31882. +
  31883. +/**
  31884. + * Specifies whether to use the internal or external supply to
  31885. + * drive the vbus with a ULPI phy.
  31886. + */
  31887. +extern int dwc_otg_set_param_phy_ulpi_ext_vbus(dwc_otg_core_if_t * core_if,
  31888. + int32_t val);
  31889. +extern int32_t dwc_otg_get_param_phy_ulpi_ext_vbus(dwc_otg_core_if_t * core_if);
  31890. +#define DWC_PHY_ULPI_INTERNAL_VBUS 0
  31891. +#define DWC_PHY_ULPI_EXTERNAL_VBUS 1
  31892. +#define dwc_param_phy_ulpi_ext_vbus_default DWC_PHY_ULPI_INTERNAL_VBUS
  31893. +
  31894. +/**
  31895. + * Specifies whether to use the I2Cinterface for full speed PHY. This
  31896. + * parameter is only applicable if PHY_TYPE is FS.
  31897. + * 0 - No (default)
  31898. + * 1 - Yes
  31899. + */
  31900. +extern int dwc_otg_set_param_i2c_enable(dwc_otg_core_if_t * core_if,
  31901. + int32_t val);
  31902. +extern int32_t dwc_otg_get_param_i2c_enable(dwc_otg_core_if_t * core_if);
  31903. +#define dwc_param_i2c_enable_default 0
  31904. +
  31905. +extern int dwc_otg_set_param_ulpi_fs_ls(dwc_otg_core_if_t * core_if,
  31906. + int32_t val);
  31907. +extern int32_t dwc_otg_get_param_ulpi_fs_ls(dwc_otg_core_if_t * core_if);
  31908. +#define dwc_param_ulpi_fs_ls_default 0
  31909. +
  31910. +extern int dwc_otg_set_param_ts_dline(dwc_otg_core_if_t * core_if, int32_t val);
  31911. +extern int32_t dwc_otg_get_param_ts_dline(dwc_otg_core_if_t * core_if);
  31912. +#define dwc_param_ts_dline_default 0
  31913. +
  31914. +/**
  31915. + * Specifies whether dedicated transmit FIFOs are
  31916. + * enabled for non periodic IN endpoints in device mode
  31917. + * 0 - No
  31918. + * 1 - Yes
  31919. + */
  31920. +extern int dwc_otg_set_param_en_multiple_tx_fifo(dwc_otg_core_if_t * core_if,
  31921. + int32_t val);
  31922. +extern int32_t dwc_otg_get_param_en_multiple_tx_fifo(dwc_otg_core_if_t *
  31923. + core_if);
  31924. +#define dwc_param_en_multiple_tx_fifo_default 1
  31925. +
  31926. +/** Number of 4-byte words in each of the Tx FIFOs in device
  31927. + * mode when dynamic FIFO sizing is enabled.
  31928. + * 4 to 768 (default 256)
  31929. + */
  31930. +extern int dwc_otg_set_param_dev_tx_fifo_size(dwc_otg_core_if_t * core_if,
  31931. + int fifo_num, int32_t val);
  31932. +extern int32_t dwc_otg_get_param_dev_tx_fifo_size(dwc_otg_core_if_t * core_if,
  31933. + int fifo_num);
  31934. +#define dwc_param_dev_tx_fifo_size_default 768
  31935. +
  31936. +/** Thresholding enable flag-
  31937. + * bit 0 - enable non-ISO Tx thresholding
  31938. + * bit 1 - enable ISO Tx thresholding
  31939. + * bit 2 - enable Rx thresholding
  31940. + */
  31941. +extern int dwc_otg_set_param_thr_ctl(dwc_otg_core_if_t * core_if, int32_t val);
  31942. +extern int32_t dwc_otg_get_thr_ctl(dwc_otg_core_if_t * core_if, int fifo_num);
  31943. +#define dwc_param_thr_ctl_default 0
  31944. +
  31945. +/** Thresholding length for Tx
  31946. + * FIFOs in 32 bit DWORDs
  31947. + */
  31948. +extern int dwc_otg_set_param_tx_thr_length(dwc_otg_core_if_t * core_if,
  31949. + int32_t val);
  31950. +extern int32_t dwc_otg_get_tx_thr_length(dwc_otg_core_if_t * core_if);
  31951. +#define dwc_param_tx_thr_length_default 64
  31952. +
  31953. +/** Thresholding length for Rx
  31954. + * FIFOs in 32 bit DWORDs
  31955. + */
  31956. +extern int dwc_otg_set_param_rx_thr_length(dwc_otg_core_if_t * core_if,
  31957. + int32_t val);
  31958. +extern int32_t dwc_otg_get_rx_thr_length(dwc_otg_core_if_t * core_if);
  31959. +#define dwc_param_rx_thr_length_default 64
  31960. +
  31961. +/**
  31962. + * Specifies whether LPM (Link Power Management) support is enabled
  31963. + */
  31964. +extern int dwc_otg_set_param_lpm_enable(dwc_otg_core_if_t * core_if,
  31965. + int32_t val);
  31966. +extern int32_t dwc_otg_get_param_lpm_enable(dwc_otg_core_if_t * core_if);
  31967. +#define dwc_param_lpm_enable_default 1
  31968. +
  31969. +/**
  31970. + * Specifies whether PTI enhancement is enabled
  31971. + */
  31972. +extern int dwc_otg_set_param_pti_enable(dwc_otg_core_if_t * core_if,
  31973. + int32_t val);
  31974. +extern int32_t dwc_otg_get_param_pti_enable(dwc_otg_core_if_t * core_if);
  31975. +#define dwc_param_pti_enable_default 0
  31976. +
  31977. +/**
  31978. + * Specifies whether MPI enhancement is enabled
  31979. + */
  31980. +extern int dwc_otg_set_param_mpi_enable(dwc_otg_core_if_t * core_if,
  31981. + int32_t val);
  31982. +extern int32_t dwc_otg_get_param_mpi_enable(dwc_otg_core_if_t * core_if);
  31983. +#define dwc_param_mpi_enable_default 0
  31984. +
  31985. +/**
  31986. + * Specifies whether ADP capability is enabled
  31987. + */
  31988. +extern int dwc_otg_set_param_adp_enable(dwc_otg_core_if_t * core_if,
  31989. + int32_t val);
  31990. +extern int32_t dwc_otg_get_param_adp_enable(dwc_otg_core_if_t * core_if);
  31991. +#define dwc_param_adp_enable_default 0
  31992. +
  31993. +/**
  31994. + * Specifies whether IC_USB capability is enabled
  31995. + */
  31996. +
  31997. +extern int dwc_otg_set_param_ic_usb_cap(dwc_otg_core_if_t * core_if,
  31998. + int32_t val);
  31999. +extern int32_t dwc_otg_get_param_ic_usb_cap(dwc_otg_core_if_t * core_if);
  32000. +#define dwc_param_ic_usb_cap_default 0
  32001. +
  32002. +extern int dwc_otg_set_param_ahb_thr_ratio(dwc_otg_core_if_t * core_if,
  32003. + int32_t val);
  32004. +extern int32_t dwc_otg_get_param_ahb_thr_ratio(dwc_otg_core_if_t * core_if);
  32005. +#define dwc_param_ahb_thr_ratio_default 0
  32006. +
  32007. +extern int dwc_otg_set_param_power_down(dwc_otg_core_if_t * core_if,
  32008. + int32_t val);
  32009. +extern int32_t dwc_otg_get_param_power_down(dwc_otg_core_if_t * core_if);
  32010. +#define dwc_param_power_down_default 0
  32011. +
  32012. +extern int dwc_otg_set_param_reload_ctl(dwc_otg_core_if_t * core_if,
  32013. + int32_t val);
  32014. +extern int32_t dwc_otg_get_param_reload_ctl(dwc_otg_core_if_t * core_if);
  32015. +#define dwc_param_reload_ctl_default 0
  32016. +
  32017. +extern int dwc_otg_set_param_dev_out_nak(dwc_otg_core_if_t * core_if,
  32018. + int32_t val);
  32019. +extern int32_t dwc_otg_get_param_dev_out_nak(dwc_otg_core_if_t * core_if);
  32020. +#define dwc_param_dev_out_nak_default 0
  32021. +
  32022. +extern int dwc_otg_set_param_cont_on_bna(dwc_otg_core_if_t * core_if,
  32023. + int32_t val);
  32024. +extern int32_t dwc_otg_get_param_cont_on_bna(dwc_otg_core_if_t * core_if);
  32025. +#define dwc_param_cont_on_bna_default 0
  32026. +
  32027. +extern int dwc_otg_set_param_ahb_single(dwc_otg_core_if_t * core_if,
  32028. + int32_t val);
  32029. +extern int32_t dwc_otg_get_param_ahb_single(dwc_otg_core_if_t * core_if);
  32030. +#define dwc_param_ahb_single_default 0
  32031. +
  32032. +extern int dwc_otg_set_param_otg_ver(dwc_otg_core_if_t * core_if, int32_t val);
  32033. +extern int32_t dwc_otg_get_param_otg_ver(dwc_otg_core_if_t * core_if);
  32034. +#define dwc_param_otg_ver_default 0
  32035. +
  32036. +/** @} */
  32037. +
  32038. +/** @name Access to registers and bit-fields */
  32039. +
  32040. +/**
  32041. + * Dump core registers and SPRAM
  32042. + */
  32043. +extern void dwc_otg_dump_dev_registers(dwc_otg_core_if_t * _core_if);
  32044. +extern void dwc_otg_dump_spram(dwc_otg_core_if_t * _core_if);
  32045. +extern void dwc_otg_dump_host_registers(dwc_otg_core_if_t * _core_if);
  32046. +extern void dwc_otg_dump_global_registers(dwc_otg_core_if_t * _core_if);
  32047. +
  32048. +/**
  32049. + * Get host negotiation status.
  32050. + */
  32051. +extern uint32_t dwc_otg_get_hnpstatus(dwc_otg_core_if_t * core_if);
  32052. +
  32053. +/**
  32054. + * Get srp status
  32055. + */
  32056. +extern uint32_t dwc_otg_get_srpstatus(dwc_otg_core_if_t * core_if);
  32057. +
  32058. +/**
  32059. + * Set hnpreq bit in the GOTGCTL register.
  32060. + */
  32061. +extern void dwc_otg_set_hnpreq(dwc_otg_core_if_t * core_if, uint32_t val);
  32062. +
  32063. +/**
  32064. + * Get Content of SNPSID register.
  32065. + */
  32066. +extern uint32_t dwc_otg_get_gsnpsid(dwc_otg_core_if_t * core_if);
  32067. +
  32068. +/**
  32069. + * Get current mode.
  32070. + * Returns 0 if in device mode, and 1 if in host mode.
  32071. + */
  32072. +extern uint32_t dwc_otg_get_mode(dwc_otg_core_if_t * core_if);
  32073. +
  32074. +/**
  32075. + * Get value of hnpcapable field in the GUSBCFG register
  32076. + */
  32077. +extern uint32_t dwc_otg_get_hnpcapable(dwc_otg_core_if_t * core_if);
  32078. +/**
  32079. + * Set value of hnpcapable field in the GUSBCFG register
  32080. + */
  32081. +extern void dwc_otg_set_hnpcapable(dwc_otg_core_if_t * core_if, uint32_t val);
  32082. +
  32083. +/**
  32084. + * Get value of srpcapable field in the GUSBCFG register
  32085. + */
  32086. +extern uint32_t dwc_otg_get_srpcapable(dwc_otg_core_if_t * core_if);
  32087. +/**
  32088. + * Set value of srpcapable field in the GUSBCFG register
  32089. + */
  32090. +extern void dwc_otg_set_srpcapable(dwc_otg_core_if_t * core_if, uint32_t val);
  32091. +
  32092. +/**
  32093. + * Get value of devspeed field in the DCFG register
  32094. + */
  32095. +extern uint32_t dwc_otg_get_devspeed(dwc_otg_core_if_t * core_if);
  32096. +/**
  32097. + * Set value of devspeed field in the DCFG register
  32098. + */
  32099. +extern void dwc_otg_set_devspeed(dwc_otg_core_if_t * core_if, uint32_t val);
  32100. +
  32101. +/**
  32102. + * Get the value of busconnected field from the HPRT0 register
  32103. + */
  32104. +extern uint32_t dwc_otg_get_busconnected(dwc_otg_core_if_t * core_if);
  32105. +
  32106. +/**
  32107. + * Gets the device enumeration Speed.
  32108. + */
  32109. +extern uint32_t dwc_otg_get_enumspeed(dwc_otg_core_if_t * core_if);
  32110. +
  32111. +/**
  32112. + * Get value of prtpwr field from the HPRT0 register
  32113. + */
  32114. +extern uint32_t dwc_otg_get_prtpower(dwc_otg_core_if_t * core_if);
  32115. +
  32116. +/**
  32117. + * Get value of flag indicating core state - hibernated or not
  32118. + */
  32119. +extern uint32_t dwc_otg_get_core_state(dwc_otg_core_if_t * core_if);
  32120. +
  32121. +/**
  32122. + * Set value of prtpwr field from the HPRT0 register
  32123. + */
  32124. +extern void dwc_otg_set_prtpower(dwc_otg_core_if_t * core_if, uint32_t val);
  32125. +
  32126. +/**
  32127. + * Get value of prtsusp field from the HPRT0 regsiter
  32128. + */
  32129. +extern uint32_t dwc_otg_get_prtsuspend(dwc_otg_core_if_t * core_if);
  32130. +/**
  32131. + * Set value of prtpwr field from the HPRT0 register
  32132. + */
  32133. +extern void dwc_otg_set_prtsuspend(dwc_otg_core_if_t * core_if, uint32_t val);
  32134. +
  32135. +/**
  32136. + * Get value of ModeChTimEn field from the HCFG regsiter
  32137. + */
  32138. +extern uint32_t dwc_otg_get_mode_ch_tim(dwc_otg_core_if_t * core_if);
  32139. +/**
  32140. + * Set value of ModeChTimEn field from the HCFG regsiter
  32141. + */
  32142. +extern void dwc_otg_set_mode_ch_tim(dwc_otg_core_if_t * core_if, uint32_t val);
  32143. +
  32144. +/**
  32145. + * Get value of Fram Interval field from the HFIR regsiter
  32146. + */
  32147. +extern uint32_t dwc_otg_get_fr_interval(dwc_otg_core_if_t * core_if);
  32148. +/**
  32149. + * Set value of Frame Interval field from the HFIR regsiter
  32150. + */
  32151. +extern void dwc_otg_set_fr_interval(dwc_otg_core_if_t * core_if, uint32_t val);
  32152. +
  32153. +/**
  32154. + * Set value of prtres field from the HPRT0 register
  32155. + *FIXME Remove?
  32156. + */
  32157. +extern void dwc_otg_set_prtresume(dwc_otg_core_if_t * core_if, uint32_t val);
  32158. +
  32159. +/**
  32160. + * Get value of rmtwkupsig bit in DCTL register
  32161. + */
  32162. +extern uint32_t dwc_otg_get_remotewakesig(dwc_otg_core_if_t * core_if);
  32163. +
  32164. +/**
  32165. + * Get value of prt_sleep_sts field from the GLPMCFG register
  32166. + */
  32167. +extern uint32_t dwc_otg_get_lpm_portsleepstatus(dwc_otg_core_if_t * core_if);
  32168. +
  32169. +/**
  32170. + * Get value of rem_wkup_en field from the GLPMCFG register
  32171. + */
  32172. +extern uint32_t dwc_otg_get_lpm_remotewakeenabled(dwc_otg_core_if_t * core_if);
  32173. +
  32174. +/**
  32175. + * Get value of appl_resp field from the GLPMCFG register
  32176. + */
  32177. +extern uint32_t dwc_otg_get_lpmresponse(dwc_otg_core_if_t * core_if);
  32178. +/**
  32179. + * Set value of appl_resp field from the GLPMCFG register
  32180. + */
  32181. +extern void dwc_otg_set_lpmresponse(dwc_otg_core_if_t * core_if, uint32_t val);
  32182. +
  32183. +/**
  32184. + * Get value of hsic_connect field from the GLPMCFG register
  32185. + */
  32186. +extern uint32_t dwc_otg_get_hsic_connect(dwc_otg_core_if_t * core_if);
  32187. +/**
  32188. + * Set value of hsic_connect field from the GLPMCFG register
  32189. + */
  32190. +extern void dwc_otg_set_hsic_connect(dwc_otg_core_if_t * core_if, uint32_t val);
  32191. +
  32192. +/**
  32193. + * Get value of inv_sel_hsic field from the GLPMCFG register.
  32194. + */
  32195. +extern uint32_t dwc_otg_get_inv_sel_hsic(dwc_otg_core_if_t * core_if);
  32196. +/**
  32197. + * Set value of inv_sel_hsic field from the GLPMFG register.
  32198. + */
  32199. +extern void dwc_otg_set_inv_sel_hsic(dwc_otg_core_if_t * core_if, uint32_t val);
  32200. +
  32201. +/*
  32202. + * Some functions for accessing registers
  32203. + */
  32204. +
  32205. +/**
  32206. + * GOTGCTL register
  32207. + */
  32208. +extern uint32_t dwc_otg_get_gotgctl(dwc_otg_core_if_t * core_if);
  32209. +extern void dwc_otg_set_gotgctl(dwc_otg_core_if_t * core_if, uint32_t val);
  32210. +
  32211. +/**
  32212. + * GUSBCFG register
  32213. + */
  32214. +extern uint32_t dwc_otg_get_gusbcfg(dwc_otg_core_if_t * core_if);
  32215. +extern void dwc_otg_set_gusbcfg(dwc_otg_core_if_t * core_if, uint32_t val);
  32216. +
  32217. +/**
  32218. + * GRXFSIZ register
  32219. + */
  32220. +extern uint32_t dwc_otg_get_grxfsiz(dwc_otg_core_if_t * core_if);
  32221. +extern void dwc_otg_set_grxfsiz(dwc_otg_core_if_t * core_if, uint32_t val);
  32222. +
  32223. +/**
  32224. + * GNPTXFSIZ register
  32225. + */
  32226. +extern uint32_t dwc_otg_get_gnptxfsiz(dwc_otg_core_if_t * core_if);
  32227. +extern void dwc_otg_set_gnptxfsiz(dwc_otg_core_if_t * core_if, uint32_t val);
  32228. +
  32229. +extern uint32_t dwc_otg_get_gpvndctl(dwc_otg_core_if_t * core_if);
  32230. +extern void dwc_otg_set_gpvndctl(dwc_otg_core_if_t * core_if, uint32_t val);
  32231. +
  32232. +/**
  32233. + * GGPIO register
  32234. + */
  32235. +extern uint32_t dwc_otg_get_ggpio(dwc_otg_core_if_t * core_if);
  32236. +extern void dwc_otg_set_ggpio(dwc_otg_core_if_t * core_if, uint32_t val);
  32237. +
  32238. +/**
  32239. + * GUID register
  32240. + */
  32241. +extern uint32_t dwc_otg_get_guid(dwc_otg_core_if_t * core_if);
  32242. +extern void dwc_otg_set_guid(dwc_otg_core_if_t * core_if, uint32_t val);
  32243. +
  32244. +/**
  32245. + * HPRT0 register
  32246. + */
  32247. +extern uint32_t dwc_otg_get_hprt0(dwc_otg_core_if_t * core_if);
  32248. +extern void dwc_otg_set_hprt0(dwc_otg_core_if_t * core_if, uint32_t val);
  32249. +
  32250. +/**
  32251. + * GHPTXFSIZE
  32252. + */
  32253. +extern uint32_t dwc_otg_get_hptxfsiz(dwc_otg_core_if_t * core_if);
  32254. +
  32255. +/** @} */
  32256. +
  32257. +#endif /* __DWC_CORE_IF_H__ */
  32258. --- /dev/null
  32259. +++ b/drivers/usb/host/dwc_otg/dwc_otg_dbg.h
  32260. @@ -0,0 +1,117 @@
  32261. +/* ==========================================================================
  32262. + *
  32263. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  32264. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  32265. + * otherwise expressly agreed to in writing between Synopsys and you.
  32266. + *
  32267. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  32268. + * any End User Software License Agreement or Agreement for Licensed Product
  32269. + * with Synopsys or any supplement thereto. You are permitted to use and
  32270. + * redistribute this Software in source and binary forms, with or without
  32271. + * modification, provided that redistributions of source code must retain this
  32272. + * notice. You may not view, use, disclose, copy or distribute this file or
  32273. + * any information contained herein except pursuant to this license grant from
  32274. + * Synopsys. If you do not agree with this notice, including the disclaimer
  32275. + * below, then you are not authorized to use the Software.
  32276. + *
  32277. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  32278. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  32279. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  32280. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  32281. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  32282. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  32283. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  32284. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32285. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  32286. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  32287. + * DAMAGE.
  32288. + * ========================================================================== */
  32289. +
  32290. +#ifndef __DWC_OTG_DBG_H__
  32291. +#define __DWC_OTG_DBG_H__
  32292. +
  32293. +/** @file
  32294. + * This file defines debug levels.
  32295. + * Debugging support vanishes in non-debug builds.
  32296. + */
  32297. +
  32298. +/**
  32299. + * The Debug Level bit-mask variable.
  32300. + */
  32301. +extern uint32_t g_dbg_lvl;
  32302. +/**
  32303. + * Set the Debug Level variable.
  32304. + */
  32305. +static inline uint32_t SET_DEBUG_LEVEL(const uint32_t new)
  32306. +{
  32307. + uint32_t old = g_dbg_lvl;
  32308. + g_dbg_lvl = new;
  32309. + return old;
  32310. +}
  32311. +
  32312. +#define DBG_USER (0x1)
  32313. +/** When debug level has the DBG_CIL bit set, display CIL Debug messages. */
  32314. +#define DBG_CIL (0x2)
  32315. +/** When debug level has the DBG_CILV bit set, display CIL Verbose debug
  32316. + * messages */
  32317. +#define DBG_CILV (0x20)
  32318. +/** When debug level has the DBG_PCD bit set, display PCD (Device) debug
  32319. + * messages */
  32320. +#define DBG_PCD (0x4)
  32321. +/** When debug level has the DBG_PCDV set, display PCD (Device) Verbose debug
  32322. + * messages */
  32323. +#define DBG_PCDV (0x40)
  32324. +/** When debug level has the DBG_HCD bit set, display Host debug messages */
  32325. +#define DBG_HCD (0x8)
  32326. +/** When debug level has the DBG_HCDV bit set, display Verbose Host debug
  32327. + * messages */
  32328. +#define DBG_HCDV (0x80)
  32329. +/** When debug level has the DBG_HCD_URB bit set, display enqueued URBs in host
  32330. + * mode. */
  32331. +#define DBG_HCD_URB (0x800)
  32332. +/** When debug level has the DBG_HCDI bit set, display host interrupt
  32333. + * messages. */
  32334. +#define DBG_HCDI (0x1000)
  32335. +
  32336. +/** When debug level has any bit set, display debug messages */
  32337. +#define DBG_ANY (0xFF)
  32338. +
  32339. +/** All debug messages off */
  32340. +#define DBG_OFF 0
  32341. +
  32342. +/** Prefix string for DWC_DEBUG print macros. */
  32343. +#define USB_DWC "DWC_otg: "
  32344. +
  32345. +/**
  32346. + * Print a debug message when the Global debug level variable contains
  32347. + * the bit defined in <code>lvl</code>.
  32348. + *
  32349. + * @param[in] lvl - Debug level, use one of the DBG_ constants above.
  32350. + * @param[in] x - like printf
  32351. + *
  32352. + * Example:<p>
  32353. + * <code>
  32354. + * DWC_DEBUGPL( DBG_ANY, "%s(%p)\n", __func__, _reg_base_addr);
  32355. + * </code>
  32356. + * <br>
  32357. + * results in:<br>
  32358. + * <code>
  32359. + * usb-DWC_otg: dwc_otg_cil_init(ca867000)
  32360. + * </code>
  32361. + */
  32362. +#ifdef DEBUG
  32363. +
  32364. +# define DWC_DEBUGPL(lvl, x...) do{ if ((lvl)&g_dbg_lvl)__DWC_DEBUG(USB_DWC x ); }while(0)
  32365. +# define DWC_DEBUGP(x...) DWC_DEBUGPL(DBG_ANY, x )
  32366. +
  32367. +# define CHK_DEBUG_LEVEL(level) ((level) & g_dbg_lvl)
  32368. +
  32369. +#else
  32370. +
  32371. +# define DWC_DEBUGPL(lvl, x...) do{}while(0)
  32372. +# define DWC_DEBUGP(x...)
  32373. +
  32374. +# define CHK_DEBUG_LEVEL(level) (0)
  32375. +
  32376. +#endif /*DEBUG*/
  32377. +#endif
  32378. --- /dev/null
  32379. +++ b/drivers/usb/host/dwc_otg/dwc_otg_driver.c
  32380. @@ -0,0 +1,1757 @@
  32381. +/* ==========================================================================
  32382. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_driver.c $
  32383. + * $Revision: #92 $
  32384. + * $Date: 2012/08/10 $
  32385. + * $Change: 2047372 $
  32386. + *
  32387. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  32388. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  32389. + * otherwise expressly agreed to in writing between Synopsys and you.
  32390. + *
  32391. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  32392. + * any End User Software License Agreement or Agreement for Licensed Product
  32393. + * with Synopsys or any supplement thereto. You are permitted to use and
  32394. + * redistribute this Software in source and binary forms, with or without
  32395. + * modification, provided that redistributions of source code must retain this
  32396. + * notice. You may not view, use, disclose, copy or distribute this file or
  32397. + * any information contained herein except pursuant to this license grant from
  32398. + * Synopsys. If you do not agree with this notice, including the disclaimer
  32399. + * below, then you are not authorized to use the Software.
  32400. + *
  32401. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  32402. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  32403. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  32404. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  32405. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  32406. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  32407. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  32408. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32409. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  32410. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  32411. + * DAMAGE.
  32412. + * ========================================================================== */
  32413. +
  32414. +/** @file
  32415. + * The dwc_otg_driver module provides the initialization and cleanup entry
  32416. + * points for the DWC_otg driver. This module will be dynamically installed
  32417. + * after Linux is booted using the insmod command. When the module is
  32418. + * installed, the dwc_otg_driver_init function is called. When the module is
  32419. + * removed (using rmmod), the dwc_otg_driver_cleanup function is called.
  32420. + *
  32421. + * This module also defines a data structure for the dwc_otg_driver, which is
  32422. + * used in conjunction with the standard ARM lm_device structure. These
  32423. + * structures allow the OTG driver to comply with the standard Linux driver
  32424. + * model in which devices and drivers are registered with a bus driver. This
  32425. + * has the benefit that Linux can expose attributes of the driver and device
  32426. + * in its special sysfs file system. Users can then read or write files in
  32427. + * this file system to perform diagnostics on the driver components or the
  32428. + * device.
  32429. + */
  32430. +
  32431. +#include "dwc_otg_os_dep.h"
  32432. +#include "dwc_os.h"
  32433. +#include "dwc_otg_dbg.h"
  32434. +#include "dwc_otg_driver.h"
  32435. +#include "dwc_otg_attr.h"
  32436. +#include "dwc_otg_core_if.h"
  32437. +#include "dwc_otg_pcd_if.h"
  32438. +#include "dwc_otg_hcd_if.h"
  32439. +#include "dwc_otg_fiq_fsm.h"
  32440. +
  32441. +#define DWC_DRIVER_VERSION "3.00a 10-AUG-2012"
  32442. +#define DWC_DRIVER_DESC "HS OTG USB Controller driver"
  32443. +
  32444. +bool microframe_schedule=true;
  32445. +
  32446. +static const char dwc_driver_name[] = "dwc_otg";
  32447. +
  32448. +
  32449. +extern int pcd_init(
  32450. +#ifdef LM_INTERFACE
  32451. + struct lm_device *_dev
  32452. +#elif defined(PCI_INTERFACE)
  32453. + struct pci_dev *_dev
  32454. +#elif defined(PLATFORM_INTERFACE)
  32455. + struct platform_device *dev
  32456. +#endif
  32457. + );
  32458. +extern int hcd_init(
  32459. +#ifdef LM_INTERFACE
  32460. + struct lm_device *_dev
  32461. +#elif defined(PCI_INTERFACE)
  32462. + struct pci_dev *_dev
  32463. +#elif defined(PLATFORM_INTERFACE)
  32464. + struct platform_device *dev
  32465. +#endif
  32466. + );
  32467. +
  32468. +extern int pcd_remove(
  32469. +#ifdef LM_INTERFACE
  32470. + struct lm_device *_dev
  32471. +#elif defined(PCI_INTERFACE)
  32472. + struct pci_dev *_dev
  32473. +#elif defined(PLATFORM_INTERFACE)
  32474. + struct platform_device *_dev
  32475. +#endif
  32476. + );
  32477. +
  32478. +extern void hcd_remove(
  32479. +#ifdef LM_INTERFACE
  32480. + struct lm_device *_dev
  32481. +#elif defined(PCI_INTERFACE)
  32482. + struct pci_dev *_dev
  32483. +#elif defined(PLATFORM_INTERFACE)
  32484. + struct platform_device *_dev
  32485. +#endif
  32486. + );
  32487. +
  32488. +extern void dwc_otg_adp_start(dwc_otg_core_if_t * core_if, uint8_t is_host);
  32489. +
  32490. +/*-------------------------------------------------------------------------*/
  32491. +/* Encapsulate the module parameter settings */
  32492. +
  32493. +struct dwc_otg_driver_module_params {
  32494. + int32_t opt;
  32495. + int32_t otg_cap;
  32496. + int32_t dma_enable;
  32497. + int32_t dma_desc_enable;
  32498. + int32_t dma_burst_size;
  32499. + int32_t speed;
  32500. + int32_t host_support_fs_ls_low_power;
  32501. + int32_t host_ls_low_power_phy_clk;
  32502. + int32_t enable_dynamic_fifo;
  32503. + int32_t data_fifo_size;
  32504. + int32_t dev_rx_fifo_size;
  32505. + int32_t dev_nperio_tx_fifo_size;
  32506. + uint32_t dev_perio_tx_fifo_size[MAX_PERIO_FIFOS];
  32507. + int32_t host_rx_fifo_size;
  32508. + int32_t host_nperio_tx_fifo_size;
  32509. + int32_t host_perio_tx_fifo_size;
  32510. + int32_t max_transfer_size;
  32511. + int32_t max_packet_count;
  32512. + int32_t host_channels;
  32513. + int32_t dev_endpoints;
  32514. + int32_t phy_type;
  32515. + int32_t phy_utmi_width;
  32516. + int32_t phy_ulpi_ddr;
  32517. + int32_t phy_ulpi_ext_vbus;
  32518. + int32_t i2c_enable;
  32519. + int32_t ulpi_fs_ls;
  32520. + int32_t ts_dline;
  32521. + int32_t en_multiple_tx_fifo;
  32522. + uint32_t dev_tx_fifo_size[MAX_TX_FIFOS];
  32523. + uint32_t thr_ctl;
  32524. + uint32_t tx_thr_length;
  32525. + uint32_t rx_thr_length;
  32526. + int32_t pti_enable;
  32527. + int32_t mpi_enable;
  32528. + int32_t lpm_enable;
  32529. + int32_t ic_usb_cap;
  32530. + int32_t ahb_thr_ratio;
  32531. + int32_t power_down;
  32532. + int32_t reload_ctl;
  32533. + int32_t dev_out_nak;
  32534. + int32_t cont_on_bna;
  32535. + int32_t ahb_single;
  32536. + int32_t otg_ver;
  32537. + int32_t adp_enable;
  32538. +};
  32539. +
  32540. +static struct dwc_otg_driver_module_params dwc_otg_module_params = {
  32541. + .opt = -1,
  32542. + .otg_cap = -1,
  32543. + .dma_enable = -1,
  32544. + .dma_desc_enable = -1,
  32545. + .dma_burst_size = -1,
  32546. + .speed = -1,
  32547. + .host_support_fs_ls_low_power = -1,
  32548. + .host_ls_low_power_phy_clk = -1,
  32549. + .enable_dynamic_fifo = -1,
  32550. + .data_fifo_size = -1,
  32551. + .dev_rx_fifo_size = -1,
  32552. + .dev_nperio_tx_fifo_size = -1,
  32553. + .dev_perio_tx_fifo_size = {
  32554. + /* dev_perio_tx_fifo_size_1 */
  32555. + -1,
  32556. + -1,
  32557. + -1,
  32558. + -1,
  32559. + -1,
  32560. + -1,
  32561. + -1,
  32562. + -1,
  32563. + -1,
  32564. + -1,
  32565. + -1,
  32566. + -1,
  32567. + -1,
  32568. + -1,
  32569. + -1
  32570. + /* 15 */
  32571. + },
  32572. + .host_rx_fifo_size = -1,
  32573. + .host_nperio_tx_fifo_size = -1,
  32574. + .host_perio_tx_fifo_size = -1,
  32575. + .max_transfer_size = -1,
  32576. + .max_packet_count = -1,
  32577. + .host_channels = -1,
  32578. + .dev_endpoints = -1,
  32579. + .phy_type = -1,
  32580. + .phy_utmi_width = -1,
  32581. + .phy_ulpi_ddr = -1,
  32582. + .phy_ulpi_ext_vbus = -1,
  32583. + .i2c_enable = -1,
  32584. + .ulpi_fs_ls = -1,
  32585. + .ts_dline = -1,
  32586. + .en_multiple_tx_fifo = -1,
  32587. + .dev_tx_fifo_size = {
  32588. + /* dev_tx_fifo_size */
  32589. + -1,
  32590. + -1,
  32591. + -1,
  32592. + -1,
  32593. + -1,
  32594. + -1,
  32595. + -1,
  32596. + -1,
  32597. + -1,
  32598. + -1,
  32599. + -1,
  32600. + -1,
  32601. + -1,
  32602. + -1,
  32603. + -1
  32604. + /* 15 */
  32605. + },
  32606. + .thr_ctl = -1,
  32607. + .tx_thr_length = -1,
  32608. + .rx_thr_length = -1,
  32609. + .pti_enable = -1,
  32610. + .mpi_enable = -1,
  32611. + .lpm_enable = 0,
  32612. + .ic_usb_cap = -1,
  32613. + .ahb_thr_ratio = -1,
  32614. + .power_down = -1,
  32615. + .reload_ctl = -1,
  32616. + .dev_out_nak = -1,
  32617. + .cont_on_bna = -1,
  32618. + .ahb_single = -1,
  32619. + .otg_ver = -1,
  32620. + .adp_enable = -1,
  32621. +};
  32622. +
  32623. +//Global variable to switch the fiq fix on or off
  32624. +bool fiq_enable = 1;
  32625. +// Global variable to enable the split transaction fix
  32626. +bool fiq_fsm_enable = true;
  32627. +//Bulk split-transaction NAK holdoff in microframes
  32628. +uint16_t nak_holdoff = 8;
  32629. +
  32630. +unsigned short fiq_fsm_mask = 0x0F;
  32631. +
  32632. +/**
  32633. + * This function shows the Driver Version.
  32634. + */
  32635. +static ssize_t version_show(struct device_driver *dev, char *buf)
  32636. +{
  32637. + return snprintf(buf, sizeof(DWC_DRIVER_VERSION) + 2, "%s\n",
  32638. + DWC_DRIVER_VERSION);
  32639. +}
  32640. +
  32641. +static DRIVER_ATTR(version, S_IRUGO, version_show, NULL);
  32642. +
  32643. +/**
  32644. + * Global Debug Level Mask.
  32645. + */
  32646. +uint32_t g_dbg_lvl = 0; /* OFF */
  32647. +
  32648. +/**
  32649. + * This function shows the driver Debug Level.
  32650. + */
  32651. +static ssize_t dbg_level_show(struct device_driver *drv, char *buf)
  32652. +{
  32653. + return sprintf(buf, "0x%0x\n", g_dbg_lvl);
  32654. +}
  32655. +
  32656. +/**
  32657. + * This function stores the driver Debug Level.
  32658. + */
  32659. +static ssize_t dbg_level_store(struct device_driver *drv, const char *buf,
  32660. + size_t count)
  32661. +{
  32662. + g_dbg_lvl = simple_strtoul(buf, NULL, 16);
  32663. + return count;
  32664. +}
  32665. +
  32666. +static DRIVER_ATTR(debuglevel, S_IRUGO | S_IWUSR, dbg_level_show,
  32667. + dbg_level_store);
  32668. +
  32669. +/**
  32670. + * This function is called during module intialization
  32671. + * to pass module parameters to the DWC_OTG CORE.
  32672. + */
  32673. +static int set_parameters(dwc_otg_core_if_t * core_if)
  32674. +{
  32675. + int retval = 0;
  32676. + int i;
  32677. +
  32678. + if (dwc_otg_module_params.otg_cap != -1) {
  32679. + retval +=
  32680. + dwc_otg_set_param_otg_cap(core_if,
  32681. + dwc_otg_module_params.otg_cap);
  32682. + }
  32683. + if (dwc_otg_module_params.dma_enable != -1) {
  32684. + retval +=
  32685. + dwc_otg_set_param_dma_enable(core_if,
  32686. + dwc_otg_module_params.
  32687. + dma_enable);
  32688. + }
  32689. + if (dwc_otg_module_params.dma_desc_enable != -1) {
  32690. + retval +=
  32691. + dwc_otg_set_param_dma_desc_enable(core_if,
  32692. + dwc_otg_module_params.
  32693. + dma_desc_enable);
  32694. + }
  32695. + if (dwc_otg_module_params.opt != -1) {
  32696. + retval +=
  32697. + dwc_otg_set_param_opt(core_if, dwc_otg_module_params.opt);
  32698. + }
  32699. + if (dwc_otg_module_params.dma_burst_size != -1) {
  32700. + retval +=
  32701. + dwc_otg_set_param_dma_burst_size(core_if,
  32702. + dwc_otg_module_params.
  32703. + dma_burst_size);
  32704. + }
  32705. + if (dwc_otg_module_params.host_support_fs_ls_low_power != -1) {
  32706. + retval +=
  32707. + dwc_otg_set_param_host_support_fs_ls_low_power(core_if,
  32708. + dwc_otg_module_params.
  32709. + host_support_fs_ls_low_power);
  32710. + }
  32711. + if (dwc_otg_module_params.enable_dynamic_fifo != -1) {
  32712. + retval +=
  32713. + dwc_otg_set_param_enable_dynamic_fifo(core_if,
  32714. + dwc_otg_module_params.
  32715. + enable_dynamic_fifo);
  32716. + }
  32717. + if (dwc_otg_module_params.data_fifo_size != -1) {
  32718. + retval +=
  32719. + dwc_otg_set_param_data_fifo_size(core_if,
  32720. + dwc_otg_module_params.
  32721. + data_fifo_size);
  32722. + }
  32723. + if (dwc_otg_module_params.dev_rx_fifo_size != -1) {
  32724. + retval +=
  32725. + dwc_otg_set_param_dev_rx_fifo_size(core_if,
  32726. + dwc_otg_module_params.
  32727. + dev_rx_fifo_size);
  32728. + }
  32729. + if (dwc_otg_module_params.dev_nperio_tx_fifo_size != -1) {
  32730. + retval +=
  32731. + dwc_otg_set_param_dev_nperio_tx_fifo_size(core_if,
  32732. + dwc_otg_module_params.
  32733. + dev_nperio_tx_fifo_size);
  32734. + }
  32735. + if (dwc_otg_module_params.host_rx_fifo_size != -1) {
  32736. + retval +=
  32737. + dwc_otg_set_param_host_rx_fifo_size(core_if,
  32738. + dwc_otg_module_params.host_rx_fifo_size);
  32739. + }
  32740. + if (dwc_otg_module_params.host_nperio_tx_fifo_size != -1) {
  32741. + retval +=
  32742. + dwc_otg_set_param_host_nperio_tx_fifo_size(core_if,
  32743. + dwc_otg_module_params.
  32744. + host_nperio_tx_fifo_size);
  32745. + }
  32746. + if (dwc_otg_module_params.host_perio_tx_fifo_size != -1) {
  32747. + retval +=
  32748. + dwc_otg_set_param_host_perio_tx_fifo_size(core_if,
  32749. + dwc_otg_module_params.
  32750. + host_perio_tx_fifo_size);
  32751. + }
  32752. + if (dwc_otg_module_params.max_transfer_size != -1) {
  32753. + retval +=
  32754. + dwc_otg_set_param_max_transfer_size(core_if,
  32755. + dwc_otg_module_params.
  32756. + max_transfer_size);
  32757. + }
  32758. + if (dwc_otg_module_params.max_packet_count != -1) {
  32759. + retval +=
  32760. + dwc_otg_set_param_max_packet_count(core_if,
  32761. + dwc_otg_module_params.
  32762. + max_packet_count);
  32763. + }
  32764. + if (dwc_otg_module_params.host_channels != -1) {
  32765. + retval +=
  32766. + dwc_otg_set_param_host_channels(core_if,
  32767. + dwc_otg_module_params.
  32768. + host_channels);
  32769. + }
  32770. + if (dwc_otg_module_params.dev_endpoints != -1) {
  32771. + retval +=
  32772. + dwc_otg_set_param_dev_endpoints(core_if,
  32773. + dwc_otg_module_params.
  32774. + dev_endpoints);
  32775. + }
  32776. + if (dwc_otg_module_params.phy_type != -1) {
  32777. + retval +=
  32778. + dwc_otg_set_param_phy_type(core_if,
  32779. + dwc_otg_module_params.phy_type);
  32780. + }
  32781. + if (dwc_otg_module_params.speed != -1) {
  32782. + retval +=
  32783. + dwc_otg_set_param_speed(core_if,
  32784. + dwc_otg_module_params.speed);
  32785. + }
  32786. + if (dwc_otg_module_params.host_ls_low_power_phy_clk != -1) {
  32787. + retval +=
  32788. + dwc_otg_set_param_host_ls_low_power_phy_clk(core_if,
  32789. + dwc_otg_module_params.
  32790. + host_ls_low_power_phy_clk);
  32791. + }
  32792. + if (dwc_otg_module_params.phy_ulpi_ddr != -1) {
  32793. + retval +=
  32794. + dwc_otg_set_param_phy_ulpi_ddr(core_if,
  32795. + dwc_otg_module_params.
  32796. + phy_ulpi_ddr);
  32797. + }
  32798. + if (dwc_otg_module_params.phy_ulpi_ext_vbus != -1) {
  32799. + retval +=
  32800. + dwc_otg_set_param_phy_ulpi_ext_vbus(core_if,
  32801. + dwc_otg_module_params.
  32802. + phy_ulpi_ext_vbus);
  32803. + }
  32804. + if (dwc_otg_module_params.phy_utmi_width != -1) {
  32805. + retval +=
  32806. + dwc_otg_set_param_phy_utmi_width(core_if,
  32807. + dwc_otg_module_params.
  32808. + phy_utmi_width);
  32809. + }
  32810. + if (dwc_otg_module_params.ulpi_fs_ls != -1) {
  32811. + retval +=
  32812. + dwc_otg_set_param_ulpi_fs_ls(core_if,
  32813. + dwc_otg_module_params.ulpi_fs_ls);
  32814. + }
  32815. + if (dwc_otg_module_params.ts_dline != -1) {
  32816. + retval +=
  32817. + dwc_otg_set_param_ts_dline(core_if,
  32818. + dwc_otg_module_params.ts_dline);
  32819. + }
  32820. + if (dwc_otg_module_params.i2c_enable != -1) {
  32821. + retval +=
  32822. + dwc_otg_set_param_i2c_enable(core_if,
  32823. + dwc_otg_module_params.
  32824. + i2c_enable);
  32825. + }
  32826. + if (dwc_otg_module_params.en_multiple_tx_fifo != -1) {
  32827. + retval +=
  32828. + dwc_otg_set_param_en_multiple_tx_fifo(core_if,
  32829. + dwc_otg_module_params.
  32830. + en_multiple_tx_fifo);
  32831. + }
  32832. + for (i = 0; i < 15; i++) {
  32833. + if (dwc_otg_module_params.dev_perio_tx_fifo_size[i] != -1) {
  32834. + retval +=
  32835. + dwc_otg_set_param_dev_perio_tx_fifo_size(core_if,
  32836. + dwc_otg_module_params.
  32837. + dev_perio_tx_fifo_size
  32838. + [i], i);
  32839. + }
  32840. + }
  32841. +
  32842. + for (i = 0; i < 15; i++) {
  32843. + if (dwc_otg_module_params.dev_tx_fifo_size[i] != -1) {
  32844. + retval += dwc_otg_set_param_dev_tx_fifo_size(core_if,
  32845. + dwc_otg_module_params.
  32846. + dev_tx_fifo_size
  32847. + [i], i);
  32848. + }
  32849. + }
  32850. + if (dwc_otg_module_params.thr_ctl != -1) {
  32851. + retval +=
  32852. + dwc_otg_set_param_thr_ctl(core_if,
  32853. + dwc_otg_module_params.thr_ctl);
  32854. + }
  32855. + if (dwc_otg_module_params.mpi_enable != -1) {
  32856. + retval +=
  32857. + dwc_otg_set_param_mpi_enable(core_if,
  32858. + dwc_otg_module_params.
  32859. + mpi_enable);
  32860. + }
  32861. + if (dwc_otg_module_params.pti_enable != -1) {
  32862. + retval +=
  32863. + dwc_otg_set_param_pti_enable(core_if,
  32864. + dwc_otg_module_params.
  32865. + pti_enable);
  32866. + }
  32867. + if (dwc_otg_module_params.lpm_enable != -1) {
  32868. + retval +=
  32869. + dwc_otg_set_param_lpm_enable(core_if,
  32870. + dwc_otg_module_params.
  32871. + lpm_enable);
  32872. + }
  32873. + if (dwc_otg_module_params.ic_usb_cap != -1) {
  32874. + retval +=
  32875. + dwc_otg_set_param_ic_usb_cap(core_if,
  32876. + dwc_otg_module_params.
  32877. + ic_usb_cap);
  32878. + }
  32879. + if (dwc_otg_module_params.tx_thr_length != -1) {
  32880. + retval +=
  32881. + dwc_otg_set_param_tx_thr_length(core_if,
  32882. + dwc_otg_module_params.tx_thr_length);
  32883. + }
  32884. + if (dwc_otg_module_params.rx_thr_length != -1) {
  32885. + retval +=
  32886. + dwc_otg_set_param_rx_thr_length(core_if,
  32887. + dwc_otg_module_params.
  32888. + rx_thr_length);
  32889. + }
  32890. + if (dwc_otg_module_params.ahb_thr_ratio != -1) {
  32891. + retval +=
  32892. + dwc_otg_set_param_ahb_thr_ratio(core_if,
  32893. + dwc_otg_module_params.ahb_thr_ratio);
  32894. + }
  32895. + if (dwc_otg_module_params.power_down != -1) {
  32896. + retval +=
  32897. + dwc_otg_set_param_power_down(core_if,
  32898. + dwc_otg_module_params.power_down);
  32899. + }
  32900. + if (dwc_otg_module_params.reload_ctl != -1) {
  32901. + retval +=
  32902. + dwc_otg_set_param_reload_ctl(core_if,
  32903. + dwc_otg_module_params.reload_ctl);
  32904. + }
  32905. +
  32906. + if (dwc_otg_module_params.dev_out_nak != -1) {
  32907. + retval +=
  32908. + dwc_otg_set_param_dev_out_nak(core_if,
  32909. + dwc_otg_module_params.dev_out_nak);
  32910. + }
  32911. +
  32912. + if (dwc_otg_module_params.cont_on_bna != -1) {
  32913. + retval +=
  32914. + dwc_otg_set_param_cont_on_bna(core_if,
  32915. + dwc_otg_module_params.cont_on_bna);
  32916. + }
  32917. +
  32918. + if (dwc_otg_module_params.ahb_single != -1) {
  32919. + retval +=
  32920. + dwc_otg_set_param_ahb_single(core_if,
  32921. + dwc_otg_module_params.ahb_single);
  32922. + }
  32923. +
  32924. + if (dwc_otg_module_params.otg_ver != -1) {
  32925. + retval +=
  32926. + dwc_otg_set_param_otg_ver(core_if,
  32927. + dwc_otg_module_params.otg_ver);
  32928. + }
  32929. + if (dwc_otg_module_params.adp_enable != -1) {
  32930. + retval +=
  32931. + dwc_otg_set_param_adp_enable(core_if,
  32932. + dwc_otg_module_params.
  32933. + adp_enable);
  32934. + }
  32935. + return retval;
  32936. +}
  32937. +
  32938. +/**
  32939. + * This function is the top level interrupt handler for the Common
  32940. + * (Device and host modes) interrupts.
  32941. + */
  32942. +static irqreturn_t dwc_otg_common_irq(int irq, void *dev)
  32943. +{
  32944. + int32_t retval = IRQ_NONE;
  32945. +
  32946. + retval = dwc_otg_handle_common_intr(dev);
  32947. + if (retval != 0) {
  32948. + S3C2410X_CLEAR_EINTPEND();
  32949. + }
  32950. + return IRQ_RETVAL(retval);
  32951. +}
  32952. +
  32953. +/**
  32954. + * This function is called when a lm_device is unregistered with the
  32955. + * dwc_otg_driver. This happens, for example, when the rmmod command is
  32956. + * executed. The device may or may not be electrically present. If it is
  32957. + * present, the driver stops device processing. Any resources used on behalf
  32958. + * of this device are freed.
  32959. + *
  32960. + * @param _dev
  32961. + */
  32962. +#ifdef LM_INTERFACE
  32963. +#define REM_RETVAL(n)
  32964. +static void dwc_otg_driver_remove( struct lm_device *_dev )
  32965. +{ dwc_otg_device_t *otg_dev = lm_get_drvdata(_dev);
  32966. +#elif defined(PCI_INTERFACE)
  32967. +#define REM_RETVAL(n)
  32968. +static void dwc_otg_driver_remove( struct pci_dev *_dev )
  32969. +{ dwc_otg_device_t *otg_dev = pci_get_drvdata(_dev);
  32970. +#elif defined(PLATFORM_INTERFACE)
  32971. +#define REM_RETVAL(n) n
  32972. +static int dwc_otg_driver_remove( struct platform_device *_dev )
  32973. +{ dwc_otg_device_t *otg_dev = platform_get_drvdata(_dev);
  32974. +#endif
  32975. +
  32976. + DWC_DEBUGPL(DBG_ANY, "%s(%p) otg_dev %p\n", __func__, _dev, otg_dev);
  32977. +
  32978. + if (!otg_dev) {
  32979. + /* Memory allocation for the dwc_otg_device failed. */
  32980. + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev NULL!\n", __func__);
  32981. + return REM_RETVAL(-ENOMEM);
  32982. + }
  32983. +#ifndef DWC_DEVICE_ONLY
  32984. + if (otg_dev->hcd) {
  32985. + hcd_remove(_dev);
  32986. + } else {
  32987. + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev->hcd NULL!\n", __func__);
  32988. + return REM_RETVAL(-EINVAL);
  32989. + }
  32990. +#endif
  32991. +
  32992. +#ifndef DWC_HOST_ONLY
  32993. + if (otg_dev->pcd) {
  32994. + pcd_remove(_dev);
  32995. + } else {
  32996. + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev->pcd NULL!\n", __func__);
  32997. + return REM_RETVAL(-EINVAL);
  32998. + }
  32999. +#endif
  33000. + /*
  33001. + * Free the IRQ
  33002. + */
  33003. + if (otg_dev->common_irq_installed) {
  33004. +#ifdef PLATFORM_INTERFACE
  33005. + free_irq(platform_get_irq(_dev, 0), otg_dev);
  33006. +#else
  33007. + free_irq(_dev->irq, otg_dev);
  33008. +#endif
  33009. + } else {
  33010. + DWC_DEBUGPL(DBG_ANY, "%s: There is no installed irq!\n", __func__);
  33011. + return REM_RETVAL(-ENXIO);
  33012. + }
  33013. +
  33014. + if (otg_dev->core_if) {
  33015. + dwc_otg_cil_remove(otg_dev->core_if);
  33016. + } else {
  33017. + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev->core_if NULL!\n", __func__);
  33018. + return REM_RETVAL(-ENXIO);
  33019. + }
  33020. +
  33021. + /*
  33022. + * Remove the device attributes
  33023. + */
  33024. + dwc_otg_attr_remove(_dev);
  33025. +
  33026. + /*
  33027. + * Return the memory.
  33028. + */
  33029. + if (otg_dev->os_dep.base) {
  33030. + iounmap(otg_dev->os_dep.base);
  33031. + }
  33032. + DWC_FREE(otg_dev);
  33033. +
  33034. + /*
  33035. + * Clear the drvdata pointer.
  33036. + */
  33037. +#ifdef LM_INTERFACE
  33038. + lm_set_drvdata(_dev, 0);
  33039. +#elif defined(PCI_INTERFACE)
  33040. + release_mem_region(otg_dev->os_dep.rsrc_start,
  33041. + otg_dev->os_dep.rsrc_len);
  33042. + pci_set_drvdata(_dev, 0);
  33043. +#elif defined(PLATFORM_INTERFACE)
  33044. + platform_set_drvdata(_dev, 0);
  33045. +#endif
  33046. + return REM_RETVAL(0);
  33047. +}
  33048. +
  33049. +/**
  33050. + * This function is called when an lm_device is bound to a
  33051. + * dwc_otg_driver. It creates the driver components required to
  33052. + * control the device (CIL, HCD, and PCD) and it initializes the
  33053. + * device. The driver components are stored in a dwc_otg_device
  33054. + * structure. A reference to the dwc_otg_device is saved in the
  33055. + * lm_device. This allows the driver to access the dwc_otg_device
  33056. + * structure on subsequent calls to driver methods for this device.
  33057. + *
  33058. + * @param _dev Bus device
  33059. + */
  33060. +static int dwc_otg_driver_probe(
  33061. +#ifdef LM_INTERFACE
  33062. + struct lm_device *_dev
  33063. +#elif defined(PCI_INTERFACE)
  33064. + struct pci_dev *_dev,
  33065. + const struct pci_device_id *id
  33066. +#elif defined(PLATFORM_INTERFACE)
  33067. + struct platform_device *_dev
  33068. +#endif
  33069. + )
  33070. +{
  33071. + int retval = 0;
  33072. + dwc_otg_device_t *dwc_otg_device;
  33073. + int devirq;
  33074. +
  33075. + dev_dbg(&_dev->dev, "dwc_otg_driver_probe(%p)\n", _dev);
  33076. +#ifdef LM_INTERFACE
  33077. + dev_dbg(&_dev->dev, "start=0x%08x\n", (unsigned)_dev->resource.start);
  33078. +#elif defined(PCI_INTERFACE)
  33079. + if (!id) {
  33080. + DWC_ERROR("Invalid pci_device_id %p", id);
  33081. + return -EINVAL;
  33082. + }
  33083. +
  33084. + if (!_dev || (pci_enable_device(_dev) < 0)) {
  33085. + DWC_ERROR("Invalid pci_device %p", _dev);
  33086. + return -ENODEV;
  33087. + }
  33088. + dev_dbg(&_dev->dev, "start=0x%08x\n", (unsigned)pci_resource_start(_dev,0));
  33089. + /* other stuff needed as well? */
  33090. +
  33091. +#elif defined(PLATFORM_INTERFACE)
  33092. + dev_dbg(&_dev->dev, "start=0x%08x (len 0x%x)\n",
  33093. + (unsigned)_dev->resource->start,
  33094. + (unsigned)(_dev->resource->end - _dev->resource->start));
  33095. +#endif
  33096. +
  33097. + dwc_otg_device = DWC_ALLOC(sizeof(dwc_otg_device_t));
  33098. +
  33099. + if (!dwc_otg_device) {
  33100. + dev_err(&_dev->dev, "kmalloc of dwc_otg_device failed\n");
  33101. + return -ENOMEM;
  33102. + }
  33103. +
  33104. + memset(dwc_otg_device, 0, sizeof(*dwc_otg_device));
  33105. + dwc_otg_device->os_dep.reg_offset = 0xFFFFFFFF;
  33106. + dwc_otg_device->os_dep.platformdev = _dev;
  33107. +
  33108. + /*
  33109. + * Map the DWC_otg Core memory into virtual address space.
  33110. + */
  33111. +#ifdef LM_INTERFACE
  33112. + dwc_otg_device->os_dep.base = ioremap(_dev->resource.start, SZ_256K);
  33113. +
  33114. + if (!dwc_otg_device->os_dep.base) {
  33115. + dev_err(&_dev->dev, "ioremap() failed\n");
  33116. + DWC_FREE(dwc_otg_device);
  33117. + return -ENOMEM;
  33118. + }
  33119. + dev_dbg(&_dev->dev, "base=0x%08x\n",
  33120. + (unsigned)dwc_otg_device->os_dep.base);
  33121. +#elif defined(PCI_INTERFACE)
  33122. + _dev->current_state = PCI_D0;
  33123. + _dev->dev.power.power_state = PMSG_ON;
  33124. +
  33125. + if (!_dev->irq) {
  33126. + DWC_ERROR("Found HC with no IRQ. Check BIOS/PCI %s setup!",
  33127. + pci_name(_dev));
  33128. + iounmap(dwc_otg_device->os_dep.base);
  33129. + DWC_FREE(dwc_otg_device);
  33130. + return -ENODEV;
  33131. + }
  33132. +
  33133. + dwc_otg_device->os_dep.rsrc_start = pci_resource_start(_dev, 0);
  33134. + dwc_otg_device->os_dep.rsrc_len = pci_resource_len(_dev, 0);
  33135. + DWC_DEBUGPL(DBG_ANY, "PCI resource: start=%08x, len=%08x\n",
  33136. + (unsigned)dwc_otg_device->os_dep.rsrc_start,
  33137. + (unsigned)dwc_otg_device->os_dep.rsrc_len);
  33138. + if (!request_mem_region
  33139. + (dwc_otg_device->os_dep.rsrc_start, dwc_otg_device->os_dep.rsrc_len,
  33140. + "dwc_otg")) {
  33141. + dev_dbg(&_dev->dev, "error requesting memory\n");
  33142. + iounmap(dwc_otg_device->os_dep.base);
  33143. + DWC_FREE(dwc_otg_device);
  33144. + return -EFAULT;
  33145. + }
  33146. +
  33147. + dwc_otg_device->os_dep.base =
  33148. + ioremap_nocache(dwc_otg_device->os_dep.rsrc_start,
  33149. + dwc_otg_device->os_dep.rsrc_len);
  33150. + if (dwc_otg_device->os_dep.base == NULL) {
  33151. + dev_dbg(&_dev->dev, "error mapping memory\n");
  33152. + release_mem_region(dwc_otg_device->os_dep.rsrc_start,
  33153. + dwc_otg_device->os_dep.rsrc_len);
  33154. + iounmap(dwc_otg_device->os_dep.base);
  33155. + DWC_FREE(dwc_otg_device);
  33156. + return -EFAULT;
  33157. + }
  33158. + dev_dbg(&_dev->dev, "base=0x%p (before adjust) \n",
  33159. + dwc_otg_device->os_dep.base);
  33160. + dwc_otg_device->os_dep.base = (char *)dwc_otg_device->os_dep.base;
  33161. + dev_dbg(&_dev->dev, "base=0x%p (after adjust) \n",
  33162. + dwc_otg_device->os_dep.base);
  33163. + dev_dbg(&_dev->dev, "%s: mapped PA 0x%x to VA 0x%p\n", __func__,
  33164. + (unsigned)dwc_otg_device->os_dep.rsrc_start,
  33165. + dwc_otg_device->os_dep.base);
  33166. +
  33167. + pci_set_master(_dev);
  33168. + pci_set_drvdata(_dev, dwc_otg_device);
  33169. +#elif defined(PLATFORM_INTERFACE)
  33170. + DWC_DEBUGPL(DBG_ANY,"Platform resource: start=%08x, len=%08x\n",
  33171. + _dev->resource->start,
  33172. + _dev->resource->end - _dev->resource->start + 1);
  33173. +#if 1
  33174. + if (!request_mem_region(_dev->resource[0].start,
  33175. + _dev->resource[0].end - _dev->resource[0].start + 1,
  33176. + "dwc_otg")) {
  33177. + dev_dbg(&_dev->dev, "error reserving mapped memory\n");
  33178. + retval = -EFAULT;
  33179. + goto fail;
  33180. + }
  33181. +
  33182. + dwc_otg_device->os_dep.base = ioremap_nocache(_dev->resource[0].start,
  33183. + _dev->resource[0].end -
  33184. + _dev->resource[0].start+1);
  33185. + if (fiq_enable)
  33186. + {
  33187. + if (!request_mem_region(_dev->resource[1].start,
  33188. + _dev->resource[1].end - _dev->resource[1].start + 1,
  33189. + "dwc_otg")) {
  33190. + dev_dbg(&_dev->dev, "error reserving mapped memory\n");
  33191. + retval = -EFAULT;
  33192. + goto fail;
  33193. + }
  33194. +
  33195. + dwc_otg_device->os_dep.mphi_base = ioremap_nocache(_dev->resource[1].start,
  33196. + _dev->resource[1].end -
  33197. + _dev->resource[1].start + 1);
  33198. + }
  33199. +
  33200. +#else
  33201. + {
  33202. + struct map_desc desc = {
  33203. + .virtual = IO_ADDRESS((unsigned)_dev->resource->start),
  33204. + .pfn = __phys_to_pfn((unsigned)_dev->resource->start),
  33205. + .length = SZ_128K,
  33206. + .type = MT_DEVICE
  33207. + };
  33208. + iotable_init(&desc, 1);
  33209. + dwc_otg_device->os_dep.base = (void *)desc.virtual;
  33210. + }
  33211. +#endif
  33212. + if (!dwc_otg_device->os_dep.base) {
  33213. + dev_err(&_dev->dev, "ioremap() failed\n");
  33214. + retval = -ENOMEM;
  33215. + goto fail;
  33216. + }
  33217. + dev_dbg(&_dev->dev, "base=0x%08x\n",
  33218. + (unsigned)dwc_otg_device->os_dep.base);
  33219. +#endif
  33220. +
  33221. + /*
  33222. + * Initialize driver data to point to the global DWC_otg
  33223. + * Device structure.
  33224. + */
  33225. +#ifdef LM_INTERFACE
  33226. + lm_set_drvdata(_dev, dwc_otg_device);
  33227. +#elif defined(PLATFORM_INTERFACE)
  33228. + platform_set_drvdata(_dev, dwc_otg_device);
  33229. +#endif
  33230. + dev_dbg(&_dev->dev, "dwc_otg_device=0x%p\n", dwc_otg_device);
  33231. +
  33232. + dwc_otg_device->core_if = dwc_otg_cil_init(dwc_otg_device->os_dep.base);
  33233. + DWC_DEBUGPL(DBG_HCDV, "probe of device %p given core_if %p\n",
  33234. + dwc_otg_device, dwc_otg_device->core_if);//GRAYG
  33235. +
  33236. + if (!dwc_otg_device->core_if) {
  33237. + dev_err(&_dev->dev, "CIL initialization failed!\n");
  33238. + retval = -ENOMEM;
  33239. + goto fail;
  33240. + }
  33241. +
  33242. + dev_dbg(&_dev->dev, "Calling get_gsnpsid\n");
  33243. + /*
  33244. + * Attempt to ensure this device is really a DWC_otg Controller.
  33245. + * Read and verify the SNPSID register contents. The value should be
  33246. + * 0x45F42XXX or 0x45F42XXX, which corresponds to either "OT2" or "OTG3",
  33247. + * as in "OTG version 2.XX" or "OTG version 3.XX".
  33248. + */
  33249. +
  33250. + if (((dwc_otg_get_gsnpsid(dwc_otg_device->core_if) & 0xFFFFF000) != 0x4F542000) &&
  33251. + ((dwc_otg_get_gsnpsid(dwc_otg_device->core_if) & 0xFFFFF000) != 0x4F543000)) {
  33252. + dev_err(&_dev->dev, "Bad value for SNPSID: 0x%08x\n",
  33253. + dwc_otg_get_gsnpsid(dwc_otg_device->core_if));
  33254. + retval = -EINVAL;
  33255. + goto fail;
  33256. + }
  33257. +
  33258. + /*
  33259. + * Validate parameter values.
  33260. + */
  33261. + dev_dbg(&_dev->dev, "Calling set_parameters\n");
  33262. + if (set_parameters(dwc_otg_device->core_if)) {
  33263. + retval = -EINVAL;
  33264. + goto fail;
  33265. + }
  33266. +
  33267. + /*
  33268. + * Create Device Attributes in sysfs
  33269. + */
  33270. + dev_dbg(&_dev->dev, "Calling attr_create\n");
  33271. + dwc_otg_attr_create(_dev);
  33272. +
  33273. + /*
  33274. + * Disable the global interrupt until all the interrupt
  33275. + * handlers are installed.
  33276. + */
  33277. + dev_dbg(&_dev->dev, "Calling disable_global_interrupts\n");
  33278. + dwc_otg_disable_global_interrupts(dwc_otg_device->core_if);
  33279. +
  33280. + /*
  33281. + * Install the interrupt handler for the common interrupts before
  33282. + * enabling common interrupts in core_init below.
  33283. + */
  33284. +
  33285. +#if defined(PLATFORM_INTERFACE)
  33286. + devirq = platform_get_irq(_dev, fiq_enable ? 0 : 1);
  33287. +#else
  33288. + devirq = _dev->irq;
  33289. +#endif
  33290. + DWC_DEBUGPL(DBG_CIL, "registering (common) handler for irq%d\n",
  33291. + devirq);
  33292. + dev_dbg(&_dev->dev, "Calling request_irq(%d)\n", devirq);
  33293. + retval = request_irq(devirq, dwc_otg_common_irq,
  33294. + IRQF_SHARED,
  33295. + "dwc_otg", dwc_otg_device);
  33296. + if (retval) {
  33297. + DWC_ERROR("request of irq%d failed\n", devirq);
  33298. + retval = -EBUSY;
  33299. + goto fail;
  33300. + } else {
  33301. + dwc_otg_device->common_irq_installed = 1;
  33302. + }
  33303. +
  33304. +#ifndef IRQF_TRIGGER_LOW
  33305. +#if defined(LM_INTERFACE) || defined(PLATFORM_INTERFACE)
  33306. + dev_dbg(&_dev->dev, "Calling set_irq_type\n");
  33307. + set_irq_type(devirq,
  33308. +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30))
  33309. + IRQT_LOW
  33310. +#else
  33311. + IRQ_TYPE_LEVEL_LOW
  33312. +#endif
  33313. + );
  33314. +#endif
  33315. +#endif /*IRQF_TRIGGER_LOW*/
  33316. +
  33317. + /*
  33318. + * Initialize the DWC_otg core.
  33319. + */
  33320. + dev_dbg(&_dev->dev, "Calling dwc_otg_core_init\n");
  33321. + dwc_otg_core_init(dwc_otg_device->core_if);
  33322. +
  33323. +#ifndef DWC_HOST_ONLY
  33324. + /*
  33325. + * Initialize the PCD
  33326. + */
  33327. + dev_dbg(&_dev->dev, "Calling pcd_init\n");
  33328. + retval = pcd_init(_dev);
  33329. + if (retval != 0) {
  33330. + DWC_ERROR("pcd_init failed\n");
  33331. + dwc_otg_device->pcd = NULL;
  33332. + goto fail;
  33333. + }
  33334. +#endif
  33335. +#ifndef DWC_DEVICE_ONLY
  33336. + /*
  33337. + * Initialize the HCD
  33338. + */
  33339. + dev_dbg(&_dev->dev, "Calling hcd_init\n");
  33340. + retval = hcd_init(_dev);
  33341. + if (retval != 0) {
  33342. + DWC_ERROR("hcd_init failed\n");
  33343. + dwc_otg_device->hcd = NULL;
  33344. + goto fail;
  33345. + }
  33346. +#endif
  33347. + /* Recover from drvdata having been overwritten by hcd_init() */
  33348. +#ifdef LM_INTERFACE
  33349. + lm_set_drvdata(_dev, dwc_otg_device);
  33350. +#elif defined(PLATFORM_INTERFACE)
  33351. + platform_set_drvdata(_dev, dwc_otg_device);
  33352. +#elif defined(PCI_INTERFACE)
  33353. + pci_set_drvdata(_dev, dwc_otg_device);
  33354. + dwc_otg_device->os_dep.pcidev = _dev;
  33355. +#endif
  33356. +
  33357. + /*
  33358. + * Enable the global interrupt after all the interrupt
  33359. + * handlers are installed if there is no ADP support else
  33360. + * perform initial actions required for Internal ADP logic.
  33361. + */
  33362. + if (!dwc_otg_get_param_adp_enable(dwc_otg_device->core_if)) {
  33363. + dev_dbg(&_dev->dev, "Calling enable_global_interrupts\n");
  33364. + dwc_otg_enable_global_interrupts(dwc_otg_device->core_if);
  33365. + dev_dbg(&_dev->dev, "Done\n");
  33366. + } else
  33367. + dwc_otg_adp_start(dwc_otg_device->core_if,
  33368. + dwc_otg_is_host_mode(dwc_otg_device->core_if));
  33369. +
  33370. + return 0;
  33371. +
  33372. +fail:
  33373. + dwc_otg_driver_remove(_dev);
  33374. + return retval;
  33375. +}
  33376. +
  33377. +/**
  33378. + * This structure defines the methods to be called by a bus driver
  33379. + * during the lifecycle of a device on that bus. Both drivers and
  33380. + * devices are registered with a bus driver. The bus driver matches
  33381. + * devices to drivers based on information in the device and driver
  33382. + * structures.
  33383. + *
  33384. + * The probe function is called when the bus driver matches a device
  33385. + * to this driver. The remove function is called when a device is
  33386. + * unregistered with the bus driver.
  33387. + */
  33388. +#ifdef LM_INTERFACE
  33389. +static struct lm_driver dwc_otg_driver = {
  33390. + .drv = {.name = (char *)dwc_driver_name,},
  33391. + .probe = dwc_otg_driver_probe,
  33392. + .remove = dwc_otg_driver_remove,
  33393. + // 'suspend' and 'resume' absent
  33394. +};
  33395. +#elif defined(PCI_INTERFACE)
  33396. +static const struct pci_device_id pci_ids[] = { {
  33397. + PCI_DEVICE(0x16c3, 0xabcd),
  33398. + .driver_data =
  33399. + (unsigned long)0xdeadbeef,
  33400. + }, { /* end: all zeroes */ }
  33401. +};
  33402. +
  33403. +MODULE_DEVICE_TABLE(pci, pci_ids);
  33404. +
  33405. +/* pci driver glue; this is a "new style" PCI driver module */
  33406. +static struct pci_driver dwc_otg_driver = {
  33407. + .name = "dwc_otg",
  33408. + .id_table = pci_ids,
  33409. +
  33410. + .probe = dwc_otg_driver_probe,
  33411. + .remove = dwc_otg_driver_remove,
  33412. +
  33413. + .driver = {
  33414. + .name = (char *)dwc_driver_name,
  33415. + },
  33416. +};
  33417. +#elif defined(PLATFORM_INTERFACE)
  33418. +static struct platform_device_id platform_ids[] = {
  33419. + {
  33420. + .name = "bcm2708_usb",
  33421. + .driver_data = (kernel_ulong_t) 0xdeadbeef,
  33422. + },
  33423. + { /* end: all zeroes */ }
  33424. +};
  33425. +MODULE_DEVICE_TABLE(platform, platform_ids);
  33426. +
  33427. +static const struct of_device_id dwc_otg_of_match_table[] = {
  33428. + { .compatible = "brcm,bcm2708-usb", },
  33429. + {},
  33430. +};
  33431. +MODULE_DEVICE_TABLE(of, dwc_otg_of_match_table);
  33432. +
  33433. +static struct platform_driver dwc_otg_driver = {
  33434. + .driver = {
  33435. + .name = (char *)dwc_driver_name,
  33436. + .of_match_table = dwc_otg_of_match_table,
  33437. + },
  33438. + .id_table = platform_ids,
  33439. +
  33440. + .probe = dwc_otg_driver_probe,
  33441. + .remove = dwc_otg_driver_remove,
  33442. + // no 'shutdown', 'suspend', 'resume', 'suspend_late' or 'resume_early'
  33443. +};
  33444. +#endif
  33445. +
  33446. +/**
  33447. + * This function is called when the dwc_otg_driver is installed with the
  33448. + * insmod command. It registers the dwc_otg_driver structure with the
  33449. + * appropriate bus driver. This will cause the dwc_otg_driver_probe function
  33450. + * to be called. In addition, the bus driver will automatically expose
  33451. + * attributes defined for the device and driver in the special sysfs file
  33452. + * system.
  33453. + *
  33454. + * @return
  33455. + */
  33456. +static int __init dwc_otg_driver_init(void)
  33457. +{
  33458. + int retval = 0;
  33459. + int error;
  33460. + struct device_driver *drv;
  33461. +
  33462. + if(fiq_fsm_enable && !fiq_enable) {
  33463. + printk(KERN_WARNING "dwc_otg: fiq_fsm_enable was set without fiq_enable! Correcting.\n");
  33464. + fiq_enable = 1;
  33465. + }
  33466. +
  33467. + printk(KERN_INFO "%s: version %s (%s bus)\n", dwc_driver_name,
  33468. + DWC_DRIVER_VERSION,
  33469. +#ifdef LM_INTERFACE
  33470. + "logicmodule");
  33471. + retval = lm_driver_register(&dwc_otg_driver);
  33472. + drv = &dwc_otg_driver.drv;
  33473. +#elif defined(PCI_INTERFACE)
  33474. + "pci");
  33475. + retval = pci_register_driver(&dwc_otg_driver);
  33476. + drv = &dwc_otg_driver.driver;
  33477. +#elif defined(PLATFORM_INTERFACE)
  33478. + "platform");
  33479. + retval = platform_driver_register(&dwc_otg_driver);
  33480. + drv = &dwc_otg_driver.driver;
  33481. +#endif
  33482. + if (retval < 0) {
  33483. + printk(KERN_ERR "%s retval=%d\n", __func__, retval);
  33484. + return retval;
  33485. + }
  33486. + printk(KERN_DEBUG "dwc_otg: FIQ %s\n", fiq_enable ? "enabled":"disabled");
  33487. + printk(KERN_DEBUG "dwc_otg: NAK holdoff %s\n", nak_holdoff ? "enabled":"disabled");
  33488. + printk(KERN_DEBUG "dwc_otg: FIQ split-transaction FSM %s\n", fiq_fsm_enable ? "enabled":"disabled");
  33489. +
  33490. + error = driver_create_file(drv, &driver_attr_version);
  33491. +#ifdef DEBUG
  33492. + error = driver_create_file(drv, &driver_attr_debuglevel);
  33493. +#endif
  33494. + return retval;
  33495. +}
  33496. +
  33497. +module_init(dwc_otg_driver_init);
  33498. +
  33499. +/**
  33500. + * This function is called when the driver is removed from the kernel
  33501. + * with the rmmod command. The driver unregisters itself with its bus
  33502. + * driver.
  33503. + *
  33504. + */
  33505. +static void __exit dwc_otg_driver_cleanup(void)
  33506. +{
  33507. + printk(KERN_DEBUG "dwc_otg_driver_cleanup()\n");
  33508. +
  33509. +#ifdef LM_INTERFACE
  33510. + driver_remove_file(&dwc_otg_driver.drv, &driver_attr_debuglevel);
  33511. + driver_remove_file(&dwc_otg_driver.drv, &driver_attr_version);
  33512. + lm_driver_unregister(&dwc_otg_driver);
  33513. +#elif defined(PCI_INTERFACE)
  33514. + driver_remove_file(&dwc_otg_driver.driver, &driver_attr_debuglevel);
  33515. + driver_remove_file(&dwc_otg_driver.driver, &driver_attr_version);
  33516. + pci_unregister_driver(&dwc_otg_driver);
  33517. +#elif defined(PLATFORM_INTERFACE)
  33518. + driver_remove_file(&dwc_otg_driver.driver, &driver_attr_debuglevel);
  33519. + driver_remove_file(&dwc_otg_driver.driver, &driver_attr_version);
  33520. + platform_driver_unregister(&dwc_otg_driver);
  33521. +#endif
  33522. +
  33523. + printk(KERN_INFO "%s module removed\n", dwc_driver_name);
  33524. +}
  33525. +
  33526. +module_exit(dwc_otg_driver_cleanup);
  33527. +
  33528. +MODULE_DESCRIPTION(DWC_DRIVER_DESC);
  33529. +MODULE_AUTHOR("Synopsys Inc.");
  33530. +MODULE_LICENSE("GPL");
  33531. +
  33532. +module_param_named(otg_cap, dwc_otg_module_params.otg_cap, int, 0444);
  33533. +MODULE_PARM_DESC(otg_cap, "OTG Capabilities 0=HNP&SRP 1=SRP Only 2=None");
  33534. +module_param_named(opt, dwc_otg_module_params.opt, int, 0444);
  33535. +MODULE_PARM_DESC(opt, "OPT Mode");
  33536. +module_param_named(dma_enable, dwc_otg_module_params.dma_enable, int, 0444);
  33537. +MODULE_PARM_DESC(dma_enable, "DMA Mode 0=Slave 1=DMA enabled");
  33538. +
  33539. +module_param_named(dma_desc_enable, dwc_otg_module_params.dma_desc_enable, int,
  33540. + 0444);
  33541. +MODULE_PARM_DESC(dma_desc_enable,
  33542. + "DMA Desc Mode 0=Address DMA 1=DMA Descriptor enabled");
  33543. +
  33544. +module_param_named(dma_burst_size, dwc_otg_module_params.dma_burst_size, int,
  33545. + 0444);
  33546. +MODULE_PARM_DESC(dma_burst_size,
  33547. + "DMA Burst Size 1, 4, 8, 16, 32, 64, 128, 256");
  33548. +module_param_named(speed, dwc_otg_module_params.speed, int, 0444);
  33549. +MODULE_PARM_DESC(speed, "Speed 0=High Speed 1=Full Speed");
  33550. +module_param_named(host_support_fs_ls_low_power,
  33551. + dwc_otg_module_params.host_support_fs_ls_low_power, int,
  33552. + 0444);
  33553. +MODULE_PARM_DESC(host_support_fs_ls_low_power,
  33554. + "Support Low Power w/FS or LS 0=Support 1=Don't Support");
  33555. +module_param_named(host_ls_low_power_phy_clk,
  33556. + dwc_otg_module_params.host_ls_low_power_phy_clk, int, 0444);
  33557. +MODULE_PARM_DESC(host_ls_low_power_phy_clk,
  33558. + "Low Speed Low Power Clock 0=48Mhz 1=6Mhz");
  33559. +module_param_named(enable_dynamic_fifo,
  33560. + dwc_otg_module_params.enable_dynamic_fifo, int, 0444);
  33561. +MODULE_PARM_DESC(enable_dynamic_fifo, "0=cC Setting 1=Allow Dynamic Sizing");
  33562. +module_param_named(data_fifo_size, dwc_otg_module_params.data_fifo_size, int,
  33563. + 0444);
  33564. +MODULE_PARM_DESC(data_fifo_size,
  33565. + "Total number of words in the data FIFO memory 32-32768");
  33566. +module_param_named(dev_rx_fifo_size, dwc_otg_module_params.dev_rx_fifo_size,
  33567. + int, 0444);
  33568. +MODULE_PARM_DESC(dev_rx_fifo_size, "Number of words in the Rx FIFO 16-32768");
  33569. +module_param_named(dev_nperio_tx_fifo_size,
  33570. + dwc_otg_module_params.dev_nperio_tx_fifo_size, int, 0444);
  33571. +MODULE_PARM_DESC(dev_nperio_tx_fifo_size,
  33572. + "Number of words in the non-periodic Tx FIFO 16-32768");
  33573. +module_param_named(dev_perio_tx_fifo_size_1,
  33574. + dwc_otg_module_params.dev_perio_tx_fifo_size[0], int, 0444);
  33575. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_1,
  33576. + "Number of words in the periodic Tx FIFO 4-768");
  33577. +module_param_named(dev_perio_tx_fifo_size_2,
  33578. + dwc_otg_module_params.dev_perio_tx_fifo_size[1], int, 0444);
  33579. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_2,
  33580. + "Number of words in the periodic Tx FIFO 4-768");
  33581. +module_param_named(dev_perio_tx_fifo_size_3,
  33582. + dwc_otg_module_params.dev_perio_tx_fifo_size[2], int, 0444);
  33583. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_3,
  33584. + "Number of words in the periodic Tx FIFO 4-768");
  33585. +module_param_named(dev_perio_tx_fifo_size_4,
  33586. + dwc_otg_module_params.dev_perio_tx_fifo_size[3], int, 0444);
  33587. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_4,
  33588. + "Number of words in the periodic Tx FIFO 4-768");
  33589. +module_param_named(dev_perio_tx_fifo_size_5,
  33590. + dwc_otg_module_params.dev_perio_tx_fifo_size[4], int, 0444);
  33591. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_5,
  33592. + "Number of words in the periodic Tx FIFO 4-768");
  33593. +module_param_named(dev_perio_tx_fifo_size_6,
  33594. + dwc_otg_module_params.dev_perio_tx_fifo_size[5], int, 0444);
  33595. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_6,
  33596. + "Number of words in the periodic Tx FIFO 4-768");
  33597. +module_param_named(dev_perio_tx_fifo_size_7,
  33598. + dwc_otg_module_params.dev_perio_tx_fifo_size[6], int, 0444);
  33599. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_7,
  33600. + "Number of words in the periodic Tx FIFO 4-768");
  33601. +module_param_named(dev_perio_tx_fifo_size_8,
  33602. + dwc_otg_module_params.dev_perio_tx_fifo_size[7], int, 0444);
  33603. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_8,
  33604. + "Number of words in the periodic Tx FIFO 4-768");
  33605. +module_param_named(dev_perio_tx_fifo_size_9,
  33606. + dwc_otg_module_params.dev_perio_tx_fifo_size[8], int, 0444);
  33607. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_9,
  33608. + "Number of words in the periodic Tx FIFO 4-768");
  33609. +module_param_named(dev_perio_tx_fifo_size_10,
  33610. + dwc_otg_module_params.dev_perio_tx_fifo_size[9], int, 0444);
  33611. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_10,
  33612. + "Number of words in the periodic Tx FIFO 4-768");
  33613. +module_param_named(dev_perio_tx_fifo_size_11,
  33614. + dwc_otg_module_params.dev_perio_tx_fifo_size[10], int, 0444);
  33615. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_11,
  33616. + "Number of words in the periodic Tx FIFO 4-768");
  33617. +module_param_named(dev_perio_tx_fifo_size_12,
  33618. + dwc_otg_module_params.dev_perio_tx_fifo_size[11], int, 0444);
  33619. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_12,
  33620. + "Number of words in the periodic Tx FIFO 4-768");
  33621. +module_param_named(dev_perio_tx_fifo_size_13,
  33622. + dwc_otg_module_params.dev_perio_tx_fifo_size[12], int, 0444);
  33623. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_13,
  33624. + "Number of words in the periodic Tx FIFO 4-768");
  33625. +module_param_named(dev_perio_tx_fifo_size_14,
  33626. + dwc_otg_module_params.dev_perio_tx_fifo_size[13], int, 0444);
  33627. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_14,
  33628. + "Number of words in the periodic Tx FIFO 4-768");
  33629. +module_param_named(dev_perio_tx_fifo_size_15,
  33630. + dwc_otg_module_params.dev_perio_tx_fifo_size[14], int, 0444);
  33631. +MODULE_PARM_DESC(dev_perio_tx_fifo_size_15,
  33632. + "Number of words in the periodic Tx FIFO 4-768");
  33633. +module_param_named(host_rx_fifo_size, dwc_otg_module_params.host_rx_fifo_size,
  33634. + int, 0444);
  33635. +MODULE_PARM_DESC(host_rx_fifo_size, "Number of words in the Rx FIFO 16-32768");
  33636. +module_param_named(host_nperio_tx_fifo_size,
  33637. + dwc_otg_module_params.host_nperio_tx_fifo_size, int, 0444);
  33638. +MODULE_PARM_DESC(host_nperio_tx_fifo_size,
  33639. + "Number of words in the non-periodic Tx FIFO 16-32768");
  33640. +module_param_named(host_perio_tx_fifo_size,
  33641. + dwc_otg_module_params.host_perio_tx_fifo_size, int, 0444);
  33642. +MODULE_PARM_DESC(host_perio_tx_fifo_size,
  33643. + "Number of words in the host periodic Tx FIFO 16-32768");
  33644. +module_param_named(max_transfer_size, dwc_otg_module_params.max_transfer_size,
  33645. + int, 0444);
  33646. +/** @todo Set the max to 512K, modify checks */
  33647. +MODULE_PARM_DESC(max_transfer_size,
  33648. + "The maximum transfer size supported in bytes 2047-65535");
  33649. +module_param_named(max_packet_count, dwc_otg_module_params.max_packet_count,
  33650. + int, 0444);
  33651. +MODULE_PARM_DESC(max_packet_count,
  33652. + "The maximum number of packets in a transfer 15-511");
  33653. +module_param_named(host_channels, dwc_otg_module_params.host_channels, int,
  33654. + 0444);
  33655. +MODULE_PARM_DESC(host_channels,
  33656. + "The number of host channel registers to use 1-16");
  33657. +module_param_named(dev_endpoints, dwc_otg_module_params.dev_endpoints, int,
  33658. + 0444);
  33659. +MODULE_PARM_DESC(dev_endpoints,
  33660. + "The number of endpoints in addition to EP0 available for device mode 1-15");
  33661. +module_param_named(phy_type, dwc_otg_module_params.phy_type, int, 0444);
  33662. +MODULE_PARM_DESC(phy_type, "0=Reserved 1=UTMI+ 2=ULPI");
  33663. +module_param_named(phy_utmi_width, dwc_otg_module_params.phy_utmi_width, int,
  33664. + 0444);
  33665. +MODULE_PARM_DESC(phy_utmi_width, "Specifies the UTMI+ Data Width 8 or 16 bits");
  33666. +module_param_named(phy_ulpi_ddr, dwc_otg_module_params.phy_ulpi_ddr, int, 0444);
  33667. +MODULE_PARM_DESC(phy_ulpi_ddr,
  33668. + "ULPI at double or single data rate 0=Single 1=Double");
  33669. +module_param_named(phy_ulpi_ext_vbus, dwc_otg_module_params.phy_ulpi_ext_vbus,
  33670. + int, 0444);
  33671. +MODULE_PARM_DESC(phy_ulpi_ext_vbus,
  33672. + "ULPI PHY using internal or external vbus 0=Internal");
  33673. +module_param_named(i2c_enable, dwc_otg_module_params.i2c_enable, int, 0444);
  33674. +MODULE_PARM_DESC(i2c_enable, "FS PHY Interface");
  33675. +module_param_named(ulpi_fs_ls, dwc_otg_module_params.ulpi_fs_ls, int, 0444);
  33676. +MODULE_PARM_DESC(ulpi_fs_ls, "ULPI PHY FS/LS mode only");
  33677. +module_param_named(ts_dline, dwc_otg_module_params.ts_dline, int, 0444);
  33678. +MODULE_PARM_DESC(ts_dline, "Term select Dline pulsing for all PHYs");
  33679. +module_param_named(debug, g_dbg_lvl, int, 0444);
  33680. +MODULE_PARM_DESC(debug, "");
  33681. +
  33682. +module_param_named(en_multiple_tx_fifo,
  33683. + dwc_otg_module_params.en_multiple_tx_fifo, int, 0444);
  33684. +MODULE_PARM_DESC(en_multiple_tx_fifo,
  33685. + "Dedicated Non Periodic Tx FIFOs 0=disabled 1=enabled");
  33686. +module_param_named(dev_tx_fifo_size_1,
  33687. + dwc_otg_module_params.dev_tx_fifo_size[0], int, 0444);
  33688. +MODULE_PARM_DESC(dev_tx_fifo_size_1, "Number of words in the Tx FIFO 4-768");
  33689. +module_param_named(dev_tx_fifo_size_2,
  33690. + dwc_otg_module_params.dev_tx_fifo_size[1], int, 0444);
  33691. +MODULE_PARM_DESC(dev_tx_fifo_size_2, "Number of words in the Tx FIFO 4-768");
  33692. +module_param_named(dev_tx_fifo_size_3,
  33693. + dwc_otg_module_params.dev_tx_fifo_size[2], int, 0444);
  33694. +MODULE_PARM_DESC(dev_tx_fifo_size_3, "Number of words in the Tx FIFO 4-768");
  33695. +module_param_named(dev_tx_fifo_size_4,
  33696. + dwc_otg_module_params.dev_tx_fifo_size[3], int, 0444);
  33697. +MODULE_PARM_DESC(dev_tx_fifo_size_4, "Number of words in the Tx FIFO 4-768");
  33698. +module_param_named(dev_tx_fifo_size_5,
  33699. + dwc_otg_module_params.dev_tx_fifo_size[4], int, 0444);
  33700. +MODULE_PARM_DESC(dev_tx_fifo_size_5, "Number of words in the Tx FIFO 4-768");
  33701. +module_param_named(dev_tx_fifo_size_6,
  33702. + dwc_otg_module_params.dev_tx_fifo_size[5], int, 0444);
  33703. +MODULE_PARM_DESC(dev_tx_fifo_size_6, "Number of words in the Tx FIFO 4-768");
  33704. +module_param_named(dev_tx_fifo_size_7,
  33705. + dwc_otg_module_params.dev_tx_fifo_size[6], int, 0444);
  33706. +MODULE_PARM_DESC(dev_tx_fifo_size_7, "Number of words in the Tx FIFO 4-768");
  33707. +module_param_named(dev_tx_fifo_size_8,
  33708. + dwc_otg_module_params.dev_tx_fifo_size[7], int, 0444);
  33709. +MODULE_PARM_DESC(dev_tx_fifo_size_8, "Number of words in the Tx FIFO 4-768");
  33710. +module_param_named(dev_tx_fifo_size_9,
  33711. + dwc_otg_module_params.dev_tx_fifo_size[8], int, 0444);
  33712. +MODULE_PARM_DESC(dev_tx_fifo_size_9, "Number of words in the Tx FIFO 4-768");
  33713. +module_param_named(dev_tx_fifo_size_10,
  33714. + dwc_otg_module_params.dev_tx_fifo_size[9], int, 0444);
  33715. +MODULE_PARM_DESC(dev_tx_fifo_size_10, "Number of words in the Tx FIFO 4-768");
  33716. +module_param_named(dev_tx_fifo_size_11,
  33717. + dwc_otg_module_params.dev_tx_fifo_size[10], int, 0444);
  33718. +MODULE_PARM_DESC(dev_tx_fifo_size_11, "Number of words in the Tx FIFO 4-768");
  33719. +module_param_named(dev_tx_fifo_size_12,
  33720. + dwc_otg_module_params.dev_tx_fifo_size[11], int, 0444);
  33721. +MODULE_PARM_DESC(dev_tx_fifo_size_12, "Number of words in the Tx FIFO 4-768");
  33722. +module_param_named(dev_tx_fifo_size_13,
  33723. + dwc_otg_module_params.dev_tx_fifo_size[12], int, 0444);
  33724. +MODULE_PARM_DESC(dev_tx_fifo_size_13, "Number of words in the Tx FIFO 4-768");
  33725. +module_param_named(dev_tx_fifo_size_14,
  33726. + dwc_otg_module_params.dev_tx_fifo_size[13], int, 0444);
  33727. +MODULE_PARM_DESC(dev_tx_fifo_size_14, "Number of words in the Tx FIFO 4-768");
  33728. +module_param_named(dev_tx_fifo_size_15,
  33729. + dwc_otg_module_params.dev_tx_fifo_size[14], int, 0444);
  33730. +MODULE_PARM_DESC(dev_tx_fifo_size_15, "Number of words in the Tx FIFO 4-768");
  33731. +
  33732. +module_param_named(thr_ctl, dwc_otg_module_params.thr_ctl, int, 0444);
  33733. +MODULE_PARM_DESC(thr_ctl,
  33734. + "Thresholding enable flag bit 0 - non ISO Tx thr., 1 - ISO Tx thr., 2 - Rx thr.- bit 0=disabled 1=enabled");
  33735. +module_param_named(tx_thr_length, dwc_otg_module_params.tx_thr_length, int,
  33736. + 0444);
  33737. +MODULE_PARM_DESC(tx_thr_length, "Tx Threshold length in 32 bit DWORDs");
  33738. +module_param_named(rx_thr_length, dwc_otg_module_params.rx_thr_length, int,
  33739. + 0444);
  33740. +MODULE_PARM_DESC(rx_thr_length, "Rx Threshold length in 32 bit DWORDs");
  33741. +
  33742. +module_param_named(pti_enable, dwc_otg_module_params.pti_enable, int, 0444);
  33743. +module_param_named(mpi_enable, dwc_otg_module_params.mpi_enable, int, 0444);
  33744. +module_param_named(lpm_enable, dwc_otg_module_params.lpm_enable, int, 0444);
  33745. +MODULE_PARM_DESC(lpm_enable, "LPM Enable 0=LPM Disabled 1=LPM Enabled");
  33746. +module_param_named(ic_usb_cap, dwc_otg_module_params.ic_usb_cap, int, 0444);
  33747. +MODULE_PARM_DESC(ic_usb_cap,
  33748. + "IC_USB Capability 0=IC_USB Disabled 1=IC_USB Enabled");
  33749. +module_param_named(ahb_thr_ratio, dwc_otg_module_params.ahb_thr_ratio, int,
  33750. + 0444);
  33751. +MODULE_PARM_DESC(ahb_thr_ratio, "AHB Threshold Ratio");
  33752. +module_param_named(power_down, dwc_otg_module_params.power_down, int, 0444);
  33753. +MODULE_PARM_DESC(power_down, "Power Down Mode");
  33754. +module_param_named(reload_ctl, dwc_otg_module_params.reload_ctl, int, 0444);
  33755. +MODULE_PARM_DESC(reload_ctl, "HFIR Reload Control");
  33756. +module_param_named(dev_out_nak, dwc_otg_module_params.dev_out_nak, int, 0444);
  33757. +MODULE_PARM_DESC(dev_out_nak, "Enable Device OUT NAK");
  33758. +module_param_named(cont_on_bna, dwc_otg_module_params.cont_on_bna, int, 0444);
  33759. +MODULE_PARM_DESC(cont_on_bna, "Enable Enable Continue on BNA");
  33760. +module_param_named(ahb_single, dwc_otg_module_params.ahb_single, int, 0444);
  33761. +MODULE_PARM_DESC(ahb_single, "Enable AHB Single Support");
  33762. +module_param_named(adp_enable, dwc_otg_module_params.adp_enable, int, 0444);
  33763. +MODULE_PARM_DESC(adp_enable, "ADP Enable 0=ADP Disabled 1=ADP Enabled");
  33764. +module_param_named(otg_ver, dwc_otg_module_params.otg_ver, int, 0444);
  33765. +MODULE_PARM_DESC(otg_ver, "OTG revision supported 0=OTG 1.3 1=OTG 2.0");
  33766. +module_param(microframe_schedule, bool, 0444);
  33767. +MODULE_PARM_DESC(microframe_schedule, "Enable the microframe scheduler");
  33768. +
  33769. +module_param(fiq_enable, bool, 0444);
  33770. +MODULE_PARM_DESC(fiq_enable, "Enable the FIQ");
  33771. +module_param(nak_holdoff, ushort, 0644);
  33772. +MODULE_PARM_DESC(nak_holdoff, "Throttle duration for bulk split-transaction endpoints on a NAK. Default 8");
  33773. +module_param(fiq_fsm_enable, bool, 0444);
  33774. +MODULE_PARM_DESC(fiq_fsm_enable, "Enable the FIQ to perform split transactions as defined by fiq_fsm_mask");
  33775. +module_param(fiq_fsm_mask, ushort, 0444);
  33776. +MODULE_PARM_DESC(fiq_fsm_mask, "Bitmask of transactions to perform in the FIQ.\n"
  33777. + "Bit 0 : Non-periodic split transactions\n"
  33778. + "Bit 1 : Periodic split transactions\n"
  33779. + "Bit 2 : High-speed multi-transfer isochronous\n"
  33780. + "All other bits should be set 0.");
  33781. +
  33782. +
  33783. +/** @page "Module Parameters"
  33784. + *
  33785. + * The following parameters may be specified when starting the module.
  33786. + * These parameters define how the DWC_otg controller should be
  33787. + * configured. Parameter values are passed to the CIL initialization
  33788. + * function dwc_otg_cil_init
  33789. + *
  33790. + * Example: <code>modprobe dwc_otg speed=1 otg_cap=1</code>
  33791. + *
  33792. +
  33793. + <table>
  33794. + <tr><td>Parameter Name</td><td>Meaning</td></tr>
  33795. +
  33796. + <tr>
  33797. + <td>otg_cap</td>
  33798. + <td>Specifies the OTG capabilities. The driver will automatically detect the
  33799. + value for this parameter if none is specified.
  33800. + - 0: HNP and SRP capable (default, if available)
  33801. + - 1: SRP Only capable
  33802. + - 2: No HNP/SRP capable
  33803. + </td></tr>
  33804. +
  33805. + <tr>
  33806. + <td>dma_enable</td>
  33807. + <td>Specifies whether to use slave or DMA mode for accessing the data FIFOs.
  33808. + The driver will automatically detect the value for this parameter if none is
  33809. + specified.
  33810. + - 0: Slave
  33811. + - 1: DMA (default, if available)
  33812. + </td></tr>
  33813. +
  33814. + <tr>
  33815. + <td>dma_burst_size</td>
  33816. + <td>The DMA Burst size (applicable only for External DMA Mode).
  33817. + - Values: 1, 4, 8 16, 32, 64, 128, 256 (default 32)
  33818. + </td></tr>
  33819. +
  33820. + <tr>
  33821. + <td>speed</td>
  33822. + <td>Specifies the maximum speed of operation in host and device mode. The
  33823. + actual speed depends on the speed of the attached device and the value of
  33824. + phy_type.
  33825. + - 0: High Speed (default)
  33826. + - 1: Full Speed
  33827. + </td></tr>
  33828. +
  33829. + <tr>
  33830. + <td>host_support_fs_ls_low_power</td>
  33831. + <td>Specifies whether low power mode is supported when attached to a Full
  33832. + Speed or Low Speed device in host mode.
  33833. + - 0: Don't support low power mode (default)
  33834. + - 1: Support low power mode
  33835. + </td></tr>
  33836. +
  33837. + <tr>
  33838. + <td>host_ls_low_power_phy_clk</td>
  33839. + <td>Specifies the PHY clock rate in low power mode when connected to a Low
  33840. + Speed device in host mode. This parameter is applicable only if
  33841. + HOST_SUPPORT_FS_LS_LOW_POWER is enabled.
  33842. + - 0: 48 MHz (default)
  33843. + - 1: 6 MHz
  33844. + </td></tr>
  33845. +
  33846. + <tr>
  33847. + <td>enable_dynamic_fifo</td>
  33848. + <td> Specifies whether FIFOs may be resized by the driver software.
  33849. + - 0: Use cC FIFO size parameters
  33850. + - 1: Allow dynamic FIFO sizing (default)
  33851. + </td></tr>
  33852. +
  33853. + <tr>
  33854. + <td>data_fifo_size</td>
  33855. + <td>Total number of 4-byte words in the data FIFO memory. This memory
  33856. + includes the Rx FIFO, non-periodic Tx FIFO, and periodic Tx FIFOs.
  33857. + - Values: 32 to 32768 (default 8192)
  33858. +
  33859. + Note: The total FIFO memory depth in the FPGA configuration is 8192.
  33860. + </td></tr>
  33861. +
  33862. + <tr>
  33863. + <td>dev_rx_fifo_size</td>
  33864. + <td>Number of 4-byte words in the Rx FIFO in device mode when dynamic
  33865. + FIFO sizing is enabled.
  33866. + - Values: 16 to 32768 (default 1064)
  33867. + </td></tr>
  33868. +
  33869. + <tr>
  33870. + <td>dev_nperio_tx_fifo_size</td>
  33871. + <td>Number of 4-byte words in the non-periodic Tx FIFO in device mode when
  33872. + dynamic FIFO sizing is enabled.
  33873. + - Values: 16 to 32768 (default 1024)
  33874. + </td></tr>
  33875. +
  33876. + <tr>
  33877. + <td>dev_perio_tx_fifo_size_n (n = 1 to 15)</td>
  33878. + <td>Number of 4-byte words in each of the periodic Tx FIFOs in device mode
  33879. + when dynamic FIFO sizing is enabled.
  33880. + - Values: 4 to 768 (default 256)
  33881. + </td></tr>
  33882. +
  33883. + <tr>
  33884. + <td>host_rx_fifo_size</td>
  33885. + <td>Number of 4-byte words in the Rx FIFO in host mode when dynamic FIFO
  33886. + sizing is enabled.
  33887. + - Values: 16 to 32768 (default 1024)
  33888. + </td></tr>
  33889. +
  33890. + <tr>
  33891. + <td>host_nperio_tx_fifo_size</td>
  33892. + <td>Number of 4-byte words in the non-periodic Tx FIFO in host mode when
  33893. + dynamic FIFO sizing is enabled in the core.
  33894. + - Values: 16 to 32768 (default 1024)
  33895. + </td></tr>
  33896. +
  33897. + <tr>
  33898. + <td>host_perio_tx_fifo_size</td>
  33899. + <td>Number of 4-byte words in the host periodic Tx FIFO when dynamic FIFO
  33900. + sizing is enabled.
  33901. + - Values: 16 to 32768 (default 1024)
  33902. + </td></tr>
  33903. +
  33904. + <tr>
  33905. + <td>max_transfer_size</td>
  33906. + <td>The maximum transfer size supported in bytes.
  33907. + - Values: 2047 to 65,535 (default 65,535)
  33908. + </td></tr>
  33909. +
  33910. + <tr>
  33911. + <td>max_packet_count</td>
  33912. + <td>The maximum number of packets in a transfer.
  33913. + - Values: 15 to 511 (default 511)
  33914. + </td></tr>
  33915. +
  33916. + <tr>
  33917. + <td>host_channels</td>
  33918. + <td>The number of host channel registers to use.
  33919. + - Values: 1 to 16 (default 12)
  33920. +
  33921. + Note: The FPGA configuration supports a maximum of 12 host channels.
  33922. + </td></tr>
  33923. +
  33924. + <tr>
  33925. + <td>dev_endpoints</td>
  33926. + <td>The number of endpoints in addition to EP0 available for device mode
  33927. + operations.
  33928. + - Values: 1 to 15 (default 6 IN and OUT)
  33929. +
  33930. + Note: The FPGA configuration supports a maximum of 6 IN and OUT endpoints in
  33931. + addition to EP0.
  33932. + </td></tr>
  33933. +
  33934. + <tr>
  33935. + <td>phy_type</td>
  33936. + <td>Specifies the type of PHY interface to use. By default, the driver will
  33937. + automatically detect the phy_type.
  33938. + - 0: Full Speed
  33939. + - 1: UTMI+ (default, if available)
  33940. + - 2: ULPI
  33941. + </td></tr>
  33942. +
  33943. + <tr>
  33944. + <td>phy_utmi_width</td>
  33945. + <td>Specifies the UTMI+ Data Width. This parameter is applicable for a
  33946. + phy_type of UTMI+. Also, this parameter is applicable only if the
  33947. + OTG_HSPHY_WIDTH cC parameter was set to "8 and 16 bits", meaning that the
  33948. + core has been configured to work at either data path width.
  33949. + - Values: 8 or 16 bits (default 16)
  33950. + </td></tr>
  33951. +
  33952. + <tr>
  33953. + <td>phy_ulpi_ddr</td>
  33954. + <td>Specifies whether the ULPI operates at double or single data rate. This
  33955. + parameter is only applicable if phy_type is ULPI.
  33956. + - 0: single data rate ULPI interface with 8 bit wide data bus (default)
  33957. + - 1: double data rate ULPI interface with 4 bit wide data bus
  33958. + </td></tr>
  33959. +
  33960. + <tr>
  33961. + <td>i2c_enable</td>
  33962. + <td>Specifies whether to use the I2C interface for full speed PHY. This
  33963. + parameter is only applicable if PHY_TYPE is FS.
  33964. + - 0: Disabled (default)
  33965. + - 1: Enabled
  33966. + </td></tr>
  33967. +
  33968. + <tr>
  33969. + <td>ulpi_fs_ls</td>
  33970. + <td>Specifies whether to use ULPI FS/LS mode only.
  33971. + - 0: Disabled (default)
  33972. + - 1: Enabled
  33973. + </td></tr>
  33974. +
  33975. + <tr>
  33976. + <td>ts_dline</td>
  33977. + <td>Specifies whether term select D-Line pulsing for all PHYs is enabled.
  33978. + - 0: Disabled (default)
  33979. + - 1: Enabled
  33980. + </td></tr>
  33981. +
  33982. + <tr>
  33983. + <td>en_multiple_tx_fifo</td>
  33984. + <td>Specifies whether dedicatedto tx fifos are enabled for non periodic IN EPs.
  33985. + The driver will automatically detect the value for this parameter if none is
  33986. + specified.
  33987. + - 0: Disabled
  33988. + - 1: Enabled (default, if available)
  33989. + </td></tr>
  33990. +
  33991. + <tr>
  33992. + <td>dev_tx_fifo_size_n (n = 1 to 15)</td>
  33993. + <td>Number of 4-byte words in each of the Tx FIFOs in device mode
  33994. + when dynamic FIFO sizing is enabled.
  33995. + - Values: 4 to 768 (default 256)
  33996. + </td></tr>
  33997. +
  33998. + <tr>
  33999. + <td>tx_thr_length</td>
  34000. + <td>Transmit Threshold length in 32 bit double words
  34001. + - Values: 8 to 128 (default 64)
  34002. + </td></tr>
  34003. +
  34004. + <tr>
  34005. + <td>rx_thr_length</td>
  34006. + <td>Receive Threshold length in 32 bit double words
  34007. + - Values: 8 to 128 (default 64)
  34008. + </td></tr>
  34009. +
  34010. +<tr>
  34011. + <td>thr_ctl</td>
  34012. + <td>Specifies whether to enable Thresholding for Device mode. Bits 0, 1, 2 of
  34013. + this parmater specifies if thresholding is enabled for non-Iso Tx, Iso Tx and
  34014. + Rx transfers accordingly.
  34015. + The driver will automatically detect the value for this parameter if none is
  34016. + specified.
  34017. + - Values: 0 to 7 (default 0)
  34018. + Bit values indicate:
  34019. + - 0: Thresholding disabled
  34020. + - 1: Thresholding enabled
  34021. + </td></tr>
  34022. +
  34023. +<tr>
  34024. + <td>dma_desc_enable</td>
  34025. + <td>Specifies whether to enable Descriptor DMA mode.
  34026. + The driver will automatically detect the value for this parameter if none is
  34027. + specified.
  34028. + - 0: Descriptor DMA disabled
  34029. + - 1: Descriptor DMA (default, if available)
  34030. + </td></tr>
  34031. +
  34032. +<tr>
  34033. + <td>mpi_enable</td>
  34034. + <td>Specifies whether to enable MPI enhancement mode.
  34035. + The driver will automatically detect the value for this parameter if none is
  34036. + specified.
  34037. + - 0: MPI disabled (default)
  34038. + - 1: MPI enable
  34039. + </td></tr>
  34040. +
  34041. +<tr>
  34042. + <td>pti_enable</td>
  34043. + <td>Specifies whether to enable PTI enhancement support.
  34044. + The driver will automatically detect the value for this parameter if none is
  34045. + specified.
  34046. + - 0: PTI disabled (default)
  34047. + - 1: PTI enable
  34048. + </td></tr>
  34049. +
  34050. +<tr>
  34051. + <td>lpm_enable</td>
  34052. + <td>Specifies whether to enable LPM support.
  34053. + The driver will automatically detect the value for this parameter if none is
  34054. + specified.
  34055. + - 0: LPM disabled
  34056. + - 1: LPM enable (default, if available)
  34057. + </td></tr>
  34058. +
  34059. +<tr>
  34060. + <td>ic_usb_cap</td>
  34061. + <td>Specifies whether to enable IC_USB capability.
  34062. + The driver will automatically detect the value for this parameter if none is
  34063. + specified.
  34064. + - 0: IC_USB disabled (default, if available)
  34065. + - 1: IC_USB enable
  34066. + </td></tr>
  34067. +
  34068. +<tr>
  34069. + <td>ahb_thr_ratio</td>
  34070. + <td>Specifies AHB Threshold ratio.
  34071. + - Values: 0 to 3 (default 0)
  34072. + </td></tr>
  34073. +
  34074. +<tr>
  34075. + <td>power_down</td>
  34076. + <td>Specifies Power Down(Hibernation) Mode.
  34077. + The driver will automatically detect the value for this parameter if none is
  34078. + specified.
  34079. + - 0: Power Down disabled (default)
  34080. + - 2: Power Down enabled
  34081. + </td></tr>
  34082. +
  34083. + <tr>
  34084. + <td>reload_ctl</td>
  34085. + <td>Specifies whether dynamic reloading of the HFIR register is allowed during
  34086. + run time. The driver will automatically detect the value for this parameter if
  34087. + none is specified. In case the HFIR value is reloaded when HFIR.RldCtrl == 1'b0
  34088. + the core might misbehave.
  34089. + - 0: Reload Control disabled (default)
  34090. + - 1: Reload Control enabled
  34091. + </td></tr>
  34092. +
  34093. + <tr>
  34094. + <td>dev_out_nak</td>
  34095. + <td>Specifies whether Device OUT NAK enhancement enabled or no.
  34096. + The driver will automatically detect the value for this parameter if
  34097. + none is specified. This parameter is valid only when OTG_EN_DESC_DMA == 1b1.
  34098. + - 0: The core does not set NAK after Bulk OUT transfer complete (default)
  34099. + - 1: The core sets NAK after Bulk OUT transfer complete
  34100. + </td></tr>
  34101. +
  34102. + <tr>
  34103. + <td>cont_on_bna</td>
  34104. + <td>Specifies whether Enable Continue on BNA enabled or no.
  34105. + After receiving BNA interrupt the core disables the endpoint,when the
  34106. + endpoint is re-enabled by the application the
  34107. + - 0: Core starts processing from the DOEPDMA descriptor (default)
  34108. + - 1: Core starts processing from the descriptor which received the BNA.
  34109. + This parameter is valid only when OTG_EN_DESC_DMA == 1b1.
  34110. + </td></tr>
  34111. +
  34112. + <tr>
  34113. + <td>ahb_single</td>
  34114. + <td>This bit when programmed supports SINGLE transfers for remainder data
  34115. + in a transfer for DMA mode of operation.
  34116. + - 0: The remainder data will be sent using INCR burst size (default)
  34117. + - 1: The remainder data will be sent using SINGLE burst size.
  34118. + </td></tr>
  34119. +
  34120. +<tr>
  34121. + <td>adp_enable</td>
  34122. + <td>Specifies whether ADP feature is enabled.
  34123. + The driver will automatically detect the value for this parameter if none is
  34124. + specified.
  34125. + - 0: ADP feature disabled (default)
  34126. + - 1: ADP feature enabled
  34127. + </td></tr>
  34128. +
  34129. + <tr>
  34130. + <td>otg_ver</td>
  34131. + <td>Specifies whether OTG is performing as USB OTG Revision 2.0 or Revision 1.3
  34132. + USB OTG device.
  34133. + - 0: OTG 2.0 support disabled (default)
  34134. + - 1: OTG 2.0 support enabled
  34135. + </td></tr>
  34136. +
  34137. +*/
  34138. --- /dev/null
  34139. +++ b/drivers/usb/host/dwc_otg/dwc_otg_driver.h
  34140. @@ -0,0 +1,86 @@
  34141. +/* ==========================================================================
  34142. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_driver.h $
  34143. + * $Revision: #19 $
  34144. + * $Date: 2010/11/15 $
  34145. + * $Change: 1627671 $
  34146. + *
  34147. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  34148. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  34149. + * otherwise expressly agreed to in writing between Synopsys and you.
  34150. + *
  34151. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  34152. + * any End User Software License Agreement or Agreement for Licensed Product
  34153. + * with Synopsys or any supplement thereto. You are permitted to use and
  34154. + * redistribute this Software in source and binary forms, with or without
  34155. + * modification, provided that redistributions of source code must retain this
  34156. + * notice. You may not view, use, disclose, copy or distribute this file or
  34157. + * any information contained herein except pursuant to this license grant from
  34158. + * Synopsys. If you do not agree with this notice, including the disclaimer
  34159. + * below, then you are not authorized to use the Software.
  34160. + *
  34161. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  34162. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  34163. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34164. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  34165. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  34166. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  34167. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  34168. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  34169. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  34170. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  34171. + * DAMAGE.
  34172. + * ========================================================================== */
  34173. +
  34174. +#ifndef __DWC_OTG_DRIVER_H__
  34175. +#define __DWC_OTG_DRIVER_H__
  34176. +
  34177. +/** @file
  34178. + * This file contains the interface to the Linux driver.
  34179. + */
  34180. +#include "dwc_otg_os_dep.h"
  34181. +#include "dwc_otg_core_if.h"
  34182. +
  34183. +/* Type declarations */
  34184. +struct dwc_otg_pcd;
  34185. +struct dwc_otg_hcd;
  34186. +
  34187. +/**
  34188. + * This structure is a wrapper that encapsulates the driver components used to
  34189. + * manage a single DWC_otg controller.
  34190. + */
  34191. +typedef struct dwc_otg_device {
  34192. + /** Structure containing OS-dependent stuff. KEEP THIS STRUCT AT THE
  34193. + * VERY BEGINNING OF THE DEVICE STRUCT. OSes such as FreeBSD and NetBSD
  34194. + * require this. */
  34195. + struct os_dependent os_dep;
  34196. +
  34197. + /** Pointer to the core interface structure. */
  34198. + dwc_otg_core_if_t *core_if;
  34199. +
  34200. + /** Pointer to the PCD structure. */
  34201. + struct dwc_otg_pcd *pcd;
  34202. +
  34203. + /** Pointer to the HCD structure. */
  34204. + struct dwc_otg_hcd *hcd;
  34205. +
  34206. + /** Flag to indicate whether the common IRQ handler is installed. */
  34207. + uint8_t common_irq_installed;
  34208. +
  34209. +} dwc_otg_device_t;
  34210. +
  34211. +/*We must clear S3C24XX_EINTPEND external interrupt register
  34212. + * because after clearing in this register trigerred IRQ from
  34213. + * H/W core in kernel interrupt can be occured again before OTG
  34214. + * handlers clear all IRQ sources of Core registers because of
  34215. + * timing latencies and Low Level IRQ Type.
  34216. + */
  34217. +#ifdef CONFIG_MACH_IPMATE
  34218. +#define S3C2410X_CLEAR_EINTPEND() \
  34219. +do { \
  34220. + __raw_writel(1UL << 11,S3C24XX_EINTPEND); \
  34221. +} while (0)
  34222. +#else
  34223. +#define S3C2410X_CLEAR_EINTPEND() do { } while (0)
  34224. +#endif
  34225. +
  34226. +#endif
  34227. --- /dev/null
  34228. +++ b/drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.c
  34229. @@ -0,0 +1,1355 @@
  34230. +/*
  34231. + * dwc_otg_fiq_fsm.c - The finite state machine FIQ
  34232. + *
  34233. + * Copyright (c) 2013 Raspberry Pi Foundation
  34234. + *
  34235. + * Author: Jonathan Bell <[email protected]>
  34236. + * All rights reserved.
  34237. + *
  34238. + * Redistribution and use in source and binary forms, with or without
  34239. + * modification, are permitted provided that the following conditions are met:
  34240. + * * Redistributions of source code must retain the above copyright
  34241. + * notice, this list of conditions and the following disclaimer.
  34242. + * * Redistributions in binary form must reproduce the above copyright
  34243. + * notice, this list of conditions and the following disclaimer in the
  34244. + * documentation and/or other materials provided with the distribution.
  34245. + * * Neither the name of Raspberry Pi nor the
  34246. + * names of its contributors may be used to endorse or promote products
  34247. + * derived from this software without specific prior written permission.
  34248. + *
  34249. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  34250. + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  34251. + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  34252. + * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  34253. + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  34254. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  34255. + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  34256. + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  34257. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  34258. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34259. + *
  34260. + * This FIQ implements functionality that performs split transactions on
  34261. + * the dwc_otg hardware without any outside intervention. A split transaction
  34262. + * is "queued" by nominating a specific host channel to perform the entirety
  34263. + * of a split transaction. This FIQ will then perform the microframe-precise
  34264. + * scheduling required in each phase of the transaction until completion.
  34265. + *
  34266. + * The FIQ functionality is glued into the Synopsys driver via the entry point
  34267. + * in the FSM enqueue function, and at the exit point in handling a HC interrupt
  34268. + * for a FSM-enabled channel.
  34269. + *
  34270. + * NB: Large parts of this implementation have architecture-specific code.
  34271. + * For porting this functionality to other ARM machines, the minimum is required:
  34272. + * - An interrupt controller allowing the top-level dwc USB interrupt to be routed
  34273. + * to the FIQ
  34274. + * - A method of forcing a software generated interrupt from FIQ mode that then
  34275. + * triggers an IRQ entry (with the dwc USB handler called by this IRQ number)
  34276. + * - Guaranteed interrupt routing such that both the FIQ and SGI occur on the same
  34277. + * processor core - there is no locking between the FIQ and IRQ (aside from
  34278. + * local_fiq_disable)
  34279. + *
  34280. + */
  34281. +
  34282. +#include "dwc_otg_fiq_fsm.h"
  34283. +
  34284. +
  34285. +char buffer[1000*16];
  34286. +int wptr;
  34287. +void notrace _fiq_print(enum fiq_debug_level dbg_lvl, volatile struct fiq_state *state, char *fmt, ...)
  34288. +{
  34289. + enum fiq_debug_level dbg_lvl_req = FIQDBG_ERR;
  34290. + va_list args;
  34291. + char text[17];
  34292. + hfnum_data_t hfnum = { .d32 = FIQ_READ(state->dwc_regs_base + 0x408) };
  34293. +
  34294. + if((dbg_lvl & dbg_lvl_req) || dbg_lvl == FIQDBG_ERR)
  34295. + {
  34296. + snprintf(text, 9, " %4d:%1u ", hfnum.b.frnum/8, hfnum.b.frnum & 7);
  34297. + va_start(args, fmt);
  34298. + vsnprintf(text+8, 9, fmt, args);
  34299. + va_end(args);
  34300. +
  34301. + memcpy(buffer + wptr, text, 16);
  34302. + wptr = (wptr + 16) % sizeof(buffer);
  34303. + }
  34304. +}
  34305. +
  34306. +/**
  34307. + * fiq_fsm_spin_lock() - ARMv6+ bare bones spinlock
  34308. + * Must be called with local interrupts and FIQ disabled.
  34309. + */
  34310. +#if defined(CONFIG_ARCH_BCM2835) && defined(CONFIG_SMP)
  34311. +inline void fiq_fsm_spin_lock(fiq_lock_t *lock)
  34312. +{
  34313. + unsigned long tmp;
  34314. + uint32_t newval;
  34315. + fiq_lock_t lockval;
  34316. + smp_mb__before_spinlock();
  34317. + /* Nested locking, yay. If we are on the same CPU as the fiq, then the disable
  34318. + * will be sufficient. If we are on a different CPU, then the lock protects us. */
  34319. + prefetchw(&lock->slock);
  34320. + asm volatile (
  34321. + "1: ldrex %0, [%3]\n"
  34322. + " add %1, %0, %4\n"
  34323. + " strex %2, %1, [%3]\n"
  34324. + " teq %2, #0\n"
  34325. + " bne 1b"
  34326. + : "=&r" (lockval), "=&r" (newval), "=&r" (tmp)
  34327. + : "r" (&lock->slock), "I" (1 << 16)
  34328. + : "cc");
  34329. +
  34330. + while (lockval.tickets.next != lockval.tickets.owner) {
  34331. + wfe();
  34332. + lockval.tickets.owner = ACCESS_ONCE(lock->tickets.owner);
  34333. + }
  34334. + smp_mb();
  34335. +}
  34336. +#else
  34337. +inline void fiq_fsm_spin_lock(fiq_lock_t *lock) { }
  34338. +#endif
  34339. +
  34340. +/**
  34341. + * fiq_fsm_spin_unlock() - ARMv6+ bare bones spinunlock
  34342. + */
  34343. +#if defined(CONFIG_ARCH_BCM2835) && defined(CONFIG_SMP)
  34344. +inline void fiq_fsm_spin_unlock(fiq_lock_t *lock)
  34345. +{
  34346. + smp_mb();
  34347. + lock->tickets.owner++;
  34348. + dsb_sev();
  34349. +}
  34350. +#else
  34351. +inline void fiq_fsm_spin_unlock(fiq_lock_t *lock) { }
  34352. +#endif
  34353. +
  34354. +/**
  34355. + * fiq_fsm_restart_channel() - Poke channel enable bit for a split transaction
  34356. + * @channel: channel to re-enable
  34357. + */
  34358. +static void fiq_fsm_restart_channel(struct fiq_state *st, int n, int force)
  34359. +{
  34360. + hcchar_data_t hcchar = { .d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR) };
  34361. +
  34362. + hcchar.b.chen = 0;
  34363. + if (st->channel[n].hcchar_copy.b.eptype & 0x1) {
  34364. + hfnum_data_t hfnum = { .d32 = FIQ_READ(st->dwc_regs_base + HFNUM) };
  34365. + /* Hardware bug workaround: update the ssplit index */
  34366. + if (st->channel[n].hcsplt_copy.b.spltena)
  34367. + st->channel[n].expected_uframe = (hfnum.b.frnum + 1) & 0x3FFF;
  34368. +
  34369. + hcchar.b.oddfrm = (hfnum.b.frnum & 0x1) ? 0 : 1;
  34370. + }
  34371. +
  34372. + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR, hcchar.d32);
  34373. + hcchar.d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR);
  34374. + hcchar.b.chen = 1;
  34375. +
  34376. + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR, hcchar.d32);
  34377. + fiq_print(FIQDBG_INT, st, "HCGO %01d %01d", n, force);
  34378. +}
  34379. +
  34380. +/**
  34381. + * fiq_fsm_setup_csplit() - Prepare a host channel for a CSplit transaction stage
  34382. + * @st: Pointer to the channel's state
  34383. + * @n : channel number
  34384. + *
  34385. + * Change host channel registers to perform a complete-split transaction. Being mindful of the
  34386. + * endpoint direction, set control regs up correctly.
  34387. + */
  34388. +static void notrace fiq_fsm_setup_csplit(struct fiq_state *st, int n)
  34389. +{
  34390. + hcsplt_data_t hcsplt = { .d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCSPLT) };
  34391. + hctsiz_data_t hctsiz = { .d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ) };
  34392. +
  34393. + hcsplt.b.compsplt = 1;
  34394. + if (st->channel[n].hcchar_copy.b.epdir == 1) {
  34395. + // If IN, the CSPLIT result contains the data or a hub handshake. hctsiz = maxpacket.
  34396. + hctsiz.b.xfersize = st->channel[n].hctsiz_copy.b.xfersize;
  34397. + } else {
  34398. + // If OUT, the CSPLIT result contains handshake only.
  34399. + hctsiz.b.xfersize = 0;
  34400. + }
  34401. + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCSPLT, hcsplt.d32);
  34402. + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ, hctsiz.d32);
  34403. + mb();
  34404. +}
  34405. +
  34406. +static inline int notrace fiq_get_xfer_len(struct fiq_state *st, int n)
  34407. +{
  34408. + /* The xfersize register is a bit wonky. For IN transfers, it decrements by the packet size. */
  34409. + hctsiz_data_t hctsiz = { .d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ) };
  34410. +
  34411. + if (st->channel[n].hcchar_copy.b.epdir == 0) {
  34412. + return st->channel[n].hctsiz_copy.b.xfersize;
  34413. + } else {
  34414. + return st->channel[n].hctsiz_copy.b.xfersize - hctsiz.b.xfersize;
  34415. + }
  34416. +
  34417. +}
  34418. +
  34419. +
  34420. +/**
  34421. + * fiq_increment_dma_buf() - update DMA address for bounce buffers after a CSPLIT
  34422. + *
  34423. + * Of use only for IN periodic transfers.
  34424. + */
  34425. +static int notrace fiq_increment_dma_buf(struct fiq_state *st, int num_channels, int n)
  34426. +{
  34427. + hcdma_data_t hcdma;
  34428. + int i = st->channel[n].dma_info.index;
  34429. + int len;
  34430. + struct fiq_dma_blob *blob = (struct fiq_dma_blob *) st->dma_base;
  34431. +
  34432. + len = fiq_get_xfer_len(st, n);
  34433. + fiq_print(FIQDBG_INT, st, "LEN: %03d", len);
  34434. + st->channel[n].dma_info.slot_len[i] = len;
  34435. + i++;
  34436. + if (i > 6)
  34437. + BUG();
  34438. +
  34439. + hcdma.d32 = (dma_addr_t) &blob->channel[n].index[i].buf[0];
  34440. + FIQ_WRITE(st->dwc_regs_base + HC_DMA + (HC_OFFSET * n), hcdma.d32);
  34441. + st->channel[n].dma_info.index = i;
  34442. + return 0;
  34443. +}
  34444. +
  34445. +/**
  34446. + * fiq_reload_hctsiz() - for IN transactions, reset HCTSIZ
  34447. + */
  34448. +static void notrace fiq_fsm_reload_hctsiz(struct fiq_state *st, int n)
  34449. +{
  34450. + hctsiz_data_t hctsiz = { .d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ) };
  34451. + hctsiz.b.xfersize = st->channel[n].hctsiz_copy.b.xfersize;
  34452. + hctsiz.b.pktcnt = 1;
  34453. + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ, hctsiz.d32);
  34454. +}
  34455. +
  34456. +/**
  34457. + * fiq_iso_out_advance() - update DMA address and split position bits
  34458. + * for isochronous OUT transactions.
  34459. + *
  34460. + * Returns 1 if this is the last packet queued, 0 otherwise. Split-ALL and
  34461. + * Split-BEGIN states are not handled - this is done when the transaction was queued.
  34462. + *
  34463. + * This function must only be called from the FIQ_ISO_OUT_ACTIVE state.
  34464. + */
  34465. +static int notrace fiq_iso_out_advance(struct fiq_state *st, int num_channels, int n)
  34466. +{
  34467. + hcsplt_data_t hcsplt;
  34468. + hctsiz_data_t hctsiz;
  34469. + hcdma_data_t hcdma;
  34470. + struct fiq_dma_blob *blob = (struct fiq_dma_blob *) st->dma_base;
  34471. + int last = 0;
  34472. + int i = st->channel[n].dma_info.index;
  34473. +
  34474. + fiq_print(FIQDBG_INT, st, "ADV %01d %01d ", n, i);
  34475. + i++;
  34476. + if (i == 4)
  34477. + last = 1;
  34478. + if (st->channel[n].dma_info.slot_len[i+1] == 255)
  34479. + last = 1;
  34480. +
  34481. + /* New DMA address - address of bounce buffer referred to in index */
  34482. + hcdma.d32 = (uint32_t) &blob->channel[n].index[i].buf[0];
  34483. + //hcdma.d32 = FIQ_READ(st->dwc_regs_base + HC_DMA + (HC_OFFSET * n));
  34484. + //hcdma.d32 += st->channel[n].dma_info.slot_len[i];
  34485. + fiq_print(FIQDBG_INT, st, "LAST: %01d ", last);
  34486. + fiq_print(FIQDBG_INT, st, "LEN: %03d", st->channel[n].dma_info.slot_len[i]);
  34487. + hcsplt.d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCSPLT);
  34488. + hctsiz.d32 = FIQ_READ(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ);
  34489. + hcsplt.b.xactpos = (last) ? ISOC_XACTPOS_END : ISOC_XACTPOS_MID;
  34490. + /* Set up new packet length */
  34491. + hctsiz.b.pktcnt = 1;
  34492. + hctsiz.b.xfersize = st->channel[n].dma_info.slot_len[i];
  34493. + fiq_print(FIQDBG_INT, st, "%08x", hctsiz.d32);
  34494. +
  34495. + st->channel[n].dma_info.index++;
  34496. + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCSPLT, hcsplt.d32);
  34497. + FIQ_WRITE(st->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ, hctsiz.d32);
  34498. + FIQ_WRITE(st->dwc_regs_base + HC_DMA + (HC_OFFSET * n), hcdma.d32);
  34499. + return last;
  34500. +}
  34501. +
  34502. +/**
  34503. + * fiq_fsm_tt_next_isoc() - queue next pending isochronous out start-split on a TT
  34504. + *
  34505. + * Despite the limitations of the DWC core, we can force a microframe pipeline of
  34506. + * isochronous OUT start-split transactions while waiting for a corresponding other-type
  34507. + * of endpoint to finish its CSPLITs. TTs have big periodic buffers therefore it
  34508. + * is very unlikely that filling the start-split FIFO will cause data loss.
  34509. + * This allows much better interleaving of transactions in an order-independent way-
  34510. + * there is no requirement to prioritise isochronous, just a state-space search has
  34511. + * to be performed on each periodic start-split complete interrupt.
  34512. + */
  34513. +static int notrace fiq_fsm_tt_next_isoc(struct fiq_state *st, int num_channels, int n)
  34514. +{
  34515. + int hub_addr = st->channel[n].hub_addr;
  34516. + int port_addr = st->channel[n].port_addr;
  34517. + int i, poked = 0;
  34518. + for (i = 0; i < num_channels; i++) {
  34519. + if (i == n || st->channel[i].fsm == FIQ_PASSTHROUGH)
  34520. + continue;
  34521. + if (st->channel[i].hub_addr == hub_addr &&
  34522. + st->channel[i].port_addr == port_addr) {
  34523. + switch (st->channel[i].fsm) {
  34524. + case FIQ_PER_ISO_OUT_PENDING:
  34525. + if (st->channel[i].nrpackets == 1) {
  34526. + st->channel[i].fsm = FIQ_PER_ISO_OUT_LAST;
  34527. + } else {
  34528. + st->channel[i].fsm = FIQ_PER_ISO_OUT_ACTIVE;
  34529. + }
  34530. + fiq_fsm_restart_channel(st, i, 0);
  34531. + poked = 1;
  34532. + break;
  34533. +
  34534. + default:
  34535. + break;
  34536. + }
  34537. + }
  34538. + if (poked)
  34539. + break;
  34540. + }
  34541. + return poked;
  34542. +}
  34543. +
  34544. +/**
  34545. + * fiq_fsm_tt_in_use() - search for host channels using this TT
  34546. + * @n: Channel to use as reference
  34547. + *
  34548. + */
  34549. +int notrace noinline fiq_fsm_tt_in_use(struct fiq_state *st, int num_channels, int n)
  34550. +{
  34551. + int hub_addr = st->channel[n].hub_addr;
  34552. + int port_addr = st->channel[n].port_addr;
  34553. + int i, in_use = 0;
  34554. + for (i = 0; i < num_channels; i++) {
  34555. + if (i == n || st->channel[i].fsm == FIQ_PASSTHROUGH)
  34556. + continue;
  34557. + switch (st->channel[i].fsm) {
  34558. + /* TT is reserved for channels that are in the middle of a periodic
  34559. + * split transaction.
  34560. + */
  34561. + case FIQ_PER_SSPLIT_STARTED:
  34562. + case FIQ_PER_CSPLIT_WAIT:
  34563. + case FIQ_PER_CSPLIT_NYET1:
  34564. + //case FIQ_PER_CSPLIT_POLL:
  34565. + case FIQ_PER_ISO_OUT_ACTIVE:
  34566. + case FIQ_PER_ISO_OUT_LAST:
  34567. + if (st->channel[i].hub_addr == hub_addr &&
  34568. + st->channel[i].port_addr == port_addr) {
  34569. + in_use = 1;
  34570. + }
  34571. + break;
  34572. + default:
  34573. + break;
  34574. + }
  34575. + if (in_use)
  34576. + break;
  34577. + }
  34578. + return in_use;
  34579. +}
  34580. +
  34581. +/**
  34582. + * fiq_fsm_more_csplits() - determine whether additional CSPLITs need
  34583. + * to be issued for this IN transaction.
  34584. + *
  34585. + * We cannot tell the inbound PID of a data packet due to hardware limitations.
  34586. + * we need to make an educated guess as to whether we need to queue another CSPLIT
  34587. + * or not. A no-brainer is when we have received enough data to fill the endpoint
  34588. + * size, but for endpoints that give variable-length data then we have to resort
  34589. + * to heuristics.
  34590. + *
  34591. + * We also return whether this is the last CSPLIT to be queued, again based on
  34592. + * heuristics. This is to allow a 1-uframe overlap of periodic split transactions.
  34593. + * Note: requires at least 1 CSPLIT to have been performed prior to being called.
  34594. + */
  34595. +
  34596. +/*
  34597. + * We need some way of guaranteeing if a returned periodic packet of size X
  34598. + * has a DATA0 PID.
  34599. + * The heuristic value of 144 bytes assumes that the received data has maximal
  34600. + * bit-stuffing and the clock frequency of the transmitting device is at the lowest
  34601. + * permissible limit. If the transfer length results in a final packet size
  34602. + * 144 < p <= 188, then an erroneous CSPLIT will be issued.
  34603. + * Also used to ensure that an endpoint will nominally only return a single
  34604. + * complete-split worth of data.
  34605. + */
  34606. +#define DATA0_PID_HEURISTIC 144
  34607. +
  34608. +static int notrace noinline fiq_fsm_more_csplits(struct fiq_state *state, int n, int *probably_last)
  34609. +{
  34610. +
  34611. + int i;
  34612. + int total_len = 0;
  34613. + int more_needed = 1;
  34614. + struct fiq_channel_state *st = &state->channel[n];
  34615. +
  34616. + for (i = 0; i < st->dma_info.index; i++) {
  34617. + total_len += st->dma_info.slot_len[i];
  34618. + }
  34619. +
  34620. + *probably_last = 0;
  34621. +
  34622. + if (st->hcchar_copy.b.eptype == 0x3) {
  34623. + /*
  34624. + * An interrupt endpoint will take max 2 CSPLITs. if we are receiving data
  34625. + * then this is definitely the last CSPLIT.
  34626. + */
  34627. + *probably_last = 1;
  34628. + } else {
  34629. + /* Isoc IN. This is a bit risky if we are the first transaction:
  34630. + * we may have been held off slightly. */
  34631. + if (i > 1 && st->dma_info.slot_len[st->dma_info.index-1] <= DATA0_PID_HEURISTIC) {
  34632. + more_needed = 0;
  34633. + }
  34634. + /* If in the next uframe we will receive enough data to fill the endpoint,
  34635. + * then only issue 1 more csplit.
  34636. + */
  34637. + if (st->hctsiz_copy.b.xfersize - total_len <= DATA0_PID_HEURISTIC)
  34638. + *probably_last = 1;
  34639. + }
  34640. +
  34641. + if (total_len >= st->hctsiz_copy.b.xfersize ||
  34642. + i == 6 || total_len == 0)
  34643. + /* Note: due to bit stuffing it is possible to have > 6 CSPLITs for
  34644. + * a single endpoint. Accepting more would completely break our scheduling mechanism though
  34645. + * - in these extreme cases we will pass through a truncated packet.
  34646. + */
  34647. + more_needed = 0;
  34648. +
  34649. + return more_needed;
  34650. +}
  34651. +
  34652. +/**
  34653. + * fiq_fsm_too_late() - Test transaction for lateness
  34654. + *
  34655. + * If a SSPLIT for a large IN transaction is issued too late in a frame,
  34656. + * the hub will disable the port to the device and respond with ERR handshakes.
  34657. + * The hub status endpoint will not reflect this change.
  34658. + * Returns 1 if we will issue a SSPLIT that will result in a device babble.
  34659. + */
  34660. +int notrace fiq_fsm_too_late(struct fiq_state *st, int n)
  34661. +{
  34662. + int uframe;
  34663. + hfnum_data_t hfnum = { .d32 = FIQ_READ(st->dwc_regs_base + HFNUM) };
  34664. + uframe = hfnum.b.frnum & 0x7;
  34665. + if ((uframe < 6) && (st->channel[n].nrpackets + 1 + uframe > 7)) {
  34666. + return 1;
  34667. + } else {
  34668. + return 0;
  34669. + }
  34670. +}
  34671. +
  34672. +
  34673. +/**
  34674. + * fiq_fsm_start_next_periodic() - A half-arsed attempt at a microframe pipeline
  34675. + *
  34676. + * Search pending transactions in the start-split pending state and queue them.
  34677. + * Don't queue packets in uframe .5 (comes out in .6) (USB2.0 11.18.4).
  34678. + * Note: we specifically don't do isochronous OUT transactions first because better
  34679. + * use of the TT's start-split fifo can be achieved by pipelining an IN before an OUT.
  34680. + */
  34681. +static void notrace noinline fiq_fsm_start_next_periodic(struct fiq_state *st, int num_channels)
  34682. +{
  34683. + int n;
  34684. + hfnum_data_t hfnum = { .d32 = FIQ_READ(st->dwc_regs_base + HFNUM) };
  34685. + if ((hfnum.b.frnum & 0x7) == 5)
  34686. + return;
  34687. + for (n = 0; n < num_channels; n++) {
  34688. + if (st->channel[n].fsm == FIQ_PER_SSPLIT_QUEUED) {
  34689. + /* Check to see if any other transactions are using this TT */
  34690. + if(!fiq_fsm_tt_in_use(st, num_channels, n)) {
  34691. + if (!fiq_fsm_too_late(st, n)) {
  34692. + st->channel[n].fsm = FIQ_PER_SSPLIT_STARTED;
  34693. + fiq_print(FIQDBG_INT, st, "NEXTPER ");
  34694. + fiq_fsm_restart_channel(st, n, 0);
  34695. + } else {
  34696. + st->channel[n].fsm = FIQ_PER_SPLIT_TIMEOUT;
  34697. + }
  34698. + break;
  34699. + }
  34700. + }
  34701. + }
  34702. + for (n = 0; n < num_channels; n++) {
  34703. + if (st->channel[n].fsm == FIQ_PER_ISO_OUT_PENDING) {
  34704. + if (!fiq_fsm_tt_in_use(st, num_channels, n)) {
  34705. + fiq_print(FIQDBG_INT, st, "NEXTISO ");
  34706. + st->channel[n].fsm = FIQ_PER_ISO_OUT_ACTIVE;
  34707. + fiq_fsm_restart_channel(st, n, 0);
  34708. + break;
  34709. + }
  34710. + }
  34711. + }
  34712. +}
  34713. +
  34714. +/**
  34715. + * fiq_fsm_update_hs_isoc() - update isochronous frame and transfer data
  34716. + * @state: Pointer to fiq_state
  34717. + * @n: Channel transaction is active on
  34718. + * @hcint: Copy of host channel interrupt register
  34719. + *
  34720. + * Returns 0 if there are no more transactions for this HC to do, 1
  34721. + * otherwise.
  34722. + */
  34723. +static int notrace noinline fiq_fsm_update_hs_isoc(struct fiq_state *state, int n, hcint_data_t hcint)
  34724. +{
  34725. + struct fiq_channel_state *st = &state->channel[n];
  34726. + int xfer_len = 0, nrpackets = 0;
  34727. + hcdma_data_t hcdma;
  34728. + fiq_print(FIQDBG_INT, state, "HSISO %02d", n);
  34729. +
  34730. + xfer_len = fiq_get_xfer_len(state, n);
  34731. + st->hs_isoc_info.iso_desc[st->hs_isoc_info.index].actual_length = xfer_len;
  34732. +
  34733. + st->hs_isoc_info.iso_desc[st->hs_isoc_info.index].status = hcint.d32;
  34734. +
  34735. + st->hs_isoc_info.index++;
  34736. + if (st->hs_isoc_info.index == st->hs_isoc_info.nrframes) {
  34737. + return 0;
  34738. + }
  34739. +
  34740. + /* grab the next DMA address offset from the array */
  34741. + hcdma.d32 = st->hcdma_copy.d32 + st->hs_isoc_info.iso_desc[st->hs_isoc_info.index].offset;
  34742. + FIQ_WRITE(state->dwc_regs_base + HC_DMA + (HC_OFFSET * n), hcdma.d32);
  34743. +
  34744. + /* We need to set multi_count. This is a bit tricky - has to be set per-transaction as
  34745. + * the core needs to be told to send the correct number. Caution: for IN transfers,
  34746. + * this is always set to the maximum size of the endpoint. */
  34747. + xfer_len = st->hs_isoc_info.iso_desc[st->hs_isoc_info.index].length;
  34748. + /* Integer divide in a FIQ: fun. FIXME: make this not suck */
  34749. + nrpackets = (xfer_len + st->hcchar_copy.b.mps - 1) / st->hcchar_copy.b.mps;
  34750. + if (nrpackets == 0)
  34751. + nrpackets = 1;
  34752. + st->hcchar_copy.b.multicnt = nrpackets;
  34753. + st->hctsiz_copy.b.pktcnt = nrpackets;
  34754. +
  34755. + /* Initial PID also needs to be set */
  34756. + if (st->hcchar_copy.b.epdir == 0) {
  34757. + st->hctsiz_copy.b.xfersize = xfer_len;
  34758. + switch (st->hcchar_copy.b.multicnt) {
  34759. + case 1:
  34760. + st->hctsiz_copy.b.pid = DWC_PID_DATA0;
  34761. + break;
  34762. + case 2:
  34763. + case 3:
  34764. + st->hctsiz_copy.b.pid = DWC_PID_MDATA;
  34765. + break;
  34766. + }
  34767. +
  34768. + } else {
  34769. + switch (st->hcchar_copy.b.multicnt) {
  34770. + st->hctsiz_copy.b.xfersize = nrpackets * st->hcchar_copy.b.mps;
  34771. + case 1:
  34772. + st->hctsiz_copy.b.pid = DWC_PID_DATA0;
  34773. + break;
  34774. + case 2:
  34775. + st->hctsiz_copy.b.pid = DWC_PID_DATA1;
  34776. + break;
  34777. + case 3:
  34778. + st->hctsiz_copy.b.pid = DWC_PID_DATA2;
  34779. + break;
  34780. + }
  34781. + }
  34782. + FIQ_WRITE(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCTSIZ, st->hctsiz_copy.d32);
  34783. + FIQ_WRITE(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR, st->hcchar_copy.d32);
  34784. + /* Channel is enabled on hcint handler exit */
  34785. + fiq_print(FIQDBG_INT, state, "HSISOOUT");
  34786. + return 1;
  34787. +}
  34788. +
  34789. +
  34790. +/**
  34791. + * fiq_fsm_do_sof() - FSM start-of-frame interrupt handler
  34792. + * @state: Pointer to the state struct passed from banked FIQ mode registers.
  34793. + * @num_channels: set according to the DWC hardware configuration
  34794. + *
  34795. + * The SOF handler in FSM mode has two functions
  34796. + * 1. Hold off SOF from causing schedule advancement in IRQ context if there's
  34797. + * nothing to do
  34798. + * 2. Advance certain FSM states that require either a microframe delay, or a microframe
  34799. + * of holdoff.
  34800. + *
  34801. + * The second part is architecture-specific to mach-bcm2835 -
  34802. + * a sane interrupt controller would have a mask register for ARM interrupt sources
  34803. + * to be promoted to the nFIQ line, but it doesn't. Instead a single interrupt
  34804. + * number (USB) can be enabled. This means that certain parts of the USB specification
  34805. + * that require "wait a little while, then issue another packet" cannot be fulfilled with
  34806. + * the timing granularity required to achieve optimal throughout. The workaround is to use
  34807. + * the SOF "timer" (125uS) to perform this task.
  34808. + */
  34809. +static int notrace noinline fiq_fsm_do_sof(struct fiq_state *state, int num_channels)
  34810. +{
  34811. + hfnum_data_t hfnum = { .d32 = FIQ_READ(state->dwc_regs_base + HFNUM) };
  34812. + int n;
  34813. + int kick_irq = 0;
  34814. +
  34815. + if ((hfnum.b.frnum & 0x7) == 1) {
  34816. + /* We cannot issue csplits for transactions in the last frame past (n+1).1
  34817. + * Check to see if there are any transactions that are stale.
  34818. + * Boot them out.
  34819. + */
  34820. + for (n = 0; n < num_channels; n++) {
  34821. + switch (state->channel[n].fsm) {
  34822. + case FIQ_PER_CSPLIT_WAIT:
  34823. + case FIQ_PER_CSPLIT_NYET1:
  34824. + case FIQ_PER_CSPLIT_POLL:
  34825. + case FIQ_PER_CSPLIT_LAST:
  34826. + /* Check if we are no longer in the same full-speed frame. */
  34827. + if (((state->channel[n].expected_uframe & 0x3FFF) & ~0x7) <
  34828. + (hfnum.b.frnum & ~0x7))
  34829. + state->channel[n].fsm = FIQ_PER_SPLIT_TIMEOUT;
  34830. + break;
  34831. + default:
  34832. + break;
  34833. + }
  34834. + }
  34835. + }
  34836. +
  34837. + for (n = 0; n < num_channels; n++) {
  34838. + switch (state->channel[n].fsm) {
  34839. +
  34840. + case FIQ_NP_SSPLIT_RETRY:
  34841. + case FIQ_NP_IN_CSPLIT_RETRY:
  34842. + case FIQ_NP_OUT_CSPLIT_RETRY:
  34843. + fiq_fsm_restart_channel(state, n, 0);
  34844. + break;
  34845. +
  34846. + case FIQ_HS_ISOC_SLEEPING:
  34847. + /* Is it time to wake this channel yet? */
  34848. + if (--state->channel[n].uframe_sleeps == 0) {
  34849. + state->channel[n].fsm = FIQ_HS_ISOC_TURBO;
  34850. + fiq_fsm_restart_channel(state, n, 0);
  34851. + }
  34852. + break;
  34853. +
  34854. + case FIQ_PER_SSPLIT_QUEUED:
  34855. + if ((hfnum.b.frnum & 0x7) == 5)
  34856. + break;
  34857. + if(!fiq_fsm_tt_in_use(state, num_channels, n)) {
  34858. + if (!fiq_fsm_too_late(state, n)) {
  34859. + fiq_print(FIQDBG_INT, state, "SOF GO %01d", n);
  34860. + fiq_fsm_restart_channel(state, n, 0);
  34861. + state->channel[n].fsm = FIQ_PER_SSPLIT_STARTED;
  34862. + } else {
  34863. + /* Transaction cannot be started without risking a device babble error */
  34864. + state->channel[n].fsm = FIQ_PER_SPLIT_TIMEOUT;
  34865. + state->haintmsk_saved.b2.chint &= ~(1 << n);
  34866. + FIQ_WRITE(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCINTMSK, 0);
  34867. + kick_irq |= 1;
  34868. + }
  34869. + }
  34870. + break;
  34871. +
  34872. + case FIQ_PER_ISO_OUT_PENDING:
  34873. + /* Ordinarily, this should be poked after the SSPLIT
  34874. + * complete interrupt for a competing transfer on the same
  34875. + * TT. Doesn't happen for aborted transactions though.
  34876. + */
  34877. + if ((hfnum.b.frnum & 0x7) >= 5)
  34878. + break;
  34879. + if (!fiq_fsm_tt_in_use(state, num_channels, n)) {
  34880. + /* Hardware bug. SOF can sometimes occur after the channel halt interrupt
  34881. + * that caused this.
  34882. + */
  34883. + fiq_fsm_restart_channel(state, n, 0);
  34884. + fiq_print(FIQDBG_INT, state, "SOF ISOC");
  34885. + if (state->channel[n].nrpackets == 1) {
  34886. + state->channel[n].fsm = FIQ_PER_ISO_OUT_LAST;
  34887. + } else {
  34888. + state->channel[n].fsm = FIQ_PER_ISO_OUT_ACTIVE;
  34889. + }
  34890. + }
  34891. + break;
  34892. +
  34893. + case FIQ_PER_CSPLIT_WAIT:
  34894. + /* we are guaranteed to be in this state if and only if the SSPLIT interrupt
  34895. + * occurred when the bus transaction occurred. The SOF interrupt reversal bug
  34896. + * will utterly bugger this up though.
  34897. + */
  34898. + if (hfnum.b.frnum != state->channel[n].expected_uframe) {
  34899. + fiq_print(FIQDBG_INT, state, "SOFCS %d ", n);
  34900. + state->channel[n].fsm = FIQ_PER_CSPLIT_POLL;
  34901. + fiq_fsm_restart_channel(state, n, 0);
  34902. + fiq_fsm_start_next_periodic(state, num_channels);
  34903. +
  34904. + }
  34905. + break;
  34906. +
  34907. + case FIQ_PER_SPLIT_TIMEOUT:
  34908. + case FIQ_DEQUEUE_ISSUED:
  34909. + /* Ugly: we have to force a HCD interrupt.
  34910. + * Poke the mask for the channel in question.
  34911. + * We will take a fake SOF because of this, but
  34912. + * that's OK.
  34913. + */
  34914. + state->haintmsk_saved.b2.chint &= ~(1 << n);
  34915. + FIQ_WRITE(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCINTMSK, 0);
  34916. + kick_irq |= 1;
  34917. + break;
  34918. +
  34919. + default:
  34920. + break;
  34921. + }
  34922. + }
  34923. +
  34924. + if (state->kick_np_queues ||
  34925. + dwc_frame_num_le(state->next_sched_frame, hfnum.b.frnum))
  34926. + kick_irq |= 1;
  34927. +
  34928. + return !kick_irq;
  34929. +}
  34930. +
  34931. +
  34932. +/**
  34933. + * fiq_fsm_do_hcintr() - FSM host channel interrupt handler
  34934. + * @state: Pointer to the FIQ state struct
  34935. + * @num_channels: Number of channels as per hardware config
  34936. + * @n: channel for which HAINT(i) was raised
  34937. + *
  34938. + * An important property is that only the CHHLT interrupt is unmasked. Unfortunately, AHBerr is as well.
  34939. + */
  34940. +static int notrace noinline fiq_fsm_do_hcintr(struct fiq_state *state, int num_channels, int n)
  34941. +{
  34942. + hcint_data_t hcint;
  34943. + hcintmsk_data_t hcintmsk;
  34944. + hcint_data_t hcint_probe;
  34945. + hcchar_data_t hcchar;
  34946. + int handled = 0;
  34947. + int restart = 0;
  34948. + int last_csplit = 0;
  34949. + int start_next_periodic = 0;
  34950. + struct fiq_channel_state *st = &state->channel[n];
  34951. + hfnum_data_t hfnum;
  34952. +
  34953. + hcint.d32 = FIQ_READ(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCINT);
  34954. + hcintmsk.d32 = FIQ_READ(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCINTMSK);
  34955. + hcint_probe.d32 = hcint.d32 & hcintmsk.d32;
  34956. +
  34957. + if (st->fsm != FIQ_PASSTHROUGH) {
  34958. + fiq_print(FIQDBG_INT, state, "HC%01d ST%02d", n, st->fsm);
  34959. + fiq_print(FIQDBG_INT, state, "%08x", hcint.d32);
  34960. + }
  34961. +
  34962. + switch (st->fsm) {
  34963. +
  34964. + case FIQ_PASSTHROUGH:
  34965. + case FIQ_DEQUEUE_ISSUED:
  34966. + /* doesn't belong to us, kick it upstairs */
  34967. + break;
  34968. +
  34969. + case FIQ_PASSTHROUGH_ERRORSTATE:
  34970. + /* We are here to emulate the error recovery mechanism of the dwc HCD.
  34971. + * Several interrupts are unmasked if a previous transaction failed - it's
  34972. + * death for the FIQ to attempt to handle them as the channel isn't halted.
  34973. + * Emulate what the HCD does in this situation: mask and continue.
  34974. + * The FSM has no other state setup so this has to be handled out-of-band.
  34975. + */
  34976. + fiq_print(FIQDBG_ERR, state, "ERRST %02d", n);
  34977. + if (hcint_probe.b.nak || hcint_probe.b.ack || hcint_probe.b.datatglerr) {
  34978. + fiq_print(FIQDBG_ERR, state, "RESET %02d", n);
  34979. + /* In some random cases we can get a NAK interrupt coincident with a Xacterr
  34980. + * interrupt, after the device has disappeared.
  34981. + */
  34982. + if (!hcint.b.xacterr)
  34983. + st->nr_errors = 0;
  34984. + hcintmsk.b.nak = 0;
  34985. + hcintmsk.b.ack = 0;
  34986. + hcintmsk.b.datatglerr = 0;
  34987. + FIQ_WRITE(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCINTMSK, hcintmsk.d32);
  34988. + return 1;
  34989. + }
  34990. + if (hcint_probe.b.chhltd) {
  34991. + fiq_print(FIQDBG_ERR, state, "CHHLT %02d", n);
  34992. + fiq_print(FIQDBG_ERR, state, "%08x", hcint.d32);
  34993. + return 0;
  34994. + }
  34995. + break;
  34996. +
  34997. + /* Non-periodic state groups */
  34998. + case FIQ_NP_SSPLIT_STARTED:
  34999. + case FIQ_NP_SSPLIT_RETRY:
  35000. + /* Got a HCINT for a NP SSPLIT. Expected ACK / NAK / fail */
  35001. + if (hcint.b.ack) {
  35002. + /* SSPLIT complete. For OUT, the data has been sent. For IN, the LS transaction
  35003. + * will start shortly. SOF needs to kick the transaction to prevent a NYET flood.
  35004. + */
  35005. + if(st->hcchar_copy.b.epdir == 1)
  35006. + st->fsm = FIQ_NP_IN_CSPLIT_RETRY;
  35007. + else
  35008. + st->fsm = FIQ_NP_OUT_CSPLIT_RETRY;
  35009. + st->nr_errors = 0;
  35010. + handled = 1;
  35011. + fiq_fsm_setup_csplit(state, n);
  35012. + } else if (hcint.b.nak) {
  35013. + // No buffer space in TT. Retry on a uframe boundary.
  35014. + st->fsm = FIQ_NP_SSPLIT_RETRY;
  35015. + handled = 1;
  35016. + } else if (hcint.b.xacterr) {
  35017. + // The only other one we care about is xacterr. This implies HS bus error - retry.
  35018. + st->nr_errors++;
  35019. + st->fsm = FIQ_NP_SSPLIT_RETRY;
  35020. + if (st->nr_errors >= 3) {
  35021. + st->fsm = FIQ_NP_SPLIT_HS_ABORTED;
  35022. + } else {
  35023. + handled = 1;
  35024. + restart = 1;
  35025. + }
  35026. + } else {
  35027. + st->fsm = FIQ_NP_SPLIT_LS_ABORTED;
  35028. + handled = 0;
  35029. + restart = 0;
  35030. + }
  35031. + break;
  35032. +
  35033. + case FIQ_NP_IN_CSPLIT_RETRY:
  35034. + /* Received a CSPLIT done interrupt.
  35035. + * Expected Data/NAK/STALL/NYET for IN.
  35036. + */
  35037. + if (hcint.b.xfercomp) {
  35038. + /* For IN, data is present. */
  35039. + st->fsm = FIQ_NP_SPLIT_DONE;
  35040. + } else if (hcint.b.nak) {
  35041. + /* no endpoint data. Punt it upstairs */
  35042. + st->fsm = FIQ_NP_SPLIT_DONE;
  35043. + } else if (hcint.b.nyet) {
  35044. + /* CSPLIT NYET - retry on a uframe boundary. */
  35045. + handled = 1;
  35046. + st->nr_errors = 0;
  35047. + } else if (hcint.b.datatglerr) {
  35048. + /* data toggle errors do not set the xfercomp bit. */
  35049. + st->fsm = FIQ_NP_SPLIT_LS_ABORTED;
  35050. + } else if (hcint.b.xacterr) {
  35051. + /* HS error. Retry immediate */
  35052. + st->fsm = FIQ_NP_IN_CSPLIT_RETRY;
  35053. + st->nr_errors++;
  35054. + if (st->nr_errors >= 3) {
  35055. + st->fsm = FIQ_NP_SPLIT_HS_ABORTED;
  35056. + } else {
  35057. + handled = 1;
  35058. + restart = 1;
  35059. + }
  35060. + } else if (hcint.b.stall || hcint.b.bblerr) {
  35061. + /* A STALL implies either a LS bus error or a genuine STALL. */
  35062. + st->fsm = FIQ_NP_SPLIT_LS_ABORTED;
  35063. + } else {
  35064. + /* Hardware bug. It's possible in some cases to
  35065. + * get a channel halt with nothing else set when
  35066. + * the response was a NYET. Treat as local 3-strikes retry.
  35067. + */
  35068. + hcint_data_t hcint_test = hcint;
  35069. + hcint_test.b.chhltd = 0;
  35070. + if (!hcint_test.d32) {
  35071. + st->nr_errors++;
  35072. + if (st->nr_errors >= 3) {
  35073. + st->fsm = FIQ_NP_SPLIT_HS_ABORTED;
  35074. + } else {
  35075. + handled = 1;
  35076. + }
  35077. + } else {
  35078. + /* Bail out if something unexpected happened */
  35079. + st->fsm = FIQ_NP_SPLIT_HS_ABORTED;
  35080. + }
  35081. + }
  35082. + break;
  35083. +
  35084. + case FIQ_NP_OUT_CSPLIT_RETRY:
  35085. + /* Received a CSPLIT done interrupt.
  35086. + * Expected ACK/NAK/STALL/NYET/XFERCOMP for OUT.*/
  35087. + if (hcint.b.xfercomp) {
  35088. + st->fsm = FIQ_NP_SPLIT_DONE;
  35089. + } else if (hcint.b.nak) {
  35090. + // The HCD will implement the holdoff on frame boundaries.
  35091. + st->fsm = FIQ_NP_SPLIT_DONE;
  35092. + } else if (hcint.b.nyet) {
  35093. + // Hub still processing.
  35094. + st->fsm = FIQ_NP_OUT_CSPLIT_RETRY;
  35095. + handled = 1;
  35096. + st->nr_errors = 0;
  35097. + //restart = 1;
  35098. + } else if (hcint.b.xacterr) {
  35099. + /* HS error. retry immediate */
  35100. + st->fsm = FIQ_NP_OUT_CSPLIT_RETRY;
  35101. + st->nr_errors++;
  35102. + if (st->nr_errors >= 3) {
  35103. + st->fsm = FIQ_NP_SPLIT_HS_ABORTED;
  35104. + } else {
  35105. + handled = 1;
  35106. + restart = 1;
  35107. + }
  35108. + } else if (hcint.b.stall) {
  35109. + /* LS bus error or genuine stall */
  35110. + st->fsm = FIQ_NP_SPLIT_LS_ABORTED;
  35111. + } else {
  35112. + /*
  35113. + * Hardware bug. It's possible in some cases to get a
  35114. + * channel halt with nothing else set when the response was a NYET.
  35115. + * Treat as local 3-strikes retry.
  35116. + */
  35117. + hcint_data_t hcint_test = hcint;
  35118. + hcint_test.b.chhltd = 0;
  35119. + if (!hcint_test.d32) {
  35120. + st->nr_errors++;
  35121. + if (st->nr_errors >= 3) {
  35122. + st->fsm = FIQ_NP_SPLIT_HS_ABORTED;
  35123. + } else {
  35124. + handled = 1;
  35125. + }
  35126. + } else {
  35127. + // Something unexpected happened. AHBerror or babble perhaps. Let the IRQ deal with it.
  35128. + st->fsm = FIQ_NP_SPLIT_HS_ABORTED;
  35129. + }
  35130. + }
  35131. + break;
  35132. +
  35133. + /* Periodic split states (except isoc out) */
  35134. + case FIQ_PER_SSPLIT_STARTED:
  35135. + /* Expect an ACK or failure for SSPLIT */
  35136. + if (hcint.b.ack) {
  35137. + /*
  35138. + * SSPLIT transfer complete interrupt - the generation of this interrupt is fraught with bugs.
  35139. + * For a packet queued in microframe n-3 to appear in n-2, if the channel is enabled near the EOF1
  35140. + * point for microframe n-3, the packet will not appear on the bus until microframe n.
  35141. + * Additionally, the generation of the actual interrupt is dodgy. For a packet appearing on the bus
  35142. + * in microframe n, sometimes the interrupt is generated immediately. Sometimes, it appears in n+1
  35143. + * coincident with SOF for n+1.
  35144. + * SOF is also buggy. It can sometimes be raised AFTER the first bus transaction has taken place.
  35145. + * These appear to be caused by timing/clock crossing bugs within the core itself.
  35146. + * State machine workaround.
  35147. + */
  35148. + hfnum.d32 = FIQ_READ(state->dwc_regs_base + HFNUM);
  35149. + hcchar.d32 = FIQ_READ(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR);
  35150. + fiq_fsm_setup_csplit(state, n);
  35151. + /* Poke the oddfrm bit. If we are equivalent, we received the interrupt at the correct
  35152. + * time. If not, then we're in the next SOF.
  35153. + */
  35154. + if ((hfnum.b.frnum & 0x1) == hcchar.b.oddfrm) {
  35155. + fiq_print(FIQDBG_INT, state, "CSWAIT %01d", n);
  35156. + st->expected_uframe = hfnum.b.frnum;
  35157. + st->fsm = FIQ_PER_CSPLIT_WAIT;
  35158. + } else {
  35159. + fiq_print(FIQDBG_INT, state, "CSPOL %01d", n);
  35160. + /* For isochronous IN endpoints,
  35161. + * we need to hold off if we are expecting a lot of data */
  35162. + if (st->hcchar_copy.b.mps < DATA0_PID_HEURISTIC) {
  35163. + start_next_periodic = 1;
  35164. + }
  35165. + /* Danger will robinson: we are in a broken state. If our first interrupt after
  35166. + * this is a NYET, it will be delayed by 1 uframe and result in an unrecoverable
  35167. + * lag. Unmask the NYET interrupt.
  35168. + */
  35169. + st->expected_uframe = (hfnum.b.frnum + 1) & 0x3FFF;
  35170. + st->fsm = FIQ_PER_CSPLIT_BROKEN_NYET1;
  35171. + restart = 1;
  35172. + }
  35173. + handled = 1;
  35174. + } else if (hcint.b.xacterr) {
  35175. + /* 3-strikes retry is enabled, we have hit our max nr_errors */
  35176. + st->fsm = FIQ_PER_SPLIT_HS_ABORTED;
  35177. + start_next_periodic = 1;
  35178. + } else {
  35179. + st->fsm = FIQ_PER_SPLIT_HS_ABORTED;
  35180. + start_next_periodic = 1;
  35181. + }
  35182. + /* We can now queue the next isochronous OUT transaction, if one is pending. */
  35183. + if(fiq_fsm_tt_next_isoc(state, num_channels, n)) {
  35184. + fiq_print(FIQDBG_INT, state, "NEXTISO ");
  35185. + }
  35186. + break;
  35187. +
  35188. + case FIQ_PER_CSPLIT_NYET1:
  35189. + /* First CSPLIT attempt was a NYET. If we get a subsequent NYET,
  35190. + * we are too late and the TT has dropped its CSPLIT fifo.
  35191. + */
  35192. + hfnum.d32 = FIQ_READ(state->dwc_regs_base + HFNUM);
  35193. + hcchar.d32 = FIQ_READ(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR);
  35194. + start_next_periodic = 1;
  35195. + if (hcint.b.nak) {
  35196. + st->fsm = FIQ_PER_SPLIT_DONE;
  35197. + } else if (hcint.b.xfercomp) {
  35198. + fiq_increment_dma_buf(state, num_channels, n);
  35199. + st->fsm = FIQ_PER_CSPLIT_POLL;
  35200. + st->nr_errors = 0;
  35201. + if (fiq_fsm_more_csplits(state, n, &last_csplit)) {
  35202. + handled = 1;
  35203. + restart = 1;
  35204. + if (!last_csplit)
  35205. + start_next_periodic = 0;
  35206. + } else {
  35207. + st->fsm = FIQ_PER_SPLIT_DONE;
  35208. + }
  35209. + } else if (hcint.b.nyet) {
  35210. + /* Doh. Data lost. */
  35211. + st->fsm = FIQ_PER_SPLIT_NYET_ABORTED;
  35212. + } else if (hcint.b.xacterr || hcint.b.stall || hcint.b.bblerr) {
  35213. + st->fsm = FIQ_PER_SPLIT_LS_ABORTED;
  35214. + } else {
  35215. + st->fsm = FIQ_PER_SPLIT_HS_ABORTED;
  35216. + }
  35217. + break;
  35218. +
  35219. + case FIQ_PER_CSPLIT_BROKEN_NYET1:
  35220. + /*
  35221. + * we got here because our host channel is in the delayed-interrupt
  35222. + * state and we cannot take a NYET interrupt any later than when it
  35223. + * occurred. Disable then re-enable the channel if this happens to force
  35224. + * CSPLITs to occur at the right time.
  35225. + */
  35226. + hfnum.d32 = FIQ_READ(state->dwc_regs_base + HFNUM);
  35227. + hcchar.d32 = FIQ_READ(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR);
  35228. + fiq_print(FIQDBG_INT, state, "BROK: %01d ", n);
  35229. + if (hcint.b.nak) {
  35230. + st->fsm = FIQ_PER_SPLIT_DONE;
  35231. + start_next_periodic = 1;
  35232. + } else if (hcint.b.xfercomp) {
  35233. + fiq_increment_dma_buf(state, num_channels, n);
  35234. + if (fiq_fsm_more_csplits(state, n, &last_csplit)) {
  35235. + st->fsm = FIQ_PER_CSPLIT_POLL;
  35236. + handled = 1;
  35237. + restart = 1;
  35238. + start_next_periodic = 1;
  35239. + /* Reload HCTSIZ for the next transfer */
  35240. + fiq_fsm_reload_hctsiz(state, n);
  35241. + if (!last_csplit)
  35242. + start_next_periodic = 0;
  35243. + } else {
  35244. + st->fsm = FIQ_PER_SPLIT_DONE;
  35245. + }
  35246. + } else if (hcint.b.nyet) {
  35247. + st->fsm = FIQ_PER_SPLIT_NYET_ABORTED;
  35248. + start_next_periodic = 1;
  35249. + } else if (hcint.b.xacterr || hcint.b.stall || hcint.b.bblerr) {
  35250. + /* Local 3-strikes retry is handled by the core. This is a ERR response.*/
  35251. + st->fsm = FIQ_PER_SPLIT_LS_ABORTED;
  35252. + } else {
  35253. + st->fsm = FIQ_PER_SPLIT_HS_ABORTED;
  35254. + }
  35255. + break;
  35256. +
  35257. + case FIQ_PER_CSPLIT_POLL:
  35258. + hfnum.d32 = FIQ_READ(state->dwc_regs_base + HFNUM);
  35259. + hcchar.d32 = FIQ_READ(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCCHAR);
  35260. + start_next_periodic = 1;
  35261. + if (hcint.b.nak) {
  35262. + st->fsm = FIQ_PER_SPLIT_DONE;
  35263. + } else if (hcint.b.xfercomp) {
  35264. + fiq_increment_dma_buf(state, num_channels, n);
  35265. + if (fiq_fsm_more_csplits(state, n, &last_csplit)) {
  35266. + handled = 1;
  35267. + restart = 1;
  35268. + /* Reload HCTSIZ for the next transfer */
  35269. + fiq_fsm_reload_hctsiz(state, n);
  35270. + if (!last_csplit)
  35271. + start_next_periodic = 0;
  35272. + } else {
  35273. + st->fsm = FIQ_PER_SPLIT_DONE;
  35274. + }
  35275. + } else if (hcint.b.nyet) {
  35276. + /* Are we a NYET after the first data packet? */
  35277. + if (st->nrpackets == 0) {
  35278. + st->fsm = FIQ_PER_CSPLIT_NYET1;
  35279. + handled = 1;
  35280. + restart = 1;
  35281. + } else {
  35282. + /* We got a NYET when polling CSPLITs. Can happen
  35283. + * if our heuristic fails, or if someone disables us
  35284. + * for any significant length of time.
  35285. + */
  35286. + if (st->nr_errors >= 3) {
  35287. + st->fsm = FIQ_PER_SPLIT_NYET_ABORTED;
  35288. + } else {
  35289. + st->fsm = FIQ_PER_SPLIT_DONE;
  35290. + }
  35291. + }
  35292. + } else if (hcint.b.xacterr || hcint.b.stall || hcint.b.bblerr) {
  35293. + /* For xacterr, Local 3-strikes retry is handled by the core. This is a ERR response.*/
  35294. + st->fsm = FIQ_PER_SPLIT_LS_ABORTED;
  35295. + } else {
  35296. + st->fsm = FIQ_PER_SPLIT_HS_ABORTED;
  35297. + }
  35298. + break;
  35299. +
  35300. + case FIQ_HS_ISOC_TURBO:
  35301. + if (fiq_fsm_update_hs_isoc(state, n, hcint)) {
  35302. + /* more transactions to come */
  35303. + handled = 1;
  35304. + fiq_print(FIQDBG_INT, state, "HSISO M ");
  35305. + /* For strided transfers, put ourselves to sleep */
  35306. + if (st->hs_isoc_info.stride > 1) {
  35307. + st->uframe_sleeps = st->hs_isoc_info.stride - 1;
  35308. + st->fsm = FIQ_HS_ISOC_SLEEPING;
  35309. + } else {
  35310. + restart = 1;
  35311. + }
  35312. + } else {
  35313. + st->fsm = FIQ_HS_ISOC_DONE;
  35314. + fiq_print(FIQDBG_INT, state, "HSISO F ");
  35315. + }
  35316. + break;
  35317. +
  35318. + case FIQ_HS_ISOC_ABORTED:
  35319. + /* This abort is called by the driver rewriting the state mid-transaction
  35320. + * which allows the dequeue mechanism to work more effectively.
  35321. + */
  35322. + break;
  35323. +
  35324. + case FIQ_PER_ISO_OUT_ACTIVE:
  35325. + if (hcint.b.ack) {
  35326. + if(fiq_iso_out_advance(state, num_channels, n)) {
  35327. + /* last OUT transfer */
  35328. + st->fsm = FIQ_PER_ISO_OUT_LAST;
  35329. + /*
  35330. + * Assuming the periodic FIFO in the dwc core
  35331. + * actually does its job properly, we can queue
  35332. + * the next ssplit now and in theory, the wire
  35333. + * transactions will be in-order.
  35334. + */
  35335. + // No it doesn't. It appears to process requests in host channel order.
  35336. + //start_next_periodic = 1;
  35337. + }
  35338. + handled = 1;
  35339. + restart = 1;
  35340. + } else {
  35341. + /*
  35342. + * Isochronous transactions carry on regardless. Log the error
  35343. + * and continue.
  35344. + */
  35345. + //explode += 1;
  35346. + st->nr_errors++;
  35347. + if(fiq_iso_out_advance(state, num_channels, n)) {
  35348. + st->fsm = FIQ_PER_ISO_OUT_LAST;
  35349. + //start_next_periodic = 1;
  35350. + }
  35351. + handled = 1;
  35352. + restart = 1;
  35353. + }
  35354. + break;
  35355. +
  35356. + case FIQ_PER_ISO_OUT_LAST:
  35357. + if (hcint.b.ack) {
  35358. + /* All done here */
  35359. + st->fsm = FIQ_PER_ISO_OUT_DONE;
  35360. + } else {
  35361. + st->fsm = FIQ_PER_ISO_OUT_DONE;
  35362. + st->nr_errors++;
  35363. + }
  35364. + start_next_periodic = 1;
  35365. + break;
  35366. +
  35367. + case FIQ_PER_SPLIT_TIMEOUT:
  35368. + /* SOF kicked us because we overran. */
  35369. + start_next_periodic = 1;
  35370. + break;
  35371. +
  35372. + default:
  35373. + break;
  35374. + }
  35375. +
  35376. + if (handled) {
  35377. + FIQ_WRITE(state->dwc_regs_base + HC_START + (HC_OFFSET * n) + HCINT, hcint.d32);
  35378. + } else {
  35379. + /* Copy the regs into the state so the IRQ knows what to do */
  35380. + st->hcint_copy.d32 = hcint.d32;
  35381. + }
  35382. +
  35383. + if (restart) {
  35384. + /* Restart always implies handled. */
  35385. + if (restart == 2) {
  35386. + /* For complete-split INs, the show must go on.
  35387. + * Force a channel restart */
  35388. + fiq_fsm_restart_channel(state, n, 1);
  35389. + } else {
  35390. + fiq_fsm_restart_channel(state, n, 0);
  35391. + }
  35392. + }
  35393. + if (start_next_periodic) {
  35394. + fiq_fsm_start_next_periodic(state, num_channels);
  35395. + }
  35396. + if (st->fsm != FIQ_PASSTHROUGH)
  35397. + fiq_print(FIQDBG_INT, state, "FSMOUT%02d", st->fsm);
  35398. +
  35399. + return handled;
  35400. +}
  35401. +
  35402. +
  35403. +/**
  35404. + * dwc_otg_fiq_fsm() - Flying State Machine (monster) FIQ
  35405. + * @state: pointer to state struct passed from the banked FIQ mode registers.
  35406. + * @num_channels: set according to the DWC hardware configuration
  35407. + * @dma: pointer to DMA bounce buffers for split transaction slots
  35408. + *
  35409. + * The FSM FIQ performs the low-level tasks that normally would be performed by the microcode
  35410. + * inside an EHCI or similar host controller regarding split transactions. The DWC core
  35411. + * interrupts each and every time a split transaction packet is received or sent successfully.
  35412. + * This results in either an interrupt storm when everything is working "properly", or
  35413. + * the interrupt latency of the system in general breaks time-sensitive periodic split
  35414. + * transactions. Pushing the low-level, but relatively easy state machine work into the FIQ
  35415. + * solves these problems.
  35416. + *
  35417. + * Return: void
  35418. + */
  35419. +void notrace dwc_otg_fiq_fsm(struct fiq_state *state, int num_channels)
  35420. +{
  35421. + gintsts_data_t gintsts, gintsts_handled;
  35422. + gintmsk_data_t gintmsk;
  35423. + //hfnum_data_t hfnum;
  35424. + haint_data_t haint, haint_handled;
  35425. + haintmsk_data_t haintmsk;
  35426. + int kick_irq = 0;
  35427. +
  35428. + gintsts_handled.d32 = 0;
  35429. + haint_handled.d32 = 0;
  35430. +
  35431. + fiq_fsm_spin_lock(&state->lock);
  35432. + gintsts.d32 = FIQ_READ(state->dwc_regs_base + GINTSTS);
  35433. + gintmsk.d32 = FIQ_READ(state->dwc_regs_base + GINTMSK);
  35434. + gintsts.d32 &= gintmsk.d32;
  35435. +
  35436. + if (gintsts.b.sofintr) {
  35437. + /* For FSM mode, SOF is required to keep the state machine advance for
  35438. + * certain stages of the periodic pipeline. It's death to mask this
  35439. + * interrupt in that case.
  35440. + */
  35441. +
  35442. + if (!fiq_fsm_do_sof(state, num_channels)) {
  35443. + /* Kick IRQ once. Queue advancement means that all pending transactions
  35444. + * will get serviced when the IRQ finally executes.
  35445. + */
  35446. + if (state->gintmsk_saved.b.sofintr == 1)
  35447. + kick_irq |= 1;
  35448. + state->gintmsk_saved.b.sofintr = 0;
  35449. + }
  35450. + gintsts_handled.b.sofintr = 1;
  35451. + }
  35452. +
  35453. + if (gintsts.b.hcintr) {
  35454. + int i;
  35455. + haint.d32 = FIQ_READ(state->dwc_regs_base + HAINT);
  35456. + haintmsk.d32 = FIQ_READ(state->dwc_regs_base + HAINTMSK);
  35457. + haint.d32 &= haintmsk.d32;
  35458. + haint_handled.d32 = 0;
  35459. + for (i=0; i<num_channels; i++) {
  35460. + if (haint.b2.chint & (1 << i)) {
  35461. + if(!fiq_fsm_do_hcintr(state, num_channels, i)) {
  35462. + /* HCINT was not handled in FIQ
  35463. + * HAINT is level-sensitive, leading to level-sensitive ginststs.b.hcint bit.
  35464. + * Mask HAINT(i) but keep top-level hcint unmasked.
  35465. + */
  35466. + state->haintmsk_saved.b2.chint &= ~(1 << i);
  35467. + } else {
  35468. + /* do_hcintr cleaned up after itself, but clear haint */
  35469. + haint_handled.b2.chint |= (1 << i);
  35470. + }
  35471. + }
  35472. + }
  35473. +
  35474. + if (haint_handled.b2.chint) {
  35475. + FIQ_WRITE(state->dwc_regs_base + HAINT, haint_handled.d32);
  35476. + }
  35477. +
  35478. + if (haintmsk.d32 != (haintmsk.d32 & state->haintmsk_saved.d32)) {
  35479. + /*
  35480. + * This is necessary to avoid multiple retriggers of the MPHI in the case
  35481. + * where interrupts are held off and HCINTs start to pile up.
  35482. + * Only wake up the IRQ if a new interrupt came in, was not handled and was
  35483. + * masked.
  35484. + */
  35485. + haintmsk.d32 &= state->haintmsk_saved.d32;
  35486. + FIQ_WRITE(state->dwc_regs_base + HAINTMSK, haintmsk.d32);
  35487. + kick_irq |= 1;
  35488. + }
  35489. + /* Top-Level interrupt - always handled because it's level-sensitive */
  35490. + gintsts_handled.b.hcintr = 1;
  35491. + }
  35492. +
  35493. +
  35494. + /* Clear the bits in the saved register that were not handled but were triggered. */
  35495. + state->gintmsk_saved.d32 &= ~(gintsts.d32 & ~gintsts_handled.d32);
  35496. +
  35497. + /* FIQ didn't handle something - mask has changed - write new mask */
  35498. + if (gintmsk.d32 != (gintmsk.d32 & state->gintmsk_saved.d32)) {
  35499. + gintmsk.d32 &= state->gintmsk_saved.d32;
  35500. + gintmsk.b.sofintr = 1;
  35501. + FIQ_WRITE(state->dwc_regs_base + GINTMSK, gintmsk.d32);
  35502. +// fiq_print(FIQDBG_INT, state, "KICKGINT");
  35503. +// fiq_print(FIQDBG_INT, state, "%08x", gintmsk.d32);
  35504. +// fiq_print(FIQDBG_INT, state, "%08x", state->gintmsk_saved.d32);
  35505. + kick_irq |= 1;
  35506. + }
  35507. +
  35508. + if (gintsts_handled.d32) {
  35509. + /* Only applies to edge-sensitive bits in GINTSTS */
  35510. + FIQ_WRITE(state->dwc_regs_base + GINTSTS, gintsts_handled.d32);
  35511. + }
  35512. +
  35513. + /* We got an interrupt, didn't handle it. */
  35514. + if (kick_irq) {
  35515. + state->mphi_int_count++;
  35516. + FIQ_WRITE(state->mphi_regs.outdda, (int) state->dummy_send);
  35517. + FIQ_WRITE(state->mphi_regs.outddb, (1<<29));
  35518. +
  35519. + }
  35520. + state->fiq_done++;
  35521. + mb();
  35522. + fiq_fsm_spin_unlock(&state->lock);
  35523. +}
  35524. +
  35525. +
  35526. +/**
  35527. + * dwc_otg_fiq_nop() - FIQ "lite"
  35528. + * @state: pointer to state struct passed from the banked FIQ mode registers.
  35529. + *
  35530. + * The "nop" handler does not intervene on any interrupts other than SOF.
  35531. + * It is limited in scope to deciding at each SOF if the IRQ SOF handler (which deals
  35532. + * with non-periodic/periodic queues) needs to be kicked.
  35533. + *
  35534. + * This is done to hold off the SOF interrupt, which occurs at a rate of 8000 per second.
  35535. + *
  35536. + * Return: void
  35537. + */
  35538. +void notrace dwc_otg_fiq_nop(struct fiq_state *state)
  35539. +{
  35540. + gintsts_data_t gintsts, gintsts_handled;
  35541. + gintmsk_data_t gintmsk;
  35542. + hfnum_data_t hfnum;
  35543. +
  35544. + fiq_fsm_spin_lock(&state->lock);
  35545. + hfnum.d32 = FIQ_READ(state->dwc_regs_base + HFNUM);
  35546. + gintsts.d32 = FIQ_READ(state->dwc_regs_base + GINTSTS);
  35547. + gintmsk.d32 = FIQ_READ(state->dwc_regs_base + GINTMSK);
  35548. + gintsts.d32 &= gintmsk.d32;
  35549. + gintsts_handled.d32 = 0;
  35550. +
  35551. + if (gintsts.b.sofintr) {
  35552. + if (!state->kick_np_queues &&
  35553. + dwc_frame_num_gt(state->next_sched_frame, hfnum.b.frnum)) {
  35554. + /* SOF handled, no work to do, just ACK interrupt */
  35555. + gintsts_handled.b.sofintr = 1;
  35556. + } else {
  35557. + /* Kick IRQ */
  35558. + state->gintmsk_saved.b.sofintr = 0;
  35559. + }
  35560. + }
  35561. +
  35562. + /* Reset handled interrupts */
  35563. + if(gintsts_handled.d32) {
  35564. + FIQ_WRITE(state->dwc_regs_base + GINTSTS, gintsts_handled.d32);
  35565. + }
  35566. +
  35567. + /* Clear the bits in the saved register that were not handled but were triggered. */
  35568. + state->gintmsk_saved.d32 &= ~(gintsts.d32 & ~gintsts_handled.d32);
  35569. +
  35570. + /* We got an interrupt, didn't handle it and want to mask it */
  35571. + if (~(state->gintmsk_saved.d32)) {
  35572. + state->mphi_int_count++;
  35573. + gintmsk.d32 &= state->gintmsk_saved.d32;
  35574. + FIQ_WRITE(state->dwc_regs_base + GINTMSK, gintmsk.d32);
  35575. + /* Force a clear before another dummy send */
  35576. + FIQ_WRITE(state->mphi_regs.intstat, (1<<29));
  35577. + FIQ_WRITE(state->mphi_regs.outdda, (int) state->dummy_send);
  35578. + FIQ_WRITE(state->mphi_regs.outddb, (1<<29));
  35579. +
  35580. + }
  35581. + state->fiq_done++;
  35582. + mb();
  35583. + fiq_fsm_spin_unlock(&state->lock);
  35584. +}
  35585. --- /dev/null
  35586. +++ b/drivers/usb/host/dwc_otg/dwc_otg_fiq_fsm.h
  35587. @@ -0,0 +1,370 @@
  35588. +/*
  35589. + * dwc_otg_fiq_fsm.h - Finite state machine FIQ header definitions
  35590. + *
  35591. + * Copyright (c) 2013 Raspberry Pi Foundation
  35592. + *
  35593. + * Author: Jonathan Bell <[email protected]>
  35594. + * All rights reserved.
  35595. + *
  35596. + * Redistribution and use in source and binary forms, with or without
  35597. + * modification, are permitted provided that the following conditions are met:
  35598. + * * Redistributions of source code must retain the above copyright
  35599. + * notice, this list of conditions and the following disclaimer.
  35600. + * * Redistributions in binary form must reproduce the above copyright
  35601. + * notice, this list of conditions and the following disclaimer in the
  35602. + * documentation and/or other materials provided with the distribution.
  35603. + * * Neither the name of Raspberry Pi nor the
  35604. + * names of its contributors may be used to endorse or promote products
  35605. + * derived from this software without specific prior written permission.
  35606. + *
  35607. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  35608. + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  35609. + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  35610. + * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  35611. + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  35612. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  35613. + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  35614. + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  35615. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  35616. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35617. + *
  35618. + * This FIQ implements functionality that performs split transactions on
  35619. + * the dwc_otg hardware without any outside intervention. A split transaction
  35620. + * is "queued" by nominating a specific host channel to perform the entirety
  35621. + * of a split transaction. This FIQ will then perform the microframe-precise
  35622. + * scheduling required in each phase of the transaction until completion.
  35623. + *
  35624. + * The FIQ functionality has been surgically implanted into the Synopsys
  35625. + * vendor-provided driver.
  35626. + *
  35627. + */
  35628. +
  35629. +#ifndef DWC_OTG_FIQ_FSM_H_
  35630. +#define DWC_OTG_FIQ_FSM_H_
  35631. +
  35632. +#include "dwc_otg_regs.h"
  35633. +#include "dwc_otg_cil.h"
  35634. +#include "dwc_otg_hcd.h"
  35635. +#include <linux/kernel.h>
  35636. +#include <linux/irqflags.h>
  35637. +#include <linux/string.h>
  35638. +#include <asm/barrier.h>
  35639. +
  35640. +#if 0
  35641. +#define FLAME_ON(x) \
  35642. +do { \
  35643. + int gpioreg; \
  35644. + \
  35645. + gpioreg = readl(__io_address(0x20200000+0x8)); \
  35646. + gpioreg &= ~(7 << (x-20)*3); \
  35647. + gpioreg |= 0x1 << (x-20)*3; \
  35648. + writel(gpioreg, __io_address(0x20200000+0x8)); \
  35649. + \
  35650. + writel(1<<x, __io_address(0x20200000+(0x1C))); \
  35651. +} while (0)
  35652. +
  35653. +#define FLAME_OFF(x) \
  35654. +do { \
  35655. + writel(1<<x, __io_address(0x20200000+(0x28))); \
  35656. +} while (0)
  35657. +#else
  35658. +#define FLAME_ON(x) do { } while (0)
  35659. +#define FLAME_OFF(X) do { } while (0)
  35660. +#endif
  35661. +
  35662. +/* This is a quick-and-dirty arch-specific register read/write. We know that
  35663. + * writes to a peripheral on BCM2835 will always arrive in-order, also that
  35664. + * reads and writes are executed in-order therefore the need for memory barriers
  35665. + * is obviated if we're only talking to USB.
  35666. + */
  35667. +#define FIQ_WRITE(_addr_,_data_) (*(volatile unsigned int *) (_addr_) = (_data_))
  35668. +#define FIQ_READ(_addr_) (*(volatile unsigned int *) (_addr_))
  35669. +
  35670. +/* FIQ-ified register definitions. Offsets are from dwc_regs_base. */
  35671. +#define GINTSTS 0x014
  35672. +#define GINTMSK 0x018
  35673. +/* Debug register. Poll the top of the received packets FIFO. */
  35674. +#define GRXSTSR 0x01C
  35675. +#define HFNUM 0x408
  35676. +#define HAINT 0x414
  35677. +#define HAINTMSK 0x418
  35678. +#define HPRT0 0x440
  35679. +
  35680. +/* HC_regs start from an offset of 0x500 */
  35681. +#define HC_START 0x500
  35682. +#define HC_OFFSET 0x020
  35683. +
  35684. +#define HC_DMA 0x514
  35685. +
  35686. +#define HCCHAR 0x00
  35687. +#define HCSPLT 0x04
  35688. +#define HCINT 0x08
  35689. +#define HCINTMSK 0x0C
  35690. +#define HCTSIZ 0x10
  35691. +
  35692. +#define ISOC_XACTPOS_ALL 0b11
  35693. +#define ISOC_XACTPOS_BEGIN 0b10
  35694. +#define ISOC_XACTPOS_MID 0b00
  35695. +#define ISOC_XACTPOS_END 0b01
  35696. +
  35697. +#define DWC_PID_DATA2 0b01
  35698. +#define DWC_PID_MDATA 0b11
  35699. +#define DWC_PID_DATA1 0b10
  35700. +#define DWC_PID_DATA0 0b00
  35701. +
  35702. +typedef struct {
  35703. + volatile void* base;
  35704. + volatile void* ctrl;
  35705. + volatile void* outdda;
  35706. + volatile void* outddb;
  35707. + volatile void* intstat;
  35708. +} mphi_regs_t;
  35709. +
  35710. +enum fiq_debug_level {
  35711. + FIQDBG_SCHED = (1 << 0),
  35712. + FIQDBG_INT = (1 << 1),
  35713. + FIQDBG_ERR = (1 << 2),
  35714. + FIQDBG_PORTHUB = (1 << 3),
  35715. +};
  35716. +
  35717. +typedef struct {
  35718. + union {
  35719. + uint32_t slock;
  35720. + struct _tickets {
  35721. + uint16_t owner;
  35722. + uint16_t next;
  35723. + } tickets;
  35724. + };
  35725. +} fiq_lock_t;
  35726. +
  35727. +struct fiq_state;
  35728. +
  35729. +extern void _fiq_print (enum fiq_debug_level dbg_lvl, volatile struct fiq_state *state, char *fmt, ...);
  35730. +#if 0
  35731. +#define fiq_print _fiq_print
  35732. +#else
  35733. +#define fiq_print(x, y, ...)
  35734. +#endif
  35735. +
  35736. +extern bool fiq_enable, fiq_fsm_enable;
  35737. +extern ushort nak_holdoff;
  35738. +
  35739. +/**
  35740. + * enum fiq_fsm_state - The FIQ FSM states.
  35741. + *
  35742. + * This is the "core" of the FIQ FSM. Broadly, the FSM states follow the
  35743. + * USB2.0 specification for host responses to various transaction states.
  35744. + * There are modifications to this host state machine because of a variety of
  35745. + * quirks and limitations in the dwc_otg hardware.
  35746. + *
  35747. + * The fsm state is also used to communicate back to the driver on completion of
  35748. + * a split transaction. The end states are used in conjunction with the interrupts
  35749. + * raised by the final transaction.
  35750. + */
  35751. +enum fiq_fsm_state {
  35752. + /* FIQ isn't enabled for this host channel */
  35753. + FIQ_PASSTHROUGH = 0,
  35754. + /* For the first interrupt received for this channel,
  35755. + * the FIQ has to ack any interrupts indicating success. */
  35756. + FIQ_PASSTHROUGH_ERRORSTATE = 31,
  35757. + /* Nonperiodic state groups */
  35758. + FIQ_NP_SSPLIT_STARTED = 1,
  35759. + FIQ_NP_SSPLIT_RETRY = 2,
  35760. + FIQ_NP_OUT_CSPLIT_RETRY = 3,
  35761. + FIQ_NP_IN_CSPLIT_RETRY = 4,
  35762. + FIQ_NP_SPLIT_DONE = 5,
  35763. + FIQ_NP_SPLIT_LS_ABORTED = 6,
  35764. + /* This differentiates a HS transaction error from a LS one
  35765. + * (handling the hub state is different) */
  35766. + FIQ_NP_SPLIT_HS_ABORTED = 7,
  35767. +
  35768. + /* Periodic state groups */
  35769. + /* Periodic transactions are either started directly by the IRQ handler
  35770. + * or deferred if the TT is already in use.
  35771. + */
  35772. + FIQ_PER_SSPLIT_QUEUED = 8,
  35773. + FIQ_PER_SSPLIT_STARTED = 9,
  35774. + FIQ_PER_SSPLIT_LAST = 10,
  35775. +
  35776. +
  35777. + FIQ_PER_ISO_OUT_PENDING = 11,
  35778. + FIQ_PER_ISO_OUT_ACTIVE = 12,
  35779. + FIQ_PER_ISO_OUT_LAST = 13,
  35780. + FIQ_PER_ISO_OUT_DONE = 27,
  35781. +
  35782. + FIQ_PER_CSPLIT_WAIT = 14,
  35783. + FIQ_PER_CSPLIT_NYET1 = 15,
  35784. + FIQ_PER_CSPLIT_BROKEN_NYET1 = 28,
  35785. + FIQ_PER_CSPLIT_NYET_FAFF = 29,
  35786. + /* For multiple CSPLITs (large isoc IN, or delayed interrupt) */
  35787. + FIQ_PER_CSPLIT_POLL = 16,
  35788. + /* The last CSPLIT for a transaction has been issued, differentiates
  35789. + * for the state machine to queue the next packet.
  35790. + */
  35791. + FIQ_PER_CSPLIT_LAST = 17,
  35792. +
  35793. + FIQ_PER_SPLIT_DONE = 18,
  35794. + FIQ_PER_SPLIT_LS_ABORTED = 19,
  35795. + FIQ_PER_SPLIT_HS_ABORTED = 20,
  35796. + FIQ_PER_SPLIT_NYET_ABORTED = 21,
  35797. + /* Frame rollover has occurred without the transaction finishing. */
  35798. + FIQ_PER_SPLIT_TIMEOUT = 22,
  35799. +
  35800. + /* FIQ-accelerated HS Isochronous state groups */
  35801. + FIQ_HS_ISOC_TURBO = 23,
  35802. + /* For interval > 1, SOF wakes up the isochronous FSM */
  35803. + FIQ_HS_ISOC_SLEEPING = 24,
  35804. + FIQ_HS_ISOC_DONE = 25,
  35805. + FIQ_HS_ISOC_ABORTED = 26,
  35806. + FIQ_DEQUEUE_ISSUED = 30,
  35807. + FIQ_TEST = 32,
  35808. +};
  35809. +
  35810. +struct fiq_stack {
  35811. + int magic1;
  35812. + uint8_t stack[2048];
  35813. + int magic2;
  35814. +};
  35815. +
  35816. +
  35817. +/**
  35818. + * struct fiq_dma_info - DMA bounce buffer utilisation information (per-channel)
  35819. + * @index: Number of slots reported used for IN transactions / number of slots
  35820. + * transmitted for an OUT transaction
  35821. + * @slot_len[6]: Number of actual transfer bytes in each slot (255 if unused)
  35822. + *
  35823. + * Split transaction transfers can have variable length depending on other bus
  35824. + * traffic. The OTG core DMA engine requires 4-byte aligned addresses therefore
  35825. + * each transaction needs a guaranteed aligned address. A maximum of 6 split transfers
  35826. + * can happen per-frame.
  35827. + */
  35828. +struct fiq_dma_info {
  35829. + u8 index;
  35830. + u8 slot_len[6];
  35831. +};
  35832. +
  35833. +struct __attribute__((packed)) fiq_split_dma_slot {
  35834. + u8 buf[188];
  35835. +};
  35836. +
  35837. +struct fiq_dma_channel {
  35838. + struct __attribute__((packed)) fiq_split_dma_slot index[6];
  35839. +};
  35840. +
  35841. +struct fiq_dma_blob {
  35842. + struct __attribute__((packed)) fiq_dma_channel channel[0];
  35843. +};
  35844. +
  35845. +/**
  35846. + * struct fiq_hs_isoc_info - USB2.0 isochronous data
  35847. + * @iso_frame: Pointer to the array of OTG URB iso_frame_descs.
  35848. + * @nrframes: Total length of iso_frame_desc array
  35849. + * @index: Current index (FIQ-maintained)
  35850. + * @stride: Interval in uframes between HS isoc transactions
  35851. + */
  35852. +struct fiq_hs_isoc_info {
  35853. + struct dwc_otg_hcd_iso_packet_desc *iso_desc;
  35854. + unsigned int nrframes;
  35855. + unsigned int index;
  35856. + unsigned int stride;
  35857. +};
  35858. +
  35859. +/**
  35860. + * struct fiq_channel_state - FIQ state machine storage
  35861. + * @fsm: Current state of the channel as understood by the FIQ
  35862. + * @nr_errors: Number of transaction errors on this split-transaction
  35863. + * @hub_addr: SSPLIT/CSPLIT destination hub
  35864. + * @port_addr: SSPLIT/CSPLIT destination port - always 1 if single TT hub
  35865. + * @nrpackets: For isoc OUT, the number of split-OUT packets to transmit. For
  35866. + * split-IN, number of CSPLIT data packets that were received.
  35867. + * @hcchar_copy:
  35868. + * @hcsplt_copy:
  35869. + * @hcintmsk_copy:
  35870. + * @hctsiz_copy: Copies of the host channel registers.
  35871. + * For use as scratch, or for returning state.
  35872. + *
  35873. + * The fiq_channel_state is state storage between interrupts for a host channel. The
  35874. + * FSM state is stored here. Members of this structure must only be set up by the
  35875. + * driver prior to enabling the FIQ for this host channel, and not touched until the FIQ
  35876. + * has updated the state to either a COMPLETE state group or ABORT state group.
  35877. + */
  35878. +
  35879. +struct fiq_channel_state {
  35880. + enum fiq_fsm_state fsm;
  35881. + unsigned int nr_errors;
  35882. + unsigned int hub_addr;
  35883. + unsigned int port_addr;
  35884. + /* Hardware bug workaround: sometimes channel halt interrupts are
  35885. + * delayed until the next SOF. Keep track of when we expected to get interrupted. */
  35886. + unsigned int expected_uframe;
  35887. + /* number of uframes remaining (for interval > 1 HS isoc transfers) before next transfer */
  35888. + unsigned int uframe_sleeps;
  35889. + /* in/out for communicating number of dma buffers used, or number of ISOC to do */
  35890. + unsigned int nrpackets;
  35891. + struct fiq_dma_info dma_info;
  35892. + struct fiq_hs_isoc_info hs_isoc_info;
  35893. + /* Copies of HC registers - in/out communication from/to IRQ handler
  35894. + * and for ease of channel setup. A bit of mungeing is performed - for
  35895. + * example the hctsiz.b.maxp is _always_ the max packet size of the endpoint.
  35896. + */
  35897. + hcchar_data_t hcchar_copy;
  35898. + hcsplt_data_t hcsplt_copy;
  35899. + hcint_data_t hcint_copy;
  35900. + hcintmsk_data_t hcintmsk_copy;
  35901. + hctsiz_data_t hctsiz_copy;
  35902. + hcdma_data_t hcdma_copy;
  35903. +};
  35904. +
  35905. +/**
  35906. + * struct fiq_state - top-level FIQ state machine storage
  35907. + * @mphi_regs: virtual address of the MPHI peripheral register file
  35908. + * @dwc_regs_base: virtual address of the base of the DWC core register file
  35909. + * @dma_base: physical address for the base of the DMA bounce buffers
  35910. + * @dummy_send: Scratch area for sending a fake message to the MPHI peripheral
  35911. + * @gintmsk_saved: Top-level mask of interrupts that the FIQ has not handled.
  35912. + * Used for determining which interrupts fired to set off the IRQ handler.
  35913. + * @haintmsk_saved: Mask of interrupts from host channels that the FIQ did not handle internally.
  35914. + * @np_count: Non-periodic transactions in the active queue
  35915. + * @np_sent: Count of non-periodic transactions that have completed
  35916. + * @next_sched_frame: For periodic transactions handled by the driver's SOF-driven queuing mechanism,
  35917. + * this is the next frame on which a SOF interrupt is required. Used to hold off
  35918. + * passing SOF through to the driver until necessary.
  35919. + * @channel[n]: Per-channel FIQ state. Allocated during init depending on the number of host
  35920. + * channels configured into the core logic.
  35921. + *
  35922. + * This is passed as the first argument to the dwc_otg_fiq_fsm top-level FIQ handler from the asm stub.
  35923. + * It contains top-level state information.
  35924. + */
  35925. +struct fiq_state {
  35926. + fiq_lock_t lock;
  35927. + mphi_regs_t mphi_regs;
  35928. + void *dwc_regs_base;
  35929. + dma_addr_t dma_base;
  35930. + struct fiq_dma_blob *fiq_dmab;
  35931. + void *dummy_send;
  35932. + gintmsk_data_t gintmsk_saved;
  35933. + haintmsk_data_t haintmsk_saved;
  35934. + int mphi_int_count;
  35935. + unsigned int fiq_done;
  35936. + unsigned int kick_np_queues;
  35937. + unsigned int next_sched_frame;
  35938. +#ifdef FIQ_DEBUG
  35939. + char * buffer;
  35940. + unsigned int bufsiz;
  35941. +#endif
  35942. + struct fiq_channel_state channel[0];
  35943. +};
  35944. +
  35945. +extern void fiq_fsm_spin_lock(fiq_lock_t *lock);
  35946. +
  35947. +extern void fiq_fsm_spin_unlock(fiq_lock_t *lock);
  35948. +
  35949. +extern int fiq_fsm_too_late(struct fiq_state *st, int n);
  35950. +
  35951. +extern int fiq_fsm_tt_in_use(struct fiq_state *st, int num_channels, int n);
  35952. +
  35953. +extern void dwc_otg_fiq_fsm(struct fiq_state *state, int num_channels);
  35954. +
  35955. +extern void dwc_otg_fiq_nop(struct fiq_state *state);
  35956. +
  35957. +#endif /* DWC_OTG_FIQ_FSM_H_ */
  35958. --- /dev/null
  35959. +++ b/drivers/usb/host/dwc_otg/dwc_otg_fiq_stub.S
  35960. @@ -0,0 +1,80 @@
  35961. +/*
  35962. + * dwc_otg_fiq_fsm.S - assembly stub for the FSM FIQ
  35963. + *
  35964. + * Copyright (c) 2013 Raspberry Pi Foundation
  35965. + *
  35966. + * Author: Jonathan Bell <[email protected]>
  35967. + * All rights reserved.
  35968. + *
  35969. + * Redistribution and use in source and binary forms, with or without
  35970. + * modification, are permitted provided that the following conditions are met:
  35971. + * * Redistributions of source code must retain the above copyright
  35972. + * notice, this list of conditions and the following disclaimer.
  35973. + * * Redistributions in binary form must reproduce the above copyright
  35974. + * notice, this list of conditions and the following disclaimer in the
  35975. + * documentation and/or other materials provided with the distribution.
  35976. + * * Neither the name of Raspberry Pi nor the
  35977. + * names of its contributors may be used to endorse or promote products
  35978. + * derived from this software without specific prior written permission.
  35979. + *
  35980. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  35981. + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  35982. + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  35983. + * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  35984. + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  35985. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  35986. + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  35987. + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  35988. + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  35989. + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35990. + */
  35991. +
  35992. +
  35993. +#include <asm/assembler.h>
  35994. +#include <linux/linkage.h>
  35995. +
  35996. +
  35997. +.text
  35998. +
  35999. +.global _dwc_otg_fiq_stub_end;
  36000. +
  36001. +/**
  36002. + * _dwc_otg_fiq_stub() - entry copied to the FIQ vector page to allow
  36003. + * a C-style function call with arguments from the FIQ banked registers.
  36004. + * r0 = &hcd->fiq_state
  36005. + * r1 = &hcd->num_channels
  36006. + * r2 = &hcd->dma_buffers
  36007. + * Tramples: r0, r1, r2, r4, fp, ip
  36008. + */
  36009. +
  36010. +ENTRY(_dwc_otg_fiq_stub)
  36011. + /* Stash unbanked regs - SP will have been set up for us */
  36012. + mov ip, sp;
  36013. + stmdb sp!, {r0-r12, lr};
  36014. +#ifdef FIQ_DEBUG
  36015. + // Cycle profiling - read cycle counter at start
  36016. + mrc p15, 0, r5, c15, c12, 1;
  36017. +#endif
  36018. + /* r11 = fp, don't trample it */
  36019. + mov r4, fp;
  36020. + /* set EABI frame size */
  36021. + sub fp, ip, #512;
  36022. +
  36023. + /* for fiq NOP mode - just need state */
  36024. + mov r0, r8;
  36025. + /* r9 = num_channels */
  36026. + mov r1, r9;
  36027. + /* r10 = struct *dma_bufs */
  36028. +// mov r2, r10;
  36029. +
  36030. + /* r4 = &fiq_c_function */
  36031. + blx r4;
  36032. +#ifdef FIQ_DEBUG
  36033. + mrc p15, 0, r4, c15, c12, 1;
  36034. + subs r5, r5, r4;
  36035. + // r5 is now the cycle count time for executing the FIQ. Store it somewhere?
  36036. +#endif
  36037. + ldmia sp!, {r0-r12, lr};
  36038. + subs pc, lr, #4;
  36039. +_dwc_otg_fiq_stub_end:
  36040. +END(_dwc_otg_fiq_stub)
  36041. --- /dev/null
  36042. +++ b/drivers/usb/host/dwc_otg/dwc_otg_hcd.c
  36043. @@ -0,0 +1,4260 @@
  36044. +
  36045. +/* ==========================================================================
  36046. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd.c $
  36047. + * $Revision: #104 $
  36048. + * $Date: 2011/10/24 $
  36049. + * $Change: 1871159 $
  36050. + *
  36051. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  36052. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  36053. + * otherwise expressly agreed to in writing between Synopsys and you.
  36054. + *
  36055. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  36056. + * any End User Software License Agreement or Agreement for Licensed Product
  36057. + * with Synopsys or any supplement thereto. You are permitted to use and
  36058. + * redistribute this Software in source and binary forms, with or without
  36059. + * modification, provided that redistributions of source code must retain this
  36060. + * notice. You may not view, use, disclose, copy or distribute this file or
  36061. + * any information contained herein except pursuant to this license grant from
  36062. + * Synopsys. If you do not agree with this notice, including the disclaimer
  36063. + * below, then you are not authorized to use the Software.
  36064. + *
  36065. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  36066. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  36067. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  36068. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  36069. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  36070. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  36071. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  36072. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36073. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  36074. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  36075. + * DAMAGE.
  36076. + * ========================================================================== */
  36077. +#ifndef DWC_DEVICE_ONLY
  36078. +
  36079. +/** @file
  36080. + * This file implements HCD Core. All code in this file is portable and doesn't
  36081. + * use any OS specific functions.
  36082. + * Interface provided by HCD Core is defined in <code><hcd_if.h></code>
  36083. + * header file.
  36084. + */
  36085. +
  36086. +#include <linux/usb.h>
  36087. +#include <linux/usb/hcd.h>
  36088. +
  36089. +#include "dwc_otg_hcd.h"
  36090. +#include "dwc_otg_regs.h"
  36091. +#include "dwc_otg_fiq_fsm.h"
  36092. +
  36093. +extern bool microframe_schedule;
  36094. +extern uint16_t fiq_fsm_mask, nak_holdoff;
  36095. +
  36096. +//#define DEBUG_HOST_CHANNELS
  36097. +#ifdef DEBUG_HOST_CHANNELS
  36098. +static int last_sel_trans_num_per_scheduled = 0;
  36099. +static int last_sel_trans_num_nonper_scheduled = 0;
  36100. +static int last_sel_trans_num_avail_hc_at_start = 0;
  36101. +static int last_sel_trans_num_avail_hc_at_end = 0;
  36102. +#endif /* DEBUG_HOST_CHANNELS */
  36103. +
  36104. +
  36105. +dwc_otg_hcd_t *dwc_otg_hcd_alloc_hcd(void)
  36106. +{
  36107. + return DWC_ALLOC(sizeof(dwc_otg_hcd_t));
  36108. +}
  36109. +
  36110. +/**
  36111. + * Connection timeout function. An OTG host is required to display a
  36112. + * message if the device does not connect within 10 seconds.
  36113. + */
  36114. +void dwc_otg_hcd_connect_timeout(void *ptr)
  36115. +{
  36116. + DWC_DEBUGPL(DBG_HCDV, "%s(%p)\n", __func__, ptr);
  36117. + DWC_PRINTF("Connect Timeout\n");
  36118. + __DWC_ERROR("Device Not Connected/Responding\n");
  36119. +}
  36120. +
  36121. +#if defined(DEBUG)
  36122. +static void dump_channel_info(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  36123. +{
  36124. + if (qh->channel != NULL) {
  36125. + dwc_hc_t *hc = qh->channel;
  36126. + dwc_list_link_t *item;
  36127. + dwc_otg_qh_t *qh_item;
  36128. + int num_channels = hcd->core_if->core_params->host_channels;
  36129. + int i;
  36130. +
  36131. + dwc_otg_hc_regs_t *hc_regs;
  36132. + hcchar_data_t hcchar;
  36133. + hcsplt_data_t hcsplt;
  36134. + hctsiz_data_t hctsiz;
  36135. + uint32_t hcdma;
  36136. +
  36137. + hc_regs = hcd->core_if->host_if->hc_regs[hc->hc_num];
  36138. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  36139. + hcsplt.d32 = DWC_READ_REG32(&hc_regs->hcsplt);
  36140. + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
  36141. + hcdma = DWC_READ_REG32(&hc_regs->hcdma);
  36142. +
  36143. + DWC_PRINTF(" Assigned to channel %p:\n", hc);
  36144. + DWC_PRINTF(" hcchar 0x%08x, hcsplt 0x%08x\n", hcchar.d32,
  36145. + hcsplt.d32);
  36146. + DWC_PRINTF(" hctsiz 0x%08x, hcdma 0x%08x\n", hctsiz.d32,
  36147. + hcdma);
  36148. + DWC_PRINTF(" dev_addr: %d, ep_num: %d, ep_is_in: %d\n",
  36149. + hc->dev_addr, hc->ep_num, hc->ep_is_in);
  36150. + DWC_PRINTF(" ep_type: %d\n", hc->ep_type);
  36151. + DWC_PRINTF(" max_packet: %d\n", hc->max_packet);
  36152. + DWC_PRINTF(" data_pid_start: %d\n", hc->data_pid_start);
  36153. + DWC_PRINTF(" xfer_started: %d\n", hc->xfer_started);
  36154. + DWC_PRINTF(" halt_status: %d\n", hc->halt_status);
  36155. + DWC_PRINTF(" xfer_buff: %p\n", hc->xfer_buff);
  36156. + DWC_PRINTF(" xfer_len: %d\n", hc->xfer_len);
  36157. + DWC_PRINTF(" qh: %p\n", hc->qh);
  36158. + DWC_PRINTF(" NP inactive sched:\n");
  36159. + DWC_LIST_FOREACH(item, &hcd->non_periodic_sched_inactive) {
  36160. + qh_item =
  36161. + DWC_LIST_ENTRY(item, dwc_otg_qh_t, qh_list_entry);
  36162. + DWC_PRINTF(" %p\n", qh_item);
  36163. + }
  36164. + DWC_PRINTF(" NP active sched:\n");
  36165. + DWC_LIST_FOREACH(item, &hcd->non_periodic_sched_active) {
  36166. + qh_item =
  36167. + DWC_LIST_ENTRY(item, dwc_otg_qh_t, qh_list_entry);
  36168. + DWC_PRINTF(" %p\n", qh_item);
  36169. + }
  36170. + DWC_PRINTF(" Channels: \n");
  36171. + for (i = 0; i < num_channels; i++) {
  36172. + dwc_hc_t *hc = hcd->hc_ptr_array[i];
  36173. + DWC_PRINTF(" %2d: %p\n", i, hc);
  36174. + }
  36175. + }
  36176. +}
  36177. +#else
  36178. +#define dump_channel_info(hcd, qh)
  36179. +#endif /* DEBUG */
  36180. +
  36181. +/**
  36182. + * Work queue function for starting the HCD when A-Cable is connected.
  36183. + * The hcd_start() must be called in a process context.
  36184. + */
  36185. +static void hcd_start_func(void *_vp)
  36186. +{
  36187. + dwc_otg_hcd_t *hcd = (dwc_otg_hcd_t *) _vp;
  36188. +
  36189. + DWC_DEBUGPL(DBG_HCDV, "%s() %p\n", __func__, hcd);
  36190. + if (hcd) {
  36191. + hcd->fops->start(hcd);
  36192. + }
  36193. +}
  36194. +
  36195. +static void del_xfer_timers(dwc_otg_hcd_t * hcd)
  36196. +{
  36197. +#ifdef DEBUG
  36198. + int i;
  36199. + int num_channels = hcd->core_if->core_params->host_channels;
  36200. + for (i = 0; i < num_channels; i++) {
  36201. + DWC_TIMER_CANCEL(hcd->core_if->hc_xfer_timer[i]);
  36202. + }
  36203. +#endif
  36204. +}
  36205. +
  36206. +static void del_timers(dwc_otg_hcd_t * hcd)
  36207. +{
  36208. + del_xfer_timers(hcd);
  36209. + DWC_TIMER_CANCEL(hcd->conn_timer);
  36210. +}
  36211. +
  36212. +/**
  36213. + * Processes all the URBs in a single list of QHs. Completes them with
  36214. + * -ESHUTDOWN and frees the QTD.
  36215. + */
  36216. +static void kill_urbs_in_qh_list(dwc_otg_hcd_t * hcd, dwc_list_link_t * qh_list)
  36217. +{
  36218. + dwc_list_link_t *qh_item, *qh_tmp;
  36219. + dwc_otg_qh_t *qh;
  36220. + dwc_otg_qtd_t *qtd, *qtd_tmp;
  36221. +
  36222. + DWC_LIST_FOREACH_SAFE(qh_item, qh_tmp, qh_list) {
  36223. + qh = DWC_LIST_ENTRY(qh_item, dwc_otg_qh_t, qh_list_entry);
  36224. + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp,
  36225. + &qh->qtd_list, qtd_list_entry) {
  36226. + qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
  36227. + if (qtd->urb != NULL) {
  36228. + hcd->fops->complete(hcd, qtd->urb->priv,
  36229. + qtd->urb, -DWC_E_SHUTDOWN);
  36230. + dwc_otg_hcd_qtd_remove_and_free(hcd, qtd, qh);
  36231. + }
  36232. +
  36233. + }
  36234. + if(qh->channel) {
  36235. + /* Using hcchar.chen == 1 is not a reliable test.
  36236. + * It is possible that the channel has already halted
  36237. + * but not yet been through the IRQ handler.
  36238. + */
  36239. + dwc_otg_hc_halt(hcd->core_if, qh->channel,
  36240. + DWC_OTG_HC_XFER_URB_DEQUEUE);
  36241. + if(microframe_schedule)
  36242. + hcd->available_host_channels++;
  36243. + qh->channel = NULL;
  36244. + }
  36245. + dwc_otg_hcd_qh_remove(hcd, qh);
  36246. + }
  36247. +}
  36248. +
  36249. +/**
  36250. + * Responds with an error status of ESHUTDOWN to all URBs in the non-periodic
  36251. + * and periodic schedules. The QTD associated with each URB is removed from
  36252. + * the schedule and freed. This function may be called when a disconnect is
  36253. + * detected or when the HCD is being stopped.
  36254. + */
  36255. +static void kill_all_urbs(dwc_otg_hcd_t * hcd)
  36256. +{
  36257. + kill_urbs_in_qh_list(hcd, &hcd->non_periodic_sched_inactive);
  36258. + kill_urbs_in_qh_list(hcd, &hcd->non_periodic_sched_active);
  36259. + kill_urbs_in_qh_list(hcd, &hcd->periodic_sched_inactive);
  36260. + kill_urbs_in_qh_list(hcd, &hcd->periodic_sched_ready);
  36261. + kill_urbs_in_qh_list(hcd, &hcd->periodic_sched_assigned);
  36262. + kill_urbs_in_qh_list(hcd, &hcd->periodic_sched_queued);
  36263. +}
  36264. +
  36265. +/**
  36266. + * Start the connection timer. An OTG host is required to display a
  36267. + * message if the device does not connect within 10 seconds. The
  36268. + * timer is deleted if a port connect interrupt occurs before the
  36269. + * timer expires.
  36270. + */
  36271. +static void dwc_otg_hcd_start_connect_timer(dwc_otg_hcd_t * hcd)
  36272. +{
  36273. + DWC_TIMER_SCHEDULE(hcd->conn_timer, 10000 /* 10 secs */ );
  36274. +}
  36275. +
  36276. +/**
  36277. + * HCD Callback function for disconnect of the HCD.
  36278. + *
  36279. + * @param p void pointer to the <code>struct usb_hcd</code>
  36280. + */
  36281. +static int32_t dwc_otg_hcd_session_start_cb(void *p)
  36282. +{
  36283. + dwc_otg_hcd_t *dwc_otg_hcd;
  36284. + DWC_DEBUGPL(DBG_HCDV, "%s(%p)\n", __func__, p);
  36285. + dwc_otg_hcd = p;
  36286. + dwc_otg_hcd_start_connect_timer(dwc_otg_hcd);
  36287. + return 1;
  36288. +}
  36289. +
  36290. +/**
  36291. + * HCD Callback function for starting the HCD when A-Cable is
  36292. + * connected.
  36293. + *
  36294. + * @param p void pointer to the <code>struct usb_hcd</code>
  36295. + */
  36296. +static int32_t dwc_otg_hcd_start_cb(void *p)
  36297. +{
  36298. + dwc_otg_hcd_t *dwc_otg_hcd = p;
  36299. + dwc_otg_core_if_t *core_if;
  36300. + hprt0_data_t hprt0;
  36301. +
  36302. + core_if = dwc_otg_hcd->core_if;
  36303. +
  36304. + if (core_if->op_state == B_HOST) {
  36305. + /*
  36306. + * Reset the port. During a HNP mode switch the reset
  36307. + * needs to occur within 1ms and have a duration of at
  36308. + * least 50ms.
  36309. + */
  36310. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  36311. + hprt0.b.prtrst = 1;
  36312. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  36313. + }
  36314. + DWC_WORKQ_SCHEDULE_DELAYED(core_if->wq_otg,
  36315. + hcd_start_func, dwc_otg_hcd, 50,
  36316. + "start hcd");
  36317. +
  36318. + return 1;
  36319. +}
  36320. +
  36321. +/**
  36322. + * HCD Callback function for disconnect of the HCD.
  36323. + *
  36324. + * @param p void pointer to the <code>struct usb_hcd</code>
  36325. + */
  36326. +static int32_t dwc_otg_hcd_disconnect_cb(void *p)
  36327. +{
  36328. + gintsts_data_t intr;
  36329. + dwc_otg_hcd_t *dwc_otg_hcd = p;
  36330. +
  36331. + /*
  36332. + * Set status flags for the hub driver.
  36333. + */
  36334. + dwc_otg_hcd->flags.b.port_connect_status_change = 1;
  36335. + dwc_otg_hcd->flags.b.port_connect_status = 0;
  36336. + if(fiq_enable)
  36337. + local_fiq_disable();
  36338. + /*
  36339. + * Shutdown any transfers in process by clearing the Tx FIFO Empty
  36340. + * interrupt mask and status bits and disabling subsequent host
  36341. + * channel interrupts.
  36342. + */
  36343. + intr.d32 = 0;
  36344. + intr.b.nptxfempty = 1;
  36345. + intr.b.ptxfempty = 1;
  36346. + intr.b.hcintr = 1;
  36347. + DWC_MODIFY_REG32(&dwc_otg_hcd->core_if->core_global_regs->gintmsk,
  36348. + intr.d32, 0);
  36349. + DWC_MODIFY_REG32(&dwc_otg_hcd->core_if->core_global_regs->gintsts,
  36350. + intr.d32, 0);
  36351. +
  36352. + del_timers(dwc_otg_hcd);
  36353. +
  36354. + /*
  36355. + * Turn off the vbus power only if the core has transitioned to device
  36356. + * mode. If still in host mode, need to keep power on to detect a
  36357. + * reconnection.
  36358. + */
  36359. + if (dwc_otg_is_device_mode(dwc_otg_hcd->core_if)) {
  36360. + if (dwc_otg_hcd->core_if->op_state != A_SUSPEND) {
  36361. + hprt0_data_t hprt0 = {.d32 = 0 };
  36362. + DWC_PRINTF("Disconnect: PortPower off\n");
  36363. + hprt0.b.prtpwr = 0;
  36364. + DWC_WRITE_REG32(dwc_otg_hcd->core_if->host_if->hprt0,
  36365. + hprt0.d32);
  36366. + }
  36367. +
  36368. + dwc_otg_disable_host_interrupts(dwc_otg_hcd->core_if);
  36369. + }
  36370. +
  36371. + /* Respond with an error status to all URBs in the schedule. */
  36372. + kill_all_urbs(dwc_otg_hcd);
  36373. +
  36374. + if (dwc_otg_is_host_mode(dwc_otg_hcd->core_if)) {
  36375. + /* Clean up any host channels that were in use. */
  36376. + int num_channels;
  36377. + int i;
  36378. + dwc_hc_t *channel;
  36379. + dwc_otg_hc_regs_t *hc_regs;
  36380. + hcchar_data_t hcchar;
  36381. +
  36382. + num_channels = dwc_otg_hcd->core_if->core_params->host_channels;
  36383. +
  36384. + if (!dwc_otg_hcd->core_if->dma_enable) {
  36385. + /* Flush out any channel requests in slave mode. */
  36386. + for (i = 0; i < num_channels; i++) {
  36387. + channel = dwc_otg_hcd->hc_ptr_array[i];
  36388. + if (DWC_CIRCLEQ_EMPTY_ENTRY
  36389. + (channel, hc_list_entry)) {
  36390. + hc_regs =
  36391. + dwc_otg_hcd->core_if->
  36392. + host_if->hc_regs[i];
  36393. + hcchar.d32 =
  36394. + DWC_READ_REG32(&hc_regs->hcchar);
  36395. + if (hcchar.b.chen) {
  36396. + hcchar.b.chen = 0;
  36397. + hcchar.b.chdis = 1;
  36398. + hcchar.b.epdir = 0;
  36399. + DWC_WRITE_REG32
  36400. + (&hc_regs->hcchar,
  36401. + hcchar.d32);
  36402. + }
  36403. + }
  36404. + }
  36405. + }
  36406. +
  36407. + for (i = 0; i < num_channels; i++) {
  36408. + channel = dwc_otg_hcd->hc_ptr_array[i];
  36409. + if (DWC_CIRCLEQ_EMPTY_ENTRY(channel, hc_list_entry)) {
  36410. + hc_regs =
  36411. + dwc_otg_hcd->core_if->host_if->hc_regs[i];
  36412. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  36413. + if (hcchar.b.chen) {
  36414. + /* Halt the channel. */
  36415. + hcchar.b.chdis = 1;
  36416. + DWC_WRITE_REG32(&hc_regs->hcchar,
  36417. + hcchar.d32);
  36418. + }
  36419. +
  36420. + dwc_otg_hc_cleanup(dwc_otg_hcd->core_if,
  36421. + channel);
  36422. + DWC_CIRCLEQ_INSERT_TAIL
  36423. + (&dwc_otg_hcd->free_hc_list, channel,
  36424. + hc_list_entry);
  36425. + /*
  36426. + * Added for Descriptor DMA to prevent channel double cleanup
  36427. + * in release_channel_ddma(). Which called from ep_disable
  36428. + * when device disconnect.
  36429. + */
  36430. + channel->qh = NULL;
  36431. + }
  36432. + }
  36433. + if(fiq_fsm_enable) {
  36434. + for(i=0; i < 128; i++) {
  36435. + dwc_otg_hcd->hub_port[i] = 0;
  36436. + }
  36437. + }
  36438. +
  36439. + }
  36440. +
  36441. + if(fiq_enable)
  36442. + local_fiq_enable();
  36443. +
  36444. + if (dwc_otg_hcd->fops->disconnect) {
  36445. + dwc_otg_hcd->fops->disconnect(dwc_otg_hcd);
  36446. + }
  36447. +
  36448. + return 1;
  36449. +}
  36450. +
  36451. +/**
  36452. + * HCD Callback function for stopping the HCD.
  36453. + *
  36454. + * @param p void pointer to the <code>struct usb_hcd</code>
  36455. + */
  36456. +static int32_t dwc_otg_hcd_stop_cb(void *p)
  36457. +{
  36458. + dwc_otg_hcd_t *dwc_otg_hcd = p;
  36459. +
  36460. + DWC_DEBUGPL(DBG_HCDV, "%s(%p)\n", __func__, p);
  36461. + dwc_otg_hcd_stop(dwc_otg_hcd);
  36462. + return 1;
  36463. +}
  36464. +
  36465. +#ifdef CONFIG_USB_DWC_OTG_LPM
  36466. +/**
  36467. + * HCD Callback function for sleep of HCD.
  36468. + *
  36469. + * @param p void pointer to the <code>struct usb_hcd</code>
  36470. + */
  36471. +static int dwc_otg_hcd_sleep_cb(void *p)
  36472. +{
  36473. + dwc_otg_hcd_t *hcd = p;
  36474. +
  36475. + dwc_otg_hcd_free_hc_from_lpm(hcd);
  36476. +
  36477. + return 0;
  36478. +}
  36479. +#endif
  36480. +
  36481. +
  36482. +/**
  36483. + * HCD Callback function for Remote Wakeup.
  36484. + *
  36485. + * @param p void pointer to the <code>struct usb_hcd</code>
  36486. + */
  36487. +static int dwc_otg_hcd_rem_wakeup_cb(void *p)
  36488. +{
  36489. + dwc_otg_hcd_t *hcd = p;
  36490. +
  36491. + if (hcd->core_if->lx_state == DWC_OTG_L2) {
  36492. + hcd->flags.b.port_suspend_change = 1;
  36493. + }
  36494. +#ifdef CONFIG_USB_DWC_OTG_LPM
  36495. + else {
  36496. + hcd->flags.b.port_l1_change = 1;
  36497. + }
  36498. +#endif
  36499. + return 0;
  36500. +}
  36501. +
  36502. +/**
  36503. + * Halts the DWC_otg host mode operations in a clean manner. USB transfers are
  36504. + * stopped.
  36505. + */
  36506. +void dwc_otg_hcd_stop(dwc_otg_hcd_t * hcd)
  36507. +{
  36508. + hprt0_data_t hprt0 = {.d32 = 0 };
  36509. +
  36510. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD STOP\n");
  36511. +
  36512. + /*
  36513. + * The root hub should be disconnected before this function is called.
  36514. + * The disconnect will clear the QTD lists (via ..._hcd_urb_dequeue)
  36515. + * and the QH lists (via ..._hcd_endpoint_disable).
  36516. + */
  36517. +
  36518. + /* Turn off all host-specific interrupts. */
  36519. + dwc_otg_disable_host_interrupts(hcd->core_if);
  36520. +
  36521. + /* Turn off the vbus power */
  36522. + DWC_PRINTF("PortPower off\n");
  36523. + hprt0.b.prtpwr = 0;
  36524. + DWC_WRITE_REG32(hcd->core_if->host_if->hprt0, hprt0.d32);
  36525. + dwc_mdelay(1);
  36526. +}
  36527. +
  36528. +int dwc_otg_hcd_urb_enqueue(dwc_otg_hcd_t * hcd,
  36529. + dwc_otg_hcd_urb_t * dwc_otg_urb, void **ep_handle,
  36530. + int atomic_alloc)
  36531. +{
  36532. + int retval = 0;
  36533. + uint8_t needs_scheduling = 0;
  36534. + dwc_otg_transaction_type_e tr_type;
  36535. + dwc_otg_qtd_t *qtd;
  36536. + gintmsk_data_t intr_mask = {.d32 = 0 };
  36537. + hprt0_data_t hprt0 = { .d32 = 0 };
  36538. +
  36539. +#ifdef DEBUG /* integrity checks (Broadcom) */
  36540. + if (NULL == hcd->core_if) {
  36541. + DWC_ERROR("**** DWC OTG HCD URB Enqueue - HCD has NULL core_if\n");
  36542. + /* No longer connected. */
  36543. + return -DWC_E_INVALID;
  36544. + }
  36545. +#endif
  36546. + if (!hcd->flags.b.port_connect_status) {
  36547. + /* No longer connected. */
  36548. + DWC_ERROR("Not connected\n");
  36549. + return -DWC_E_NO_DEVICE;
  36550. + }
  36551. +
  36552. + /* Some core configurations cannot support LS traffic on a FS root port */
  36553. + if ((hcd->fops->speed(hcd, dwc_otg_urb->priv) == USB_SPEED_LOW) &&
  36554. + (hcd->core_if->hwcfg2.b.fs_phy_type == 1) &&
  36555. + (hcd->core_if->hwcfg2.b.hs_phy_type == 1)) {
  36556. + hprt0.d32 = DWC_READ_REG32(hcd->core_if->host_if->hprt0);
  36557. + if (hprt0.b.prtspd == DWC_HPRT0_PRTSPD_FULL_SPEED) {
  36558. + return -DWC_E_NO_DEVICE;
  36559. + }
  36560. + }
  36561. +
  36562. + qtd = dwc_otg_hcd_qtd_create(dwc_otg_urb, atomic_alloc);
  36563. + if (qtd == NULL) {
  36564. + DWC_ERROR("DWC OTG HCD URB Enqueue failed creating QTD\n");
  36565. + return -DWC_E_NO_MEMORY;
  36566. + }
  36567. +#ifdef DEBUG /* integrity checks (Broadcom) */
  36568. + if (qtd->urb == NULL) {
  36569. + DWC_ERROR("**** DWC OTG HCD URB Enqueue created QTD with no URBs\n");
  36570. + return -DWC_E_NO_MEMORY;
  36571. + }
  36572. + if (qtd->urb->priv == NULL) {
  36573. + DWC_ERROR("**** DWC OTG HCD URB Enqueue created QTD URB with no URB handle\n");
  36574. + return -DWC_E_NO_MEMORY;
  36575. + }
  36576. +#endif
  36577. + intr_mask.d32 = DWC_READ_REG32(&hcd->core_if->core_global_regs->gintmsk);
  36578. + if(!intr_mask.b.sofintr || fiq_enable) needs_scheduling = 1;
  36579. + if((((dwc_otg_qh_t *)ep_handle)->ep_type == UE_BULK) && !(qtd->urb->flags & URB_GIVEBACK_ASAP))
  36580. + /* Do not schedule SG transactions until qtd has URB_GIVEBACK_ASAP set */
  36581. + needs_scheduling = 0;
  36582. +
  36583. + retval = dwc_otg_hcd_qtd_add(qtd, hcd, (dwc_otg_qh_t **) ep_handle, atomic_alloc);
  36584. + // creates a new queue in ep_handle if it doesn't exist already
  36585. + if (retval < 0) {
  36586. + DWC_ERROR("DWC OTG HCD URB Enqueue failed adding QTD. "
  36587. + "Error status %d\n", retval);
  36588. + dwc_otg_hcd_qtd_free(qtd);
  36589. + return retval;
  36590. + }
  36591. +
  36592. + if(needs_scheduling) {
  36593. + tr_type = dwc_otg_hcd_select_transactions(hcd);
  36594. + if (tr_type != DWC_OTG_TRANSACTION_NONE) {
  36595. + dwc_otg_hcd_queue_transactions(hcd, tr_type);
  36596. + }
  36597. + }
  36598. + return retval;
  36599. +}
  36600. +
  36601. +int dwc_otg_hcd_urb_dequeue(dwc_otg_hcd_t * hcd,
  36602. + dwc_otg_hcd_urb_t * dwc_otg_urb)
  36603. +{
  36604. + dwc_otg_qh_t *qh;
  36605. + dwc_otg_qtd_t *urb_qtd;
  36606. + BUG_ON(!hcd);
  36607. + BUG_ON(!dwc_otg_urb);
  36608. +
  36609. +#ifdef DEBUG /* integrity checks (Broadcom) */
  36610. +
  36611. + if (hcd == NULL) {
  36612. + DWC_ERROR("**** DWC OTG HCD URB Dequeue has NULL HCD\n");
  36613. + return -DWC_E_INVALID;
  36614. + }
  36615. + if (dwc_otg_urb == NULL) {
  36616. + DWC_ERROR("**** DWC OTG HCD URB Dequeue has NULL URB\n");
  36617. + return -DWC_E_INVALID;
  36618. + }
  36619. + if (dwc_otg_urb->qtd == NULL) {
  36620. + DWC_ERROR("**** DWC OTG HCD URB Dequeue with NULL QTD\n");
  36621. + return -DWC_E_INVALID;
  36622. + }
  36623. + urb_qtd = dwc_otg_urb->qtd;
  36624. + BUG_ON(!urb_qtd);
  36625. + if (urb_qtd->qh == NULL) {
  36626. + DWC_ERROR("**** DWC OTG HCD URB Dequeue with QTD with NULL Q handler\n");
  36627. + return -DWC_E_INVALID;
  36628. + }
  36629. +#else
  36630. + urb_qtd = dwc_otg_urb->qtd;
  36631. + BUG_ON(!urb_qtd);
  36632. +#endif
  36633. + qh = urb_qtd->qh;
  36634. + BUG_ON(!qh);
  36635. + if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
  36636. + if (urb_qtd->in_process) {
  36637. + dump_channel_info(hcd, qh);
  36638. + }
  36639. + }
  36640. +#ifdef DEBUG /* integrity checks (Broadcom) */
  36641. + if (hcd->core_if == NULL) {
  36642. + DWC_ERROR("**** DWC OTG HCD URB Dequeue HCD has NULL core_if\n");
  36643. + return -DWC_E_INVALID;
  36644. + }
  36645. +#endif
  36646. + if (urb_qtd->in_process && qh->channel) {
  36647. + /* The QTD is in process (it has been assigned to a channel). */
  36648. + if (hcd->flags.b.port_connect_status) {
  36649. + int n = qh->channel->hc_num;
  36650. + /*
  36651. + * If still connected (i.e. in host mode), halt the
  36652. + * channel so it can be used for other transfers. If
  36653. + * no longer connected, the host registers can't be
  36654. + * written to halt the channel since the core is in
  36655. + * device mode.
  36656. + */
  36657. + /* In FIQ FSM mode, we need to shut down carefully.
  36658. + * The FIQ may attempt to restart a disabled channel */
  36659. + if (fiq_fsm_enable && (hcd->fiq_state->channel[n].fsm != FIQ_PASSTHROUGH)) {
  36660. + qh->channel->halt_status = DWC_OTG_HC_XFER_URB_DEQUEUE;
  36661. + qh->channel->halt_pending = 1;
  36662. + hcd->fiq_state->channel[n].fsm = FIQ_DEQUEUE_ISSUED;
  36663. + } else {
  36664. + dwc_otg_hc_halt(hcd->core_if, qh->channel,
  36665. + DWC_OTG_HC_XFER_URB_DEQUEUE);
  36666. + }
  36667. + }
  36668. + }
  36669. +
  36670. + /*
  36671. + * Free the QTD and clean up the associated QH. Leave the QH in the
  36672. + * schedule if it has any remaining QTDs.
  36673. + */
  36674. +
  36675. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD URB Dequeue - "
  36676. + "delete %sQueue handler\n",
  36677. + hcd->core_if->dma_desc_enable?"DMA ":"");
  36678. + if (!hcd->core_if->dma_desc_enable) {
  36679. + uint8_t b = urb_qtd->in_process;
  36680. + dwc_otg_hcd_qtd_remove_and_free(hcd, urb_qtd, qh);
  36681. + if (b) {
  36682. + dwc_otg_hcd_qh_deactivate(hcd, qh, 0);
  36683. + qh->channel = NULL;
  36684. + } else if (DWC_CIRCLEQ_EMPTY(&qh->qtd_list)) {
  36685. + dwc_otg_hcd_qh_remove(hcd, qh);
  36686. + }
  36687. + } else {
  36688. + dwc_otg_hcd_qtd_remove_and_free(hcd, urb_qtd, qh);
  36689. + }
  36690. + return 0;
  36691. +}
  36692. +
  36693. +int dwc_otg_hcd_endpoint_disable(dwc_otg_hcd_t * hcd, void *ep_handle,
  36694. + int retry)
  36695. +{
  36696. + dwc_otg_qh_t *qh = (dwc_otg_qh_t *) ep_handle;
  36697. + int retval = 0;
  36698. + dwc_irqflags_t flags;
  36699. +
  36700. + if (retry < 0) {
  36701. + retval = -DWC_E_INVALID;
  36702. + goto done;
  36703. + }
  36704. +
  36705. + if (!qh) {
  36706. + retval = -DWC_E_INVALID;
  36707. + goto done;
  36708. + }
  36709. +
  36710. + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
  36711. +
  36712. + while (!DWC_CIRCLEQ_EMPTY(&qh->qtd_list) && retry) {
  36713. + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
  36714. + retry--;
  36715. + dwc_msleep(5);
  36716. + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
  36717. + }
  36718. +
  36719. + dwc_otg_hcd_qh_remove(hcd, qh);
  36720. +
  36721. + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
  36722. + /*
  36723. + * Split dwc_otg_hcd_qh_remove_and_free() into qh_remove
  36724. + * and qh_free to prevent stack dump on DWC_DMA_FREE() with
  36725. + * irq_disabled (spinlock_irqsave) in dwc_otg_hcd_desc_list_free()
  36726. + * and dwc_otg_hcd_frame_list_alloc().
  36727. + */
  36728. + dwc_otg_hcd_qh_free(hcd, qh);
  36729. +
  36730. +done:
  36731. + return retval;
  36732. +}
  36733. +
  36734. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30)
  36735. +int dwc_otg_hcd_endpoint_reset(dwc_otg_hcd_t * hcd, void *ep_handle)
  36736. +{
  36737. + int retval = 0;
  36738. + dwc_otg_qh_t *qh = (dwc_otg_qh_t *) ep_handle;
  36739. + if (!qh)
  36740. + return -DWC_E_INVALID;
  36741. +
  36742. + qh->data_toggle = DWC_OTG_HC_PID_DATA0;
  36743. + return retval;
  36744. +}
  36745. +#endif
  36746. +
  36747. +/**
  36748. + * HCD Callback structure for handling mode switching.
  36749. + */
  36750. +static dwc_otg_cil_callbacks_t hcd_cil_callbacks = {
  36751. + .start = dwc_otg_hcd_start_cb,
  36752. + .stop = dwc_otg_hcd_stop_cb,
  36753. + .disconnect = dwc_otg_hcd_disconnect_cb,
  36754. + .session_start = dwc_otg_hcd_session_start_cb,
  36755. + .resume_wakeup = dwc_otg_hcd_rem_wakeup_cb,
  36756. +#ifdef CONFIG_USB_DWC_OTG_LPM
  36757. + .sleep = dwc_otg_hcd_sleep_cb,
  36758. +#endif
  36759. + .p = 0,
  36760. +};
  36761. +
  36762. +/**
  36763. + * Reset tasklet function
  36764. + */
  36765. +static void reset_tasklet_func(void *data)
  36766. +{
  36767. + dwc_otg_hcd_t *dwc_otg_hcd = (dwc_otg_hcd_t *) data;
  36768. + dwc_otg_core_if_t *core_if = dwc_otg_hcd->core_if;
  36769. + hprt0_data_t hprt0;
  36770. +
  36771. + DWC_DEBUGPL(DBG_HCDV, "USB RESET tasklet called\n");
  36772. +
  36773. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  36774. + hprt0.b.prtrst = 1;
  36775. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  36776. + dwc_mdelay(60);
  36777. +
  36778. + hprt0.b.prtrst = 0;
  36779. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  36780. + dwc_otg_hcd->flags.b.port_reset_change = 1;
  36781. +}
  36782. +
  36783. +static void completion_tasklet_func(void *ptr)
  36784. +{
  36785. + dwc_otg_hcd_t *hcd = (dwc_otg_hcd_t *) ptr;
  36786. + struct urb *urb;
  36787. + urb_tq_entry_t *item;
  36788. + dwc_irqflags_t flags;
  36789. +
  36790. + /* This could just be spin_lock_irq */
  36791. + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
  36792. + while (!DWC_TAILQ_EMPTY(&hcd->completed_urb_list)) {
  36793. + item = DWC_TAILQ_FIRST(&hcd->completed_urb_list);
  36794. + urb = item->urb;
  36795. + DWC_TAILQ_REMOVE(&hcd->completed_urb_list, item,
  36796. + urb_tq_entries);
  36797. + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
  36798. + DWC_FREE(item);
  36799. +
  36800. + usb_hcd_giveback_urb(hcd->priv, urb, urb->status);
  36801. +
  36802. +
  36803. + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
  36804. + }
  36805. + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
  36806. + return;
  36807. +}
  36808. +
  36809. +static void qh_list_free(dwc_otg_hcd_t * hcd, dwc_list_link_t * qh_list)
  36810. +{
  36811. + dwc_list_link_t *item;
  36812. + dwc_otg_qh_t *qh;
  36813. + dwc_irqflags_t flags;
  36814. +
  36815. + if (!qh_list->next) {
  36816. + /* The list hasn't been initialized yet. */
  36817. + return;
  36818. + }
  36819. + /*
  36820. + * Hold spinlock here. Not needed in that case if bellow
  36821. + * function is being called from ISR
  36822. + */
  36823. + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
  36824. + /* Ensure there are no QTDs or URBs left. */
  36825. + kill_urbs_in_qh_list(hcd, qh_list);
  36826. + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
  36827. +
  36828. + DWC_LIST_FOREACH(item, qh_list) {
  36829. + qh = DWC_LIST_ENTRY(item, dwc_otg_qh_t, qh_list_entry);
  36830. + dwc_otg_hcd_qh_remove_and_free(hcd, qh);
  36831. + }
  36832. +}
  36833. +
  36834. +/**
  36835. + * Exit from Hibernation if Host did not detect SRP from connected SRP capable
  36836. + * Device during SRP time by host power up.
  36837. + */
  36838. +void dwc_otg_hcd_power_up(void *ptr)
  36839. +{
  36840. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  36841. + dwc_otg_core_if_t *core_if = (dwc_otg_core_if_t *) ptr;
  36842. +
  36843. + DWC_PRINTF("%s called\n", __FUNCTION__);
  36844. +
  36845. + if (!core_if->hibernation_suspend) {
  36846. + DWC_PRINTF("Already exited from Hibernation\n");
  36847. + return;
  36848. + }
  36849. +
  36850. + /* Switch on the voltage to the core */
  36851. + gpwrdn.b.pwrdnswtch = 1;
  36852. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  36853. + dwc_udelay(10);
  36854. +
  36855. + /* Reset the core */
  36856. + gpwrdn.d32 = 0;
  36857. + gpwrdn.b.pwrdnrstn = 1;
  36858. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  36859. + dwc_udelay(10);
  36860. +
  36861. + /* Disable power clamps */
  36862. + gpwrdn.d32 = 0;
  36863. + gpwrdn.b.pwrdnclmp = 1;
  36864. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  36865. +
  36866. + /* Remove reset the core signal */
  36867. + gpwrdn.d32 = 0;
  36868. + gpwrdn.b.pwrdnrstn = 1;
  36869. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0, gpwrdn.d32);
  36870. + dwc_udelay(10);
  36871. +
  36872. + /* Disable PMU interrupt */
  36873. + gpwrdn.d32 = 0;
  36874. + gpwrdn.b.pmuintsel = 1;
  36875. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  36876. +
  36877. + core_if->hibernation_suspend = 0;
  36878. +
  36879. + /* Disable PMU */
  36880. + gpwrdn.d32 = 0;
  36881. + gpwrdn.b.pmuactv = 1;
  36882. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  36883. + dwc_udelay(10);
  36884. +
  36885. + /* Enable VBUS */
  36886. + gpwrdn.d32 = 0;
  36887. + gpwrdn.b.dis_vbus = 1;
  36888. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, gpwrdn.d32, 0);
  36889. +
  36890. + core_if->op_state = A_HOST;
  36891. + dwc_otg_core_init(core_if);
  36892. + dwc_otg_enable_global_interrupts(core_if);
  36893. + cil_hcd_start(core_if);
  36894. +}
  36895. +
  36896. +void dwc_otg_cleanup_fiq_channel(dwc_otg_hcd_t *hcd, uint32_t num)
  36897. +{
  36898. + struct fiq_channel_state *st = &hcd->fiq_state->channel[num];
  36899. + struct fiq_dma_blob *blob = hcd->fiq_dmab;
  36900. + int i;
  36901. +
  36902. + st->fsm = FIQ_PASSTHROUGH;
  36903. + st->hcchar_copy.d32 = 0;
  36904. + st->hcsplt_copy.d32 = 0;
  36905. + st->hcint_copy.d32 = 0;
  36906. + st->hcintmsk_copy.d32 = 0;
  36907. + st->hctsiz_copy.d32 = 0;
  36908. + st->hcdma_copy.d32 = 0;
  36909. + st->nr_errors = 0;
  36910. + st->hub_addr = 0;
  36911. + st->port_addr = 0;
  36912. + st->expected_uframe = 0;
  36913. + st->nrpackets = 0;
  36914. + st->dma_info.index = 0;
  36915. + for (i = 0; i < 6; i++)
  36916. + st->dma_info.slot_len[i] = 255;
  36917. + st->hs_isoc_info.index = 0;
  36918. + st->hs_isoc_info.iso_desc = NULL;
  36919. + st->hs_isoc_info.nrframes = 0;
  36920. +
  36921. + DWC_MEMSET(&blob->channel[num].index[0], 0x6b, 1128);
  36922. +}
  36923. +
  36924. +/**
  36925. + * Frees secondary storage associated with the dwc_otg_hcd structure contained
  36926. + * in the struct usb_hcd field.
  36927. + */
  36928. +static void dwc_otg_hcd_free(dwc_otg_hcd_t * dwc_otg_hcd)
  36929. +{
  36930. + struct device *dev = dwc_otg_hcd_to_dev(dwc_otg_hcd);
  36931. + int i;
  36932. +
  36933. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD FREE\n");
  36934. +
  36935. + del_timers(dwc_otg_hcd);
  36936. +
  36937. + /* Free memory for QH/QTD lists */
  36938. + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->non_periodic_sched_inactive);
  36939. + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->non_periodic_sched_active);
  36940. + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->periodic_sched_inactive);
  36941. + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->periodic_sched_ready);
  36942. + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->periodic_sched_assigned);
  36943. + qh_list_free(dwc_otg_hcd, &dwc_otg_hcd->periodic_sched_queued);
  36944. +
  36945. + /* Free memory for the host channels. */
  36946. + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  36947. + dwc_hc_t *hc = dwc_otg_hcd->hc_ptr_array[i];
  36948. +
  36949. +#ifdef DEBUG
  36950. + if (dwc_otg_hcd->core_if->hc_xfer_timer[i]) {
  36951. + DWC_TIMER_FREE(dwc_otg_hcd->core_if->hc_xfer_timer[i]);
  36952. + }
  36953. +#endif
  36954. + if (hc != NULL) {
  36955. + DWC_DEBUGPL(DBG_HCDV, "HCD Free channel #%i, hc=%p\n",
  36956. + i, hc);
  36957. + DWC_FREE(hc);
  36958. + }
  36959. + }
  36960. +
  36961. + if (dwc_otg_hcd->core_if->dma_enable) {
  36962. + if (dwc_otg_hcd->status_buf_dma) {
  36963. + DWC_DMA_FREE(dev, DWC_OTG_HCD_STATUS_BUF_SIZE,
  36964. + dwc_otg_hcd->status_buf,
  36965. + dwc_otg_hcd->status_buf_dma);
  36966. + }
  36967. + } else if (dwc_otg_hcd->status_buf != NULL) {
  36968. + DWC_FREE(dwc_otg_hcd->status_buf);
  36969. + }
  36970. + DWC_SPINLOCK_FREE(dwc_otg_hcd->channel_lock);
  36971. + DWC_SPINLOCK_FREE(dwc_otg_hcd->lock);
  36972. + /* Set core_if's lock pointer to NULL */
  36973. + dwc_otg_hcd->core_if->lock = NULL;
  36974. +
  36975. + DWC_TIMER_FREE(dwc_otg_hcd->conn_timer);
  36976. + DWC_TASK_FREE(dwc_otg_hcd->reset_tasklet);
  36977. + DWC_TASK_FREE(dwc_otg_hcd->completion_tasklet);
  36978. + DWC_FREE(dwc_otg_hcd->fiq_state);
  36979. +
  36980. +#ifdef DWC_DEV_SRPCAP
  36981. + if (dwc_otg_hcd->core_if->power_down == 2 &&
  36982. + dwc_otg_hcd->core_if->pwron_timer) {
  36983. + DWC_TIMER_FREE(dwc_otg_hcd->core_if->pwron_timer);
  36984. + }
  36985. +#endif
  36986. + DWC_FREE(dwc_otg_hcd);
  36987. +}
  36988. +
  36989. +int init_hcd_usecs(dwc_otg_hcd_t *_hcd);
  36990. +
  36991. +int dwc_otg_hcd_init(dwc_otg_hcd_t * hcd, dwc_otg_core_if_t * core_if)
  36992. +{
  36993. + struct device *dev = dwc_otg_hcd_to_dev(hcd);
  36994. + int retval = 0;
  36995. + int num_channels;
  36996. + int i;
  36997. + dwc_hc_t *channel;
  36998. +
  36999. +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_SPINLOCK))
  37000. + DWC_SPINLOCK_ALLOC_LINUX_DEBUG(hcd->lock);
  37001. + DWC_SPINLOCK_ALLOC_LINUX_DEBUG(hcd->channel_lock);
  37002. +#else
  37003. + hcd->lock = DWC_SPINLOCK_ALLOC();
  37004. + hcd->channel_lock = DWC_SPINLOCK_ALLOC();
  37005. +#endif
  37006. + DWC_DEBUGPL(DBG_HCDV, "init of HCD %p given core_if %p\n",
  37007. + hcd, core_if);
  37008. + if (!hcd->lock) {
  37009. + DWC_ERROR("Could not allocate lock for pcd");
  37010. + DWC_FREE(hcd);
  37011. + retval = -DWC_E_NO_MEMORY;
  37012. + goto out;
  37013. + }
  37014. + hcd->core_if = core_if;
  37015. +
  37016. + /* Register the HCD CIL Callbacks */
  37017. + dwc_otg_cil_register_hcd_callbacks(hcd->core_if,
  37018. + &hcd_cil_callbacks, hcd);
  37019. +
  37020. + /* Initialize the non-periodic schedule. */
  37021. + DWC_LIST_INIT(&hcd->non_periodic_sched_inactive);
  37022. + DWC_LIST_INIT(&hcd->non_periodic_sched_active);
  37023. +
  37024. + /* Initialize the periodic schedule. */
  37025. + DWC_LIST_INIT(&hcd->periodic_sched_inactive);
  37026. + DWC_LIST_INIT(&hcd->periodic_sched_ready);
  37027. + DWC_LIST_INIT(&hcd->periodic_sched_assigned);
  37028. + DWC_LIST_INIT(&hcd->periodic_sched_queued);
  37029. + DWC_TAILQ_INIT(&hcd->completed_urb_list);
  37030. + /*
  37031. + * Create a host channel descriptor for each host channel implemented
  37032. + * in the controller. Initialize the channel descriptor array.
  37033. + */
  37034. + DWC_CIRCLEQ_INIT(&hcd->free_hc_list);
  37035. + num_channels = hcd->core_if->core_params->host_channels;
  37036. + DWC_MEMSET(hcd->hc_ptr_array, 0, sizeof(hcd->hc_ptr_array));
  37037. + for (i = 0; i < num_channels; i++) {
  37038. + channel = DWC_ALLOC(sizeof(dwc_hc_t));
  37039. + if (channel == NULL) {
  37040. + retval = -DWC_E_NO_MEMORY;
  37041. + DWC_ERROR("%s: host channel allocation failed\n",
  37042. + __func__);
  37043. + dwc_otg_hcd_free(hcd);
  37044. + goto out;
  37045. + }
  37046. + channel->hc_num = i;
  37047. + hcd->hc_ptr_array[i] = channel;
  37048. +#ifdef DEBUG
  37049. + hcd->core_if->hc_xfer_timer[i] =
  37050. + DWC_TIMER_ALLOC("hc timer", hc_xfer_timeout,
  37051. + &hcd->core_if->hc_xfer_info[i]);
  37052. +#endif
  37053. + DWC_DEBUGPL(DBG_HCDV, "HCD Added channel #%d, hc=%p\n", i,
  37054. + channel);
  37055. + }
  37056. +
  37057. + if (fiq_enable) {
  37058. + hcd->fiq_state = DWC_ALLOC(sizeof(struct fiq_state) + (sizeof(struct fiq_channel_state) * num_channels));
  37059. + if (!hcd->fiq_state) {
  37060. + retval = -DWC_E_NO_MEMORY;
  37061. + DWC_ERROR("%s: cannot allocate fiq_state structure\n", __func__);
  37062. + dwc_otg_hcd_free(hcd);
  37063. + goto out;
  37064. + }
  37065. + DWC_MEMSET(hcd->fiq_state, 0, (sizeof(struct fiq_state) + (sizeof(struct fiq_channel_state) * num_channels)));
  37066. +
  37067. + for (i = 0; i < num_channels; i++) {
  37068. + hcd->fiq_state->channel[i].fsm = FIQ_PASSTHROUGH;
  37069. + }
  37070. + hcd->fiq_state->dummy_send = DWC_ALLOC_ATOMIC(16);
  37071. +
  37072. + hcd->fiq_stack = DWC_ALLOC(sizeof(struct fiq_stack));
  37073. + if (!hcd->fiq_stack) {
  37074. + retval = -DWC_E_NO_MEMORY;
  37075. + DWC_ERROR("%s: cannot allocate fiq_stack structure\n", __func__);
  37076. + dwc_otg_hcd_free(hcd);
  37077. + goto out;
  37078. + }
  37079. + hcd->fiq_stack->magic1 = 0xDEADBEEF;
  37080. + hcd->fiq_stack->magic2 = 0xD00DFEED;
  37081. + hcd->fiq_state->gintmsk_saved.d32 = ~0;
  37082. + hcd->fiq_state->haintmsk_saved.b2.chint = ~0;
  37083. +
  37084. + /* This bit is terrible and uses no API, but necessary. The FIQ has no concept of DMA pools
  37085. + * (and if it did, would be a lot slower). This allocates a chunk of memory (~9kiB for 8 host channels)
  37086. + * for use as transaction bounce buffers in a 2-D array. Our access into this chunk is done by some
  37087. + * moderately readable array casts.
  37088. + */
  37089. + hcd->fiq_dmab = DWC_DMA_ALLOC(dev, (sizeof(struct fiq_dma_channel) * num_channels), &hcd->fiq_state->dma_base);
  37090. + DWC_WARN("FIQ DMA bounce buffers: virt = 0x%08x dma = 0x%08x len=%d",
  37091. + (unsigned int)hcd->fiq_dmab, (unsigned int)hcd->fiq_state->dma_base,
  37092. + sizeof(struct fiq_dma_channel) * num_channels);
  37093. +
  37094. + DWC_MEMSET(hcd->fiq_dmab, 0x6b, 9024);
  37095. +
  37096. + /* pointer for debug in fiq_print */
  37097. + hcd->fiq_state->fiq_dmab = hcd->fiq_dmab;
  37098. + if (fiq_fsm_enable) {
  37099. + int i;
  37100. + for (i=0; i < hcd->core_if->core_params->host_channels; i++) {
  37101. + dwc_otg_cleanup_fiq_channel(hcd, i);
  37102. + }
  37103. + DWC_PRINTF("FIQ FSM acceleration enabled for :\n%s%s%s%s",
  37104. + (fiq_fsm_mask & 0x1) ? "Non-periodic Split Transactions\n" : "",
  37105. + (fiq_fsm_mask & 0x2) ? "Periodic Split Transactions\n" : "",
  37106. + (fiq_fsm_mask & 0x4) ? "High-Speed Isochronous Endpoints\n" : "",
  37107. + (fiq_fsm_mask & 0x8) ? "Interrupt/Control Split Transaction hack enabled\n" : "");
  37108. + }
  37109. + }
  37110. +
  37111. + /* Initialize the Connection timeout timer. */
  37112. + hcd->conn_timer = DWC_TIMER_ALLOC("Connection timer",
  37113. + dwc_otg_hcd_connect_timeout, 0);
  37114. +
  37115. + printk(KERN_DEBUG "dwc_otg: Microframe scheduler %s\n", microframe_schedule ? "enabled":"disabled");
  37116. + if (microframe_schedule)
  37117. + init_hcd_usecs(hcd);
  37118. +
  37119. + /* Initialize reset tasklet. */
  37120. + hcd->reset_tasklet = DWC_TASK_ALLOC("reset_tasklet", reset_tasklet_func, hcd);
  37121. +
  37122. + hcd->completion_tasklet = DWC_TASK_ALLOC("completion_tasklet",
  37123. + completion_tasklet_func, hcd);
  37124. +#ifdef DWC_DEV_SRPCAP
  37125. + if (hcd->core_if->power_down == 2) {
  37126. + /* Initialize Power on timer for Host power up in case hibernation */
  37127. + hcd->core_if->pwron_timer = DWC_TIMER_ALLOC("PWRON TIMER",
  37128. + dwc_otg_hcd_power_up, core_if);
  37129. + }
  37130. +#endif
  37131. +
  37132. + /*
  37133. + * Allocate space for storing data on status transactions. Normally no
  37134. + * data is sent, but this space acts as a bit bucket. This must be
  37135. + * done after usb_add_hcd since that function allocates the DMA buffer
  37136. + * pool.
  37137. + */
  37138. + if (hcd->core_if->dma_enable) {
  37139. + hcd->status_buf =
  37140. + DWC_DMA_ALLOC(dev, DWC_OTG_HCD_STATUS_BUF_SIZE,
  37141. + &hcd->status_buf_dma);
  37142. + } else {
  37143. + hcd->status_buf = DWC_ALLOC(DWC_OTG_HCD_STATUS_BUF_SIZE);
  37144. + }
  37145. + if (!hcd->status_buf) {
  37146. + retval = -DWC_E_NO_MEMORY;
  37147. + DWC_ERROR("%s: status_buf allocation failed\n", __func__);
  37148. + dwc_otg_hcd_free(hcd);
  37149. + goto out;
  37150. + }
  37151. +
  37152. + hcd->otg_port = 1;
  37153. + hcd->frame_list = NULL;
  37154. + hcd->frame_list_dma = 0;
  37155. + hcd->periodic_qh_count = 0;
  37156. +
  37157. + DWC_MEMSET(hcd->hub_port, 0, sizeof(hcd->hub_port));
  37158. +#ifdef FIQ_DEBUG
  37159. + DWC_MEMSET(hcd->hub_port_alloc, -1, sizeof(hcd->hub_port_alloc));
  37160. +#endif
  37161. +
  37162. +out:
  37163. + return retval;
  37164. +}
  37165. +
  37166. +void dwc_otg_hcd_remove(dwc_otg_hcd_t * hcd)
  37167. +{
  37168. + /* Turn off all host-specific interrupts. */
  37169. + dwc_otg_disable_host_interrupts(hcd->core_if);
  37170. +
  37171. + dwc_otg_hcd_free(hcd);
  37172. +}
  37173. +
  37174. +/**
  37175. + * Initializes dynamic portions of the DWC_otg HCD state.
  37176. + */
  37177. +static void dwc_otg_hcd_reinit(dwc_otg_hcd_t * hcd)
  37178. +{
  37179. + int num_channels;
  37180. + int i;
  37181. + dwc_hc_t *channel;
  37182. + dwc_hc_t *channel_tmp;
  37183. +
  37184. + hcd->flags.d32 = 0;
  37185. +
  37186. + hcd->non_periodic_qh_ptr = &hcd->non_periodic_sched_active;
  37187. + if (!microframe_schedule) {
  37188. + hcd->non_periodic_channels = 0;
  37189. + hcd->periodic_channels = 0;
  37190. + } else {
  37191. + hcd->available_host_channels = hcd->core_if->core_params->host_channels;
  37192. + }
  37193. + /*
  37194. + * Put all channels in the free channel list and clean up channel
  37195. + * states.
  37196. + */
  37197. + DWC_CIRCLEQ_FOREACH_SAFE(channel, channel_tmp,
  37198. + &hcd->free_hc_list, hc_list_entry) {
  37199. + DWC_CIRCLEQ_REMOVE(&hcd->free_hc_list, channel, hc_list_entry);
  37200. + }
  37201. +
  37202. + num_channels = hcd->core_if->core_params->host_channels;
  37203. + for (i = 0; i < num_channels; i++) {
  37204. + channel = hcd->hc_ptr_array[i];
  37205. + DWC_CIRCLEQ_INSERT_TAIL(&hcd->free_hc_list, channel,
  37206. + hc_list_entry);
  37207. + dwc_otg_hc_cleanup(hcd->core_if, channel);
  37208. + }
  37209. +
  37210. + /* Initialize the DWC core for host mode operation. */
  37211. + dwc_otg_core_host_init(hcd->core_if);
  37212. +
  37213. + /* Set core_if's lock pointer to the hcd->lock */
  37214. + hcd->core_if->lock = hcd->lock;
  37215. +}
  37216. +
  37217. +/**
  37218. + * Assigns transactions from a QTD to a free host channel and initializes the
  37219. + * host channel to perform the transactions. The host channel is removed from
  37220. + * the free list.
  37221. + *
  37222. + * @param hcd The HCD state structure.
  37223. + * @param qh Transactions from the first QTD for this QH are selected and
  37224. + * assigned to a free host channel.
  37225. + */
  37226. +static void assign_and_init_hc(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  37227. +{
  37228. + dwc_hc_t *hc;
  37229. + dwc_otg_qtd_t *qtd;
  37230. + dwc_otg_hcd_urb_t *urb;
  37231. + void* ptr = NULL;
  37232. + uint32_t intr_enable;
  37233. + unsigned long flags;
  37234. + gintmsk_data_t gintmsk = { .d32 = 0, };
  37235. + struct device *dev = dwc_otg_hcd_to_dev(hcd);
  37236. +
  37237. + qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
  37238. +
  37239. + urb = qtd->urb;
  37240. +
  37241. + DWC_DEBUGPL(DBG_HCDV, "%s(%p,%p) - urb %x, actual_length %d\n", __func__, hcd, qh, (unsigned int)urb, urb->actual_length);
  37242. +
  37243. + if (((urb->actual_length < 0) || (urb->actual_length > urb->length)) && !dwc_otg_hcd_is_pipe_in(&urb->pipe_info))
  37244. + urb->actual_length = urb->length;
  37245. +
  37246. +
  37247. + hc = DWC_CIRCLEQ_FIRST(&hcd->free_hc_list);
  37248. +
  37249. + /* Remove the host channel from the free list. */
  37250. + DWC_CIRCLEQ_REMOVE_INIT(&hcd->free_hc_list, hc, hc_list_entry);
  37251. +
  37252. + qh->channel = hc;
  37253. +
  37254. + qtd->in_process = 1;
  37255. +
  37256. + /*
  37257. + * Use usb_pipedevice to determine device address. This address is
  37258. + * 0 before the SET_ADDRESS command and the correct address afterward.
  37259. + */
  37260. + hc->dev_addr = dwc_otg_hcd_get_dev_addr(&urb->pipe_info);
  37261. + hc->ep_num = dwc_otg_hcd_get_ep_num(&urb->pipe_info);
  37262. + hc->speed = qh->dev_speed;
  37263. + hc->max_packet = dwc_max_packet(qh->maxp);
  37264. +
  37265. + hc->xfer_started = 0;
  37266. + hc->halt_status = DWC_OTG_HC_XFER_NO_HALT_STATUS;
  37267. + hc->error_state = (qtd->error_count > 0);
  37268. + hc->halt_on_queue = 0;
  37269. + hc->halt_pending = 0;
  37270. + hc->requests = 0;
  37271. +
  37272. + /*
  37273. + * The following values may be modified in the transfer type section
  37274. + * below. The xfer_len value may be reduced when the transfer is
  37275. + * started to accommodate the max widths of the XferSize and PktCnt
  37276. + * fields in the HCTSIZn register.
  37277. + */
  37278. +
  37279. + hc->ep_is_in = (dwc_otg_hcd_is_pipe_in(&urb->pipe_info) != 0);
  37280. + if (hc->ep_is_in) {
  37281. + hc->do_ping = 0;
  37282. + } else {
  37283. + hc->do_ping = qh->ping_state;
  37284. + }
  37285. +
  37286. + hc->data_pid_start = qh->data_toggle;
  37287. + hc->multi_count = 1;
  37288. +
  37289. + if (hcd->core_if->dma_enable) {
  37290. + hc->xfer_buff = (uint8_t *) urb->dma + urb->actual_length;
  37291. +
  37292. + /* For non-dword aligned case */
  37293. + if (((unsigned long)hc->xfer_buff & 0x3)
  37294. + && !hcd->core_if->dma_desc_enable) {
  37295. + ptr = (uint8_t *) urb->buf + urb->actual_length;
  37296. + }
  37297. + } else {
  37298. + hc->xfer_buff = (uint8_t *) urb->buf + urb->actual_length;
  37299. + }
  37300. + hc->xfer_len = urb->length - urb->actual_length;
  37301. + hc->xfer_count = 0;
  37302. +
  37303. + /*
  37304. + * Set the split attributes
  37305. + */
  37306. + hc->do_split = 0;
  37307. + if (qh->do_split) {
  37308. + uint32_t hub_addr, port_addr;
  37309. + hc->do_split = 1;
  37310. + hc->xact_pos = qtd->isoc_split_pos;
  37311. + /* We don't need to do complete splits anymore */
  37312. +// if(fiq_fsm_enable)
  37313. + if (0)
  37314. + hc->complete_split = qtd->complete_split = 0;
  37315. + else
  37316. + hc->complete_split = qtd->complete_split;
  37317. +
  37318. + hcd->fops->hub_info(hcd, urb->priv, &hub_addr, &port_addr);
  37319. + hc->hub_addr = (uint8_t) hub_addr;
  37320. + hc->port_addr = (uint8_t) port_addr;
  37321. + }
  37322. +
  37323. + switch (dwc_otg_hcd_get_pipe_type(&urb->pipe_info)) {
  37324. + case UE_CONTROL:
  37325. + hc->ep_type = DWC_OTG_EP_TYPE_CONTROL;
  37326. + switch (qtd->control_phase) {
  37327. + case DWC_OTG_CONTROL_SETUP:
  37328. + DWC_DEBUGPL(DBG_HCDV, " Control setup transaction\n");
  37329. + hc->do_ping = 0;
  37330. + hc->ep_is_in = 0;
  37331. + hc->data_pid_start = DWC_OTG_HC_PID_SETUP;
  37332. + if (hcd->core_if->dma_enable) {
  37333. + hc->xfer_buff = (uint8_t *) urb->setup_dma;
  37334. + } else {
  37335. + hc->xfer_buff = (uint8_t *) urb->setup_packet;
  37336. + }
  37337. + hc->xfer_len = 8;
  37338. + ptr = NULL;
  37339. + break;
  37340. + case DWC_OTG_CONTROL_DATA:
  37341. + DWC_DEBUGPL(DBG_HCDV, " Control data transaction\n");
  37342. + hc->data_pid_start = qtd->data_toggle;
  37343. + break;
  37344. + case DWC_OTG_CONTROL_STATUS:
  37345. + /*
  37346. + * Direction is opposite of data direction or IN if no
  37347. + * data.
  37348. + */
  37349. + DWC_DEBUGPL(DBG_HCDV, " Control status transaction\n");
  37350. + if (urb->length == 0) {
  37351. + hc->ep_is_in = 1;
  37352. + } else {
  37353. + hc->ep_is_in =
  37354. + dwc_otg_hcd_is_pipe_out(&urb->pipe_info);
  37355. + }
  37356. + if (hc->ep_is_in) {
  37357. + hc->do_ping = 0;
  37358. + }
  37359. +
  37360. + hc->data_pid_start = DWC_OTG_HC_PID_DATA1;
  37361. +
  37362. + hc->xfer_len = 0;
  37363. + if (hcd->core_if->dma_enable) {
  37364. + hc->xfer_buff = (uint8_t *) hcd->status_buf_dma;
  37365. + } else {
  37366. + hc->xfer_buff = (uint8_t *) hcd->status_buf;
  37367. + }
  37368. + ptr = NULL;
  37369. + break;
  37370. + }
  37371. + break;
  37372. + case UE_BULK:
  37373. + hc->ep_type = DWC_OTG_EP_TYPE_BULK;
  37374. + break;
  37375. + case UE_INTERRUPT:
  37376. + hc->ep_type = DWC_OTG_EP_TYPE_INTR;
  37377. + break;
  37378. + case UE_ISOCHRONOUS:
  37379. + {
  37380. + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
  37381. +
  37382. + hc->ep_type = DWC_OTG_EP_TYPE_ISOC;
  37383. +
  37384. + if (hcd->core_if->dma_desc_enable)
  37385. + break;
  37386. +
  37387. + frame_desc = &urb->iso_descs[qtd->isoc_frame_index];
  37388. +
  37389. + frame_desc->status = 0;
  37390. +
  37391. + if (hcd->core_if->dma_enable) {
  37392. + hc->xfer_buff = (uint8_t *) urb->dma;
  37393. + } else {
  37394. + hc->xfer_buff = (uint8_t *) urb->buf;
  37395. + }
  37396. + hc->xfer_buff +=
  37397. + frame_desc->offset + qtd->isoc_split_offset;
  37398. + hc->xfer_len =
  37399. + frame_desc->length - qtd->isoc_split_offset;
  37400. +
  37401. + /* For non-dword aligned buffers */
  37402. + if (((unsigned long)hc->xfer_buff & 0x3)
  37403. + && hcd->core_if->dma_enable) {
  37404. + ptr =
  37405. + (uint8_t *) urb->buf + frame_desc->offset +
  37406. + qtd->isoc_split_offset;
  37407. + } else
  37408. + ptr = NULL;
  37409. +
  37410. + if (hc->xact_pos == DWC_HCSPLIT_XACTPOS_ALL) {
  37411. + if (hc->xfer_len <= 188) {
  37412. + hc->xact_pos = DWC_HCSPLIT_XACTPOS_ALL;
  37413. + } else {
  37414. + hc->xact_pos =
  37415. + DWC_HCSPLIT_XACTPOS_BEGIN;
  37416. + }
  37417. + }
  37418. + }
  37419. + break;
  37420. + }
  37421. + /* non DWORD-aligned buffer case */
  37422. + if (ptr) {
  37423. + uint32_t buf_size;
  37424. + if (hc->ep_type != DWC_OTG_EP_TYPE_ISOC) {
  37425. + buf_size = hcd->core_if->core_params->max_transfer_size;
  37426. + } else {
  37427. + buf_size = 4096;
  37428. + }
  37429. + if (!qh->dw_align_buf) {
  37430. + qh->dw_align_buf = DWC_DMA_ALLOC_ATOMIC(dev, buf_size,
  37431. + &qh->dw_align_buf_dma);
  37432. + if (!qh->dw_align_buf) {
  37433. + DWC_ERROR
  37434. + ("%s: Failed to allocate memory to handle "
  37435. + "non-dword aligned buffer case\n",
  37436. + __func__);
  37437. + return;
  37438. + }
  37439. + }
  37440. + if (!hc->ep_is_in) {
  37441. + dwc_memcpy(qh->dw_align_buf, ptr, hc->xfer_len);
  37442. + }
  37443. + hc->align_buff = qh->dw_align_buf_dma;
  37444. + } else {
  37445. + hc->align_buff = 0;
  37446. + }
  37447. +
  37448. + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
  37449. + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
  37450. + /*
  37451. + * This value may be modified when the transfer is started to
  37452. + * reflect the actual transfer length.
  37453. + */
  37454. + hc->multi_count = dwc_hb_mult(qh->maxp);
  37455. + }
  37456. +
  37457. + if (hcd->core_if->dma_desc_enable)
  37458. + hc->desc_list_addr = qh->desc_list_dma;
  37459. +
  37460. + dwc_otg_hc_init(hcd->core_if, hc);
  37461. +
  37462. + local_irq_save(flags);
  37463. +
  37464. + if (fiq_enable) {
  37465. + local_fiq_disable();
  37466. + fiq_fsm_spin_lock(&hcd->fiq_state->lock);
  37467. + }
  37468. +
  37469. + /* Enable the top level host channel interrupt. */
  37470. + intr_enable = (1 << hc->hc_num);
  37471. + DWC_MODIFY_REG32(&hcd->core_if->host_if->host_global_regs->haintmsk, 0, intr_enable);
  37472. +
  37473. + /* Make sure host channel interrupts are enabled. */
  37474. + gintmsk.b.hcintr = 1;
  37475. + DWC_MODIFY_REG32(&hcd->core_if->core_global_regs->gintmsk, 0, gintmsk.d32);
  37476. +
  37477. + if (fiq_enable) {
  37478. + fiq_fsm_spin_unlock(&hcd->fiq_state->lock);
  37479. + local_fiq_enable();
  37480. + }
  37481. +
  37482. + local_irq_restore(flags);
  37483. + hc->qh = qh;
  37484. +}
  37485. +
  37486. +
  37487. +/**
  37488. + * fiq_fsm_transaction_suitable() - Test a QH for compatibility with the FIQ
  37489. + * @qh: pointer to the endpoint's queue head
  37490. + *
  37491. + * Transaction start/end control flow is grafted onto the existing dwc_otg
  37492. + * mechanisms, to avoid spaghettifying the functions more than they already are.
  37493. + * This function's eligibility check is altered by debug parameter.
  37494. + *
  37495. + * Returns: 0 for unsuitable, 1 implies the FIQ can be enabled for this transaction.
  37496. + */
  37497. +
  37498. +int fiq_fsm_transaction_suitable(dwc_otg_qh_t *qh)
  37499. +{
  37500. + if (qh->do_split) {
  37501. + switch (qh->ep_type) {
  37502. + case UE_CONTROL:
  37503. + case UE_BULK:
  37504. + if (fiq_fsm_mask & (1 << 0))
  37505. + return 1;
  37506. + break;
  37507. + case UE_INTERRUPT:
  37508. + case UE_ISOCHRONOUS:
  37509. + if (fiq_fsm_mask & (1 << 1))
  37510. + return 1;
  37511. + break;
  37512. + default:
  37513. + break;
  37514. + }
  37515. + } else if (qh->ep_type == UE_ISOCHRONOUS) {
  37516. + if (fiq_fsm_mask & (1 << 2)) {
  37517. + /* HS ISOCH support. We test for compatibility:
  37518. + * - DWORD aligned buffers
  37519. + * - Must be at least 2 transfers (otherwise pointless to use the FIQ)
  37520. + * If yes, then the fsm enqueue function will handle the state machine setup.
  37521. + */
  37522. + dwc_otg_qtd_t *qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
  37523. + dwc_otg_hcd_urb_t *urb = qtd->urb;
  37524. + struct dwc_otg_hcd_iso_packet_desc (*iso_descs)[0] = &urb->iso_descs;
  37525. + int nr_iso_frames = urb->packet_count;
  37526. + int i;
  37527. + uint32_t ptr;
  37528. +
  37529. + if (nr_iso_frames < 2)
  37530. + return 0;
  37531. + for (i = 0; i < nr_iso_frames; i++) {
  37532. + ptr = urb->dma + iso_descs[i]->offset;
  37533. + if (ptr & 0x3) {
  37534. + printk_ratelimited("%s: Non-Dword aligned isochronous frame offset."
  37535. + " Cannot queue FIQ-accelerated transfer to device %d endpoint %d\n",
  37536. + __FUNCTION__, qh->channel->dev_addr, qh->channel->ep_num);
  37537. + return 0;
  37538. + }
  37539. + }
  37540. + return 1;
  37541. + }
  37542. + }
  37543. + return 0;
  37544. +}
  37545. +
  37546. +/**
  37547. + * fiq_fsm_setup_periodic_dma() - Set up DMA bounce buffers
  37548. + * @hcd: Pointer to the dwc_otg_hcd struct
  37549. + * @qh: Pointer to the endpoint's queue head
  37550. + *
  37551. + * Periodic split transactions are transmitted modulo 188 bytes.
  37552. + * This necessitates slicing data up into buckets for isochronous out
  37553. + * and fixing up the DMA address for all IN transfers.
  37554. + *
  37555. + * Returns 1 if the DMA bounce buffers have been used, 0 if the default
  37556. + * HC buffer has been used.
  37557. + */
  37558. +int fiq_fsm_setup_periodic_dma(dwc_otg_hcd_t *hcd, struct fiq_channel_state *st, dwc_otg_qh_t *qh)
  37559. + {
  37560. + int frame_length, i = 0;
  37561. + uint8_t *ptr = NULL;
  37562. + dwc_hc_t *hc = qh->channel;
  37563. + struct fiq_dma_blob *blob;
  37564. + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
  37565. +
  37566. + for (i = 0; i < 6; i++) {
  37567. + st->dma_info.slot_len[i] = 255;
  37568. + }
  37569. + st->dma_info.index = 0;
  37570. + i = 0;
  37571. + if (hc->ep_is_in) {
  37572. + /*
  37573. + * Set dma_regs to bounce buffer. FIQ will update the
  37574. + * state depending on transaction progress.
  37575. + */
  37576. + blob = (struct fiq_dma_blob *) hcd->fiq_state->dma_base;
  37577. + st->hcdma_copy.d32 = (uint32_t) &blob->channel[hc->hc_num].index[0].buf[0];
  37578. + /* Calculate the max number of CSPLITS such that the FIQ can time out
  37579. + * a transaction if it fails.
  37580. + */
  37581. + frame_length = st->hcchar_copy.b.mps;
  37582. + do {
  37583. + i++;
  37584. + frame_length -= 188;
  37585. + } while (frame_length >= 0);
  37586. + st->nrpackets = i;
  37587. + return 1;
  37588. + } else {
  37589. + if (qh->ep_type == UE_ISOCHRONOUS) {
  37590. +
  37591. + dwc_otg_qtd_t *qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
  37592. +
  37593. + frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
  37594. + frame_length = frame_desc->length;
  37595. +
  37596. + /* Virtual address for bounce buffers */
  37597. + blob = hcd->fiq_dmab;
  37598. +
  37599. + ptr = qtd->urb->buf + frame_desc->offset;
  37600. + if (frame_length == 0) {
  37601. + /*
  37602. + * for isochronous transactions, we must still transmit a packet
  37603. + * even if the length is zero.
  37604. + */
  37605. + st->dma_info.slot_len[0] = 0;
  37606. + st->nrpackets = 1;
  37607. + } else {
  37608. + do {
  37609. + if (frame_length <= 188) {
  37610. + dwc_memcpy(&blob->channel[hc->hc_num].index[i].buf[0], ptr, frame_length);
  37611. + st->dma_info.slot_len[i] = frame_length;
  37612. + ptr += frame_length;
  37613. + } else {
  37614. + dwc_memcpy(&blob->channel[hc->hc_num].index[i].buf[0], ptr, 188);
  37615. + st->dma_info.slot_len[i] = 188;
  37616. + ptr += 188;
  37617. + }
  37618. + i++;
  37619. + frame_length -= 188;
  37620. + } while (frame_length > 0);
  37621. + st->nrpackets = i;
  37622. + }
  37623. + ptr = qtd->urb->buf + frame_desc->offset;
  37624. + /* Point the HC at the DMA address of the bounce buffers */
  37625. + blob = (struct fiq_dma_blob *) hcd->fiq_state->dma_base;
  37626. + st->hcdma_copy.d32 = (uint32_t) &blob->channel[hc->hc_num].index[0].buf[0];
  37627. +
  37628. + /* fixup xfersize to the actual packet size */
  37629. + st->hctsiz_copy.b.pid = 0;
  37630. + st->hctsiz_copy.b.xfersize = st->dma_info.slot_len[0];
  37631. + return 1;
  37632. + } else {
  37633. + /* For interrupt, single OUT packet required, goes in the SSPLIT from hc_buff. */
  37634. + return 0;
  37635. + }
  37636. + }
  37637. +}
  37638. +
  37639. +/*
  37640. + * Pushing a periodic request into the queue near the EOF1 point
  37641. + * in a microframe causes erroneous behaviour (frmovrun) interrupt.
  37642. + * Usually, the request goes out on the bus causing a transfer but
  37643. + * the core does not transfer the data to memory.
  37644. + * This guard interval (in number of 60MHz clocks) is required which
  37645. + * must cater for CPU latency between reading the value and enabling
  37646. + * the channel.
  37647. + */
  37648. +#define PERIODIC_FRREM_BACKOFF 1000
  37649. +
  37650. +int fiq_fsm_queue_isoc_transaction(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh)
  37651. +{
  37652. + dwc_hc_t *hc = qh->channel;
  37653. + dwc_otg_hc_regs_t *hc_regs = hcd->core_if->host_if->hc_regs[hc->hc_num];
  37654. + dwc_otg_qtd_t *qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
  37655. + int frame;
  37656. + struct fiq_channel_state *st = &hcd->fiq_state->channel[hc->hc_num];
  37657. + int xfer_len, nrpackets;
  37658. + hcdma_data_t hcdma;
  37659. + hfnum_data_t hfnum;
  37660. +
  37661. + if (st->fsm != FIQ_PASSTHROUGH)
  37662. + return 0;
  37663. +
  37664. + st->nr_errors = 0;
  37665. +
  37666. + st->hcchar_copy.d32 = 0;
  37667. + st->hcchar_copy.b.mps = hc->max_packet;
  37668. + st->hcchar_copy.b.epdir = hc->ep_is_in;
  37669. + st->hcchar_copy.b.devaddr = hc->dev_addr;
  37670. + st->hcchar_copy.b.epnum = hc->ep_num;
  37671. + st->hcchar_copy.b.eptype = hc->ep_type;
  37672. +
  37673. + st->hcintmsk_copy.b.chhltd = 1;
  37674. +
  37675. + frame = dwc_otg_hcd_get_frame_number(hcd);
  37676. + st->hcchar_copy.b.oddfrm = (frame & 0x1) ? 0 : 1;
  37677. +
  37678. + st->hcchar_copy.b.lspddev = 0;
  37679. + /* Enable the channel later as a final register write. */
  37680. +
  37681. + st->hcsplt_copy.d32 = 0;
  37682. +
  37683. + st->hs_isoc_info.iso_desc = (struct dwc_otg_hcd_iso_packet_desc *) &qtd->urb->iso_descs;
  37684. + st->hs_isoc_info.nrframes = qtd->urb->packet_count;
  37685. + /* grab the next DMA address offset from the array */
  37686. + st->hcdma_copy.d32 = qtd->urb->dma;
  37687. + hcdma.d32 = st->hcdma_copy.d32 + st->hs_isoc_info.iso_desc[0].offset;
  37688. +
  37689. + /* We need to set multi_count. This is a bit tricky - has to be set per-transaction as
  37690. + * the core needs to be told to send the correct number. Caution: for IN transfers,
  37691. + * this is always set to the maximum size of the endpoint. */
  37692. + xfer_len = st->hs_isoc_info.iso_desc[0].length;
  37693. + nrpackets = (xfer_len + st->hcchar_copy.b.mps - 1) / st->hcchar_copy.b.mps;
  37694. + if (nrpackets == 0)
  37695. + nrpackets = 1;
  37696. + st->hcchar_copy.b.multicnt = nrpackets;
  37697. + st->hctsiz_copy.b.pktcnt = nrpackets;
  37698. +
  37699. + /* Initial PID also needs to be set */
  37700. + if (st->hcchar_copy.b.epdir == 0) {
  37701. + st->hctsiz_copy.b.xfersize = xfer_len;
  37702. + switch (st->hcchar_copy.b.multicnt) {
  37703. + case 1:
  37704. + st->hctsiz_copy.b.pid = DWC_PID_DATA0;
  37705. + break;
  37706. + case 2:
  37707. + case 3:
  37708. + st->hctsiz_copy.b.pid = DWC_PID_MDATA;
  37709. + break;
  37710. + }
  37711. +
  37712. + } else {
  37713. + st->hctsiz_copy.b.xfersize = nrpackets * st->hcchar_copy.b.mps;
  37714. + switch (st->hcchar_copy.b.multicnt) {
  37715. + case 1:
  37716. + st->hctsiz_copy.b.pid = DWC_PID_DATA0;
  37717. + break;
  37718. + case 2:
  37719. + st->hctsiz_copy.b.pid = DWC_PID_DATA1;
  37720. + break;
  37721. + case 3:
  37722. + st->hctsiz_copy.b.pid = DWC_PID_DATA2;
  37723. + break;
  37724. + }
  37725. + }
  37726. +
  37727. + st->hs_isoc_info.stride = qh->interval;
  37728. + st->uframe_sleeps = 0;
  37729. +
  37730. + fiq_print(FIQDBG_INT, hcd->fiq_state, "FSMQ %01d ", hc->hc_num);
  37731. + fiq_print(FIQDBG_INT, hcd->fiq_state, "%08x", st->hcchar_copy.d32);
  37732. + fiq_print(FIQDBG_INT, hcd->fiq_state, "%08x", st->hctsiz_copy.d32);
  37733. + fiq_print(FIQDBG_INT, hcd->fiq_state, "%08x", st->hcdma_copy.d32);
  37734. + hfnum.d32 = DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hfnum);
  37735. + local_fiq_disable();
  37736. + fiq_fsm_spin_lock(&hcd->fiq_state->lock);
  37737. + DWC_WRITE_REG32(&hc_regs->hctsiz, st->hctsiz_copy.d32);
  37738. + DWC_WRITE_REG32(&hc_regs->hcsplt, st->hcsplt_copy.d32);
  37739. + DWC_WRITE_REG32(&hc_regs->hcdma, st->hcdma_copy.d32);
  37740. + DWC_WRITE_REG32(&hc_regs->hcchar, st->hcchar_copy.d32);
  37741. + DWC_WRITE_REG32(&hc_regs->hcintmsk, st->hcintmsk_copy.d32);
  37742. + if (hfnum.b.frrem < PERIODIC_FRREM_BACKOFF) {
  37743. + /* Prevent queueing near EOF1. Bad things happen if a periodic
  37744. + * split transaction is queued very close to EOF. SOF interrupt handler
  37745. + * will wake this channel at the next interrupt.
  37746. + */
  37747. + st->fsm = FIQ_HS_ISOC_SLEEPING;
  37748. + st->uframe_sleeps = 1;
  37749. + } else {
  37750. + st->fsm = FIQ_HS_ISOC_TURBO;
  37751. + st->hcchar_copy.b.chen = 1;
  37752. + DWC_WRITE_REG32(&hc_regs->hcchar, st->hcchar_copy.d32);
  37753. + }
  37754. + mb();
  37755. + st->hcchar_copy.b.chen = 0;
  37756. + fiq_fsm_spin_unlock(&hcd->fiq_state->lock);
  37757. + local_fiq_enable();
  37758. + return 0;
  37759. +}
  37760. +
  37761. +
  37762. +/**
  37763. + * fiq_fsm_queue_split_transaction() - Set up a host channel and FIQ state
  37764. + * @hcd: Pointer to the dwc_otg_hcd struct
  37765. + * @qh: Pointer to the endpoint's queue head
  37766. + *
  37767. + * This overrides the dwc_otg driver's normal method of queueing a transaction.
  37768. + * Called from dwc_otg_hcd_queue_transactions(), this performs specific setup
  37769. + * for the nominated host channel.
  37770. + *
  37771. + * For periodic transfers, it also peeks at the FIQ state to see if an immediate
  37772. + * start is possible. If not, then the FIQ is left to start the transfer.
  37773. + */
  37774. +int fiq_fsm_queue_split_transaction(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh)
  37775. +{
  37776. + int start_immediate = 1, i;
  37777. + hfnum_data_t hfnum;
  37778. + dwc_hc_t *hc = qh->channel;
  37779. + dwc_otg_hc_regs_t *hc_regs = hcd->core_if->host_if->hc_regs[hc->hc_num];
  37780. + /* Program HC registers, setup FIQ_state, examine FIQ if periodic, start transfer (not if uframe 5) */
  37781. + int hub_addr, port_addr, frame, uframe;
  37782. + struct fiq_channel_state *st = &hcd->fiq_state->channel[hc->hc_num];
  37783. +
  37784. + if (st->fsm != FIQ_PASSTHROUGH)
  37785. + return 0;
  37786. + st->nr_errors = 0;
  37787. +
  37788. + st->hcchar_copy.d32 = 0;
  37789. + st->hcchar_copy.b.mps = hc->max_packet;
  37790. + st->hcchar_copy.b.epdir = hc->ep_is_in;
  37791. + st->hcchar_copy.b.devaddr = hc->dev_addr;
  37792. + st->hcchar_copy.b.epnum = hc->ep_num;
  37793. + st->hcchar_copy.b.eptype = hc->ep_type;
  37794. + if (hc->ep_type & 0x1) {
  37795. + if (hc->ep_is_in)
  37796. + st->hcchar_copy.b.multicnt = 3;
  37797. + else
  37798. + /* Docs say set this to 1, but driver sets to 0! */
  37799. + st->hcchar_copy.b.multicnt = 0;
  37800. + } else {
  37801. + st->hcchar_copy.b.multicnt = 1;
  37802. + st->hcchar_copy.b.oddfrm = 0;
  37803. + }
  37804. + st->hcchar_copy.b.lspddev = (hc->speed == DWC_OTG_EP_SPEED_LOW) ? 1 : 0;
  37805. + /* Enable the channel later as a final register write. */
  37806. +
  37807. + st->hcsplt_copy.d32 = 0;
  37808. + if(qh->do_split) {
  37809. + hcd->fops->hub_info(hcd, DWC_CIRCLEQ_FIRST(&qh->qtd_list)->urb->priv, &hub_addr, &port_addr);
  37810. + st->hcsplt_copy.b.compsplt = 0;
  37811. + st->hcsplt_copy.b.spltena = 1;
  37812. + // XACTPOS is for isoc-out only but needs initialising anyway.
  37813. + st->hcsplt_copy.b.xactpos = ISOC_XACTPOS_ALL;
  37814. + if((qh->ep_type == DWC_OTG_EP_TYPE_ISOC) && (!qh->ep_is_in)) {
  37815. + /* For packetsize 0 < L < 188, ISOC_XACTPOS_ALL.
  37816. + * for longer than this, ISOC_XACTPOS_BEGIN and the FIQ
  37817. + * will update as necessary.
  37818. + */
  37819. + if (hc->xfer_len > 188) {
  37820. + st->hcsplt_copy.b.xactpos = ISOC_XACTPOS_BEGIN;
  37821. + }
  37822. + }
  37823. + st->hcsplt_copy.b.hubaddr = (uint8_t) hub_addr;
  37824. + st->hcsplt_copy.b.prtaddr = (uint8_t) port_addr;
  37825. + st->hub_addr = hub_addr;
  37826. + st->port_addr = port_addr;
  37827. + }
  37828. +
  37829. + st->hctsiz_copy.d32 = 0;
  37830. + st->hctsiz_copy.b.dopng = 0;
  37831. + st->hctsiz_copy.b.pid = hc->data_pid_start;
  37832. +
  37833. + if (hc->ep_is_in || (hc->xfer_len > hc->max_packet)) {
  37834. + hc->xfer_len = hc->max_packet;
  37835. + } else if (!hc->ep_is_in && (hc->xfer_len > 188)) {
  37836. + hc->xfer_len = 188;
  37837. + }
  37838. + st->hctsiz_copy.b.xfersize = hc->xfer_len;
  37839. +
  37840. + st->hctsiz_copy.b.pktcnt = 1;
  37841. +
  37842. + if (hc->ep_type & 0x1) {
  37843. + /*
  37844. + * For potentially multi-packet transfers, must use the DMA bounce buffers. For IN transfers,
  37845. + * the DMA address is the address of the first 188byte slot buffer in the bounce buffer array.
  37846. + * For multi-packet OUT transfers, we need to copy the data into the bounce buffer array so the FIQ can punt
  37847. + * the right address out as necessary. hc->xfer_buff and hc->xfer_len have already been set
  37848. + * in assign_and_init_hc(), but this is for the eventual transaction completion only. The FIQ
  37849. + * must not touch internal driver state.
  37850. + */
  37851. + if(!fiq_fsm_setup_periodic_dma(hcd, st, qh)) {
  37852. + if (hc->align_buff) {
  37853. + st->hcdma_copy.d32 = hc->align_buff;
  37854. + } else {
  37855. + st->hcdma_copy.d32 = ((unsigned long) hc->xfer_buff & 0xFFFFFFFF);
  37856. + }
  37857. + }
  37858. + } else {
  37859. + if (hc->align_buff) {
  37860. + st->hcdma_copy.d32 = hc->align_buff;
  37861. + } else {
  37862. + st->hcdma_copy.d32 = ((unsigned long) hc->xfer_buff & 0xFFFFFFFF);
  37863. + }
  37864. + }
  37865. + /* The FIQ depends upon no other interrupts being enabled except channel halt.
  37866. + * Fixup channel interrupt mask. */
  37867. + st->hcintmsk_copy.d32 = 0;
  37868. + st->hcintmsk_copy.b.chhltd = 1;
  37869. + st->hcintmsk_copy.b.ahberr = 1;
  37870. +
  37871. + /* Hack courtesy of FreeBSD: apparently forcing Interrupt Split transactions
  37872. + * as Control puts the transfer into the non-periodic request queue and the
  37873. + * non-periodic handler in the hub. Makes things lots easier.
  37874. + */
  37875. + if ((fiq_fsm_mask & 0x8) && hc->ep_type == UE_INTERRUPT) {
  37876. + st->hcchar_copy.b.multicnt = 0;
  37877. + st->hcchar_copy.b.oddfrm = 0;
  37878. + st->hcchar_copy.b.eptype = UE_CONTROL;
  37879. + if (hc->align_buff) {
  37880. + st->hcdma_copy.d32 = hc->align_buff;
  37881. + } else {
  37882. + st->hcdma_copy.d32 = ((unsigned long) hc->xfer_buff & 0xFFFFFFFF);
  37883. + }
  37884. + }
  37885. + DWC_WRITE_REG32(&hc_regs->hcdma, st->hcdma_copy.d32);
  37886. + DWC_WRITE_REG32(&hc_regs->hctsiz, st->hctsiz_copy.d32);
  37887. + DWC_WRITE_REG32(&hc_regs->hcsplt, st->hcsplt_copy.d32);
  37888. + DWC_WRITE_REG32(&hc_regs->hcchar, st->hcchar_copy.d32);
  37889. + DWC_WRITE_REG32(&hc_regs->hcintmsk, st->hcintmsk_copy.d32);
  37890. +
  37891. + local_fiq_disable();
  37892. + fiq_fsm_spin_lock(&hcd->fiq_state->lock);
  37893. +
  37894. + if (hc->ep_type & 0x1) {
  37895. + hfnum.d32 = DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hfnum);
  37896. + frame = (hfnum.b.frnum & ~0x7) >> 3;
  37897. + uframe = hfnum.b.frnum & 0x7;
  37898. + if (hfnum.b.frrem < PERIODIC_FRREM_BACKOFF) {
  37899. + /* Prevent queueing near EOF1. Bad things happen if a periodic
  37900. + * split transaction is queued very close to EOF.
  37901. + */
  37902. + start_immediate = 0;
  37903. + } else if (uframe == 5) {
  37904. + start_immediate = 0;
  37905. + } else if (hc->ep_type == UE_ISOCHRONOUS && !hc->ep_is_in) {
  37906. + start_immediate = 0;
  37907. + } else if (hc->ep_is_in && fiq_fsm_too_late(hcd->fiq_state, hc->hc_num)) {
  37908. + start_immediate = 0;
  37909. + } else {
  37910. + /* Search through all host channels to determine if a transaction
  37911. + * is currently in progress */
  37912. + for (i = 0; i < hcd->core_if->core_params->host_channels; i++) {
  37913. + if (i == hc->hc_num || hcd->fiq_state->channel[i].fsm == FIQ_PASSTHROUGH)
  37914. + continue;
  37915. + switch (hcd->fiq_state->channel[i].fsm) {
  37916. + /* TT is reserved for channels that are in the middle of a periodic
  37917. + * split transaction.
  37918. + */
  37919. + case FIQ_PER_SSPLIT_STARTED:
  37920. + case FIQ_PER_CSPLIT_WAIT:
  37921. + case FIQ_PER_CSPLIT_NYET1:
  37922. + case FIQ_PER_CSPLIT_POLL:
  37923. + case FIQ_PER_ISO_OUT_ACTIVE:
  37924. + case FIQ_PER_ISO_OUT_LAST:
  37925. + if (hcd->fiq_state->channel[i].hub_addr == hub_addr &&
  37926. + hcd->fiq_state->channel[i].port_addr == port_addr) {
  37927. + start_immediate = 0;
  37928. + }
  37929. + break;
  37930. + default:
  37931. + break;
  37932. + }
  37933. + if (!start_immediate)
  37934. + break;
  37935. + }
  37936. + }
  37937. + }
  37938. + if ((fiq_fsm_mask & 0x8) && hc->ep_type == UE_INTERRUPT)
  37939. + start_immediate = 1;
  37940. +
  37941. + fiq_print(FIQDBG_INT, hcd->fiq_state, "FSMQ %01d %01d", hc->hc_num, start_immediate);
  37942. + fiq_print(FIQDBG_INT, hcd->fiq_state, "%08d", hfnum.b.frrem);
  37943. + //fiq_print(FIQDBG_INT, hcd->fiq_state, "H:%02dP:%02d", hub_addr, port_addr);
  37944. + //fiq_print(FIQDBG_INT, hcd->fiq_state, "%08x", st->hctsiz_copy.d32);
  37945. + //fiq_print(FIQDBG_INT, hcd->fiq_state, "%08x", st->hcdma_copy.d32);
  37946. + switch (hc->ep_type) {
  37947. + case UE_CONTROL:
  37948. + case UE_BULK:
  37949. + st->fsm = FIQ_NP_SSPLIT_STARTED;
  37950. + break;
  37951. + case UE_ISOCHRONOUS:
  37952. + if (hc->ep_is_in) {
  37953. + if (start_immediate) {
  37954. + st->fsm = FIQ_PER_SSPLIT_STARTED;
  37955. + } else {
  37956. + st->fsm = FIQ_PER_SSPLIT_QUEUED;
  37957. + }
  37958. + } else {
  37959. + if (start_immediate) {
  37960. + /* Single-isoc OUT packets don't require FIQ involvement */
  37961. + if (st->nrpackets == 1) {
  37962. + st->fsm = FIQ_PER_ISO_OUT_LAST;
  37963. + } else {
  37964. + st->fsm = FIQ_PER_ISO_OUT_ACTIVE;
  37965. + }
  37966. + } else {
  37967. + st->fsm = FIQ_PER_ISO_OUT_PENDING;
  37968. + }
  37969. + }
  37970. + break;
  37971. + case UE_INTERRUPT:
  37972. + if (fiq_fsm_mask & 0x8) {
  37973. + st->fsm = FIQ_NP_SSPLIT_STARTED;
  37974. + } else if (start_immediate) {
  37975. + st->fsm = FIQ_PER_SSPLIT_STARTED;
  37976. + } else {
  37977. + st->fsm = FIQ_PER_SSPLIT_QUEUED;
  37978. + }
  37979. + default:
  37980. + break;
  37981. + }
  37982. + if (start_immediate) {
  37983. + /* Set the oddfrm bit as close as possible to actual queueing */
  37984. + frame = dwc_otg_hcd_get_frame_number(hcd);
  37985. + st->expected_uframe = (frame + 1) & 0x3FFF;
  37986. + st->hcchar_copy.b.oddfrm = (frame & 0x1) ? 0 : 1;
  37987. + st->hcchar_copy.b.chen = 1;
  37988. + DWC_WRITE_REG32(&hc_regs->hcchar, st->hcchar_copy.d32);
  37989. + }
  37990. + mb();
  37991. + fiq_fsm_spin_unlock(&hcd->fiq_state->lock);
  37992. + local_fiq_enable();
  37993. + return 0;
  37994. +}
  37995. +
  37996. +
  37997. +/**
  37998. + * This function selects transactions from the HCD transfer schedule and
  37999. + * assigns them to available host channels. It is called from HCD interrupt
  38000. + * handler functions.
  38001. + *
  38002. + * @param hcd The HCD state structure.
  38003. + *
  38004. + * @return The types of new transactions that were assigned to host channels.
  38005. + */
  38006. +dwc_otg_transaction_type_e dwc_otg_hcd_select_transactions(dwc_otg_hcd_t * hcd)
  38007. +{
  38008. + dwc_list_link_t *qh_ptr;
  38009. + dwc_otg_qh_t *qh;
  38010. + int num_channels;
  38011. + dwc_irqflags_t flags;
  38012. + dwc_spinlock_t *channel_lock = hcd->channel_lock;
  38013. + dwc_otg_transaction_type_e ret_val = DWC_OTG_TRANSACTION_NONE;
  38014. +
  38015. +#ifdef DEBUG_HOST_CHANNELS
  38016. + last_sel_trans_num_per_scheduled = 0;
  38017. + last_sel_trans_num_nonper_scheduled = 0;
  38018. + last_sel_trans_num_avail_hc_at_start = hcd->available_host_channels;
  38019. +#endif /* DEBUG_HOST_CHANNELS */
  38020. +
  38021. + /* Process entries in the periodic ready list. */
  38022. + qh_ptr = DWC_LIST_FIRST(&hcd->periodic_sched_ready);
  38023. +
  38024. + while (qh_ptr != &hcd->periodic_sched_ready &&
  38025. + !DWC_CIRCLEQ_EMPTY(&hcd->free_hc_list)) {
  38026. +
  38027. + qh = DWC_LIST_ENTRY(qh_ptr, dwc_otg_qh_t, qh_list_entry);
  38028. +
  38029. + if (microframe_schedule) {
  38030. + // Make sure we leave one channel for non periodic transactions.
  38031. + DWC_SPINLOCK_IRQSAVE(channel_lock, &flags);
  38032. + if (hcd->available_host_channels <= 1) {
  38033. + DWC_SPINUNLOCK_IRQRESTORE(channel_lock, flags);
  38034. + break;
  38035. + }
  38036. + hcd->available_host_channels--;
  38037. + DWC_SPINUNLOCK_IRQRESTORE(channel_lock, flags);
  38038. +#ifdef DEBUG_HOST_CHANNELS
  38039. + last_sel_trans_num_per_scheduled++;
  38040. +#endif /* DEBUG_HOST_CHANNELS */
  38041. + }
  38042. + qh = DWC_LIST_ENTRY(qh_ptr, dwc_otg_qh_t, qh_list_entry);
  38043. + assign_and_init_hc(hcd, qh);
  38044. +
  38045. + /*
  38046. + * Move the QH from the periodic ready schedule to the
  38047. + * periodic assigned schedule.
  38048. + */
  38049. + qh_ptr = DWC_LIST_NEXT(qh_ptr);
  38050. + DWC_SPINLOCK_IRQSAVE(channel_lock, &flags);
  38051. + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_assigned,
  38052. + &qh->qh_list_entry);
  38053. + DWC_SPINUNLOCK_IRQRESTORE(channel_lock, flags);
  38054. + }
  38055. +
  38056. + /*
  38057. + * Process entries in the inactive portion of the non-periodic
  38058. + * schedule. Some free host channels may not be used if they are
  38059. + * reserved for periodic transfers.
  38060. + */
  38061. + qh_ptr = hcd->non_periodic_sched_inactive.next;
  38062. + num_channels = hcd->core_if->core_params->host_channels;
  38063. + while (qh_ptr != &hcd->non_periodic_sched_inactive &&
  38064. + (microframe_schedule || hcd->non_periodic_channels <
  38065. + num_channels - hcd->periodic_channels) &&
  38066. + !DWC_CIRCLEQ_EMPTY(&hcd->free_hc_list)) {
  38067. +
  38068. + qh = DWC_LIST_ENTRY(qh_ptr, dwc_otg_qh_t, qh_list_entry);
  38069. + /*
  38070. + * Check to see if this is a NAK'd retransmit, in which case ignore for retransmission
  38071. + * we hold off on bulk retransmissions to reduce NAK interrupt overhead for full-speed
  38072. + * cheeky devices that just hold off using NAKs
  38073. + */
  38074. + if (fiq_enable && nak_holdoff && qh->do_split) {
  38075. + if (qh->nak_frame != 0xffff) {
  38076. + uint16_t next_frame = dwc_frame_num_inc(qh->nak_frame, (qh->ep_type == UE_BULK) ? nak_holdoff : 8);
  38077. + uint16_t frame = dwc_otg_hcd_get_frame_number(hcd);
  38078. + if (dwc_frame_num_le(frame, next_frame)) {
  38079. + if(dwc_frame_num_le(next_frame, hcd->fiq_state->next_sched_frame)) {
  38080. + hcd->fiq_state->next_sched_frame = next_frame;
  38081. + }
  38082. + qh_ptr = DWC_LIST_NEXT(qh_ptr);
  38083. + continue;
  38084. + } else {
  38085. + qh->nak_frame = 0xFFFF;
  38086. + }
  38087. + }
  38088. + }
  38089. +
  38090. + if (microframe_schedule) {
  38091. + DWC_SPINLOCK_IRQSAVE(channel_lock, &flags);
  38092. + if (hcd->available_host_channels < 1) {
  38093. + DWC_SPINUNLOCK_IRQRESTORE(channel_lock, flags);
  38094. + break;
  38095. + }
  38096. + hcd->available_host_channels--;
  38097. + DWC_SPINUNLOCK_IRQRESTORE(channel_lock, flags);
  38098. +#ifdef DEBUG_HOST_CHANNELS
  38099. + last_sel_trans_num_nonper_scheduled++;
  38100. +#endif /* DEBUG_HOST_CHANNELS */
  38101. + }
  38102. +
  38103. + assign_and_init_hc(hcd, qh);
  38104. +
  38105. + /*
  38106. + * Move the QH from the non-periodic inactive schedule to the
  38107. + * non-periodic active schedule.
  38108. + */
  38109. + qh_ptr = DWC_LIST_NEXT(qh_ptr);
  38110. + DWC_SPINLOCK_IRQSAVE(channel_lock, &flags);
  38111. + DWC_LIST_MOVE_HEAD(&hcd->non_periodic_sched_active,
  38112. + &qh->qh_list_entry);
  38113. + DWC_SPINUNLOCK_IRQRESTORE(channel_lock, flags);
  38114. +
  38115. +
  38116. + if (!microframe_schedule)
  38117. + hcd->non_periodic_channels++;
  38118. + }
  38119. + /* we moved a non-periodic QH to the active schedule. If the inactive queue is empty,
  38120. + * stop the FIQ from kicking us. We could potentially still have elements here if we
  38121. + * ran out of host channels.
  38122. + */
  38123. + if (fiq_enable) {
  38124. + if (DWC_LIST_EMPTY(&hcd->non_periodic_sched_inactive)) {
  38125. + hcd->fiq_state->kick_np_queues = 0;
  38126. + } else {
  38127. + /* For each entry remaining in the NP inactive queue,
  38128. + * if this a NAK'd retransmit then don't set the kick flag.
  38129. + */
  38130. + if(nak_holdoff) {
  38131. + DWC_LIST_FOREACH(qh_ptr, &hcd->non_periodic_sched_inactive) {
  38132. + qh = DWC_LIST_ENTRY(qh_ptr, dwc_otg_qh_t, qh_list_entry);
  38133. + if (qh->nak_frame == 0xFFFF) {
  38134. + hcd->fiq_state->kick_np_queues = 1;
  38135. + }
  38136. + }
  38137. + }
  38138. + }
  38139. + }
  38140. + if(!DWC_LIST_EMPTY(&hcd->periodic_sched_assigned))
  38141. + ret_val |= DWC_OTG_TRANSACTION_PERIODIC;
  38142. +
  38143. + if(!DWC_LIST_EMPTY(&hcd->non_periodic_sched_active))
  38144. + ret_val |= DWC_OTG_TRANSACTION_NON_PERIODIC;
  38145. +
  38146. +
  38147. +#ifdef DEBUG_HOST_CHANNELS
  38148. + last_sel_trans_num_avail_hc_at_end = hcd->available_host_channels;
  38149. +#endif /* DEBUG_HOST_CHANNELS */
  38150. + return ret_val;
  38151. +}
  38152. +
  38153. +/**
  38154. + * Attempts to queue a single transaction request for a host channel
  38155. + * associated with either a periodic or non-periodic transfer. This function
  38156. + * assumes that there is space available in the appropriate request queue. For
  38157. + * an OUT transfer or SETUP transaction in Slave mode, it checks whether space
  38158. + * is available in the appropriate Tx FIFO.
  38159. + *
  38160. + * @param hcd The HCD state structure.
  38161. + * @param hc Host channel descriptor associated with either a periodic or
  38162. + * non-periodic transfer.
  38163. + * @param fifo_dwords_avail Number of DWORDs available in the periodic Tx
  38164. + * FIFO for periodic transfers or the non-periodic Tx FIFO for non-periodic
  38165. + * transfers.
  38166. + *
  38167. + * @return 1 if a request is queued and more requests may be needed to
  38168. + * complete the transfer, 0 if no more requests are required for this
  38169. + * transfer, -1 if there is insufficient space in the Tx FIFO.
  38170. + */
  38171. +static int queue_transaction(dwc_otg_hcd_t * hcd,
  38172. + dwc_hc_t * hc, uint16_t fifo_dwords_avail)
  38173. +{
  38174. + int retval;
  38175. +
  38176. + if (hcd->core_if->dma_enable) {
  38177. + if (hcd->core_if->dma_desc_enable) {
  38178. + if (!hc->xfer_started
  38179. + || (hc->ep_type == DWC_OTG_EP_TYPE_ISOC)) {
  38180. + dwc_otg_hcd_start_xfer_ddma(hcd, hc->qh);
  38181. + hc->qh->ping_state = 0;
  38182. + }
  38183. + } else if (!hc->xfer_started) {
  38184. + if (fiq_fsm_enable && hc->error_state) {
  38185. + hcd->fiq_state->channel[hc->hc_num].nr_errors =
  38186. + DWC_CIRCLEQ_FIRST(&hc->qh->qtd_list)->error_count;
  38187. + hcd->fiq_state->channel[hc->hc_num].fsm =
  38188. + FIQ_PASSTHROUGH_ERRORSTATE;
  38189. + }
  38190. + dwc_otg_hc_start_transfer(hcd->core_if, hc);
  38191. + hc->qh->ping_state = 0;
  38192. + }
  38193. + retval = 0;
  38194. + } else if (hc->halt_pending) {
  38195. + /* Don't queue a request if the channel has been halted. */
  38196. + retval = 0;
  38197. + } else if (hc->halt_on_queue) {
  38198. + dwc_otg_hc_halt(hcd->core_if, hc, hc->halt_status);
  38199. + retval = 0;
  38200. + } else if (hc->do_ping) {
  38201. + if (!hc->xfer_started) {
  38202. + dwc_otg_hc_start_transfer(hcd->core_if, hc);
  38203. + }
  38204. + retval = 0;
  38205. + } else if (!hc->ep_is_in || hc->data_pid_start == DWC_OTG_HC_PID_SETUP) {
  38206. + if ((fifo_dwords_avail * 4) >= hc->max_packet) {
  38207. + if (!hc->xfer_started) {
  38208. + dwc_otg_hc_start_transfer(hcd->core_if, hc);
  38209. + retval = 1;
  38210. + } else {
  38211. + retval =
  38212. + dwc_otg_hc_continue_transfer(hcd->core_if,
  38213. + hc);
  38214. + }
  38215. + } else {
  38216. + retval = -1;
  38217. + }
  38218. + } else {
  38219. + if (!hc->xfer_started) {
  38220. + dwc_otg_hc_start_transfer(hcd->core_if, hc);
  38221. + retval = 1;
  38222. + } else {
  38223. + retval = dwc_otg_hc_continue_transfer(hcd->core_if, hc);
  38224. + }
  38225. + }
  38226. +
  38227. + return retval;
  38228. +}
  38229. +
  38230. +/**
  38231. + * Processes periodic channels for the next frame and queues transactions for
  38232. + * these channels to the DWC_otg controller. After queueing transactions, the
  38233. + * Periodic Tx FIFO Empty interrupt is enabled if there are more transactions
  38234. + * to queue as Periodic Tx FIFO or request queue space becomes available.
  38235. + * Otherwise, the Periodic Tx FIFO Empty interrupt is disabled.
  38236. + */
  38237. +static void process_periodic_channels(dwc_otg_hcd_t * hcd)
  38238. +{
  38239. + hptxsts_data_t tx_status;
  38240. + dwc_list_link_t *qh_ptr;
  38241. + dwc_otg_qh_t *qh;
  38242. + int status = 0;
  38243. + int no_queue_space = 0;
  38244. + int no_fifo_space = 0;
  38245. +
  38246. + dwc_otg_host_global_regs_t *host_regs;
  38247. + host_regs = hcd->core_if->host_if->host_global_regs;
  38248. +
  38249. + DWC_DEBUGPL(DBG_HCDV, "Queue periodic transactions\n");
  38250. +#ifdef DEBUG
  38251. + tx_status.d32 = DWC_READ_REG32(&host_regs->hptxsts);
  38252. + DWC_DEBUGPL(DBG_HCDV,
  38253. + " P Tx Req Queue Space Avail (before queue): %d\n",
  38254. + tx_status.b.ptxqspcavail);
  38255. + DWC_DEBUGPL(DBG_HCDV, " P Tx FIFO Space Avail (before queue): %d\n",
  38256. + tx_status.b.ptxfspcavail);
  38257. +#endif
  38258. +
  38259. + qh_ptr = hcd->periodic_sched_assigned.next;
  38260. + while (qh_ptr != &hcd->periodic_sched_assigned) {
  38261. + tx_status.d32 = DWC_READ_REG32(&host_regs->hptxsts);
  38262. + if (tx_status.b.ptxqspcavail == 0) {
  38263. + no_queue_space = 1;
  38264. + break;
  38265. + }
  38266. +
  38267. + qh = DWC_LIST_ENTRY(qh_ptr, dwc_otg_qh_t, qh_list_entry);
  38268. +
  38269. + // Do not send a split start transaction any later than frame .6
  38270. + // Note, we have to schedule a periodic in .5 to make it go in .6
  38271. + if(fiq_fsm_enable && qh->do_split && ((dwc_otg_hcd_get_frame_number(hcd) + 1) & 7) > 6)
  38272. + {
  38273. + qh_ptr = qh_ptr->next;
  38274. + hcd->fiq_state->next_sched_frame = dwc_otg_hcd_get_frame_number(hcd) | 7;
  38275. + continue;
  38276. + }
  38277. +
  38278. + if (fiq_fsm_enable && fiq_fsm_transaction_suitable(qh)) {
  38279. + if (qh->do_split)
  38280. + fiq_fsm_queue_split_transaction(hcd, qh);
  38281. + else
  38282. + fiq_fsm_queue_isoc_transaction(hcd, qh);
  38283. + } else {
  38284. +
  38285. + /*
  38286. + * Set a flag if we're queueing high-bandwidth in slave mode.
  38287. + * The flag prevents any halts to get into the request queue in
  38288. + * the middle of multiple high-bandwidth packets getting queued.
  38289. + */
  38290. + if (!hcd->core_if->dma_enable && qh->channel->multi_count > 1) {
  38291. + hcd->core_if->queuing_high_bandwidth = 1;
  38292. + }
  38293. + status = queue_transaction(hcd, qh->channel,
  38294. + tx_status.b.ptxfspcavail);
  38295. + if (status < 0) {
  38296. + no_fifo_space = 1;
  38297. + break;
  38298. + }
  38299. + }
  38300. +
  38301. + /*
  38302. + * In Slave mode, stay on the current transfer until there is
  38303. + * nothing more to do or the high-bandwidth request count is
  38304. + * reached. In DMA mode, only need to queue one request. The
  38305. + * controller automatically handles multiple packets for
  38306. + * high-bandwidth transfers.
  38307. + */
  38308. + if (hcd->core_if->dma_enable || status == 0 ||
  38309. + qh->channel->requests == qh->channel->multi_count) {
  38310. + qh_ptr = qh_ptr->next;
  38311. + /*
  38312. + * Move the QH from the periodic assigned schedule to
  38313. + * the periodic queued schedule.
  38314. + */
  38315. + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_queued,
  38316. + &qh->qh_list_entry);
  38317. +
  38318. + /* done queuing high bandwidth */
  38319. + hcd->core_if->queuing_high_bandwidth = 0;
  38320. + }
  38321. + }
  38322. +
  38323. + if (!hcd->core_if->dma_enable) {
  38324. + dwc_otg_core_global_regs_t *global_regs;
  38325. + gintmsk_data_t intr_mask = {.d32 = 0 };
  38326. +
  38327. + global_regs = hcd->core_if->core_global_regs;
  38328. + intr_mask.b.ptxfempty = 1;
  38329. +#ifdef DEBUG
  38330. + tx_status.d32 = DWC_READ_REG32(&host_regs->hptxsts);
  38331. + DWC_DEBUGPL(DBG_HCDV,
  38332. + " P Tx Req Queue Space Avail (after queue): %d\n",
  38333. + tx_status.b.ptxqspcavail);
  38334. + DWC_DEBUGPL(DBG_HCDV,
  38335. + " P Tx FIFO Space Avail (after queue): %d\n",
  38336. + tx_status.b.ptxfspcavail);
  38337. +#endif
  38338. + if (!DWC_LIST_EMPTY(&hcd->periodic_sched_assigned) ||
  38339. + no_queue_space || no_fifo_space) {
  38340. + /*
  38341. + * May need to queue more transactions as the request
  38342. + * queue or Tx FIFO empties. Enable the periodic Tx
  38343. + * FIFO empty interrupt. (Always use the half-empty
  38344. + * level to ensure that new requests are loaded as
  38345. + * soon as possible.)
  38346. + */
  38347. + DWC_MODIFY_REG32(&global_regs->gintmsk, 0,
  38348. + intr_mask.d32);
  38349. + } else {
  38350. + /*
  38351. + * Disable the Tx FIFO empty interrupt since there are
  38352. + * no more transactions that need to be queued right
  38353. + * now. This function is called from interrupt
  38354. + * handlers to queue more transactions as transfer
  38355. + * states change.
  38356. + */
  38357. + DWC_MODIFY_REG32(&global_regs->gintmsk, intr_mask.d32,
  38358. + 0);
  38359. + }
  38360. + }
  38361. +}
  38362. +
  38363. +/**
  38364. + * Processes active non-periodic channels and queues transactions for these
  38365. + * channels to the DWC_otg controller. After queueing transactions, the NP Tx
  38366. + * FIFO Empty interrupt is enabled if there are more transactions to queue as
  38367. + * NP Tx FIFO or request queue space becomes available. Otherwise, the NP Tx
  38368. + * FIFO Empty interrupt is disabled.
  38369. + */
  38370. +static void process_non_periodic_channels(dwc_otg_hcd_t * hcd)
  38371. +{
  38372. + gnptxsts_data_t tx_status;
  38373. + dwc_list_link_t *orig_qh_ptr;
  38374. + dwc_otg_qh_t *qh;
  38375. + int status;
  38376. + int no_queue_space = 0;
  38377. + int no_fifo_space = 0;
  38378. + int more_to_do = 0;
  38379. +
  38380. + dwc_otg_core_global_regs_t *global_regs =
  38381. + hcd->core_if->core_global_regs;
  38382. +
  38383. + DWC_DEBUGPL(DBG_HCDV, "Queue non-periodic transactions\n");
  38384. +#ifdef DEBUG
  38385. + tx_status.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
  38386. + DWC_DEBUGPL(DBG_HCDV,
  38387. + " NP Tx Req Queue Space Avail (before queue): %d\n",
  38388. + tx_status.b.nptxqspcavail);
  38389. + DWC_DEBUGPL(DBG_HCDV, " NP Tx FIFO Space Avail (before queue): %d\n",
  38390. + tx_status.b.nptxfspcavail);
  38391. +#endif
  38392. + /*
  38393. + * Keep track of the starting point. Skip over the start-of-list
  38394. + * entry.
  38395. + */
  38396. + if (hcd->non_periodic_qh_ptr == &hcd->non_periodic_sched_active) {
  38397. + hcd->non_periodic_qh_ptr = hcd->non_periodic_qh_ptr->next;
  38398. + }
  38399. + orig_qh_ptr = hcd->non_periodic_qh_ptr;
  38400. +
  38401. + /*
  38402. + * Process once through the active list or until no more space is
  38403. + * available in the request queue or the Tx FIFO.
  38404. + */
  38405. + do {
  38406. + tx_status.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
  38407. + if (!hcd->core_if->dma_enable && tx_status.b.nptxqspcavail == 0) {
  38408. + no_queue_space = 1;
  38409. + break;
  38410. + }
  38411. +
  38412. + qh = DWC_LIST_ENTRY(hcd->non_periodic_qh_ptr, dwc_otg_qh_t,
  38413. + qh_list_entry);
  38414. +
  38415. + if(fiq_fsm_enable && fiq_fsm_transaction_suitable(qh)) {
  38416. + fiq_fsm_queue_split_transaction(hcd, qh);
  38417. + } else {
  38418. + status = queue_transaction(hcd, qh->channel,
  38419. + tx_status.b.nptxfspcavail);
  38420. +
  38421. + if (status > 0) {
  38422. + more_to_do = 1;
  38423. + } else if (status < 0) {
  38424. + no_fifo_space = 1;
  38425. + break;
  38426. + }
  38427. + }
  38428. + /* Advance to next QH, skipping start-of-list entry. */
  38429. + hcd->non_periodic_qh_ptr = hcd->non_periodic_qh_ptr->next;
  38430. + if (hcd->non_periodic_qh_ptr == &hcd->non_periodic_sched_active) {
  38431. + hcd->non_periodic_qh_ptr =
  38432. + hcd->non_periodic_qh_ptr->next;
  38433. + }
  38434. +
  38435. + } while (hcd->non_periodic_qh_ptr != orig_qh_ptr);
  38436. +
  38437. + if (!hcd->core_if->dma_enable) {
  38438. + gintmsk_data_t intr_mask = {.d32 = 0 };
  38439. + intr_mask.b.nptxfempty = 1;
  38440. +
  38441. +#ifdef DEBUG
  38442. + tx_status.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
  38443. + DWC_DEBUGPL(DBG_HCDV,
  38444. + " NP Tx Req Queue Space Avail (after queue): %d\n",
  38445. + tx_status.b.nptxqspcavail);
  38446. + DWC_DEBUGPL(DBG_HCDV,
  38447. + " NP Tx FIFO Space Avail (after queue): %d\n",
  38448. + tx_status.b.nptxfspcavail);
  38449. +#endif
  38450. + if (more_to_do || no_queue_space || no_fifo_space) {
  38451. + /*
  38452. + * May need to queue more transactions as the request
  38453. + * queue or Tx FIFO empties. Enable the non-periodic
  38454. + * Tx FIFO empty interrupt. (Always use the half-empty
  38455. + * level to ensure that new requests are loaded as
  38456. + * soon as possible.)
  38457. + */
  38458. + DWC_MODIFY_REG32(&global_regs->gintmsk, 0,
  38459. + intr_mask.d32);
  38460. + } else {
  38461. + /*
  38462. + * Disable the Tx FIFO empty interrupt since there are
  38463. + * no more transactions that need to be queued right
  38464. + * now. This function is called from interrupt
  38465. + * handlers to queue more transactions as transfer
  38466. + * states change.
  38467. + */
  38468. + DWC_MODIFY_REG32(&global_regs->gintmsk, intr_mask.d32,
  38469. + 0);
  38470. + }
  38471. + }
  38472. +}
  38473. +
  38474. +/**
  38475. + * This function processes the currently active host channels and queues
  38476. + * transactions for these channels to the DWC_otg controller. It is called
  38477. + * from HCD interrupt handler functions.
  38478. + *
  38479. + * @param hcd The HCD state structure.
  38480. + * @param tr_type The type(s) of transactions to queue (non-periodic,
  38481. + * periodic, or both).
  38482. + */
  38483. +void dwc_otg_hcd_queue_transactions(dwc_otg_hcd_t * hcd,
  38484. + dwc_otg_transaction_type_e tr_type)
  38485. +{
  38486. +#ifdef DEBUG_SOF
  38487. + DWC_DEBUGPL(DBG_HCD, "Queue Transactions\n");
  38488. +#endif
  38489. + /* Process host channels associated with periodic transfers. */
  38490. + if ((tr_type == DWC_OTG_TRANSACTION_PERIODIC ||
  38491. + tr_type == DWC_OTG_TRANSACTION_ALL) &&
  38492. + !DWC_LIST_EMPTY(&hcd->periodic_sched_assigned)) {
  38493. +
  38494. + process_periodic_channels(hcd);
  38495. + }
  38496. +
  38497. + /* Process host channels associated with non-periodic transfers. */
  38498. + if (tr_type == DWC_OTG_TRANSACTION_NON_PERIODIC ||
  38499. + tr_type == DWC_OTG_TRANSACTION_ALL) {
  38500. + if (!DWC_LIST_EMPTY(&hcd->non_periodic_sched_active)) {
  38501. + process_non_periodic_channels(hcd);
  38502. + } else {
  38503. + /*
  38504. + * Ensure NP Tx FIFO empty interrupt is disabled when
  38505. + * there are no non-periodic transfers to process.
  38506. + */
  38507. + gintmsk_data_t gintmsk = {.d32 = 0 };
  38508. + gintmsk.b.nptxfempty = 1;
  38509. +
  38510. + if (fiq_enable) {
  38511. + local_fiq_disable();
  38512. + fiq_fsm_spin_lock(&hcd->fiq_state->lock);
  38513. + DWC_MODIFY_REG32(&hcd->core_if->core_global_regs->gintmsk, gintmsk.d32, 0);
  38514. + fiq_fsm_spin_unlock(&hcd->fiq_state->lock);
  38515. + local_fiq_enable();
  38516. + } else {
  38517. + DWC_MODIFY_REG32(&hcd->core_if->core_global_regs->gintmsk, gintmsk.d32, 0);
  38518. + }
  38519. + }
  38520. + }
  38521. +}
  38522. +
  38523. +#ifdef DWC_HS_ELECT_TST
  38524. +/*
  38525. + * Quick and dirty hack to implement the HS Electrical Test
  38526. + * SINGLE_STEP_GET_DEVICE_DESCRIPTOR feature.
  38527. + *
  38528. + * This code was copied from our userspace app "hset". It sends a
  38529. + * Get Device Descriptor control sequence in two parts, first the
  38530. + * Setup packet by itself, followed some time later by the In and
  38531. + * Ack packets. Rather than trying to figure out how to add this
  38532. + * functionality to the normal driver code, we just hijack the
  38533. + * hardware, using these two function to drive the hardware
  38534. + * directly.
  38535. + */
  38536. +
  38537. +static dwc_otg_core_global_regs_t *global_regs;
  38538. +static dwc_otg_host_global_regs_t *hc_global_regs;
  38539. +static dwc_otg_hc_regs_t *hc_regs;
  38540. +static uint32_t *data_fifo;
  38541. +
  38542. +static void do_setup(void)
  38543. +{
  38544. + gintsts_data_t gintsts;
  38545. + hctsiz_data_t hctsiz;
  38546. + hcchar_data_t hcchar;
  38547. + haint_data_t haint;
  38548. + hcint_data_t hcint;
  38549. +
  38550. + /* Enable HAINTs */
  38551. + DWC_WRITE_REG32(&hc_global_regs->haintmsk, 0x0001);
  38552. +
  38553. + /* Enable HCINTs */
  38554. + DWC_WRITE_REG32(&hc_regs->hcintmsk, 0x04a3);
  38555. +
  38556. + /* Read GINTSTS */
  38557. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  38558. +
  38559. + /* Read HAINT */
  38560. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  38561. +
  38562. + /* Read HCINT */
  38563. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  38564. +
  38565. + /* Read HCCHAR */
  38566. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  38567. +
  38568. + /* Clear HCINT */
  38569. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  38570. +
  38571. + /* Clear HAINT */
  38572. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  38573. +
  38574. + /* Clear GINTSTS */
  38575. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  38576. +
  38577. + /* Read GINTSTS */
  38578. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  38579. +
  38580. + /*
  38581. + * Send Setup packet (Get Device Descriptor)
  38582. + */
  38583. +
  38584. + /* Make sure channel is disabled */
  38585. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  38586. + if (hcchar.b.chen) {
  38587. + hcchar.b.chdis = 1;
  38588. +// hcchar.b.chen = 1;
  38589. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  38590. + //sleep(1);
  38591. + dwc_mdelay(1000);
  38592. +
  38593. + /* Read GINTSTS */
  38594. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  38595. +
  38596. + /* Read HAINT */
  38597. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  38598. +
  38599. + /* Read HCINT */
  38600. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  38601. +
  38602. + /* Read HCCHAR */
  38603. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  38604. +
  38605. + /* Clear HCINT */
  38606. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  38607. +
  38608. + /* Clear HAINT */
  38609. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  38610. +
  38611. + /* Clear GINTSTS */
  38612. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  38613. +
  38614. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  38615. + }
  38616. +
  38617. + /* Set HCTSIZ */
  38618. + hctsiz.d32 = 0;
  38619. + hctsiz.b.xfersize = 8;
  38620. + hctsiz.b.pktcnt = 1;
  38621. + hctsiz.b.pid = DWC_OTG_HC_PID_SETUP;
  38622. + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
  38623. +
  38624. + /* Set HCCHAR */
  38625. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  38626. + hcchar.b.eptype = DWC_OTG_EP_TYPE_CONTROL;
  38627. + hcchar.b.epdir = 0;
  38628. + hcchar.b.epnum = 0;
  38629. + hcchar.b.mps = 8;
  38630. + hcchar.b.chen = 1;
  38631. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  38632. +
  38633. + /* Fill FIFO with Setup data for Get Device Descriptor */
  38634. + data_fifo = (uint32_t *) ((char *)global_regs + 0x1000);
  38635. + DWC_WRITE_REG32(data_fifo++, 0x01000680);
  38636. + DWC_WRITE_REG32(data_fifo++, 0x00080000);
  38637. +
  38638. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  38639. +
  38640. + /* Wait for host channel interrupt */
  38641. + do {
  38642. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  38643. + } while (gintsts.b.hcintr == 0);
  38644. +
  38645. + /* Disable HCINTs */
  38646. + DWC_WRITE_REG32(&hc_regs->hcintmsk, 0x0000);
  38647. +
  38648. + /* Disable HAINTs */
  38649. + DWC_WRITE_REG32(&hc_global_regs->haintmsk, 0x0000);
  38650. +
  38651. + /* Read HAINT */
  38652. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  38653. +
  38654. + /* Read HCINT */
  38655. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  38656. +
  38657. + /* Read HCCHAR */
  38658. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  38659. +
  38660. + /* Clear HCINT */
  38661. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  38662. +
  38663. + /* Clear HAINT */
  38664. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  38665. +
  38666. + /* Clear GINTSTS */
  38667. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  38668. +
  38669. + /* Read GINTSTS */
  38670. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  38671. +}
  38672. +
  38673. +static void do_in_ack(void)
  38674. +{
  38675. + gintsts_data_t gintsts;
  38676. + hctsiz_data_t hctsiz;
  38677. + hcchar_data_t hcchar;
  38678. + haint_data_t haint;
  38679. + hcint_data_t hcint;
  38680. + host_grxsts_data_t grxsts;
  38681. +
  38682. + /* Enable HAINTs */
  38683. + DWC_WRITE_REG32(&hc_global_regs->haintmsk, 0x0001);
  38684. +
  38685. + /* Enable HCINTs */
  38686. + DWC_WRITE_REG32(&hc_regs->hcintmsk, 0x04a3);
  38687. +
  38688. + /* Read GINTSTS */
  38689. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  38690. +
  38691. + /* Read HAINT */
  38692. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  38693. +
  38694. + /* Read HCINT */
  38695. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  38696. +
  38697. + /* Read HCCHAR */
  38698. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  38699. +
  38700. + /* Clear HCINT */
  38701. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  38702. +
  38703. + /* Clear HAINT */
  38704. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  38705. +
  38706. + /* Clear GINTSTS */
  38707. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  38708. +
  38709. + /* Read GINTSTS */
  38710. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  38711. +
  38712. + /*
  38713. + * Receive Control In packet
  38714. + */
  38715. +
  38716. + /* Make sure channel is disabled */
  38717. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  38718. + if (hcchar.b.chen) {
  38719. + hcchar.b.chdis = 1;
  38720. + hcchar.b.chen = 1;
  38721. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  38722. + //sleep(1);
  38723. + dwc_mdelay(1000);
  38724. +
  38725. + /* Read GINTSTS */
  38726. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  38727. +
  38728. + /* Read HAINT */
  38729. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  38730. +
  38731. + /* Read HCINT */
  38732. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  38733. +
  38734. + /* Read HCCHAR */
  38735. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  38736. +
  38737. + /* Clear HCINT */
  38738. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  38739. +
  38740. + /* Clear HAINT */
  38741. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  38742. +
  38743. + /* Clear GINTSTS */
  38744. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  38745. +
  38746. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  38747. + }
  38748. +
  38749. + /* Set HCTSIZ */
  38750. + hctsiz.d32 = 0;
  38751. + hctsiz.b.xfersize = 8;
  38752. + hctsiz.b.pktcnt = 1;
  38753. + hctsiz.b.pid = DWC_OTG_HC_PID_DATA1;
  38754. + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
  38755. +
  38756. + /* Set HCCHAR */
  38757. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  38758. + hcchar.b.eptype = DWC_OTG_EP_TYPE_CONTROL;
  38759. + hcchar.b.epdir = 1;
  38760. + hcchar.b.epnum = 0;
  38761. + hcchar.b.mps = 8;
  38762. + hcchar.b.chen = 1;
  38763. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  38764. +
  38765. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  38766. +
  38767. + /* Wait for receive status queue interrupt */
  38768. + do {
  38769. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  38770. + } while (gintsts.b.rxstsqlvl == 0);
  38771. +
  38772. + /* Read RXSTS */
  38773. + grxsts.d32 = DWC_READ_REG32(&global_regs->grxstsp);
  38774. +
  38775. + /* Clear RXSTSQLVL in GINTSTS */
  38776. + gintsts.d32 = 0;
  38777. + gintsts.b.rxstsqlvl = 1;
  38778. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  38779. +
  38780. + switch (grxsts.b.pktsts) {
  38781. + case DWC_GRXSTS_PKTSTS_IN:
  38782. + /* Read the data into the host buffer */
  38783. + if (grxsts.b.bcnt > 0) {
  38784. + int i;
  38785. + int word_count = (grxsts.b.bcnt + 3) / 4;
  38786. +
  38787. + data_fifo = (uint32_t *) ((char *)global_regs + 0x1000);
  38788. +
  38789. + for (i = 0; i < word_count; i++) {
  38790. + (void)DWC_READ_REG32(data_fifo++);
  38791. + }
  38792. + }
  38793. + break;
  38794. +
  38795. + default:
  38796. + break;
  38797. + }
  38798. +
  38799. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  38800. +
  38801. + /* Wait for receive status queue interrupt */
  38802. + do {
  38803. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  38804. + } while (gintsts.b.rxstsqlvl == 0);
  38805. +
  38806. + /* Read RXSTS */
  38807. + grxsts.d32 = DWC_READ_REG32(&global_regs->grxstsp);
  38808. +
  38809. + /* Clear RXSTSQLVL in GINTSTS */
  38810. + gintsts.d32 = 0;
  38811. + gintsts.b.rxstsqlvl = 1;
  38812. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  38813. +
  38814. + switch (grxsts.b.pktsts) {
  38815. + case DWC_GRXSTS_PKTSTS_IN_XFER_COMP:
  38816. + break;
  38817. +
  38818. + default:
  38819. + break;
  38820. + }
  38821. +
  38822. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  38823. +
  38824. + /* Wait for host channel interrupt */
  38825. + do {
  38826. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  38827. + } while (gintsts.b.hcintr == 0);
  38828. +
  38829. + /* Read HAINT */
  38830. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  38831. +
  38832. + /* Read HCINT */
  38833. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  38834. +
  38835. + /* Read HCCHAR */
  38836. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  38837. +
  38838. + /* Clear HCINT */
  38839. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  38840. +
  38841. + /* Clear HAINT */
  38842. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  38843. +
  38844. + /* Clear GINTSTS */
  38845. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  38846. +
  38847. + /* Read GINTSTS */
  38848. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  38849. +
  38850. +// usleep(100000);
  38851. +// mdelay(100);
  38852. + dwc_mdelay(1);
  38853. +
  38854. + /*
  38855. + * Send handshake packet
  38856. + */
  38857. +
  38858. + /* Read HAINT */
  38859. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  38860. +
  38861. + /* Read HCINT */
  38862. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  38863. +
  38864. + /* Read HCCHAR */
  38865. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  38866. +
  38867. + /* Clear HCINT */
  38868. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  38869. +
  38870. + /* Clear HAINT */
  38871. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  38872. +
  38873. + /* Clear GINTSTS */
  38874. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  38875. +
  38876. + /* Read GINTSTS */
  38877. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  38878. +
  38879. + /* Make sure channel is disabled */
  38880. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  38881. + if (hcchar.b.chen) {
  38882. + hcchar.b.chdis = 1;
  38883. + hcchar.b.chen = 1;
  38884. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  38885. + //sleep(1);
  38886. + dwc_mdelay(1000);
  38887. +
  38888. + /* Read GINTSTS */
  38889. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  38890. +
  38891. + /* Read HAINT */
  38892. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  38893. +
  38894. + /* Read HCINT */
  38895. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  38896. +
  38897. + /* Read HCCHAR */
  38898. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  38899. +
  38900. + /* Clear HCINT */
  38901. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  38902. +
  38903. + /* Clear HAINT */
  38904. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  38905. +
  38906. + /* Clear GINTSTS */
  38907. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  38908. +
  38909. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  38910. + }
  38911. +
  38912. + /* Set HCTSIZ */
  38913. + hctsiz.d32 = 0;
  38914. + hctsiz.b.xfersize = 0;
  38915. + hctsiz.b.pktcnt = 1;
  38916. + hctsiz.b.pid = DWC_OTG_HC_PID_DATA1;
  38917. + DWC_WRITE_REG32(&hc_regs->hctsiz, hctsiz.d32);
  38918. +
  38919. + /* Set HCCHAR */
  38920. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  38921. + hcchar.b.eptype = DWC_OTG_EP_TYPE_CONTROL;
  38922. + hcchar.b.epdir = 0;
  38923. + hcchar.b.epnum = 0;
  38924. + hcchar.b.mps = 8;
  38925. + hcchar.b.chen = 1;
  38926. + DWC_WRITE_REG32(&hc_regs->hcchar, hcchar.d32);
  38927. +
  38928. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  38929. +
  38930. + /* Wait for host channel interrupt */
  38931. + do {
  38932. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  38933. + } while (gintsts.b.hcintr == 0);
  38934. +
  38935. + /* Disable HCINTs */
  38936. + DWC_WRITE_REG32(&hc_regs->hcintmsk, 0x0000);
  38937. +
  38938. + /* Disable HAINTs */
  38939. + DWC_WRITE_REG32(&hc_global_regs->haintmsk, 0x0000);
  38940. +
  38941. + /* Read HAINT */
  38942. + haint.d32 = DWC_READ_REG32(&hc_global_regs->haint);
  38943. +
  38944. + /* Read HCINT */
  38945. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  38946. +
  38947. + /* Read HCCHAR */
  38948. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  38949. +
  38950. + /* Clear HCINT */
  38951. + DWC_WRITE_REG32(&hc_regs->hcint, hcint.d32);
  38952. +
  38953. + /* Clear HAINT */
  38954. + DWC_WRITE_REG32(&hc_global_regs->haint, haint.d32);
  38955. +
  38956. + /* Clear GINTSTS */
  38957. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  38958. +
  38959. + /* Read GINTSTS */
  38960. + gintsts.d32 = DWC_READ_REG32(&global_regs->gintsts);
  38961. +}
  38962. +#endif
  38963. +
  38964. +/** Handles hub class-specific requests. */
  38965. +int dwc_otg_hcd_hub_control(dwc_otg_hcd_t * dwc_otg_hcd,
  38966. + uint16_t typeReq,
  38967. + uint16_t wValue,
  38968. + uint16_t wIndex, uint8_t * buf, uint16_t wLength)
  38969. +{
  38970. + int retval = 0;
  38971. +
  38972. + dwc_otg_core_if_t *core_if = dwc_otg_hcd->core_if;
  38973. + usb_hub_descriptor_t *hub_desc;
  38974. + hprt0_data_t hprt0 = {.d32 = 0 };
  38975. +
  38976. + uint32_t port_status;
  38977. +
  38978. + switch (typeReq) {
  38979. + case UCR_CLEAR_HUB_FEATURE:
  38980. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  38981. + "ClearHubFeature 0x%x\n", wValue);
  38982. + switch (wValue) {
  38983. + case UHF_C_HUB_LOCAL_POWER:
  38984. + case UHF_C_HUB_OVER_CURRENT:
  38985. + /* Nothing required here */
  38986. + break;
  38987. + default:
  38988. + retval = -DWC_E_INVALID;
  38989. + DWC_ERROR("DWC OTG HCD - "
  38990. + "ClearHubFeature request %xh unknown\n",
  38991. + wValue);
  38992. + }
  38993. + break;
  38994. + case UCR_CLEAR_PORT_FEATURE:
  38995. +#ifdef CONFIG_USB_DWC_OTG_LPM
  38996. + if (wValue != UHF_PORT_L1)
  38997. +#endif
  38998. + if (!wIndex || wIndex > 1)
  38999. + goto error;
  39000. +
  39001. + switch (wValue) {
  39002. + case UHF_PORT_ENABLE:
  39003. + DWC_DEBUGPL(DBG_ANY, "DWC OTG HCD HUB CONTROL - "
  39004. + "ClearPortFeature USB_PORT_FEAT_ENABLE\n");
  39005. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  39006. + hprt0.b.prtena = 1;
  39007. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  39008. + break;
  39009. + case UHF_PORT_SUSPEND:
  39010. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  39011. + "ClearPortFeature USB_PORT_FEAT_SUSPEND\n");
  39012. +
  39013. + if (core_if->power_down == 2) {
  39014. + dwc_otg_host_hibernation_restore(core_if, 0, 0);
  39015. + } else {
  39016. + DWC_WRITE_REG32(core_if->pcgcctl, 0);
  39017. + dwc_mdelay(5);
  39018. +
  39019. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  39020. + hprt0.b.prtres = 1;
  39021. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  39022. + hprt0.b.prtsusp = 0;
  39023. + /* Clear Resume bit */
  39024. + dwc_mdelay(100);
  39025. + hprt0.b.prtres = 0;
  39026. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  39027. + }
  39028. + break;
  39029. +#ifdef CONFIG_USB_DWC_OTG_LPM
  39030. + case UHF_PORT_L1:
  39031. + {
  39032. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  39033. + glpmcfg_data_t lpmcfg = {.d32 = 0 };
  39034. +
  39035. + lpmcfg.d32 =
  39036. + DWC_READ_REG32(&core_if->
  39037. + core_global_regs->glpmcfg);
  39038. + lpmcfg.b.en_utmi_sleep = 0;
  39039. + lpmcfg.b.hird_thres &= (~(1 << 4));
  39040. + lpmcfg.b.prt_sleep_sts = 1;
  39041. + DWC_WRITE_REG32(&core_if->
  39042. + core_global_regs->glpmcfg,
  39043. + lpmcfg.d32);
  39044. +
  39045. + /* Clear Enbl_L1Gating bit. */
  39046. + pcgcctl.b.enbl_sleep_gating = 1;
  39047. + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32,
  39048. + 0);
  39049. +
  39050. + dwc_mdelay(5);
  39051. +
  39052. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  39053. + hprt0.b.prtres = 1;
  39054. + DWC_WRITE_REG32(core_if->host_if->hprt0,
  39055. + hprt0.d32);
  39056. + /* This bit will be cleared in wakeup interrupt handle */
  39057. + break;
  39058. + }
  39059. +#endif
  39060. + case UHF_PORT_POWER:
  39061. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  39062. + "ClearPortFeature USB_PORT_FEAT_POWER\n");
  39063. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  39064. + hprt0.b.prtpwr = 0;
  39065. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  39066. + break;
  39067. + case UHF_PORT_INDICATOR:
  39068. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  39069. + "ClearPortFeature USB_PORT_FEAT_INDICATOR\n");
  39070. + /* Port inidicator not supported */
  39071. + break;
  39072. + case UHF_C_PORT_CONNECTION:
  39073. + /* Clears drivers internal connect status change
  39074. + * flag */
  39075. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  39076. + "ClearPortFeature USB_PORT_FEAT_C_CONNECTION\n");
  39077. + dwc_otg_hcd->flags.b.port_connect_status_change = 0;
  39078. + break;
  39079. + case UHF_C_PORT_RESET:
  39080. + /* Clears the driver's internal Port Reset Change
  39081. + * flag */
  39082. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  39083. + "ClearPortFeature USB_PORT_FEAT_C_RESET\n");
  39084. + dwc_otg_hcd->flags.b.port_reset_change = 0;
  39085. + break;
  39086. + case UHF_C_PORT_ENABLE:
  39087. + /* Clears the driver's internal Port
  39088. + * Enable/Disable Change flag */
  39089. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  39090. + "ClearPortFeature USB_PORT_FEAT_C_ENABLE\n");
  39091. + dwc_otg_hcd->flags.b.port_enable_change = 0;
  39092. + break;
  39093. + case UHF_C_PORT_SUSPEND:
  39094. + /* Clears the driver's internal Port Suspend
  39095. + * Change flag, which is set when resume signaling on
  39096. + * the host port is complete */
  39097. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  39098. + "ClearPortFeature USB_PORT_FEAT_C_SUSPEND\n");
  39099. + dwc_otg_hcd->flags.b.port_suspend_change = 0;
  39100. + break;
  39101. +#ifdef CONFIG_USB_DWC_OTG_LPM
  39102. + case UHF_C_PORT_L1:
  39103. + dwc_otg_hcd->flags.b.port_l1_change = 0;
  39104. + break;
  39105. +#endif
  39106. + case UHF_C_PORT_OVER_CURRENT:
  39107. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  39108. + "ClearPortFeature USB_PORT_FEAT_C_OVER_CURRENT\n");
  39109. + dwc_otg_hcd->flags.b.port_over_current_change = 0;
  39110. + break;
  39111. + default:
  39112. + retval = -DWC_E_INVALID;
  39113. + DWC_ERROR("DWC OTG HCD - "
  39114. + "ClearPortFeature request %xh "
  39115. + "unknown or unsupported\n", wValue);
  39116. + }
  39117. + break;
  39118. + case UCR_GET_HUB_DESCRIPTOR:
  39119. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  39120. + "GetHubDescriptor\n");
  39121. + hub_desc = (usb_hub_descriptor_t *) buf;
  39122. + hub_desc->bDescLength = 9;
  39123. + hub_desc->bDescriptorType = 0x29;
  39124. + hub_desc->bNbrPorts = 1;
  39125. + USETW(hub_desc->wHubCharacteristics, 0x08);
  39126. + hub_desc->bPwrOn2PwrGood = 1;
  39127. + hub_desc->bHubContrCurrent = 0;
  39128. + hub_desc->DeviceRemovable[0] = 0;
  39129. + hub_desc->DeviceRemovable[1] = 0xff;
  39130. + break;
  39131. + case UCR_GET_HUB_STATUS:
  39132. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  39133. + "GetHubStatus\n");
  39134. + DWC_MEMSET(buf, 0, 4);
  39135. + break;
  39136. + case UCR_GET_PORT_STATUS:
  39137. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  39138. + "GetPortStatus wIndex = 0x%04x FLAGS=0x%08x\n",
  39139. + wIndex, dwc_otg_hcd->flags.d32);
  39140. + if (!wIndex || wIndex > 1)
  39141. + goto error;
  39142. +
  39143. + port_status = 0;
  39144. +
  39145. + if (dwc_otg_hcd->flags.b.port_connect_status_change)
  39146. + port_status |= (1 << UHF_C_PORT_CONNECTION);
  39147. +
  39148. + if (dwc_otg_hcd->flags.b.port_enable_change)
  39149. + port_status |= (1 << UHF_C_PORT_ENABLE);
  39150. +
  39151. + if (dwc_otg_hcd->flags.b.port_suspend_change)
  39152. + port_status |= (1 << UHF_C_PORT_SUSPEND);
  39153. +
  39154. + if (dwc_otg_hcd->flags.b.port_l1_change)
  39155. + port_status |= (1 << UHF_C_PORT_L1);
  39156. +
  39157. + if (dwc_otg_hcd->flags.b.port_reset_change) {
  39158. + port_status |= (1 << UHF_C_PORT_RESET);
  39159. + }
  39160. +
  39161. + if (dwc_otg_hcd->flags.b.port_over_current_change) {
  39162. + DWC_WARN("Overcurrent change detected\n");
  39163. + port_status |= (1 << UHF_C_PORT_OVER_CURRENT);
  39164. + }
  39165. +
  39166. + if (!dwc_otg_hcd->flags.b.port_connect_status) {
  39167. + /*
  39168. + * The port is disconnected, which means the core is
  39169. + * either in device mode or it soon will be. Just
  39170. + * return 0's for the remainder of the port status
  39171. + * since the port register can't be read if the core
  39172. + * is in device mode.
  39173. + */
  39174. + *((__le32 *) buf) = dwc_cpu_to_le32(&port_status);
  39175. + break;
  39176. + }
  39177. +
  39178. + hprt0.d32 = DWC_READ_REG32(core_if->host_if->hprt0);
  39179. + DWC_DEBUGPL(DBG_HCDV, " HPRT0: 0x%08x\n", hprt0.d32);
  39180. +
  39181. + if (hprt0.b.prtconnsts)
  39182. + port_status |= (1 << UHF_PORT_CONNECTION);
  39183. +
  39184. + if (hprt0.b.prtena)
  39185. + port_status |= (1 << UHF_PORT_ENABLE);
  39186. +
  39187. + if (hprt0.b.prtsusp)
  39188. + port_status |= (1 << UHF_PORT_SUSPEND);
  39189. +
  39190. + if (hprt0.b.prtovrcurract)
  39191. + port_status |= (1 << UHF_PORT_OVER_CURRENT);
  39192. +
  39193. + if (hprt0.b.prtrst)
  39194. + port_status |= (1 << UHF_PORT_RESET);
  39195. +
  39196. + if (hprt0.b.prtpwr)
  39197. + port_status |= (1 << UHF_PORT_POWER);
  39198. +
  39199. + if (hprt0.b.prtspd == DWC_HPRT0_PRTSPD_HIGH_SPEED)
  39200. + port_status |= (1 << UHF_PORT_HIGH_SPEED);
  39201. + else if (hprt0.b.prtspd == DWC_HPRT0_PRTSPD_LOW_SPEED)
  39202. + port_status |= (1 << UHF_PORT_LOW_SPEED);
  39203. +
  39204. + if (hprt0.b.prttstctl)
  39205. + port_status |= (1 << UHF_PORT_TEST);
  39206. + if (dwc_otg_get_lpm_portsleepstatus(dwc_otg_hcd->core_if)) {
  39207. + port_status |= (1 << UHF_PORT_L1);
  39208. + }
  39209. + /*
  39210. + For Synopsys HW emulation of Power down wkup_control asserts the
  39211. + hreset_n and prst_n on suspned. This causes the HPRT0 to be zero.
  39212. + We intentionally tell the software that port is in L2Suspend state.
  39213. + Only for STE.
  39214. + */
  39215. + if ((core_if->power_down == 2)
  39216. + && (core_if->hibernation_suspend == 1)) {
  39217. + port_status |= (1 << UHF_PORT_SUSPEND);
  39218. + }
  39219. + /* USB_PORT_FEAT_INDICATOR unsupported always 0 */
  39220. +
  39221. + *((__le32 *) buf) = dwc_cpu_to_le32(&port_status);
  39222. +
  39223. + break;
  39224. + case UCR_SET_HUB_FEATURE:
  39225. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  39226. + "SetHubFeature\n");
  39227. + /* No HUB features supported */
  39228. + break;
  39229. + case UCR_SET_PORT_FEATURE:
  39230. + if (wValue != UHF_PORT_TEST && (!wIndex || wIndex > 1))
  39231. + goto error;
  39232. +
  39233. + if (!dwc_otg_hcd->flags.b.port_connect_status) {
  39234. + /*
  39235. + * The port is disconnected, which means the core is
  39236. + * either in device mode or it soon will be. Just
  39237. + * return without doing anything since the port
  39238. + * register can't be written if the core is in device
  39239. + * mode.
  39240. + */
  39241. + break;
  39242. + }
  39243. +
  39244. + switch (wValue) {
  39245. + case UHF_PORT_SUSPEND:
  39246. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  39247. + "SetPortFeature - USB_PORT_FEAT_SUSPEND\n");
  39248. + if (dwc_otg_hcd_otg_port(dwc_otg_hcd) != wIndex) {
  39249. + goto error;
  39250. + }
  39251. + if (core_if->power_down == 2) {
  39252. + int timeout = 300;
  39253. + dwc_irqflags_t flags;
  39254. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  39255. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  39256. + gusbcfg_data_t gusbcfg = {.d32 = 0 };
  39257. +#ifdef DWC_DEV_SRPCAP
  39258. + int32_t otg_cap_param = core_if->core_params->otg_cap;
  39259. +#endif
  39260. + DWC_PRINTF("Preparing for complete power-off\n");
  39261. +
  39262. + /* Save registers before hibernation */
  39263. + dwc_otg_save_global_regs(core_if);
  39264. + dwc_otg_save_host_regs(core_if);
  39265. +
  39266. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  39267. + hprt0.b.prtsusp = 1;
  39268. + hprt0.b.prtena = 0;
  39269. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  39270. + /* Spin hprt0.b.prtsusp to became 1 */
  39271. + do {
  39272. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  39273. + if (hprt0.b.prtsusp) {
  39274. + break;
  39275. + }
  39276. + dwc_mdelay(1);
  39277. + } while (--timeout);
  39278. + if (!timeout) {
  39279. + DWC_WARN("Suspend wasn't genereted\n");
  39280. + }
  39281. + dwc_udelay(10);
  39282. +
  39283. + /*
  39284. + * We need to disable interrupts to prevent servicing of any IRQ
  39285. + * during going to hibernation
  39286. + */
  39287. + DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &flags);
  39288. + core_if->lx_state = DWC_OTG_L2;
  39289. +#ifdef DWC_DEV_SRPCAP
  39290. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  39291. + hprt0.b.prtpwr = 0;
  39292. + hprt0.b.prtena = 0;
  39293. + DWC_WRITE_REG32(core_if->host_if->hprt0,
  39294. + hprt0.d32);
  39295. +#endif
  39296. + gusbcfg.d32 =
  39297. + DWC_READ_REG32(&core_if->core_global_regs->
  39298. + gusbcfg);
  39299. + if (gusbcfg.b.ulpi_utmi_sel == 1) {
  39300. + /* ULPI interface */
  39301. + /* Suspend the Phy Clock */
  39302. + pcgcctl.d32 = 0;
  39303. + pcgcctl.b.stoppclk = 1;
  39304. + DWC_MODIFY_REG32(core_if->pcgcctl, 0,
  39305. + pcgcctl.d32);
  39306. + dwc_udelay(10);
  39307. + gpwrdn.b.pmuactv = 1;
  39308. + DWC_MODIFY_REG32(&core_if->
  39309. + core_global_regs->
  39310. + gpwrdn, 0, gpwrdn.d32);
  39311. + } else {
  39312. + /* UTMI+ Interface */
  39313. + gpwrdn.b.pmuactv = 1;
  39314. + DWC_MODIFY_REG32(&core_if->
  39315. + core_global_regs->
  39316. + gpwrdn, 0, gpwrdn.d32);
  39317. + dwc_udelay(10);
  39318. + pcgcctl.b.stoppclk = 1;
  39319. + DWC_MODIFY_REG32(core_if->pcgcctl, 0, pcgcctl.d32);
  39320. + dwc_udelay(10);
  39321. + }
  39322. +#ifdef DWC_DEV_SRPCAP
  39323. + gpwrdn.d32 = 0;
  39324. + gpwrdn.b.dis_vbus = 1;
  39325. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  39326. + gpwrdn, 0, gpwrdn.d32);
  39327. +#endif
  39328. + gpwrdn.d32 = 0;
  39329. + gpwrdn.b.pmuintsel = 1;
  39330. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  39331. + gpwrdn, 0, gpwrdn.d32);
  39332. + dwc_udelay(10);
  39333. +
  39334. + gpwrdn.d32 = 0;
  39335. +#ifdef DWC_DEV_SRPCAP
  39336. + gpwrdn.b.srp_det_msk = 1;
  39337. +#endif
  39338. + gpwrdn.b.disconn_det_msk = 1;
  39339. + gpwrdn.b.lnstchng_msk = 1;
  39340. + gpwrdn.b.sts_chngint_msk = 1;
  39341. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  39342. + gpwrdn, 0, gpwrdn.d32);
  39343. + dwc_udelay(10);
  39344. +
  39345. + /* Enable Power Down Clamp and all interrupts in GPWRDN */
  39346. + gpwrdn.d32 = 0;
  39347. + gpwrdn.b.pwrdnclmp = 1;
  39348. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  39349. + gpwrdn, 0, gpwrdn.d32);
  39350. + dwc_udelay(10);
  39351. +
  39352. + /* Switch off VDD */
  39353. + gpwrdn.d32 = 0;
  39354. + gpwrdn.b.pwrdnswtch = 1;
  39355. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  39356. + gpwrdn, 0, gpwrdn.d32);
  39357. +
  39358. +#ifdef DWC_DEV_SRPCAP
  39359. + if (otg_cap_param == DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE)
  39360. + {
  39361. + core_if->pwron_timer_started = 1;
  39362. + DWC_TIMER_SCHEDULE(core_if->pwron_timer, 6000 /* 6 secs */ );
  39363. + }
  39364. +#endif
  39365. + /* Save gpwrdn register for further usage if stschng interrupt */
  39366. + core_if->gr_backup->gpwrdn_local =
  39367. + DWC_READ_REG32(&core_if->core_global_regs->gpwrdn);
  39368. +
  39369. + /* Set flag to indicate that we are in hibernation */
  39370. + core_if->hibernation_suspend = 1;
  39371. + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock,flags);
  39372. +
  39373. + DWC_PRINTF("Host hibernation completed\n");
  39374. + // Exit from case statement
  39375. + break;
  39376. +
  39377. + }
  39378. + if (dwc_otg_hcd_otg_port(dwc_otg_hcd) == wIndex &&
  39379. + dwc_otg_hcd->fops->get_b_hnp_enable(dwc_otg_hcd)) {
  39380. + gotgctl_data_t gotgctl = {.d32 = 0 };
  39381. + gotgctl.b.hstsethnpen = 1;
  39382. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  39383. + gotgctl, 0, gotgctl.d32);
  39384. + core_if->op_state = A_SUSPEND;
  39385. + }
  39386. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  39387. + hprt0.b.prtsusp = 1;
  39388. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  39389. + {
  39390. + dwc_irqflags_t flags;
  39391. + /* Update lx_state */
  39392. + DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &flags);
  39393. + core_if->lx_state = DWC_OTG_L2;
  39394. + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, flags);
  39395. + }
  39396. + /* Suspend the Phy Clock */
  39397. + {
  39398. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  39399. + pcgcctl.b.stoppclk = 1;
  39400. + DWC_MODIFY_REG32(core_if->pcgcctl, 0,
  39401. + pcgcctl.d32);
  39402. + dwc_udelay(10);
  39403. + }
  39404. +
  39405. + /* For HNP the bus must be suspended for at least 200ms. */
  39406. + if (dwc_otg_hcd->fops->get_b_hnp_enable(dwc_otg_hcd)) {
  39407. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  39408. + pcgcctl.b.stoppclk = 1;
  39409. + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
  39410. + dwc_mdelay(200);
  39411. + }
  39412. +
  39413. + /** @todo - check how sw can wait for 1 sec to check asesvld??? */
  39414. +#if 0 //vahrama !!!!!!!!!!!!!!!!!!
  39415. + if (core_if->adp_enable) {
  39416. + gotgctl_data_t gotgctl = {.d32 = 0 };
  39417. + gpwrdn_data_t gpwrdn;
  39418. +
  39419. + while (gotgctl.b.asesvld == 1) {
  39420. + gotgctl.d32 =
  39421. + DWC_READ_REG32(&core_if->
  39422. + core_global_regs->
  39423. + gotgctl);
  39424. + dwc_mdelay(100);
  39425. + }
  39426. +
  39427. + /* Enable Power Down Logic */
  39428. + gpwrdn.d32 = 0;
  39429. + gpwrdn.b.pmuactv = 1;
  39430. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  39431. + gpwrdn, 0, gpwrdn.d32);
  39432. +
  39433. + /* Unmask SRP detected interrupt from Power Down Logic */
  39434. + gpwrdn.d32 = 0;
  39435. + gpwrdn.b.srp_det_msk = 1;
  39436. + DWC_MODIFY_REG32(&core_if->core_global_regs->
  39437. + gpwrdn, 0, gpwrdn.d32);
  39438. +
  39439. + dwc_otg_adp_probe_start(core_if);
  39440. + }
  39441. +#endif
  39442. + break;
  39443. + case UHF_PORT_POWER:
  39444. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  39445. + "SetPortFeature - USB_PORT_FEAT_POWER\n");
  39446. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  39447. + hprt0.b.prtpwr = 1;
  39448. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  39449. + break;
  39450. + case UHF_PORT_RESET:
  39451. + if ((core_if->power_down == 2)
  39452. + && (core_if->hibernation_suspend == 1)) {
  39453. + /* If we are going to exit from Hibernated
  39454. + * state via USB RESET.
  39455. + */
  39456. + dwc_otg_host_hibernation_restore(core_if, 0, 1);
  39457. + } else {
  39458. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  39459. +
  39460. + DWC_DEBUGPL(DBG_HCD,
  39461. + "DWC OTG HCD HUB CONTROL - "
  39462. + "SetPortFeature - USB_PORT_FEAT_RESET\n");
  39463. + {
  39464. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  39465. + pcgcctl.b.enbl_sleep_gating = 1;
  39466. + pcgcctl.b.stoppclk = 1;
  39467. + DWC_MODIFY_REG32(core_if->pcgcctl, pcgcctl.d32, 0);
  39468. + DWC_WRITE_REG32(core_if->pcgcctl, 0);
  39469. + }
  39470. +#ifdef CONFIG_USB_DWC_OTG_LPM
  39471. + {
  39472. + glpmcfg_data_t lpmcfg;
  39473. + lpmcfg.d32 =
  39474. + DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  39475. + if (lpmcfg.b.prt_sleep_sts) {
  39476. + lpmcfg.b.en_utmi_sleep = 0;
  39477. + lpmcfg.b.hird_thres &= (~(1 << 4));
  39478. + DWC_WRITE_REG32
  39479. + (&core_if->core_global_regs->glpmcfg,
  39480. + lpmcfg.d32);
  39481. + dwc_mdelay(1);
  39482. + }
  39483. + }
  39484. +#endif
  39485. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  39486. + /* Clear suspend bit if resetting from suspended state. */
  39487. + hprt0.b.prtsusp = 0;
  39488. + /* When B-Host the Port reset bit is set in
  39489. + * the Start HCD Callback function, so that
  39490. + * the reset is started within 1ms of the HNP
  39491. + * success interrupt. */
  39492. + if (!dwc_otg_hcd_is_b_host(dwc_otg_hcd)) {
  39493. + hprt0.b.prtpwr = 1;
  39494. + hprt0.b.prtrst = 1;
  39495. + DWC_PRINTF("Indeed it is in host mode hprt0 = %08x\n",hprt0.d32);
  39496. + DWC_WRITE_REG32(core_if->host_if->hprt0,
  39497. + hprt0.d32);
  39498. + }
  39499. + /* Clear reset bit in 10ms (FS/LS) or 50ms (HS) */
  39500. + dwc_mdelay(60);
  39501. + hprt0.b.prtrst = 0;
  39502. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  39503. + core_if->lx_state = DWC_OTG_L0; /* Now back to the on state */
  39504. + }
  39505. + break;
  39506. +#ifdef DWC_HS_ELECT_TST
  39507. + case UHF_PORT_TEST:
  39508. + {
  39509. + uint32_t t;
  39510. + gintmsk_data_t gintmsk;
  39511. +
  39512. + t = (wIndex >> 8); /* MSB wIndex USB */
  39513. + DWC_DEBUGPL(DBG_HCD,
  39514. + "DWC OTG HCD HUB CONTROL - "
  39515. + "SetPortFeature - USB_PORT_FEAT_TEST %d\n",
  39516. + t);
  39517. + DWC_WARN("USB_PORT_FEAT_TEST %d\n", t);
  39518. + if (t < 6) {
  39519. + hprt0.d32 = dwc_otg_read_hprt0(core_if);
  39520. + hprt0.b.prttstctl = t;
  39521. + DWC_WRITE_REG32(core_if->host_if->hprt0,
  39522. + hprt0.d32);
  39523. + } else {
  39524. + /* Setup global vars with reg addresses (quick and
  39525. + * dirty hack, should be cleaned up)
  39526. + */
  39527. + global_regs = core_if->core_global_regs;
  39528. + hc_global_regs =
  39529. + core_if->host_if->host_global_regs;
  39530. + hc_regs =
  39531. + (dwc_otg_hc_regs_t *) ((char *)
  39532. + global_regs +
  39533. + 0x500);
  39534. + data_fifo =
  39535. + (uint32_t *) ((char *)global_regs +
  39536. + 0x1000);
  39537. +
  39538. + if (t == 6) { /* HS_HOST_PORT_SUSPEND_RESUME */
  39539. + /* Save current interrupt mask */
  39540. + gintmsk.d32 =
  39541. + DWC_READ_REG32
  39542. + (&global_regs->gintmsk);
  39543. +
  39544. + /* Disable all interrupts while we muck with
  39545. + * the hardware directly
  39546. + */
  39547. + DWC_WRITE_REG32(&global_regs->gintmsk, 0);
  39548. +
  39549. + /* 15 second delay per the test spec */
  39550. + dwc_mdelay(15000);
  39551. +
  39552. + /* Drive suspend on the root port */
  39553. + hprt0.d32 =
  39554. + dwc_otg_read_hprt0(core_if);
  39555. + hprt0.b.prtsusp = 1;
  39556. + hprt0.b.prtres = 0;
  39557. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  39558. +
  39559. + /* 15 second delay per the test spec */
  39560. + dwc_mdelay(15000);
  39561. +
  39562. + /* Drive resume on the root port */
  39563. + hprt0.d32 =
  39564. + dwc_otg_read_hprt0(core_if);
  39565. + hprt0.b.prtsusp = 0;
  39566. + hprt0.b.prtres = 1;
  39567. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  39568. + dwc_mdelay(100);
  39569. +
  39570. + /* Clear the resume bit */
  39571. + hprt0.b.prtres = 0;
  39572. + DWC_WRITE_REG32(core_if->host_if->hprt0, hprt0.d32);
  39573. +
  39574. + /* Restore interrupts */
  39575. + DWC_WRITE_REG32(&global_regs->gintmsk, gintmsk.d32);
  39576. + } else if (t == 7) { /* SINGLE_STEP_GET_DEVICE_DESCRIPTOR setup */
  39577. + /* Save current interrupt mask */
  39578. + gintmsk.d32 =
  39579. + DWC_READ_REG32
  39580. + (&global_regs->gintmsk);
  39581. +
  39582. + /* Disable all interrupts while we muck with
  39583. + * the hardware directly
  39584. + */
  39585. + DWC_WRITE_REG32(&global_regs->gintmsk, 0);
  39586. +
  39587. + /* 15 second delay per the test spec */
  39588. + dwc_mdelay(15000);
  39589. +
  39590. + /* Send the Setup packet */
  39591. + do_setup();
  39592. +
  39593. + /* 15 second delay so nothing else happens for awhile */
  39594. + dwc_mdelay(15000);
  39595. +
  39596. + /* Restore interrupts */
  39597. + DWC_WRITE_REG32(&global_regs->gintmsk, gintmsk.d32);
  39598. + } else if (t == 8) { /* SINGLE_STEP_GET_DEVICE_DESCRIPTOR execute */
  39599. + /* Save current interrupt mask */
  39600. + gintmsk.d32 =
  39601. + DWC_READ_REG32
  39602. + (&global_regs->gintmsk);
  39603. +
  39604. + /* Disable all interrupts while we muck with
  39605. + * the hardware directly
  39606. + */
  39607. + DWC_WRITE_REG32(&global_regs->gintmsk, 0);
  39608. +
  39609. + /* Send the Setup packet */
  39610. + do_setup();
  39611. +
  39612. + /* 15 second delay so nothing else happens for awhile */
  39613. + dwc_mdelay(15000);
  39614. +
  39615. + /* Send the In and Ack packets */
  39616. + do_in_ack();
  39617. +
  39618. + /* 15 second delay so nothing else happens for awhile */
  39619. + dwc_mdelay(15000);
  39620. +
  39621. + /* Restore interrupts */
  39622. + DWC_WRITE_REG32(&global_regs->gintmsk, gintmsk.d32);
  39623. + }
  39624. + }
  39625. + break;
  39626. + }
  39627. +#endif /* DWC_HS_ELECT_TST */
  39628. +
  39629. + case UHF_PORT_INDICATOR:
  39630. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB CONTROL - "
  39631. + "SetPortFeature - USB_PORT_FEAT_INDICATOR\n");
  39632. + /* Not supported */
  39633. + break;
  39634. + default:
  39635. + retval = -DWC_E_INVALID;
  39636. + DWC_ERROR("DWC OTG HCD - "
  39637. + "SetPortFeature request %xh "
  39638. + "unknown or unsupported\n", wValue);
  39639. + break;
  39640. + }
  39641. + break;
  39642. +#ifdef CONFIG_USB_DWC_OTG_LPM
  39643. + case UCR_SET_AND_TEST_PORT_FEATURE:
  39644. + if (wValue != UHF_PORT_L1) {
  39645. + goto error;
  39646. + }
  39647. + {
  39648. + int portnum, hird, devaddr, remwake;
  39649. + glpmcfg_data_t lpmcfg;
  39650. + uint32_t time_usecs;
  39651. + gintsts_data_t gintsts;
  39652. + gintmsk_data_t gintmsk;
  39653. +
  39654. + if (!dwc_otg_get_param_lpm_enable(core_if)) {
  39655. + goto error;
  39656. + }
  39657. + if (wValue != UHF_PORT_L1 || wLength != 1) {
  39658. + goto error;
  39659. + }
  39660. + /* Check if the port currently is in SLEEP state */
  39661. + lpmcfg.d32 =
  39662. + DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  39663. + if (lpmcfg.b.prt_sleep_sts) {
  39664. + DWC_INFO("Port is already in sleep mode\n");
  39665. + buf[0] = 0; /* Return success */
  39666. + break;
  39667. + }
  39668. +
  39669. + portnum = wIndex & 0xf;
  39670. + hird = (wIndex >> 4) & 0xf;
  39671. + devaddr = (wIndex >> 8) & 0x7f;
  39672. + remwake = (wIndex >> 15);
  39673. +
  39674. + if (portnum != 1) {
  39675. + retval = -DWC_E_INVALID;
  39676. + DWC_WARN
  39677. + ("Wrong port number(%d) in SetandTestPortFeature request\n",
  39678. + portnum);
  39679. + break;
  39680. + }
  39681. +
  39682. + DWC_PRINTF
  39683. + ("SetandTestPortFeature request: portnum = %d, hird = %d, devaddr = %d, rewake = %d\n",
  39684. + portnum, hird, devaddr, remwake);
  39685. + /* Disable LPM interrupt */
  39686. + gintmsk.d32 = 0;
  39687. + gintmsk.b.lpmtranrcvd = 1;
  39688. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk,
  39689. + gintmsk.d32, 0);
  39690. +
  39691. + if (dwc_otg_hcd_send_lpm
  39692. + (dwc_otg_hcd, devaddr, hird, remwake)) {
  39693. + retval = -DWC_E_INVALID;
  39694. + break;
  39695. + }
  39696. +
  39697. + time_usecs = 10 * (lpmcfg.b.retry_count + 1);
  39698. + /* We will consider timeout if time_usecs microseconds pass,
  39699. + * and we don't receive LPM transaction status.
  39700. + * After receiving non-error responce(ACK/NYET/STALL) from device,
  39701. + * core will set lpmtranrcvd bit.
  39702. + */
  39703. + do {
  39704. + gintsts.d32 =
  39705. + DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  39706. + if (gintsts.b.lpmtranrcvd) {
  39707. + break;
  39708. + }
  39709. + dwc_udelay(1);
  39710. + } while (--time_usecs);
  39711. + /* lpm_int bit will be cleared in LPM interrupt handler */
  39712. +
  39713. + /* Now fill status
  39714. + * 0x00 - Success
  39715. + * 0x10 - NYET
  39716. + * 0x11 - Timeout
  39717. + */
  39718. + if (!gintsts.b.lpmtranrcvd) {
  39719. + buf[0] = 0x3; /* Completion code is Timeout */
  39720. + dwc_otg_hcd_free_hc_from_lpm(dwc_otg_hcd);
  39721. + } else {
  39722. + lpmcfg.d32 =
  39723. + DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  39724. + if (lpmcfg.b.lpm_resp == 0x3) {
  39725. + /* ACK responce from the device */
  39726. + buf[0] = 0x00; /* Success */
  39727. + } else if (lpmcfg.b.lpm_resp == 0x2) {
  39728. + /* NYET responce from the device */
  39729. + buf[0] = 0x2;
  39730. + } else {
  39731. + /* Otherwise responce with Timeout */
  39732. + buf[0] = 0x3;
  39733. + }
  39734. + }
  39735. + DWC_PRINTF("Device responce to LPM trans is %x\n",
  39736. + lpmcfg.b.lpm_resp);
  39737. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, 0,
  39738. + gintmsk.d32);
  39739. +
  39740. + break;
  39741. + }
  39742. +#endif /* CONFIG_USB_DWC_OTG_LPM */
  39743. + default:
  39744. +error:
  39745. + retval = -DWC_E_INVALID;
  39746. + DWC_WARN("DWC OTG HCD - "
  39747. + "Unknown hub control request type or invalid typeReq: %xh wIndex: %xh wValue: %xh\n",
  39748. + typeReq, wIndex, wValue);
  39749. + break;
  39750. + }
  39751. +
  39752. + return retval;
  39753. +}
  39754. +
  39755. +#ifdef CONFIG_USB_DWC_OTG_LPM
  39756. +/** Returns index of host channel to perform LPM transaction. */
  39757. +int dwc_otg_hcd_get_hc_for_lpm_tran(dwc_otg_hcd_t * hcd, uint8_t devaddr)
  39758. +{
  39759. + dwc_otg_core_if_t *core_if = hcd->core_if;
  39760. + dwc_hc_t *hc;
  39761. + hcchar_data_t hcchar;
  39762. + gintmsk_data_t gintmsk = {.d32 = 0 };
  39763. +
  39764. + if (DWC_CIRCLEQ_EMPTY(&hcd->free_hc_list)) {
  39765. + DWC_PRINTF("No free channel to select for LPM transaction\n");
  39766. + return -1;
  39767. + }
  39768. +
  39769. + hc = DWC_CIRCLEQ_FIRST(&hcd->free_hc_list);
  39770. +
  39771. + /* Mask host channel interrupts. */
  39772. + gintmsk.b.hcintr = 1;
  39773. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, gintmsk.d32, 0);
  39774. +
  39775. + /* Fill fields that core needs for LPM transaction */
  39776. + hcchar.b.devaddr = devaddr;
  39777. + hcchar.b.epnum = 0;
  39778. + hcchar.b.eptype = DWC_OTG_EP_TYPE_CONTROL;
  39779. + hcchar.b.mps = 64;
  39780. + hcchar.b.lspddev = (hc->speed == DWC_OTG_EP_SPEED_LOW);
  39781. + hcchar.b.epdir = 0; /* OUT */
  39782. + DWC_WRITE_REG32(&core_if->host_if->hc_regs[hc->hc_num]->hcchar,
  39783. + hcchar.d32);
  39784. +
  39785. + /* Remove the host channel from the free list. */
  39786. + DWC_CIRCLEQ_REMOVE_INIT(&hcd->free_hc_list, hc, hc_list_entry);
  39787. +
  39788. + DWC_PRINTF("hcnum = %d devaddr = %d\n", hc->hc_num, devaddr);
  39789. +
  39790. + return hc->hc_num;
  39791. +}
  39792. +
  39793. +/** Release hc after performing LPM transaction */
  39794. +void dwc_otg_hcd_free_hc_from_lpm(dwc_otg_hcd_t * hcd)
  39795. +{
  39796. + dwc_hc_t *hc;
  39797. + glpmcfg_data_t lpmcfg;
  39798. + uint8_t hc_num;
  39799. +
  39800. + lpmcfg.d32 = DWC_READ_REG32(&hcd->core_if->core_global_regs->glpmcfg);
  39801. + hc_num = lpmcfg.b.lpm_chan_index;
  39802. +
  39803. + hc = hcd->hc_ptr_array[hc_num];
  39804. +
  39805. + DWC_PRINTF("Freeing channel %d after LPM\n", hc_num);
  39806. + /* Return host channel to free list */
  39807. + DWC_CIRCLEQ_INSERT_TAIL(&hcd->free_hc_list, hc, hc_list_entry);
  39808. +}
  39809. +
  39810. +int dwc_otg_hcd_send_lpm(dwc_otg_hcd_t * hcd, uint8_t devaddr, uint8_t hird,
  39811. + uint8_t bRemoteWake)
  39812. +{
  39813. + glpmcfg_data_t lpmcfg;
  39814. + pcgcctl_data_t pcgcctl = {.d32 = 0 };
  39815. + int channel;
  39816. +
  39817. + channel = dwc_otg_hcd_get_hc_for_lpm_tran(hcd, devaddr);
  39818. + if (channel < 0) {
  39819. + return channel;
  39820. + }
  39821. +
  39822. + pcgcctl.b.enbl_sleep_gating = 1;
  39823. + DWC_MODIFY_REG32(hcd->core_if->pcgcctl, 0, pcgcctl.d32);
  39824. +
  39825. + /* Read LPM config register */
  39826. + lpmcfg.d32 = DWC_READ_REG32(&hcd->core_if->core_global_regs->glpmcfg);
  39827. +
  39828. + /* Program LPM transaction fields */
  39829. + lpmcfg.b.rem_wkup_en = bRemoteWake;
  39830. + lpmcfg.b.hird = hird;
  39831. + lpmcfg.b.hird_thres = 0x1c;
  39832. + lpmcfg.b.lpm_chan_index = channel;
  39833. + lpmcfg.b.en_utmi_sleep = 1;
  39834. + /* Program LPM config register */
  39835. + DWC_WRITE_REG32(&hcd->core_if->core_global_regs->glpmcfg, lpmcfg.d32);
  39836. +
  39837. + /* Send LPM transaction */
  39838. + lpmcfg.b.send_lpm = 1;
  39839. + DWC_WRITE_REG32(&hcd->core_if->core_global_regs->glpmcfg, lpmcfg.d32);
  39840. +
  39841. + return 0;
  39842. +}
  39843. +
  39844. +#endif /* CONFIG_USB_DWC_OTG_LPM */
  39845. +
  39846. +int dwc_otg_hcd_is_status_changed(dwc_otg_hcd_t * hcd, int port)
  39847. +{
  39848. + int retval;
  39849. +
  39850. + if (port != 1) {
  39851. + return -DWC_E_INVALID;
  39852. + }
  39853. +
  39854. + retval = (hcd->flags.b.port_connect_status_change ||
  39855. + hcd->flags.b.port_reset_change ||
  39856. + hcd->flags.b.port_enable_change ||
  39857. + hcd->flags.b.port_suspend_change ||
  39858. + hcd->flags.b.port_over_current_change);
  39859. +#ifdef DEBUG
  39860. + if (retval) {
  39861. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD HUB STATUS DATA:"
  39862. + " Root port status changed\n");
  39863. + DWC_DEBUGPL(DBG_HCDV, " port_connect_status_change: %d\n",
  39864. + hcd->flags.b.port_connect_status_change);
  39865. + DWC_DEBUGPL(DBG_HCDV, " port_reset_change: %d\n",
  39866. + hcd->flags.b.port_reset_change);
  39867. + DWC_DEBUGPL(DBG_HCDV, " port_enable_change: %d\n",
  39868. + hcd->flags.b.port_enable_change);
  39869. + DWC_DEBUGPL(DBG_HCDV, " port_suspend_change: %d\n",
  39870. + hcd->flags.b.port_suspend_change);
  39871. + DWC_DEBUGPL(DBG_HCDV, " port_over_current_change: %d\n",
  39872. + hcd->flags.b.port_over_current_change);
  39873. + }
  39874. +#endif
  39875. + return retval;
  39876. +}
  39877. +
  39878. +int dwc_otg_hcd_get_frame_number(dwc_otg_hcd_t * dwc_otg_hcd)
  39879. +{
  39880. + hfnum_data_t hfnum;
  39881. + hfnum.d32 =
  39882. + DWC_READ_REG32(&dwc_otg_hcd->core_if->host_if->host_global_regs->
  39883. + hfnum);
  39884. +
  39885. +#ifdef DEBUG_SOF
  39886. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD GET FRAME NUMBER %d\n",
  39887. + hfnum.b.frnum);
  39888. +#endif
  39889. + return hfnum.b.frnum;
  39890. +}
  39891. +
  39892. +int dwc_otg_hcd_start(dwc_otg_hcd_t * hcd,
  39893. + struct dwc_otg_hcd_function_ops *fops)
  39894. +{
  39895. + int retval = 0;
  39896. +
  39897. + hcd->fops = fops;
  39898. + if (!dwc_otg_is_device_mode(hcd->core_if) &&
  39899. + (!hcd->core_if->adp_enable || hcd->core_if->adp.adp_started)) {
  39900. + dwc_otg_hcd_reinit(hcd);
  39901. + } else {
  39902. + retval = -DWC_E_NO_DEVICE;
  39903. + }
  39904. +
  39905. + return retval;
  39906. +}
  39907. +
  39908. +void *dwc_otg_hcd_get_priv_data(dwc_otg_hcd_t * hcd)
  39909. +{
  39910. + return hcd->priv;
  39911. +}
  39912. +
  39913. +void dwc_otg_hcd_set_priv_data(dwc_otg_hcd_t * hcd, void *priv_data)
  39914. +{
  39915. + hcd->priv = priv_data;
  39916. +}
  39917. +
  39918. +uint32_t dwc_otg_hcd_otg_port(dwc_otg_hcd_t * hcd)
  39919. +{
  39920. + return hcd->otg_port;
  39921. +}
  39922. +
  39923. +uint32_t dwc_otg_hcd_is_b_host(dwc_otg_hcd_t * hcd)
  39924. +{
  39925. + uint32_t is_b_host;
  39926. + if (hcd->core_if->op_state == B_HOST) {
  39927. + is_b_host = 1;
  39928. + } else {
  39929. + is_b_host = 0;
  39930. + }
  39931. +
  39932. + return is_b_host;
  39933. +}
  39934. +
  39935. +dwc_otg_hcd_urb_t *dwc_otg_hcd_urb_alloc(dwc_otg_hcd_t * hcd,
  39936. + int iso_desc_count, int atomic_alloc)
  39937. +{
  39938. + dwc_otg_hcd_urb_t *dwc_otg_urb;
  39939. + uint32_t size;
  39940. +
  39941. + size =
  39942. + sizeof(*dwc_otg_urb) +
  39943. + iso_desc_count * sizeof(struct dwc_otg_hcd_iso_packet_desc);
  39944. + if (atomic_alloc)
  39945. + dwc_otg_urb = DWC_ALLOC_ATOMIC(size);
  39946. + else
  39947. + dwc_otg_urb = DWC_ALLOC(size);
  39948. +
  39949. + if (dwc_otg_urb)
  39950. + dwc_otg_urb->packet_count = iso_desc_count;
  39951. + else {
  39952. + DWC_ERROR("**** DWC OTG HCD URB alloc - "
  39953. + "%salloc of %db failed\n",
  39954. + atomic_alloc?"atomic ":"", size);
  39955. + }
  39956. + return dwc_otg_urb;
  39957. +}
  39958. +
  39959. +void dwc_otg_hcd_urb_set_pipeinfo(dwc_otg_hcd_urb_t * dwc_otg_urb,
  39960. + uint8_t dev_addr, uint8_t ep_num,
  39961. + uint8_t ep_type, uint8_t ep_dir, uint16_t mps)
  39962. +{
  39963. + dwc_otg_hcd_fill_pipe(&dwc_otg_urb->pipe_info, dev_addr, ep_num,
  39964. + ep_type, ep_dir, mps);
  39965. +#if 0
  39966. + DWC_PRINTF
  39967. + ("addr = %d, ep_num = %d, ep_dir = 0x%x, ep_type = 0x%x, mps = %d\n",
  39968. + dev_addr, ep_num, ep_dir, ep_type, mps);
  39969. +#endif
  39970. +}
  39971. +
  39972. +void dwc_otg_hcd_urb_set_params(dwc_otg_hcd_urb_t * dwc_otg_urb,
  39973. + void *urb_handle, void *buf, dwc_dma_t dma,
  39974. + uint32_t buflen, void *setup_packet,
  39975. + dwc_dma_t setup_dma, uint32_t flags,
  39976. + uint16_t interval)
  39977. +{
  39978. + dwc_otg_urb->priv = urb_handle;
  39979. + dwc_otg_urb->buf = buf;
  39980. + dwc_otg_urb->dma = dma;
  39981. + dwc_otg_urb->length = buflen;
  39982. + dwc_otg_urb->setup_packet = setup_packet;
  39983. + dwc_otg_urb->setup_dma = setup_dma;
  39984. + dwc_otg_urb->flags = flags;
  39985. + dwc_otg_urb->interval = interval;
  39986. + dwc_otg_urb->status = -DWC_E_IN_PROGRESS;
  39987. +}
  39988. +
  39989. +uint32_t dwc_otg_hcd_urb_get_status(dwc_otg_hcd_urb_t * dwc_otg_urb)
  39990. +{
  39991. + return dwc_otg_urb->status;
  39992. +}
  39993. +
  39994. +uint32_t dwc_otg_hcd_urb_get_actual_length(dwc_otg_hcd_urb_t * dwc_otg_urb)
  39995. +{
  39996. + return dwc_otg_urb->actual_length;
  39997. +}
  39998. +
  39999. +uint32_t dwc_otg_hcd_urb_get_error_count(dwc_otg_hcd_urb_t * dwc_otg_urb)
  40000. +{
  40001. + return dwc_otg_urb->error_count;
  40002. +}
  40003. +
  40004. +void dwc_otg_hcd_urb_set_iso_desc_params(dwc_otg_hcd_urb_t * dwc_otg_urb,
  40005. + int desc_num, uint32_t offset,
  40006. + uint32_t length)
  40007. +{
  40008. + dwc_otg_urb->iso_descs[desc_num].offset = offset;
  40009. + dwc_otg_urb->iso_descs[desc_num].length = length;
  40010. +}
  40011. +
  40012. +uint32_t dwc_otg_hcd_urb_get_iso_desc_status(dwc_otg_hcd_urb_t * dwc_otg_urb,
  40013. + int desc_num)
  40014. +{
  40015. + return dwc_otg_urb->iso_descs[desc_num].status;
  40016. +}
  40017. +
  40018. +uint32_t dwc_otg_hcd_urb_get_iso_desc_actual_length(dwc_otg_hcd_urb_t *
  40019. + dwc_otg_urb, int desc_num)
  40020. +{
  40021. + return dwc_otg_urb->iso_descs[desc_num].actual_length;
  40022. +}
  40023. +
  40024. +int dwc_otg_hcd_is_bandwidth_allocated(dwc_otg_hcd_t * hcd, void *ep_handle)
  40025. +{
  40026. + int allocated = 0;
  40027. + dwc_otg_qh_t *qh = (dwc_otg_qh_t *) ep_handle;
  40028. +
  40029. + if (qh) {
  40030. + if (!DWC_LIST_EMPTY(&qh->qh_list_entry)) {
  40031. + allocated = 1;
  40032. + }
  40033. + }
  40034. + return allocated;
  40035. +}
  40036. +
  40037. +int dwc_otg_hcd_is_bandwidth_freed(dwc_otg_hcd_t * hcd, void *ep_handle)
  40038. +{
  40039. + dwc_otg_qh_t *qh = (dwc_otg_qh_t *) ep_handle;
  40040. + int freed = 0;
  40041. + DWC_ASSERT(qh, "qh is not allocated\n");
  40042. +
  40043. + if (DWC_LIST_EMPTY(&qh->qh_list_entry)) {
  40044. + freed = 1;
  40045. + }
  40046. +
  40047. + return freed;
  40048. +}
  40049. +
  40050. +uint8_t dwc_otg_hcd_get_ep_bandwidth(dwc_otg_hcd_t * hcd, void *ep_handle)
  40051. +{
  40052. + dwc_otg_qh_t *qh = (dwc_otg_qh_t *) ep_handle;
  40053. + DWC_ASSERT(qh, "qh is not allocated\n");
  40054. + return qh->usecs;
  40055. +}
  40056. +
  40057. +void dwc_otg_hcd_dump_state(dwc_otg_hcd_t * hcd)
  40058. +{
  40059. +#ifdef DEBUG
  40060. + int num_channels;
  40061. + int i;
  40062. + gnptxsts_data_t np_tx_status;
  40063. + hptxsts_data_t p_tx_status;
  40064. +
  40065. + num_channels = hcd->core_if->core_params->host_channels;
  40066. + DWC_PRINTF("\n");
  40067. + DWC_PRINTF
  40068. + ("************************************************************\n");
  40069. + DWC_PRINTF("HCD State:\n");
  40070. + DWC_PRINTF(" Num channels: %d\n", num_channels);
  40071. + for (i = 0; i < num_channels; i++) {
  40072. + dwc_hc_t *hc = hcd->hc_ptr_array[i];
  40073. + DWC_PRINTF(" Channel %d:\n", i);
  40074. + DWC_PRINTF(" dev_addr: %d, ep_num: %d, ep_is_in: %d\n",
  40075. + hc->dev_addr, hc->ep_num, hc->ep_is_in);
  40076. + DWC_PRINTF(" speed: %d\n", hc->speed);
  40077. + DWC_PRINTF(" ep_type: %d\n", hc->ep_type);
  40078. + DWC_PRINTF(" max_packet: %d\n", hc->max_packet);
  40079. + DWC_PRINTF(" data_pid_start: %d\n", hc->data_pid_start);
  40080. + DWC_PRINTF(" multi_count: %d\n", hc->multi_count);
  40081. + DWC_PRINTF(" xfer_started: %d\n", hc->xfer_started);
  40082. + DWC_PRINTF(" xfer_buff: %p\n", hc->xfer_buff);
  40083. + DWC_PRINTF(" xfer_len: %d\n", hc->xfer_len);
  40084. + DWC_PRINTF(" xfer_count: %d\n", hc->xfer_count);
  40085. + DWC_PRINTF(" halt_on_queue: %d\n", hc->halt_on_queue);
  40086. + DWC_PRINTF(" halt_pending: %d\n", hc->halt_pending);
  40087. + DWC_PRINTF(" halt_status: %d\n", hc->halt_status);
  40088. + DWC_PRINTF(" do_split: %d\n", hc->do_split);
  40089. + DWC_PRINTF(" complete_split: %d\n", hc->complete_split);
  40090. + DWC_PRINTF(" hub_addr: %d\n", hc->hub_addr);
  40091. + DWC_PRINTF(" port_addr: %d\n", hc->port_addr);
  40092. + DWC_PRINTF(" xact_pos: %d\n", hc->xact_pos);
  40093. + DWC_PRINTF(" requests: %d\n", hc->requests);
  40094. + DWC_PRINTF(" qh: %p\n", hc->qh);
  40095. + if (hc->xfer_started) {
  40096. + hfnum_data_t hfnum;
  40097. + hcchar_data_t hcchar;
  40098. + hctsiz_data_t hctsiz;
  40099. + hcint_data_t hcint;
  40100. + hcintmsk_data_t hcintmsk;
  40101. + hfnum.d32 =
  40102. + DWC_READ_REG32(&hcd->core_if->
  40103. + host_if->host_global_regs->hfnum);
  40104. + hcchar.d32 =
  40105. + DWC_READ_REG32(&hcd->core_if->host_if->
  40106. + hc_regs[i]->hcchar);
  40107. + hctsiz.d32 =
  40108. + DWC_READ_REG32(&hcd->core_if->host_if->
  40109. + hc_regs[i]->hctsiz);
  40110. + hcint.d32 =
  40111. + DWC_READ_REG32(&hcd->core_if->host_if->
  40112. + hc_regs[i]->hcint);
  40113. + hcintmsk.d32 =
  40114. + DWC_READ_REG32(&hcd->core_if->host_if->
  40115. + hc_regs[i]->hcintmsk);
  40116. + DWC_PRINTF(" hfnum: 0x%08x\n", hfnum.d32);
  40117. + DWC_PRINTF(" hcchar: 0x%08x\n", hcchar.d32);
  40118. + DWC_PRINTF(" hctsiz: 0x%08x\n", hctsiz.d32);
  40119. + DWC_PRINTF(" hcint: 0x%08x\n", hcint.d32);
  40120. + DWC_PRINTF(" hcintmsk: 0x%08x\n", hcintmsk.d32);
  40121. + }
  40122. + if (hc->xfer_started && hc->qh) {
  40123. + dwc_otg_qtd_t *qtd;
  40124. + dwc_otg_hcd_urb_t *urb;
  40125. +
  40126. + DWC_CIRCLEQ_FOREACH(qtd, &hc->qh->qtd_list, qtd_list_entry) {
  40127. + if (!qtd->in_process)
  40128. + break;
  40129. +
  40130. + urb = qtd->urb;
  40131. + DWC_PRINTF(" URB Info:\n");
  40132. + DWC_PRINTF(" qtd: %p, urb: %p\n", qtd, urb);
  40133. + if (urb) {
  40134. + DWC_PRINTF(" Dev: %d, EP: %d %s\n",
  40135. + dwc_otg_hcd_get_dev_addr(&urb->
  40136. + pipe_info),
  40137. + dwc_otg_hcd_get_ep_num(&urb->
  40138. + pipe_info),
  40139. + dwc_otg_hcd_is_pipe_in(&urb->
  40140. + pipe_info) ?
  40141. + "IN" : "OUT");
  40142. + DWC_PRINTF(" Max packet size: %d\n",
  40143. + dwc_otg_hcd_get_mps(&urb->
  40144. + pipe_info));
  40145. + DWC_PRINTF(" transfer_buffer: %p\n",
  40146. + urb->buf);
  40147. + DWC_PRINTF(" transfer_dma: %p\n",
  40148. + (void *)urb->dma);
  40149. + DWC_PRINTF(" transfer_buffer_length: %d\n",
  40150. + urb->length);
  40151. + DWC_PRINTF(" actual_length: %d\n",
  40152. + urb->actual_length);
  40153. + }
  40154. + }
  40155. + }
  40156. + }
  40157. + DWC_PRINTF(" non_periodic_channels: %d\n", hcd->non_periodic_channels);
  40158. + DWC_PRINTF(" periodic_channels: %d\n", hcd->periodic_channels);
  40159. + DWC_PRINTF(" periodic_usecs: %d\n", hcd->periodic_usecs);
  40160. + np_tx_status.d32 =
  40161. + DWC_READ_REG32(&hcd->core_if->core_global_regs->gnptxsts);
  40162. + DWC_PRINTF(" NP Tx Req Queue Space Avail: %d\n",
  40163. + np_tx_status.b.nptxqspcavail);
  40164. + DWC_PRINTF(" NP Tx FIFO Space Avail: %d\n",
  40165. + np_tx_status.b.nptxfspcavail);
  40166. + p_tx_status.d32 =
  40167. + DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hptxsts);
  40168. + DWC_PRINTF(" P Tx Req Queue Space Avail: %d\n",
  40169. + p_tx_status.b.ptxqspcavail);
  40170. + DWC_PRINTF(" P Tx FIFO Space Avail: %d\n", p_tx_status.b.ptxfspcavail);
  40171. + dwc_otg_hcd_dump_frrem(hcd);
  40172. + dwc_otg_dump_global_registers(hcd->core_if);
  40173. + dwc_otg_dump_host_registers(hcd->core_if);
  40174. + DWC_PRINTF
  40175. + ("************************************************************\n");
  40176. + DWC_PRINTF("\n");
  40177. +#endif
  40178. +}
  40179. +
  40180. +#ifdef DEBUG
  40181. +void dwc_print_setup_data(uint8_t * setup)
  40182. +{
  40183. + int i;
  40184. + if (CHK_DEBUG_LEVEL(DBG_HCD)) {
  40185. + DWC_PRINTF("Setup Data = MSB ");
  40186. + for (i = 7; i >= 0; i--)
  40187. + DWC_PRINTF("%02x ", setup[i]);
  40188. + DWC_PRINTF("\n");
  40189. + DWC_PRINTF(" bmRequestType Tranfer = %s\n",
  40190. + (setup[0] & 0x80) ? "Device-to-Host" :
  40191. + "Host-to-Device");
  40192. + DWC_PRINTF(" bmRequestType Type = ");
  40193. + switch ((setup[0] & 0x60) >> 5) {
  40194. + case 0:
  40195. + DWC_PRINTF("Standard\n");
  40196. + break;
  40197. + case 1:
  40198. + DWC_PRINTF("Class\n");
  40199. + break;
  40200. + case 2:
  40201. + DWC_PRINTF("Vendor\n");
  40202. + break;
  40203. + case 3:
  40204. + DWC_PRINTF("Reserved\n");
  40205. + break;
  40206. + }
  40207. + DWC_PRINTF(" bmRequestType Recipient = ");
  40208. + switch (setup[0] & 0x1f) {
  40209. + case 0:
  40210. + DWC_PRINTF("Device\n");
  40211. + break;
  40212. + case 1:
  40213. + DWC_PRINTF("Interface\n");
  40214. + break;
  40215. + case 2:
  40216. + DWC_PRINTF("Endpoint\n");
  40217. + break;
  40218. + case 3:
  40219. + DWC_PRINTF("Other\n");
  40220. + break;
  40221. + default:
  40222. + DWC_PRINTF("Reserved\n");
  40223. + break;
  40224. + }
  40225. + DWC_PRINTF(" bRequest = 0x%0x\n", setup[1]);
  40226. + DWC_PRINTF(" wValue = 0x%0x\n", *((uint16_t *) & setup[2]));
  40227. + DWC_PRINTF(" wIndex = 0x%0x\n", *((uint16_t *) & setup[4]));
  40228. + DWC_PRINTF(" wLength = 0x%0x\n\n", *((uint16_t *) & setup[6]));
  40229. + }
  40230. +}
  40231. +#endif
  40232. +
  40233. +void dwc_otg_hcd_dump_frrem(dwc_otg_hcd_t * hcd)
  40234. +{
  40235. +#if 0
  40236. + DWC_PRINTF("Frame remaining at SOF:\n");
  40237. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  40238. + hcd->frrem_samples, hcd->frrem_accum,
  40239. + (hcd->frrem_samples > 0) ?
  40240. + hcd->frrem_accum / hcd->frrem_samples : 0);
  40241. +
  40242. + DWC_PRINTF("\n");
  40243. + DWC_PRINTF("Frame remaining at start_transfer (uframe 7):\n");
  40244. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  40245. + hcd->core_if->hfnum_7_samples,
  40246. + hcd->core_if->hfnum_7_frrem_accum,
  40247. + (hcd->core_if->hfnum_7_samples >
  40248. + 0) ? hcd->core_if->hfnum_7_frrem_accum /
  40249. + hcd->core_if->hfnum_7_samples : 0);
  40250. + DWC_PRINTF("Frame remaining at start_transfer (uframe 0):\n");
  40251. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  40252. + hcd->core_if->hfnum_0_samples,
  40253. + hcd->core_if->hfnum_0_frrem_accum,
  40254. + (hcd->core_if->hfnum_0_samples >
  40255. + 0) ? hcd->core_if->hfnum_0_frrem_accum /
  40256. + hcd->core_if->hfnum_0_samples : 0);
  40257. + DWC_PRINTF("Frame remaining at start_transfer (uframe 1-6):\n");
  40258. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  40259. + hcd->core_if->hfnum_other_samples,
  40260. + hcd->core_if->hfnum_other_frrem_accum,
  40261. + (hcd->core_if->hfnum_other_samples >
  40262. + 0) ? hcd->core_if->hfnum_other_frrem_accum /
  40263. + hcd->core_if->hfnum_other_samples : 0);
  40264. +
  40265. + DWC_PRINTF("\n");
  40266. + DWC_PRINTF("Frame remaining at sample point A (uframe 7):\n");
  40267. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  40268. + hcd->hfnum_7_samples_a, hcd->hfnum_7_frrem_accum_a,
  40269. + (hcd->hfnum_7_samples_a > 0) ?
  40270. + hcd->hfnum_7_frrem_accum_a / hcd->hfnum_7_samples_a : 0);
  40271. + DWC_PRINTF("Frame remaining at sample point A (uframe 0):\n");
  40272. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  40273. + hcd->hfnum_0_samples_a, hcd->hfnum_0_frrem_accum_a,
  40274. + (hcd->hfnum_0_samples_a > 0) ?
  40275. + hcd->hfnum_0_frrem_accum_a / hcd->hfnum_0_samples_a : 0);
  40276. + DWC_PRINTF("Frame remaining at sample point A (uframe 1-6):\n");
  40277. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  40278. + hcd->hfnum_other_samples_a, hcd->hfnum_other_frrem_accum_a,
  40279. + (hcd->hfnum_other_samples_a > 0) ?
  40280. + hcd->hfnum_other_frrem_accum_a /
  40281. + hcd->hfnum_other_samples_a : 0);
  40282. +
  40283. + DWC_PRINTF("\n");
  40284. + DWC_PRINTF("Frame remaining at sample point B (uframe 7):\n");
  40285. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  40286. + hcd->hfnum_7_samples_b, hcd->hfnum_7_frrem_accum_b,
  40287. + (hcd->hfnum_7_samples_b > 0) ?
  40288. + hcd->hfnum_7_frrem_accum_b / hcd->hfnum_7_samples_b : 0);
  40289. + DWC_PRINTF("Frame remaining at sample point B (uframe 0):\n");
  40290. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  40291. + hcd->hfnum_0_samples_b, hcd->hfnum_0_frrem_accum_b,
  40292. + (hcd->hfnum_0_samples_b > 0) ?
  40293. + hcd->hfnum_0_frrem_accum_b / hcd->hfnum_0_samples_b : 0);
  40294. + DWC_PRINTF("Frame remaining at sample point B (uframe 1-6):\n");
  40295. + DWC_PRINTF(" samples %u, accum %llu, avg %llu\n",
  40296. + hcd->hfnum_other_samples_b, hcd->hfnum_other_frrem_accum_b,
  40297. + (hcd->hfnum_other_samples_b > 0) ?
  40298. + hcd->hfnum_other_frrem_accum_b /
  40299. + hcd->hfnum_other_samples_b : 0);
  40300. +#endif
  40301. +}
  40302. +
  40303. +#endif /* DWC_DEVICE_ONLY */
  40304. --- /dev/null
  40305. +++ b/drivers/usb/host/dwc_otg/dwc_otg_hcd.h
  40306. @@ -0,0 +1,868 @@
  40307. +/* ==========================================================================
  40308. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd.h $
  40309. + * $Revision: #58 $
  40310. + * $Date: 2011/09/15 $
  40311. + * $Change: 1846647 $
  40312. + *
  40313. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  40314. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  40315. + * otherwise expressly agreed to in writing between Synopsys and you.
  40316. + *
  40317. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  40318. + * any End User Software License Agreement or Agreement for Licensed Product
  40319. + * with Synopsys or any supplement thereto. You are permitted to use and
  40320. + * redistribute this Software in source and binary forms, with or without
  40321. + * modification, provided that redistributions of source code must retain this
  40322. + * notice. You may not view, use, disclose, copy or distribute this file or
  40323. + * any information contained herein except pursuant to this license grant from
  40324. + * Synopsys. If you do not agree with this notice, including the disclaimer
  40325. + * below, then you are not authorized to use the Software.
  40326. + *
  40327. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  40328. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  40329. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  40330. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  40331. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  40332. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  40333. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  40334. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  40335. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  40336. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  40337. + * DAMAGE.
  40338. + * ========================================================================== */
  40339. +#ifndef DWC_DEVICE_ONLY
  40340. +#ifndef __DWC_HCD_H__
  40341. +#define __DWC_HCD_H__
  40342. +
  40343. +#include "dwc_otg_os_dep.h"
  40344. +#include "usb.h"
  40345. +#include "dwc_otg_hcd_if.h"
  40346. +#include "dwc_otg_core_if.h"
  40347. +#include "dwc_list.h"
  40348. +#include "dwc_otg_cil.h"
  40349. +#include "dwc_otg_fiq_fsm.h"
  40350. +#include "dwc_otg_driver.h"
  40351. +
  40352. +
  40353. +/**
  40354. + * @file
  40355. + *
  40356. + * This file contains the structures, constants, and interfaces for
  40357. + * the Host Contoller Driver (HCD).
  40358. + *
  40359. + * The Host Controller Driver (HCD) is responsible for translating requests
  40360. + * from the USB Driver into the appropriate actions on the DWC_otg controller.
  40361. + * It isolates the USBD from the specifics of the controller by providing an
  40362. + * API to the USBD.
  40363. + */
  40364. +
  40365. +struct dwc_otg_hcd_pipe_info {
  40366. + uint8_t dev_addr;
  40367. + uint8_t ep_num;
  40368. + uint8_t pipe_type;
  40369. + uint8_t pipe_dir;
  40370. + uint16_t mps;
  40371. +};
  40372. +
  40373. +struct dwc_otg_hcd_iso_packet_desc {
  40374. + uint32_t offset;
  40375. + uint32_t length;
  40376. + uint32_t actual_length;
  40377. + uint32_t status;
  40378. +};
  40379. +
  40380. +struct dwc_otg_qtd;
  40381. +
  40382. +struct dwc_otg_hcd_urb {
  40383. + void *priv;
  40384. + struct dwc_otg_qtd *qtd;
  40385. + void *buf;
  40386. + dwc_dma_t dma;
  40387. + void *setup_packet;
  40388. + dwc_dma_t setup_dma;
  40389. + uint32_t length;
  40390. + uint32_t actual_length;
  40391. + uint32_t status;
  40392. + uint32_t error_count;
  40393. + uint32_t packet_count;
  40394. + uint32_t flags;
  40395. + uint16_t interval;
  40396. + struct dwc_otg_hcd_pipe_info pipe_info;
  40397. + struct dwc_otg_hcd_iso_packet_desc iso_descs[0];
  40398. +};
  40399. +
  40400. +static inline uint8_t dwc_otg_hcd_get_ep_num(struct dwc_otg_hcd_pipe_info *pipe)
  40401. +{
  40402. + return pipe->ep_num;
  40403. +}
  40404. +
  40405. +static inline uint8_t dwc_otg_hcd_get_pipe_type(struct dwc_otg_hcd_pipe_info
  40406. + *pipe)
  40407. +{
  40408. + return pipe->pipe_type;
  40409. +}
  40410. +
  40411. +static inline uint16_t dwc_otg_hcd_get_mps(struct dwc_otg_hcd_pipe_info *pipe)
  40412. +{
  40413. + return pipe->mps;
  40414. +}
  40415. +
  40416. +static inline uint8_t dwc_otg_hcd_get_dev_addr(struct dwc_otg_hcd_pipe_info
  40417. + *pipe)
  40418. +{
  40419. + return pipe->dev_addr;
  40420. +}
  40421. +
  40422. +static inline uint8_t dwc_otg_hcd_is_pipe_isoc(struct dwc_otg_hcd_pipe_info
  40423. + *pipe)
  40424. +{
  40425. + return (pipe->pipe_type == UE_ISOCHRONOUS);
  40426. +}
  40427. +
  40428. +static inline uint8_t dwc_otg_hcd_is_pipe_int(struct dwc_otg_hcd_pipe_info
  40429. + *pipe)
  40430. +{
  40431. + return (pipe->pipe_type == UE_INTERRUPT);
  40432. +}
  40433. +
  40434. +static inline uint8_t dwc_otg_hcd_is_pipe_bulk(struct dwc_otg_hcd_pipe_info
  40435. + *pipe)
  40436. +{
  40437. + return (pipe->pipe_type == UE_BULK);
  40438. +}
  40439. +
  40440. +static inline uint8_t dwc_otg_hcd_is_pipe_control(struct dwc_otg_hcd_pipe_info
  40441. + *pipe)
  40442. +{
  40443. + return (pipe->pipe_type == UE_CONTROL);
  40444. +}
  40445. +
  40446. +static inline uint8_t dwc_otg_hcd_is_pipe_in(struct dwc_otg_hcd_pipe_info *pipe)
  40447. +{
  40448. + return (pipe->pipe_dir == UE_DIR_IN);
  40449. +}
  40450. +
  40451. +static inline uint8_t dwc_otg_hcd_is_pipe_out(struct dwc_otg_hcd_pipe_info
  40452. + *pipe)
  40453. +{
  40454. + return (!dwc_otg_hcd_is_pipe_in(pipe));
  40455. +}
  40456. +
  40457. +static inline void dwc_otg_hcd_fill_pipe(struct dwc_otg_hcd_pipe_info *pipe,
  40458. + uint8_t devaddr, uint8_t ep_num,
  40459. + uint8_t pipe_type, uint8_t pipe_dir,
  40460. + uint16_t mps)
  40461. +{
  40462. + pipe->dev_addr = devaddr;
  40463. + pipe->ep_num = ep_num;
  40464. + pipe->pipe_type = pipe_type;
  40465. + pipe->pipe_dir = pipe_dir;
  40466. + pipe->mps = mps;
  40467. +}
  40468. +
  40469. +/**
  40470. + * Phases for control transfers.
  40471. + */
  40472. +typedef enum dwc_otg_control_phase {
  40473. + DWC_OTG_CONTROL_SETUP,
  40474. + DWC_OTG_CONTROL_DATA,
  40475. + DWC_OTG_CONTROL_STATUS
  40476. +} dwc_otg_control_phase_e;
  40477. +
  40478. +/** Transaction types. */
  40479. +typedef enum dwc_otg_transaction_type {
  40480. + DWC_OTG_TRANSACTION_NONE = 0,
  40481. + DWC_OTG_TRANSACTION_PERIODIC = 1,
  40482. + DWC_OTG_TRANSACTION_NON_PERIODIC = 2,
  40483. + DWC_OTG_TRANSACTION_ALL = DWC_OTG_TRANSACTION_PERIODIC + DWC_OTG_TRANSACTION_NON_PERIODIC
  40484. +} dwc_otg_transaction_type_e;
  40485. +
  40486. +struct dwc_otg_qh;
  40487. +
  40488. +/**
  40489. + * A Queue Transfer Descriptor (QTD) holds the state of a bulk, control,
  40490. + * interrupt, or isochronous transfer. A single QTD is created for each URB
  40491. + * (of one of these types) submitted to the HCD. The transfer associated with
  40492. + * a QTD may require one or multiple transactions.
  40493. + *
  40494. + * A QTD is linked to a Queue Head, which is entered in either the
  40495. + * non-periodic or periodic schedule for execution. When a QTD is chosen for
  40496. + * execution, some or all of its transactions may be executed. After
  40497. + * execution, the state of the QTD is updated. The QTD may be retired if all
  40498. + * its transactions are complete or if an error occurred. Otherwise, it
  40499. + * remains in the schedule so more transactions can be executed later.
  40500. + */
  40501. +typedef struct dwc_otg_qtd {
  40502. + /**
  40503. + * Determines the PID of the next data packet for the data phase of
  40504. + * control transfers. Ignored for other transfer types.<br>
  40505. + * One of the following values:
  40506. + * - DWC_OTG_HC_PID_DATA0
  40507. + * - DWC_OTG_HC_PID_DATA1
  40508. + */
  40509. + uint8_t data_toggle;
  40510. +
  40511. + /** Current phase for control transfers (Setup, Data, or Status). */
  40512. + dwc_otg_control_phase_e control_phase;
  40513. +
  40514. + /** Keep track of the current split type
  40515. + * for FS/LS endpoints on a HS Hub */
  40516. + uint8_t complete_split;
  40517. +
  40518. + /** How many bytes transferred during SSPLIT OUT */
  40519. + uint32_t ssplit_out_xfer_count;
  40520. +
  40521. + /**
  40522. + * Holds the number of bus errors that have occurred for a transaction
  40523. + * within this transfer.
  40524. + */
  40525. + uint8_t error_count;
  40526. +
  40527. + /**
  40528. + * Index of the next frame descriptor for an isochronous transfer. A
  40529. + * frame descriptor describes the buffer position and length of the
  40530. + * data to be transferred in the next scheduled (micro)frame of an
  40531. + * isochronous transfer. It also holds status for that transaction.
  40532. + * The frame index starts at 0.
  40533. + */
  40534. + uint16_t isoc_frame_index;
  40535. +
  40536. + /** Position of the ISOC split on full/low speed */
  40537. + uint8_t isoc_split_pos;
  40538. +
  40539. + /** Position of the ISOC split in the buffer for the current frame */
  40540. + uint16_t isoc_split_offset;
  40541. +
  40542. + /** URB for this transfer */
  40543. + struct dwc_otg_hcd_urb *urb;
  40544. +
  40545. + struct dwc_otg_qh *qh;
  40546. +
  40547. + /** This list of QTDs */
  40548. + DWC_CIRCLEQ_ENTRY(dwc_otg_qtd) qtd_list_entry;
  40549. +
  40550. + /** Indicates if this QTD is currently processed by HW. */
  40551. + uint8_t in_process;
  40552. +
  40553. + /** Number of DMA descriptors for this QTD */
  40554. + uint8_t n_desc;
  40555. +
  40556. + /**
  40557. + * Last activated frame(packet) index.
  40558. + * Used in Descriptor DMA mode only.
  40559. + */
  40560. + uint16_t isoc_frame_index_last;
  40561. +
  40562. +} dwc_otg_qtd_t;
  40563. +
  40564. +DWC_CIRCLEQ_HEAD(dwc_otg_qtd_list, dwc_otg_qtd);
  40565. +
  40566. +/**
  40567. + * A Queue Head (QH) holds the static characteristics of an endpoint and
  40568. + * maintains a list of transfers (QTDs) for that endpoint. A QH structure may
  40569. + * be entered in either the non-periodic or periodic schedule.
  40570. + */
  40571. +typedef struct dwc_otg_qh {
  40572. + /**
  40573. + * Endpoint type.
  40574. + * One of the following values:
  40575. + * - UE_CONTROL
  40576. + * - UE_BULK
  40577. + * - UE_INTERRUPT
  40578. + * - UE_ISOCHRONOUS
  40579. + */
  40580. + uint8_t ep_type;
  40581. + uint8_t ep_is_in;
  40582. +
  40583. + /** wMaxPacketSize Field of Endpoint Descriptor. */
  40584. + uint16_t maxp;
  40585. +
  40586. + /**
  40587. + * Device speed.
  40588. + * One of the following values:
  40589. + * - DWC_OTG_EP_SPEED_LOW
  40590. + * - DWC_OTG_EP_SPEED_FULL
  40591. + * - DWC_OTG_EP_SPEED_HIGH
  40592. + */
  40593. + uint8_t dev_speed;
  40594. +
  40595. + /**
  40596. + * Determines the PID of the next data packet for non-control
  40597. + * transfers. Ignored for control transfers.<br>
  40598. + * One of the following values:
  40599. + * - DWC_OTG_HC_PID_DATA0
  40600. + * - DWC_OTG_HC_PID_DATA1
  40601. + */
  40602. + uint8_t data_toggle;
  40603. +
  40604. + /** Ping state if 1. */
  40605. + uint8_t ping_state;
  40606. +
  40607. + /**
  40608. + * List of QTDs for this QH.
  40609. + */
  40610. + struct dwc_otg_qtd_list qtd_list;
  40611. +
  40612. + /** Host channel currently processing transfers for this QH. */
  40613. + struct dwc_hc *channel;
  40614. +
  40615. + /** Full/low speed endpoint on high-speed hub requires split. */
  40616. + uint8_t do_split;
  40617. +
  40618. + /** @name Periodic schedule information */
  40619. + /** @{ */
  40620. +
  40621. + /** Bandwidth in microseconds per (micro)frame. */
  40622. + uint16_t usecs;
  40623. +
  40624. + /** Interval between transfers in (micro)frames. */
  40625. + uint16_t interval;
  40626. +
  40627. + /**
  40628. + * (micro)frame to initialize a periodic transfer. The transfer
  40629. + * executes in the following (micro)frame.
  40630. + */
  40631. + uint16_t sched_frame;
  40632. +
  40633. + /*
  40634. + ** Frame a NAK was received on this queue head, used to minimise NAK retransmission
  40635. + */
  40636. + uint16_t nak_frame;
  40637. +
  40638. + /** (micro)frame at which last start split was initialized. */
  40639. + uint16_t start_split_frame;
  40640. +
  40641. + /** @} */
  40642. +
  40643. + /**
  40644. + * Used instead of original buffer if
  40645. + * it(physical address) is not dword-aligned.
  40646. + */
  40647. + uint8_t *dw_align_buf;
  40648. + dwc_dma_t dw_align_buf_dma;
  40649. +
  40650. + /** Entry for QH in either the periodic or non-periodic schedule. */
  40651. + dwc_list_link_t qh_list_entry;
  40652. +
  40653. + /** @name Descriptor DMA support */
  40654. + /** @{ */
  40655. +
  40656. + /** Descriptor List. */
  40657. + dwc_otg_host_dma_desc_t *desc_list;
  40658. +
  40659. + /** Descriptor List physical address. */
  40660. + dwc_dma_t desc_list_dma;
  40661. +
  40662. + /**
  40663. + * Xfer Bytes array.
  40664. + * Each element corresponds to a descriptor and indicates
  40665. + * original XferSize size value for the descriptor.
  40666. + */
  40667. + uint32_t *n_bytes;
  40668. +
  40669. + /** Actual number of transfer descriptors in a list. */
  40670. + uint16_t ntd;
  40671. +
  40672. + /** First activated isochronous transfer descriptor index. */
  40673. + uint8_t td_first;
  40674. + /** Last activated isochronous transfer descriptor index. */
  40675. + uint8_t td_last;
  40676. +
  40677. + /** @} */
  40678. +
  40679. +
  40680. + uint16_t speed;
  40681. + uint16_t frame_usecs[8];
  40682. +
  40683. + uint32_t skip_count;
  40684. +} dwc_otg_qh_t;
  40685. +
  40686. +DWC_CIRCLEQ_HEAD(hc_list, dwc_hc);
  40687. +
  40688. +typedef struct urb_tq_entry {
  40689. + struct urb *urb;
  40690. + DWC_TAILQ_ENTRY(urb_tq_entry) urb_tq_entries;
  40691. +} urb_tq_entry_t;
  40692. +
  40693. +DWC_TAILQ_HEAD(urb_list, urb_tq_entry);
  40694. +
  40695. +/**
  40696. + * This structure holds the state of the HCD, including the non-periodic and
  40697. + * periodic schedules.
  40698. + */
  40699. +struct dwc_otg_hcd {
  40700. + /** The DWC otg device pointer */
  40701. + struct dwc_otg_device *otg_dev;
  40702. + /** DWC OTG Core Interface Layer */
  40703. + dwc_otg_core_if_t *core_if;
  40704. +
  40705. + /** Function HCD driver callbacks */
  40706. + struct dwc_otg_hcd_function_ops *fops;
  40707. +
  40708. + /** Internal DWC HCD Flags */
  40709. + volatile union dwc_otg_hcd_internal_flags {
  40710. + uint32_t d32;
  40711. + struct {
  40712. + unsigned port_connect_status_change:1;
  40713. + unsigned port_connect_status:1;
  40714. + unsigned port_reset_change:1;
  40715. + unsigned port_enable_change:1;
  40716. + unsigned port_suspend_change:1;
  40717. + unsigned port_over_current_change:1;
  40718. + unsigned port_l1_change:1;
  40719. + unsigned reserved:26;
  40720. + } b;
  40721. + } flags;
  40722. +
  40723. + /**
  40724. + * Inactive items in the non-periodic schedule. This is a list of
  40725. + * Queue Heads. Transfers associated with these Queue Heads are not
  40726. + * currently assigned to a host channel.
  40727. + */
  40728. + dwc_list_link_t non_periodic_sched_inactive;
  40729. +
  40730. + /**
  40731. + * Active items in the non-periodic schedule. This is a list of
  40732. + * Queue Heads. Transfers associated with these Queue Heads are
  40733. + * currently assigned to a host channel.
  40734. + */
  40735. + dwc_list_link_t non_periodic_sched_active;
  40736. +
  40737. + /**
  40738. + * Pointer to the next Queue Head to process in the active
  40739. + * non-periodic schedule.
  40740. + */
  40741. + dwc_list_link_t *non_periodic_qh_ptr;
  40742. +
  40743. + /**
  40744. + * Inactive items in the periodic schedule. This is a list of QHs for
  40745. + * periodic transfers that are _not_ scheduled for the next frame.
  40746. + * Each QH in the list has an interval counter that determines when it
  40747. + * needs to be scheduled for execution. This scheduling mechanism
  40748. + * allows only a simple calculation for periodic bandwidth used (i.e.
  40749. + * must assume that all periodic transfers may need to execute in the
  40750. + * same frame). However, it greatly simplifies scheduling and should
  40751. + * be sufficient for the vast majority of OTG hosts, which need to
  40752. + * connect to a small number of peripherals at one time.
  40753. + *
  40754. + * Items move from this list to periodic_sched_ready when the QH
  40755. + * interval counter is 0 at SOF.
  40756. + */
  40757. + dwc_list_link_t periodic_sched_inactive;
  40758. +
  40759. + /**
  40760. + * List of periodic QHs that are ready for execution in the next
  40761. + * frame, but have not yet been assigned to host channels.
  40762. + *
  40763. + * Items move from this list to periodic_sched_assigned as host
  40764. + * channels become available during the current frame.
  40765. + */
  40766. + dwc_list_link_t periodic_sched_ready;
  40767. +
  40768. + /**
  40769. + * List of periodic QHs to be executed in the next frame that are
  40770. + * assigned to host channels.
  40771. + *
  40772. + * Items move from this list to periodic_sched_queued as the
  40773. + * transactions for the QH are queued to the DWC_otg controller.
  40774. + */
  40775. + dwc_list_link_t periodic_sched_assigned;
  40776. +
  40777. + /**
  40778. + * List of periodic QHs that have been queued for execution.
  40779. + *
  40780. + * Items move from this list to either periodic_sched_inactive or
  40781. + * periodic_sched_ready when the channel associated with the transfer
  40782. + * is released. If the interval for the QH is 1, the item moves to
  40783. + * periodic_sched_ready because it must be rescheduled for the next
  40784. + * frame. Otherwise, the item moves to periodic_sched_inactive.
  40785. + */
  40786. + dwc_list_link_t periodic_sched_queued;
  40787. +
  40788. + /**
  40789. + * Total bandwidth claimed so far for periodic transfers. This value
  40790. + * is in microseconds per (micro)frame. The assumption is that all
  40791. + * periodic transfers may occur in the same (micro)frame.
  40792. + */
  40793. + uint16_t periodic_usecs;
  40794. +
  40795. + /**
  40796. + * Total bandwidth claimed so far for all periodic transfers
  40797. + * in a frame.
  40798. + * This will include a mixture of HS and FS transfers.
  40799. + * Units are microseconds per (micro)frame.
  40800. + * We have a budget per frame and have to schedule
  40801. + * transactions accordingly.
  40802. + * Watch out for the fact that things are actually scheduled for the
  40803. + * "next frame".
  40804. + */
  40805. + uint16_t frame_usecs[8];
  40806. +
  40807. +
  40808. + /**
  40809. + * Frame number read from the core at SOF. The value ranges from 0 to
  40810. + * DWC_HFNUM_MAX_FRNUM.
  40811. + */
  40812. + uint16_t frame_number;
  40813. +
  40814. + /**
  40815. + * Count of periodic QHs, if using several eps. For SOF enable/disable.
  40816. + */
  40817. + uint16_t periodic_qh_count;
  40818. +
  40819. + /**
  40820. + * Free host channels in the controller. This is a list of
  40821. + * dwc_hc_t items.
  40822. + */
  40823. + struct hc_list free_hc_list;
  40824. + /**
  40825. + * Number of host channels assigned to periodic transfers. Currently
  40826. + * assuming that there is a dedicated host channel for each periodic
  40827. + * transaction and at least one host channel available for
  40828. + * non-periodic transactions.
  40829. + */
  40830. + int periodic_channels; /* microframe_schedule==0 */
  40831. +
  40832. + /**
  40833. + * Number of host channels assigned to non-periodic transfers.
  40834. + */
  40835. + int non_periodic_channels; /* microframe_schedule==0 */
  40836. +
  40837. + /**
  40838. + * Number of host channels assigned to non-periodic transfers.
  40839. + */
  40840. + int available_host_channels;
  40841. +
  40842. + /**
  40843. + * Array of pointers to the host channel descriptors. Allows accessing
  40844. + * a host channel descriptor given the host channel number. This is
  40845. + * useful in interrupt handlers.
  40846. + */
  40847. + struct dwc_hc *hc_ptr_array[MAX_EPS_CHANNELS];
  40848. +
  40849. + /**
  40850. + * Buffer to use for any data received during the status phase of a
  40851. + * control transfer. Normally no data is transferred during the status
  40852. + * phase. This buffer is used as a bit bucket.
  40853. + */
  40854. + uint8_t *status_buf;
  40855. +
  40856. + /**
  40857. + * DMA address for status_buf.
  40858. + */
  40859. + dma_addr_t status_buf_dma;
  40860. +#define DWC_OTG_HCD_STATUS_BUF_SIZE 64
  40861. +
  40862. + /**
  40863. + * Connection timer. An OTG host must display a message if the device
  40864. + * does not connect. Started when the VBus power is turned on via
  40865. + * sysfs attribute "buspower".
  40866. + */
  40867. + dwc_timer_t *conn_timer;
  40868. +
  40869. + /* Tasket to do a reset */
  40870. + dwc_tasklet_t *reset_tasklet;
  40871. +
  40872. + dwc_tasklet_t *completion_tasklet;
  40873. + struct urb_list completed_urb_list;
  40874. +
  40875. + /* */
  40876. + dwc_spinlock_t *lock;
  40877. + dwc_spinlock_t *channel_lock;
  40878. + /**
  40879. + * Private data that could be used by OS wrapper.
  40880. + */
  40881. + void *priv;
  40882. +
  40883. + uint8_t otg_port;
  40884. +
  40885. + /** Frame List */
  40886. + uint32_t *frame_list;
  40887. +
  40888. + /** Hub - Port assignment */
  40889. + int hub_port[128];
  40890. +#ifdef FIQ_DEBUG
  40891. + int hub_port_alloc[2048];
  40892. +#endif
  40893. +
  40894. + /** Frame List DMA address */
  40895. + dma_addr_t frame_list_dma;
  40896. +
  40897. + struct fiq_stack *fiq_stack;
  40898. + struct fiq_state *fiq_state;
  40899. +
  40900. + /** Virtual address for split transaction DMA bounce buffers */
  40901. + struct fiq_dma_blob *fiq_dmab;
  40902. +
  40903. +#ifdef DEBUG
  40904. + uint32_t frrem_samples;
  40905. + uint64_t frrem_accum;
  40906. +
  40907. + uint32_t hfnum_7_samples_a;
  40908. + uint64_t hfnum_7_frrem_accum_a;
  40909. + uint32_t hfnum_0_samples_a;
  40910. + uint64_t hfnum_0_frrem_accum_a;
  40911. + uint32_t hfnum_other_samples_a;
  40912. + uint64_t hfnum_other_frrem_accum_a;
  40913. +
  40914. + uint32_t hfnum_7_samples_b;
  40915. + uint64_t hfnum_7_frrem_accum_b;
  40916. + uint32_t hfnum_0_samples_b;
  40917. + uint64_t hfnum_0_frrem_accum_b;
  40918. + uint32_t hfnum_other_samples_b;
  40919. + uint64_t hfnum_other_frrem_accum_b;
  40920. +#endif
  40921. +};
  40922. +
  40923. +static inline struct device *dwc_otg_hcd_to_dev(struct dwc_otg_hcd *hcd)
  40924. +{
  40925. + return &hcd->otg_dev->os_dep.platformdev->dev;
  40926. +}
  40927. +
  40928. +/** @name Transaction Execution Functions */
  40929. +/** @{ */
  40930. +extern dwc_otg_transaction_type_e dwc_otg_hcd_select_transactions(dwc_otg_hcd_t
  40931. + * hcd);
  40932. +extern void dwc_otg_hcd_queue_transactions(dwc_otg_hcd_t * hcd,
  40933. + dwc_otg_transaction_type_e tr_type);
  40934. +
  40935. +int dwc_otg_hcd_allocate_port(dwc_otg_hcd_t * hcd, dwc_otg_qh_t *qh);
  40936. +void dwc_otg_hcd_release_port(dwc_otg_hcd_t * dwc_otg_hcd, dwc_otg_qh_t *qh);
  40937. +
  40938. +extern int fiq_fsm_queue_transaction(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh);
  40939. +extern int fiq_fsm_transaction_suitable(dwc_otg_qh_t *qh);
  40940. +extern void dwc_otg_cleanup_fiq_channel(dwc_otg_hcd_t *hcd, uint32_t num);
  40941. +
  40942. +/** @} */
  40943. +
  40944. +/** @name Interrupt Handler Functions */
  40945. +/** @{ */
  40946. +extern int32_t dwc_otg_hcd_handle_intr(dwc_otg_hcd_t * dwc_otg_hcd);
  40947. +extern int32_t dwc_otg_hcd_handle_sof_intr(dwc_otg_hcd_t * dwc_otg_hcd);
  40948. +extern int32_t dwc_otg_hcd_handle_rx_status_q_level_intr(dwc_otg_hcd_t *
  40949. + dwc_otg_hcd);
  40950. +extern int32_t dwc_otg_hcd_handle_np_tx_fifo_empty_intr(dwc_otg_hcd_t *
  40951. + dwc_otg_hcd);
  40952. +extern int32_t dwc_otg_hcd_handle_perio_tx_fifo_empty_intr(dwc_otg_hcd_t *
  40953. + dwc_otg_hcd);
  40954. +extern int32_t dwc_otg_hcd_handle_incomplete_periodic_intr(dwc_otg_hcd_t *
  40955. + dwc_otg_hcd);
  40956. +extern int32_t dwc_otg_hcd_handle_port_intr(dwc_otg_hcd_t * dwc_otg_hcd);
  40957. +extern int32_t dwc_otg_hcd_handle_conn_id_status_change_intr(dwc_otg_hcd_t *
  40958. + dwc_otg_hcd);
  40959. +extern int32_t dwc_otg_hcd_handle_disconnect_intr(dwc_otg_hcd_t * dwc_otg_hcd);
  40960. +extern int32_t dwc_otg_hcd_handle_hc_intr(dwc_otg_hcd_t * dwc_otg_hcd);
  40961. +extern int32_t dwc_otg_hcd_handle_hc_n_intr(dwc_otg_hcd_t * dwc_otg_hcd,
  40962. + uint32_t num);
  40963. +extern int32_t dwc_otg_hcd_handle_session_req_intr(dwc_otg_hcd_t * dwc_otg_hcd);
  40964. +extern int32_t dwc_otg_hcd_handle_wakeup_detected_intr(dwc_otg_hcd_t *
  40965. + dwc_otg_hcd);
  40966. +/** @} */
  40967. +
  40968. +/** @name Schedule Queue Functions */
  40969. +/** @{ */
  40970. +
  40971. +/* Implemented in dwc_otg_hcd_queue.c */
  40972. +extern dwc_otg_qh_t *dwc_otg_hcd_qh_create(dwc_otg_hcd_t * hcd,
  40973. + dwc_otg_hcd_urb_t * urb, int atomic_alloc);
  40974. +extern void dwc_otg_hcd_qh_free(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
  40975. +extern int dwc_otg_hcd_qh_add(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
  40976. +extern void dwc_otg_hcd_qh_remove(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
  40977. +extern void dwc_otg_hcd_qh_deactivate(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh,
  40978. + int sched_csplit);
  40979. +
  40980. +/** Remove and free a QH */
  40981. +static inline void dwc_otg_hcd_qh_remove_and_free(dwc_otg_hcd_t * hcd,
  40982. + dwc_otg_qh_t * qh)
  40983. +{
  40984. + dwc_irqflags_t flags;
  40985. + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
  40986. + dwc_otg_hcd_qh_remove(hcd, qh);
  40987. + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
  40988. + dwc_otg_hcd_qh_free(hcd, qh);
  40989. +}
  40990. +
  40991. +/** Allocates memory for a QH structure.
  40992. + * @return Returns the memory allocate or NULL on error. */
  40993. +static inline dwc_otg_qh_t *dwc_otg_hcd_qh_alloc(int atomic_alloc)
  40994. +{
  40995. + if (atomic_alloc)
  40996. + return (dwc_otg_qh_t *) DWC_ALLOC_ATOMIC(sizeof(dwc_otg_qh_t));
  40997. + else
  40998. + return (dwc_otg_qh_t *) DWC_ALLOC(sizeof(dwc_otg_qh_t));
  40999. +}
  41000. +
  41001. +extern dwc_otg_qtd_t *dwc_otg_hcd_qtd_create(dwc_otg_hcd_urb_t * urb,
  41002. + int atomic_alloc);
  41003. +extern void dwc_otg_hcd_qtd_init(dwc_otg_qtd_t * qtd, dwc_otg_hcd_urb_t * urb);
  41004. +extern int dwc_otg_hcd_qtd_add(dwc_otg_qtd_t * qtd, dwc_otg_hcd_t * dwc_otg_hcd,
  41005. + dwc_otg_qh_t ** qh, int atomic_alloc);
  41006. +
  41007. +/** Allocates memory for a QTD structure.
  41008. + * @return Returns the memory allocate or NULL on error. */
  41009. +static inline dwc_otg_qtd_t *dwc_otg_hcd_qtd_alloc(int atomic_alloc)
  41010. +{
  41011. + if (atomic_alloc)
  41012. + return (dwc_otg_qtd_t *) DWC_ALLOC_ATOMIC(sizeof(dwc_otg_qtd_t));
  41013. + else
  41014. + return (dwc_otg_qtd_t *) DWC_ALLOC(sizeof(dwc_otg_qtd_t));
  41015. +}
  41016. +
  41017. +/** Frees the memory for a QTD structure. QTD should already be removed from
  41018. + * list.
  41019. + * @param qtd QTD to free.*/
  41020. +static inline void dwc_otg_hcd_qtd_free(dwc_otg_qtd_t * qtd)
  41021. +{
  41022. + DWC_FREE(qtd);
  41023. +}
  41024. +
  41025. +/** Removes a QTD from list.
  41026. + * @param hcd HCD instance.
  41027. + * @param qtd QTD to remove from list.
  41028. + * @param qh QTD belongs to.
  41029. + */
  41030. +static inline void dwc_otg_hcd_qtd_remove(dwc_otg_hcd_t * hcd,
  41031. + dwc_otg_qtd_t * qtd,
  41032. + dwc_otg_qh_t * qh)
  41033. +{
  41034. + DWC_CIRCLEQ_REMOVE(&qh->qtd_list, qtd, qtd_list_entry);
  41035. +}
  41036. +
  41037. +/** Remove and free a QTD
  41038. + * Need to disable IRQ and hold hcd lock while calling this function out of
  41039. + * interrupt servicing chain */
  41040. +static inline void dwc_otg_hcd_qtd_remove_and_free(dwc_otg_hcd_t * hcd,
  41041. + dwc_otg_qtd_t * qtd,
  41042. + dwc_otg_qh_t * qh)
  41043. +{
  41044. + dwc_otg_hcd_qtd_remove(hcd, qtd, qh);
  41045. + dwc_otg_hcd_qtd_free(qtd);
  41046. +}
  41047. +
  41048. +/** @} */
  41049. +
  41050. +/** @name Descriptor DMA Supporting Functions */
  41051. +/** @{ */
  41052. +
  41053. +extern void dwc_otg_hcd_start_xfer_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
  41054. +extern void dwc_otg_hcd_complete_xfer_ddma(dwc_otg_hcd_t * hcd,
  41055. + dwc_hc_t * hc,
  41056. + dwc_otg_hc_regs_t * hc_regs,
  41057. + dwc_otg_halt_status_e halt_status);
  41058. +
  41059. +extern int dwc_otg_hcd_qh_init_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
  41060. +extern void dwc_otg_hcd_qh_free_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh);
  41061. +
  41062. +/** @} */
  41063. +
  41064. +/** @name Internal Functions */
  41065. +/** @{ */
  41066. +dwc_otg_qh_t *dwc_urb_to_qh(dwc_otg_hcd_urb_t * urb);
  41067. +/** @} */
  41068. +
  41069. +#ifdef CONFIG_USB_DWC_OTG_LPM
  41070. +extern int dwc_otg_hcd_get_hc_for_lpm_tran(dwc_otg_hcd_t * hcd,
  41071. + uint8_t devaddr);
  41072. +extern void dwc_otg_hcd_free_hc_from_lpm(dwc_otg_hcd_t * hcd);
  41073. +#endif
  41074. +
  41075. +/** Gets the QH that contains the list_head */
  41076. +#define dwc_list_to_qh(_list_head_ptr_) container_of(_list_head_ptr_, dwc_otg_qh_t, qh_list_entry)
  41077. +
  41078. +/** Gets the QTD that contains the list_head */
  41079. +#define dwc_list_to_qtd(_list_head_ptr_) container_of(_list_head_ptr_, dwc_otg_qtd_t, qtd_list_entry)
  41080. +
  41081. +/** Check if QH is non-periodic */
  41082. +#define dwc_qh_is_non_per(_qh_ptr_) ((_qh_ptr_->ep_type == UE_BULK) || \
  41083. + (_qh_ptr_->ep_type == UE_CONTROL))
  41084. +
  41085. +/** High bandwidth multiplier as encoded in highspeed endpoint descriptors */
  41086. +#define dwc_hb_mult(wMaxPacketSize) (1 + (((wMaxPacketSize) >> 11) & 0x03))
  41087. +
  41088. +/** Packet size for any kind of endpoint descriptor */
  41089. +#define dwc_max_packet(wMaxPacketSize) ((wMaxPacketSize) & 0x07ff)
  41090. +
  41091. +/**
  41092. + * Returns true if _frame1 is less than or equal to _frame2. The comparison is
  41093. + * done modulo DWC_HFNUM_MAX_FRNUM. This accounts for the rollover of the
  41094. + * frame number when the max frame number is reached.
  41095. + */
  41096. +static inline int dwc_frame_num_le(uint16_t frame1, uint16_t frame2)
  41097. +{
  41098. + return ((frame2 - frame1) & DWC_HFNUM_MAX_FRNUM) <=
  41099. + (DWC_HFNUM_MAX_FRNUM >> 1);
  41100. +}
  41101. +
  41102. +/**
  41103. + * Returns true if _frame1 is greater than _frame2. The comparison is done
  41104. + * modulo DWC_HFNUM_MAX_FRNUM. This accounts for the rollover of the frame
  41105. + * number when the max frame number is reached.
  41106. + */
  41107. +static inline int dwc_frame_num_gt(uint16_t frame1, uint16_t frame2)
  41108. +{
  41109. + return (frame1 != frame2) &&
  41110. + (((frame1 - frame2) & DWC_HFNUM_MAX_FRNUM) <
  41111. + (DWC_HFNUM_MAX_FRNUM >> 1));
  41112. +}
  41113. +
  41114. +/**
  41115. + * Increments _frame by the amount specified by _inc. The addition is done
  41116. + * modulo DWC_HFNUM_MAX_FRNUM. Returns the incremented value.
  41117. + */
  41118. +static inline uint16_t dwc_frame_num_inc(uint16_t frame, uint16_t inc)
  41119. +{
  41120. + return (frame + inc) & DWC_HFNUM_MAX_FRNUM;
  41121. +}
  41122. +
  41123. +static inline uint16_t dwc_full_frame_num(uint16_t frame)
  41124. +{
  41125. + return (frame & DWC_HFNUM_MAX_FRNUM) >> 3;
  41126. +}
  41127. +
  41128. +static inline uint16_t dwc_micro_frame_num(uint16_t frame)
  41129. +{
  41130. + return frame & 0x7;
  41131. +}
  41132. +
  41133. +void dwc_otg_hcd_save_data_toggle(dwc_hc_t * hc,
  41134. + dwc_otg_hc_regs_t * hc_regs,
  41135. + dwc_otg_qtd_t * qtd);
  41136. +
  41137. +#ifdef DEBUG
  41138. +/**
  41139. + * Macro to sample the remaining PHY clocks left in the current frame. This
  41140. + * may be used during debugging to determine the average time it takes to
  41141. + * execute sections of code. There are two possible sample points, "a" and
  41142. + * "b", so the _letter argument must be one of these values.
  41143. + *
  41144. + * To dump the average sample times, read the "hcd_frrem" sysfs attribute. For
  41145. + * example, "cat /sys/devices/lm0/hcd_frrem".
  41146. + */
  41147. +#define dwc_sample_frrem(_hcd, _qh, _letter) \
  41148. +{ \
  41149. + hfnum_data_t hfnum; \
  41150. + dwc_otg_qtd_t *qtd; \
  41151. + qtd = list_entry(_qh->qtd_list.next, dwc_otg_qtd_t, qtd_list_entry); \
  41152. + if (usb_pipeint(qtd->urb->pipe) && _qh->start_split_frame != 0 && !qtd->complete_split) { \
  41153. + hfnum.d32 = DWC_READ_REG32(&_hcd->core_if->host_if->host_global_regs->hfnum); \
  41154. + switch (hfnum.b.frnum & 0x7) { \
  41155. + case 7: \
  41156. + _hcd->hfnum_7_samples_##_letter++; \
  41157. + _hcd->hfnum_7_frrem_accum_##_letter += hfnum.b.frrem; \
  41158. + break; \
  41159. + case 0: \
  41160. + _hcd->hfnum_0_samples_##_letter++; \
  41161. + _hcd->hfnum_0_frrem_accum_##_letter += hfnum.b.frrem; \
  41162. + break; \
  41163. + default: \
  41164. + _hcd->hfnum_other_samples_##_letter++; \
  41165. + _hcd->hfnum_other_frrem_accum_##_letter += hfnum.b.frrem; \
  41166. + break; \
  41167. + } \
  41168. + } \
  41169. +}
  41170. +#else
  41171. +#define dwc_sample_frrem(_hcd, _qh, _letter)
  41172. +#endif
  41173. +#endif
  41174. +#endif /* DWC_DEVICE_ONLY */
  41175. --- /dev/null
  41176. +++ b/drivers/usb/host/dwc_otg/dwc_otg_hcd_ddma.c
  41177. @@ -0,0 +1,1139 @@
  41178. +/*==========================================================================
  41179. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd_ddma.c $
  41180. + * $Revision: #10 $
  41181. + * $Date: 2011/10/20 $
  41182. + * $Change: 1869464 $
  41183. + *
  41184. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  41185. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  41186. + * otherwise expressly agreed to in writing between Synopsys and you.
  41187. + *
  41188. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  41189. + * any End User Software License Agreement or Agreement for Licensed Product
  41190. + * with Synopsys or any supplement thereto. You are permitted to use and
  41191. + * redistribute this Software in source and binary forms, with or without
  41192. + * modification, provided that redistributions of source code must retain this
  41193. + * notice. You may not view, use, disclose, copy or distribute this file or
  41194. + * any information contained herein except pursuant to this license grant from
  41195. + * Synopsys. If you do not agree with this notice, including the disclaimer
  41196. + * below, then you are not authorized to use the Software.
  41197. + *
  41198. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  41199. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  41200. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  41201. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  41202. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  41203. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  41204. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  41205. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  41206. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  41207. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  41208. + * DAMAGE.
  41209. + * ========================================================================== */
  41210. +#ifndef DWC_DEVICE_ONLY
  41211. +
  41212. +/** @file
  41213. + * This file contains Descriptor DMA support implementation for host mode.
  41214. + */
  41215. +
  41216. +#include "dwc_otg_hcd.h"
  41217. +#include "dwc_otg_regs.h"
  41218. +
  41219. +extern bool microframe_schedule;
  41220. +
  41221. +static inline uint8_t frame_list_idx(uint16_t frame)
  41222. +{
  41223. + return (frame & (MAX_FRLIST_EN_NUM - 1));
  41224. +}
  41225. +
  41226. +static inline uint16_t desclist_idx_inc(uint16_t idx, uint16_t inc, uint8_t speed)
  41227. +{
  41228. + return (idx + inc) &
  41229. + (((speed ==
  41230. + DWC_OTG_EP_SPEED_HIGH) ? MAX_DMA_DESC_NUM_HS_ISOC :
  41231. + MAX_DMA_DESC_NUM_GENERIC) - 1);
  41232. +}
  41233. +
  41234. +static inline uint16_t desclist_idx_dec(uint16_t idx, uint16_t inc, uint8_t speed)
  41235. +{
  41236. + return (idx - inc) &
  41237. + (((speed ==
  41238. + DWC_OTG_EP_SPEED_HIGH) ? MAX_DMA_DESC_NUM_HS_ISOC :
  41239. + MAX_DMA_DESC_NUM_GENERIC) - 1);
  41240. +}
  41241. +
  41242. +static inline uint16_t max_desc_num(dwc_otg_qh_t * qh)
  41243. +{
  41244. + return (((qh->ep_type == UE_ISOCHRONOUS)
  41245. + && (qh->dev_speed == DWC_OTG_EP_SPEED_HIGH))
  41246. + ? MAX_DMA_DESC_NUM_HS_ISOC : MAX_DMA_DESC_NUM_GENERIC);
  41247. +}
  41248. +static inline uint16_t frame_incr_val(dwc_otg_qh_t * qh)
  41249. +{
  41250. + return ((qh->dev_speed == DWC_OTG_EP_SPEED_HIGH)
  41251. + ? ((qh->interval + 8 - 1) / 8)
  41252. + : qh->interval);
  41253. +}
  41254. +
  41255. +static int desc_list_alloc(struct device *dev, dwc_otg_qh_t * qh)
  41256. +{
  41257. + int retval = 0;
  41258. +
  41259. + qh->desc_list = (dwc_otg_host_dma_desc_t *)
  41260. + DWC_DMA_ALLOC(dev, sizeof(dwc_otg_host_dma_desc_t) * max_desc_num(qh),
  41261. + &qh->desc_list_dma);
  41262. +
  41263. + if (!qh->desc_list) {
  41264. + retval = -DWC_E_NO_MEMORY;
  41265. + DWC_ERROR("%s: DMA descriptor list allocation failed\n", __func__);
  41266. +
  41267. + }
  41268. +
  41269. + dwc_memset(qh->desc_list, 0x00,
  41270. + sizeof(dwc_otg_host_dma_desc_t) * max_desc_num(qh));
  41271. +
  41272. + qh->n_bytes =
  41273. + (uint32_t *) DWC_ALLOC(sizeof(uint32_t) * max_desc_num(qh));
  41274. +
  41275. + if (!qh->n_bytes) {
  41276. + retval = -DWC_E_NO_MEMORY;
  41277. + DWC_ERROR
  41278. + ("%s: Failed to allocate array for descriptors' size actual values\n",
  41279. + __func__);
  41280. +
  41281. + }
  41282. + return retval;
  41283. +
  41284. +}
  41285. +
  41286. +static void desc_list_free(struct device *dev, dwc_otg_qh_t * qh)
  41287. +{
  41288. + if (qh->desc_list) {
  41289. + DWC_DMA_FREE(dev, max_desc_num(qh), qh->desc_list,
  41290. + qh->desc_list_dma);
  41291. + qh->desc_list = NULL;
  41292. + }
  41293. +
  41294. + if (qh->n_bytes) {
  41295. + DWC_FREE(qh->n_bytes);
  41296. + qh->n_bytes = NULL;
  41297. + }
  41298. +}
  41299. +
  41300. +static int frame_list_alloc(dwc_otg_hcd_t * hcd)
  41301. +{
  41302. + struct device *dev = dwc_otg_hcd_to_dev(hcd);
  41303. + int retval = 0;
  41304. +
  41305. + if (hcd->frame_list)
  41306. + return 0;
  41307. +
  41308. + hcd->frame_list = DWC_DMA_ALLOC(dev, 4 * MAX_FRLIST_EN_NUM,
  41309. + &hcd->frame_list_dma);
  41310. + if (!hcd->frame_list) {
  41311. + retval = -DWC_E_NO_MEMORY;
  41312. + DWC_ERROR("%s: Frame List allocation failed\n", __func__);
  41313. + }
  41314. +
  41315. + dwc_memset(hcd->frame_list, 0x00, 4 * MAX_FRLIST_EN_NUM);
  41316. +
  41317. + return retval;
  41318. +}
  41319. +
  41320. +static void frame_list_free(dwc_otg_hcd_t * hcd)
  41321. +{
  41322. + struct device *dev = dwc_otg_hcd_to_dev(hcd);
  41323. +
  41324. + if (!hcd->frame_list)
  41325. + return;
  41326. +
  41327. + DWC_DMA_FREE(dev, 4 * MAX_FRLIST_EN_NUM, hcd->frame_list, hcd->frame_list_dma);
  41328. + hcd->frame_list = NULL;
  41329. +}
  41330. +
  41331. +static void per_sched_enable(dwc_otg_hcd_t * hcd, uint16_t fr_list_en)
  41332. +{
  41333. +
  41334. + hcfg_data_t hcfg;
  41335. +
  41336. + hcfg.d32 = DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hcfg);
  41337. +
  41338. + if (hcfg.b.perschedena) {
  41339. + /* already enabled */
  41340. + return;
  41341. + }
  41342. +
  41343. + DWC_WRITE_REG32(&hcd->core_if->host_if->host_global_regs->hflbaddr,
  41344. + hcd->frame_list_dma);
  41345. +
  41346. + switch (fr_list_en) {
  41347. + case 64:
  41348. + hcfg.b.frlisten = 3;
  41349. + break;
  41350. + case 32:
  41351. + hcfg.b.frlisten = 2;
  41352. + break;
  41353. + case 16:
  41354. + hcfg.b.frlisten = 1;
  41355. + break;
  41356. + case 8:
  41357. + hcfg.b.frlisten = 0;
  41358. + break;
  41359. + default:
  41360. + break;
  41361. + }
  41362. +
  41363. + hcfg.b.perschedena = 1;
  41364. +
  41365. + DWC_DEBUGPL(DBG_HCD, "Enabling Periodic schedule\n");
  41366. + DWC_WRITE_REG32(&hcd->core_if->host_if->host_global_regs->hcfg, hcfg.d32);
  41367. +
  41368. +}
  41369. +
  41370. +static void per_sched_disable(dwc_otg_hcd_t * hcd)
  41371. +{
  41372. + hcfg_data_t hcfg;
  41373. +
  41374. + hcfg.d32 = DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hcfg);
  41375. +
  41376. + if (!hcfg.b.perschedena) {
  41377. + /* already disabled */
  41378. + return;
  41379. + }
  41380. + hcfg.b.perschedena = 0;
  41381. +
  41382. + DWC_DEBUGPL(DBG_HCD, "Disabling Periodic schedule\n");
  41383. + DWC_WRITE_REG32(&hcd->core_if->host_if->host_global_regs->hcfg, hcfg.d32);
  41384. +}
  41385. +
  41386. +/*
  41387. + * Activates/Deactivates FrameList entries for the channel
  41388. + * based on endpoint servicing period.
  41389. + */
  41390. +void update_frame_list(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh, uint8_t enable)
  41391. +{
  41392. + uint16_t i, j, inc;
  41393. + dwc_hc_t *hc = NULL;
  41394. +
  41395. + if (!qh->channel) {
  41396. + DWC_ERROR("qh->channel = %p", qh->channel);
  41397. + return;
  41398. + }
  41399. +
  41400. + if (!hcd) {
  41401. + DWC_ERROR("------hcd = %p", hcd);
  41402. + return;
  41403. + }
  41404. +
  41405. + if (!hcd->frame_list) {
  41406. + DWC_ERROR("-------hcd->frame_list = %p", hcd->frame_list);
  41407. + return;
  41408. + }
  41409. +
  41410. + hc = qh->channel;
  41411. + inc = frame_incr_val(qh);
  41412. + if (qh->ep_type == UE_ISOCHRONOUS)
  41413. + i = frame_list_idx(qh->sched_frame);
  41414. + else
  41415. + i = 0;
  41416. +
  41417. + j = i;
  41418. + do {
  41419. + if (enable)
  41420. + hcd->frame_list[j] |= (1 << hc->hc_num);
  41421. + else
  41422. + hcd->frame_list[j] &= ~(1 << hc->hc_num);
  41423. + j = (j + inc) & (MAX_FRLIST_EN_NUM - 1);
  41424. + }
  41425. + while (j != i);
  41426. + if (!enable)
  41427. + return;
  41428. + hc->schinfo = 0;
  41429. + if (qh->channel->speed == DWC_OTG_EP_SPEED_HIGH) {
  41430. + j = 1;
  41431. + /* TODO - check this */
  41432. + inc = (8 + qh->interval - 1) / qh->interval;
  41433. + for (i = 0; i < inc; i++) {
  41434. + hc->schinfo |= j;
  41435. + j = j << qh->interval;
  41436. + }
  41437. + } else {
  41438. + hc->schinfo = 0xff;
  41439. + }
  41440. +}
  41441. +
  41442. +#if 1
  41443. +void dump_frame_list(dwc_otg_hcd_t * hcd)
  41444. +{
  41445. + int i = 0;
  41446. + DWC_PRINTF("--FRAME LIST (hex) --\n");
  41447. + for (i = 0; i < MAX_FRLIST_EN_NUM; i++) {
  41448. + DWC_PRINTF("%x\t", hcd->frame_list[i]);
  41449. + if (!(i % 8) && i)
  41450. + DWC_PRINTF("\n");
  41451. + }
  41452. + DWC_PRINTF("\n----\n");
  41453. +
  41454. +}
  41455. +#endif
  41456. +
  41457. +static void release_channel_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  41458. +{
  41459. + dwc_irqflags_t flags;
  41460. + dwc_spinlock_t *channel_lock = hcd->channel_lock;
  41461. +
  41462. + dwc_hc_t *hc = qh->channel;
  41463. + if (dwc_qh_is_non_per(qh)) {
  41464. + DWC_SPINLOCK_IRQSAVE(channel_lock, &flags);
  41465. + if (!microframe_schedule)
  41466. + hcd->non_periodic_channels--;
  41467. + else
  41468. + hcd->available_host_channels++;
  41469. + DWC_SPINUNLOCK_IRQRESTORE(channel_lock, flags);
  41470. + } else
  41471. + update_frame_list(hcd, qh, 0);
  41472. +
  41473. + /*
  41474. + * The condition is added to prevent double cleanup try in case of device
  41475. + * disconnect. See channel cleanup in dwc_otg_hcd_disconnect_cb().
  41476. + */
  41477. + if (hc->qh) {
  41478. + dwc_otg_hc_cleanup(hcd->core_if, hc);
  41479. + DWC_CIRCLEQ_INSERT_TAIL(&hcd->free_hc_list, hc, hc_list_entry);
  41480. + hc->qh = NULL;
  41481. + }
  41482. +
  41483. + qh->channel = NULL;
  41484. + qh->ntd = 0;
  41485. +
  41486. + if (qh->desc_list) {
  41487. + dwc_memset(qh->desc_list, 0x00,
  41488. + sizeof(dwc_otg_host_dma_desc_t) * max_desc_num(qh));
  41489. + }
  41490. +}
  41491. +
  41492. +/**
  41493. + * Initializes a QH structure's Descriptor DMA related members.
  41494. + * Allocates memory for descriptor list.
  41495. + * On first periodic QH, allocates memory for FrameList
  41496. + * and enables periodic scheduling.
  41497. + *
  41498. + * @param hcd The HCD state structure for the DWC OTG controller.
  41499. + * @param qh The QH to init.
  41500. + *
  41501. + * @return 0 if successful, negative error code otherwise.
  41502. + */
  41503. +int dwc_otg_hcd_qh_init_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  41504. +{
  41505. + struct device *dev = dwc_otg_hcd_to_dev(hcd);
  41506. + int retval = 0;
  41507. +
  41508. + if (qh->do_split) {
  41509. + DWC_ERROR("SPLIT Transfers are not supported in Descriptor DMA.\n");
  41510. + return -1;
  41511. + }
  41512. +
  41513. + retval = desc_list_alloc(dev, qh);
  41514. +
  41515. + if ((retval == 0)
  41516. + && (qh->ep_type == UE_ISOCHRONOUS || qh->ep_type == UE_INTERRUPT)) {
  41517. + if (!hcd->frame_list) {
  41518. + retval = frame_list_alloc(hcd);
  41519. + /* Enable periodic schedule on first periodic QH */
  41520. + if (retval == 0)
  41521. + per_sched_enable(hcd, MAX_FRLIST_EN_NUM);
  41522. + }
  41523. + }
  41524. +
  41525. + qh->ntd = 0;
  41526. +
  41527. + return retval;
  41528. +}
  41529. +
  41530. +/**
  41531. + * Frees descriptor list memory associated with the QH.
  41532. + * If QH is periodic and the last, frees FrameList memory
  41533. + * and disables periodic scheduling.
  41534. + *
  41535. + * @param hcd The HCD state structure for the DWC OTG controller.
  41536. + * @param qh The QH to init.
  41537. + */
  41538. +void dwc_otg_hcd_qh_free_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  41539. +{
  41540. + struct device *dev = dwc_otg_hcd_to_dev(hcd);
  41541. +
  41542. + desc_list_free(dev, qh);
  41543. +
  41544. + /*
  41545. + * Channel still assigned due to some reasons.
  41546. + * Seen on Isoc URB dequeue. Channel halted but no subsequent
  41547. + * ChHalted interrupt to release the channel. Afterwards
  41548. + * when it comes here from endpoint disable routine
  41549. + * channel remains assigned.
  41550. + */
  41551. + if (qh->channel)
  41552. + release_channel_ddma(hcd, qh);
  41553. +
  41554. + if ((qh->ep_type == UE_ISOCHRONOUS || qh->ep_type == UE_INTERRUPT)
  41555. + && (microframe_schedule || !hcd->periodic_channels) && hcd->frame_list) {
  41556. +
  41557. + per_sched_disable(hcd);
  41558. + frame_list_free(hcd);
  41559. + }
  41560. +}
  41561. +
  41562. +static uint8_t frame_to_desc_idx(dwc_otg_qh_t * qh, uint16_t frame_idx)
  41563. +{
  41564. + if (qh->dev_speed == DWC_OTG_EP_SPEED_HIGH) {
  41565. + /*
  41566. + * Descriptor set(8 descriptors) index
  41567. + * which is 8-aligned.
  41568. + */
  41569. + return (frame_idx & ((MAX_DMA_DESC_NUM_HS_ISOC / 8) - 1)) * 8;
  41570. + } else {
  41571. + return (frame_idx & (MAX_DMA_DESC_NUM_GENERIC - 1));
  41572. + }
  41573. +}
  41574. +
  41575. +/*
  41576. + * Determine starting frame for Isochronous transfer.
  41577. + * Few frames skipped to prevent race condition with HC.
  41578. + */
  41579. +static uint8_t calc_starting_frame(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh,
  41580. + uint8_t * skip_frames)
  41581. +{
  41582. + uint16_t frame = 0;
  41583. + hcd->frame_number = dwc_otg_hcd_get_frame_number(hcd);
  41584. +
  41585. + /* sched_frame is always frame number(not uFrame) both in FS and HS !! */
  41586. +
  41587. + /*
  41588. + * skip_frames is used to limit activated descriptors number
  41589. + * to avoid the situation when HC services the last activated
  41590. + * descriptor firstly.
  41591. + * Example for FS:
  41592. + * Current frame is 1, scheduled frame is 3. Since HC always fetches the descriptor
  41593. + * corresponding to curr_frame+1, the descriptor corresponding to frame 2
  41594. + * will be fetched. If the number of descriptors is max=64 (or greather) the
  41595. + * list will be fully programmed with Active descriptors and it is possible
  41596. + * case(rare) that the latest descriptor(considering rollback) corresponding
  41597. + * to frame 2 will be serviced first. HS case is more probable because, in fact,
  41598. + * up to 11 uframes(16 in the code) may be skipped.
  41599. + */
  41600. + if (qh->dev_speed == DWC_OTG_EP_SPEED_HIGH) {
  41601. + /*
  41602. + * Consider uframe counter also, to start xfer asap.
  41603. + * If half of the frame elapsed skip 2 frames otherwise
  41604. + * just 1 frame.
  41605. + * Starting descriptor index must be 8-aligned, so
  41606. + * if the current frame is near to complete the next one
  41607. + * is skipped as well.
  41608. + */
  41609. +
  41610. + if (dwc_micro_frame_num(hcd->frame_number) >= 5) {
  41611. + *skip_frames = 2 * 8;
  41612. + frame = dwc_frame_num_inc(hcd->frame_number, *skip_frames);
  41613. + } else {
  41614. + *skip_frames = 1 * 8;
  41615. + frame = dwc_frame_num_inc(hcd->frame_number, *skip_frames);
  41616. + }
  41617. +
  41618. + frame = dwc_full_frame_num(frame);
  41619. + } else {
  41620. + /*
  41621. + * Two frames are skipped for FS - the current and the next.
  41622. + * But for descriptor programming, 1 frame(descriptor) is enough,
  41623. + * see example above.
  41624. + */
  41625. + *skip_frames = 1;
  41626. + frame = dwc_frame_num_inc(hcd->frame_number, 2);
  41627. + }
  41628. +
  41629. + return frame;
  41630. +}
  41631. +
  41632. +/*
  41633. + * Calculate initial descriptor index for isochronous transfer
  41634. + * based on scheduled frame.
  41635. + */
  41636. +static uint8_t recalc_initial_desc_idx(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  41637. +{
  41638. + uint16_t frame = 0, fr_idx, fr_idx_tmp;
  41639. + uint8_t skip_frames = 0;
  41640. + /*
  41641. + * With current ISOC processing algorithm the channel is being
  41642. + * released when no more QTDs in the list(qh->ntd == 0).
  41643. + * Thus this function is called only when qh->ntd == 0 and qh->channel == 0.
  41644. + *
  41645. + * So qh->channel != NULL branch is not used and just not removed from the
  41646. + * source file. It is required for another possible approach which is,
  41647. + * do not disable and release the channel when ISOC session completed,
  41648. + * just move QH to inactive schedule until new QTD arrives.
  41649. + * On new QTD, the QH moved back to 'ready' schedule,
  41650. + * starting frame and therefore starting desc_index are recalculated.
  41651. + * In this case channel is released only on ep_disable.
  41652. + */
  41653. +
  41654. + /* Calculate starting descriptor index. For INTERRUPT endpoint it is always 0. */
  41655. + if (qh->channel) {
  41656. + frame = calc_starting_frame(hcd, qh, &skip_frames);
  41657. + /*
  41658. + * Calculate initial descriptor index based on FrameList current bitmap
  41659. + * and servicing period.
  41660. + */
  41661. + fr_idx_tmp = frame_list_idx(frame);
  41662. + fr_idx =
  41663. + (MAX_FRLIST_EN_NUM + frame_list_idx(qh->sched_frame) -
  41664. + fr_idx_tmp)
  41665. + % frame_incr_val(qh);
  41666. + fr_idx = (fr_idx + fr_idx_tmp) % MAX_FRLIST_EN_NUM;
  41667. + } else {
  41668. + qh->sched_frame = calc_starting_frame(hcd, qh, &skip_frames);
  41669. + fr_idx = frame_list_idx(qh->sched_frame);
  41670. + }
  41671. +
  41672. + qh->td_first = qh->td_last = frame_to_desc_idx(qh, fr_idx);
  41673. +
  41674. + return skip_frames;
  41675. +}
  41676. +
  41677. +#define ISOC_URB_GIVEBACK_ASAP
  41678. +
  41679. +#define MAX_ISOC_XFER_SIZE_FS 1023
  41680. +#define MAX_ISOC_XFER_SIZE_HS 3072
  41681. +#define DESCNUM_THRESHOLD 4
  41682. +
  41683. +static void init_isoc_dma_desc(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh,
  41684. + uint8_t skip_frames)
  41685. +{
  41686. + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
  41687. + dwc_otg_qtd_t *qtd;
  41688. + dwc_otg_host_dma_desc_t *dma_desc;
  41689. + uint16_t idx, inc, n_desc, ntd_max, max_xfer_size;
  41690. +
  41691. + idx = qh->td_last;
  41692. + inc = qh->interval;
  41693. + n_desc = 0;
  41694. +
  41695. + ntd_max = (max_desc_num(qh) + qh->interval - 1) / qh->interval;
  41696. + if (skip_frames && !qh->channel)
  41697. + ntd_max = ntd_max - skip_frames / qh->interval;
  41698. +
  41699. + max_xfer_size =
  41700. + (qh->dev_speed ==
  41701. + DWC_OTG_EP_SPEED_HIGH) ? MAX_ISOC_XFER_SIZE_HS :
  41702. + MAX_ISOC_XFER_SIZE_FS;
  41703. +
  41704. + DWC_CIRCLEQ_FOREACH(qtd, &qh->qtd_list, qtd_list_entry) {
  41705. + while ((qh->ntd < ntd_max)
  41706. + && (qtd->isoc_frame_index_last <
  41707. + qtd->urb->packet_count)) {
  41708. +
  41709. + dma_desc = &qh->desc_list[idx];
  41710. + dwc_memset(dma_desc, 0x00, sizeof(dwc_otg_host_dma_desc_t));
  41711. +
  41712. + frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index_last];
  41713. +
  41714. + if (frame_desc->length > max_xfer_size)
  41715. + qh->n_bytes[idx] = max_xfer_size;
  41716. + else
  41717. + qh->n_bytes[idx] = frame_desc->length;
  41718. + dma_desc->status.b_isoc.n_bytes = qh->n_bytes[idx];
  41719. + dma_desc->status.b_isoc.a = 1;
  41720. + dma_desc->status.b_isoc.sts = 0;
  41721. +
  41722. + dma_desc->buf = qtd->urb->dma + frame_desc->offset;
  41723. +
  41724. + qh->ntd++;
  41725. +
  41726. + qtd->isoc_frame_index_last++;
  41727. +
  41728. +#ifdef ISOC_URB_GIVEBACK_ASAP
  41729. + /*
  41730. + * Set IOC for each descriptor corresponding to the
  41731. + * last frame of the URB.
  41732. + */
  41733. + if (qtd->isoc_frame_index_last ==
  41734. + qtd->urb->packet_count)
  41735. + dma_desc->status.b_isoc.ioc = 1;
  41736. +
  41737. +#endif
  41738. + idx = desclist_idx_inc(idx, inc, qh->dev_speed);
  41739. + n_desc++;
  41740. +
  41741. + }
  41742. + qtd->in_process = 1;
  41743. + }
  41744. +
  41745. + qh->td_last = idx;
  41746. +
  41747. +#ifdef ISOC_URB_GIVEBACK_ASAP
  41748. + /* Set IOC for the last descriptor if descriptor list is full */
  41749. + if (qh->ntd == ntd_max) {
  41750. + idx = desclist_idx_dec(qh->td_last, inc, qh->dev_speed);
  41751. + qh->desc_list[idx].status.b_isoc.ioc = 1;
  41752. + }
  41753. +#else
  41754. + /*
  41755. + * Set IOC bit only for one descriptor.
  41756. + * Always try to be ahead of HW processing,
  41757. + * i.e. on IOC generation driver activates next descriptors but
  41758. + * core continues to process descriptors followed the one with IOC set.
  41759. + */
  41760. +
  41761. + if (n_desc > DESCNUM_THRESHOLD) {
  41762. + /*
  41763. + * Move IOC "up". Required even if there is only one QTD
  41764. + * in the list, cause QTDs migth continue to be queued,
  41765. + * but during the activation it was only one queued.
  41766. + * Actually more than one QTD might be in the list if this function called
  41767. + * from XferCompletion - QTDs was queued during HW processing of the previous
  41768. + * descriptor chunk.
  41769. + */
  41770. + idx = dwc_desclist_idx_dec(idx, inc * ((qh->ntd + 1) / 2), qh->dev_speed);
  41771. + } else {
  41772. + /*
  41773. + * Set the IOC for the latest descriptor
  41774. + * if either number of descriptor is not greather than threshold
  41775. + * or no more new descriptors activated.
  41776. + */
  41777. + idx = dwc_desclist_idx_dec(qh->td_last, inc, qh->dev_speed);
  41778. + }
  41779. +
  41780. + qh->desc_list[idx].status.b_isoc.ioc = 1;
  41781. +#endif
  41782. +}
  41783. +
  41784. +static void init_non_isoc_dma_desc(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  41785. +{
  41786. +
  41787. + dwc_hc_t *hc;
  41788. + dwc_otg_host_dma_desc_t *dma_desc;
  41789. + dwc_otg_qtd_t *qtd;
  41790. + int num_packets, len, n_desc = 0;
  41791. +
  41792. + hc = qh->channel;
  41793. +
  41794. + /*
  41795. + * Start with hc->xfer_buff initialized in
  41796. + * assign_and_init_hc(), then if SG transfer consists of multiple URBs,
  41797. + * this pointer re-assigned to the buffer of the currently processed QTD.
  41798. + * For non-SG request there is always one QTD active.
  41799. + */
  41800. +
  41801. + DWC_CIRCLEQ_FOREACH(qtd, &qh->qtd_list, qtd_list_entry) {
  41802. +
  41803. + if (n_desc) {
  41804. + /* SG request - more than 1 QTDs */
  41805. + hc->xfer_buff = (uint8_t *)qtd->urb->dma + qtd->urb->actual_length;
  41806. + hc->xfer_len = qtd->urb->length - qtd->urb->actual_length;
  41807. + }
  41808. +
  41809. + qtd->n_desc = 0;
  41810. +
  41811. + do {
  41812. + dma_desc = &qh->desc_list[n_desc];
  41813. + len = hc->xfer_len;
  41814. +
  41815. + if (len > MAX_DMA_DESC_SIZE)
  41816. + len = MAX_DMA_DESC_SIZE - hc->max_packet + 1;
  41817. +
  41818. + if (hc->ep_is_in) {
  41819. + if (len > 0) {
  41820. + num_packets = (len + hc->max_packet - 1) / hc->max_packet;
  41821. + } else {
  41822. + /* Need 1 packet for transfer length of 0. */
  41823. + num_packets = 1;
  41824. + }
  41825. + /* Always program an integral # of max packets for IN transfers. */
  41826. + len = num_packets * hc->max_packet;
  41827. + }
  41828. +
  41829. + dma_desc->status.b.n_bytes = len;
  41830. +
  41831. + qh->n_bytes[n_desc] = len;
  41832. +
  41833. + if ((qh->ep_type == UE_CONTROL)
  41834. + && (qtd->control_phase == DWC_OTG_CONTROL_SETUP))
  41835. + dma_desc->status.b.sup = 1; /* Setup Packet */
  41836. +
  41837. + dma_desc->status.b.a = 1; /* Active descriptor */
  41838. + dma_desc->status.b.sts = 0;
  41839. +
  41840. + dma_desc->buf =
  41841. + ((unsigned long)hc->xfer_buff & 0xffffffff);
  41842. +
  41843. + /*
  41844. + * Last descriptor(or single) of IN transfer
  41845. + * with actual size less than MaxPacket.
  41846. + */
  41847. + if (len > hc->xfer_len) {
  41848. + hc->xfer_len = 0;
  41849. + } else {
  41850. + hc->xfer_buff += len;
  41851. + hc->xfer_len -= len;
  41852. + }
  41853. +
  41854. + qtd->n_desc++;
  41855. + n_desc++;
  41856. + }
  41857. + while ((hc->xfer_len > 0) && (n_desc != MAX_DMA_DESC_NUM_GENERIC));
  41858. +
  41859. +
  41860. + qtd->in_process = 1;
  41861. +
  41862. + if (qh->ep_type == UE_CONTROL)
  41863. + break;
  41864. +
  41865. + if (n_desc == MAX_DMA_DESC_NUM_GENERIC)
  41866. + break;
  41867. + }
  41868. +
  41869. + if (n_desc) {
  41870. + /* Request Transfer Complete interrupt for the last descriptor */
  41871. + qh->desc_list[n_desc - 1].status.b.ioc = 1;
  41872. + /* End of List indicator */
  41873. + qh->desc_list[n_desc - 1].status.b.eol = 1;
  41874. +
  41875. + hc->ntd = n_desc;
  41876. + }
  41877. +}
  41878. +
  41879. +/**
  41880. + * For Control and Bulk endpoints initializes descriptor list
  41881. + * and starts the transfer.
  41882. + *
  41883. + * For Interrupt and Isochronous endpoints initializes descriptor list
  41884. + * then updates FrameList, marking appropriate entries as active.
  41885. + * In case of Isochronous, the starting descriptor index is calculated based
  41886. + * on the scheduled frame, but only on the first transfer descriptor within a session.
  41887. + * Then starts the transfer via enabling the channel.
  41888. + * For Isochronous endpoint the channel is not halted on XferComplete
  41889. + * interrupt so remains assigned to the endpoint(QH) until session is done.
  41890. + *
  41891. + * @param hcd The HCD state structure for the DWC OTG controller.
  41892. + * @param qh The QH to init.
  41893. + *
  41894. + * @return 0 if successful, negative error code otherwise.
  41895. + */
  41896. +void dwc_otg_hcd_start_xfer_ddma(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  41897. +{
  41898. + /* Channel is already assigned */
  41899. + dwc_hc_t *hc = qh->channel;
  41900. + uint8_t skip_frames = 0;
  41901. +
  41902. + switch (hc->ep_type) {
  41903. + case DWC_OTG_EP_TYPE_CONTROL:
  41904. + case DWC_OTG_EP_TYPE_BULK:
  41905. + init_non_isoc_dma_desc(hcd, qh);
  41906. +
  41907. + dwc_otg_hc_start_transfer_ddma(hcd->core_if, hc);
  41908. + break;
  41909. + case DWC_OTG_EP_TYPE_INTR:
  41910. + init_non_isoc_dma_desc(hcd, qh);
  41911. +
  41912. + update_frame_list(hcd, qh, 1);
  41913. +
  41914. + dwc_otg_hc_start_transfer_ddma(hcd->core_if, hc);
  41915. + break;
  41916. + case DWC_OTG_EP_TYPE_ISOC:
  41917. +
  41918. + if (!qh->ntd)
  41919. + skip_frames = recalc_initial_desc_idx(hcd, qh);
  41920. +
  41921. + init_isoc_dma_desc(hcd, qh, skip_frames);
  41922. +
  41923. + if (!hc->xfer_started) {
  41924. +
  41925. + update_frame_list(hcd, qh, 1);
  41926. +
  41927. + /*
  41928. + * Always set to max, instead of actual size.
  41929. + * Otherwise ntd will be changed with
  41930. + * channel being enabled. Not recommended.
  41931. + *
  41932. + */
  41933. + hc->ntd = max_desc_num(qh);
  41934. + /* Enable channel only once for ISOC */
  41935. + dwc_otg_hc_start_transfer_ddma(hcd->core_if, hc);
  41936. + }
  41937. +
  41938. + break;
  41939. + default:
  41940. +
  41941. + break;
  41942. + }
  41943. +}
  41944. +
  41945. +static void complete_isoc_xfer_ddma(dwc_otg_hcd_t * hcd,
  41946. + dwc_hc_t * hc,
  41947. + dwc_otg_hc_regs_t * hc_regs,
  41948. + dwc_otg_halt_status_e halt_status)
  41949. +{
  41950. + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
  41951. + dwc_otg_qtd_t *qtd, *qtd_tmp;
  41952. + dwc_otg_qh_t *qh;
  41953. + dwc_otg_host_dma_desc_t *dma_desc;
  41954. + uint16_t idx, remain;
  41955. + uint8_t urb_compl;
  41956. +
  41957. + qh = hc->qh;
  41958. + idx = qh->td_first;
  41959. +
  41960. + if (hc->halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE) {
  41961. + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &hc->qh->qtd_list, qtd_list_entry)
  41962. + qtd->in_process = 0;
  41963. + return;
  41964. + } else if ((halt_status == DWC_OTG_HC_XFER_AHB_ERR) ||
  41965. + (halt_status == DWC_OTG_HC_XFER_BABBLE_ERR)) {
  41966. + /*
  41967. + * Channel is halted in these error cases.
  41968. + * Considered as serious issues.
  41969. + * Complete all URBs marking all frames as failed,
  41970. + * irrespective whether some of the descriptors(frames) succeeded or no.
  41971. + * Pass error code to completion routine as well, to
  41972. + * update urb->status, some of class drivers might use it to stop
  41973. + * queing transfer requests.
  41974. + */
  41975. + int err = (halt_status == DWC_OTG_HC_XFER_AHB_ERR)
  41976. + ? (-DWC_E_IO)
  41977. + : (-DWC_E_OVERFLOW);
  41978. +
  41979. + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &hc->qh->qtd_list, qtd_list_entry) {
  41980. + for (idx = 0; idx < qtd->urb->packet_count; idx++) {
  41981. + frame_desc = &qtd->urb->iso_descs[idx];
  41982. + frame_desc->status = err;
  41983. + }
  41984. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, err);
  41985. + dwc_otg_hcd_qtd_remove_and_free(hcd, qtd, qh);
  41986. + }
  41987. + return;
  41988. + }
  41989. +
  41990. + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &hc->qh->qtd_list, qtd_list_entry) {
  41991. +
  41992. + if (!qtd->in_process)
  41993. + break;
  41994. +
  41995. + urb_compl = 0;
  41996. +
  41997. + do {
  41998. +
  41999. + dma_desc = &qh->desc_list[idx];
  42000. +
  42001. + frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
  42002. + remain = hc->ep_is_in ? dma_desc->status.b_isoc.n_bytes : 0;
  42003. +
  42004. + if (dma_desc->status.b_isoc.sts == DMA_DESC_STS_PKTERR) {
  42005. + /*
  42006. + * XactError or, unable to complete all the transactions
  42007. + * in the scheduled micro-frame/frame,
  42008. + * both indicated by DMA_DESC_STS_PKTERR.
  42009. + */
  42010. + qtd->urb->error_count++;
  42011. + frame_desc->actual_length = qh->n_bytes[idx] - remain;
  42012. + frame_desc->status = -DWC_E_PROTOCOL;
  42013. + } else {
  42014. + /* Success */
  42015. +
  42016. + frame_desc->actual_length = qh->n_bytes[idx] - remain;
  42017. + frame_desc->status = 0;
  42018. + }
  42019. +
  42020. + if (++qtd->isoc_frame_index == qtd->urb->packet_count) {
  42021. + /*
  42022. + * urb->status is not used for isoc transfers here.
  42023. + * The individual frame_desc status are used instead.
  42024. + */
  42025. +
  42026. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
  42027. + dwc_otg_hcd_qtd_remove_and_free(hcd, qtd, qh);
  42028. +
  42029. + /*
  42030. + * This check is necessary because urb_dequeue can be called
  42031. + * from urb complete callback(sound driver example).
  42032. + * All pending URBs are dequeued there, so no need for
  42033. + * further processing.
  42034. + */
  42035. + if (hc->halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE) {
  42036. + return;
  42037. + }
  42038. +
  42039. + urb_compl = 1;
  42040. +
  42041. + }
  42042. +
  42043. + qh->ntd--;
  42044. +
  42045. + /* Stop if IOC requested descriptor reached */
  42046. + if (dma_desc->status.b_isoc.ioc) {
  42047. + idx = desclist_idx_inc(idx, qh->interval, hc->speed);
  42048. + goto stop_scan;
  42049. + }
  42050. +
  42051. + idx = desclist_idx_inc(idx, qh->interval, hc->speed);
  42052. +
  42053. + if (urb_compl)
  42054. + break;
  42055. + }
  42056. + while (idx != qh->td_first);
  42057. + }
  42058. +stop_scan:
  42059. + qh->td_first = idx;
  42060. +}
  42061. +
  42062. +uint8_t update_non_isoc_urb_state_ddma(dwc_otg_hcd_t * hcd,
  42063. + dwc_hc_t * hc,
  42064. + dwc_otg_qtd_t * qtd,
  42065. + dwc_otg_host_dma_desc_t * dma_desc,
  42066. + dwc_otg_halt_status_e halt_status,
  42067. + uint32_t n_bytes, uint8_t * xfer_done)
  42068. +{
  42069. +
  42070. + uint16_t remain = hc->ep_is_in ? dma_desc->status.b.n_bytes : 0;
  42071. + dwc_otg_hcd_urb_t *urb = qtd->urb;
  42072. +
  42073. + if (halt_status == DWC_OTG_HC_XFER_AHB_ERR) {
  42074. + urb->status = -DWC_E_IO;
  42075. + return 1;
  42076. + }
  42077. + if (dma_desc->status.b.sts == DMA_DESC_STS_PKTERR) {
  42078. + switch (halt_status) {
  42079. + case DWC_OTG_HC_XFER_STALL:
  42080. + urb->status = -DWC_E_PIPE;
  42081. + break;
  42082. + case DWC_OTG_HC_XFER_BABBLE_ERR:
  42083. + urb->status = -DWC_E_OVERFLOW;
  42084. + break;
  42085. + case DWC_OTG_HC_XFER_XACT_ERR:
  42086. + urb->status = -DWC_E_PROTOCOL;
  42087. + break;
  42088. + default:
  42089. + DWC_ERROR("%s: Unhandled descriptor error status (%d)\n", __func__,
  42090. + halt_status);
  42091. + break;
  42092. + }
  42093. + return 1;
  42094. + }
  42095. +
  42096. + if (dma_desc->status.b.a == 1) {
  42097. + DWC_DEBUGPL(DBG_HCDV,
  42098. + "Active descriptor encountered on channel %d\n",
  42099. + hc->hc_num);
  42100. + return 0;
  42101. + }
  42102. +
  42103. + if (hc->ep_type == DWC_OTG_EP_TYPE_CONTROL) {
  42104. + if (qtd->control_phase == DWC_OTG_CONTROL_DATA) {
  42105. + urb->actual_length += n_bytes - remain;
  42106. + if (remain || urb->actual_length == urb->length) {
  42107. + /*
  42108. + * For Control Data stage do not set urb->status=0 to prevent
  42109. + * URB callback. Set it when Status phase done. See below.
  42110. + */
  42111. + *xfer_done = 1;
  42112. + }
  42113. +
  42114. + } else if (qtd->control_phase == DWC_OTG_CONTROL_STATUS) {
  42115. + urb->status = 0;
  42116. + *xfer_done = 1;
  42117. + }
  42118. + /* No handling for SETUP stage */
  42119. + } else {
  42120. + /* BULK and INTR */
  42121. + urb->actual_length += n_bytes - remain;
  42122. + if (remain || urb->actual_length == urb->length) {
  42123. + urb->status = 0;
  42124. + *xfer_done = 1;
  42125. + }
  42126. + }
  42127. +
  42128. + return 0;
  42129. +}
  42130. +
  42131. +static void complete_non_isoc_xfer_ddma(dwc_otg_hcd_t * hcd,
  42132. + dwc_hc_t * hc,
  42133. + dwc_otg_hc_regs_t * hc_regs,
  42134. + dwc_otg_halt_status_e halt_status)
  42135. +{
  42136. + dwc_otg_hcd_urb_t *urb = NULL;
  42137. + dwc_otg_qtd_t *qtd, *qtd_tmp;
  42138. + dwc_otg_qh_t *qh;
  42139. + dwc_otg_host_dma_desc_t *dma_desc;
  42140. + uint32_t n_bytes, n_desc, i;
  42141. + uint8_t failed = 0, xfer_done;
  42142. +
  42143. + n_desc = 0;
  42144. +
  42145. + qh = hc->qh;
  42146. +
  42147. + if (hc->halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE) {
  42148. + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &hc->qh->qtd_list, qtd_list_entry) {
  42149. + qtd->in_process = 0;
  42150. + }
  42151. + return;
  42152. + }
  42153. +
  42154. + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &qh->qtd_list, qtd_list_entry) {
  42155. +
  42156. + urb = qtd->urb;
  42157. +
  42158. + n_bytes = 0;
  42159. + xfer_done = 0;
  42160. +
  42161. + for (i = 0; i < qtd->n_desc; i++) {
  42162. + dma_desc = &qh->desc_list[n_desc];
  42163. +
  42164. + n_bytes = qh->n_bytes[n_desc];
  42165. +
  42166. + failed =
  42167. + update_non_isoc_urb_state_ddma(hcd, hc, qtd,
  42168. + dma_desc,
  42169. + halt_status, n_bytes,
  42170. + &xfer_done);
  42171. +
  42172. + if (failed
  42173. + || (xfer_done
  42174. + && (urb->status != -DWC_E_IN_PROGRESS))) {
  42175. +
  42176. + hcd->fops->complete(hcd, urb->priv, urb,
  42177. + urb->status);
  42178. + dwc_otg_hcd_qtd_remove_and_free(hcd, qtd, qh);
  42179. +
  42180. + if (failed)
  42181. + goto stop_scan;
  42182. + } else if (qh->ep_type == UE_CONTROL) {
  42183. + if (qtd->control_phase == DWC_OTG_CONTROL_SETUP) {
  42184. + if (urb->length > 0) {
  42185. + qtd->control_phase = DWC_OTG_CONTROL_DATA;
  42186. + } else {
  42187. + qtd->control_phase = DWC_OTG_CONTROL_STATUS;
  42188. + }
  42189. + DWC_DEBUGPL(DBG_HCDV, " Control setup transaction done\n");
  42190. + } else if (qtd->control_phase == DWC_OTG_CONTROL_DATA) {
  42191. + if (xfer_done) {
  42192. + qtd->control_phase = DWC_OTG_CONTROL_STATUS;
  42193. + DWC_DEBUGPL(DBG_HCDV, " Control data transfer done\n");
  42194. + } else if (i + 1 == qtd->n_desc) {
  42195. + /*
  42196. + * Last descriptor for Control data stage which is
  42197. + * not completed yet.
  42198. + */
  42199. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  42200. + }
  42201. + }
  42202. + }
  42203. +
  42204. + n_desc++;
  42205. + }
  42206. +
  42207. + }
  42208. +
  42209. +stop_scan:
  42210. +
  42211. + if (qh->ep_type != UE_CONTROL) {
  42212. + /*
  42213. + * Resetting the data toggle for bulk
  42214. + * and interrupt endpoints in case of stall. See handle_hc_stall_intr()
  42215. + */
  42216. + if (halt_status == DWC_OTG_HC_XFER_STALL)
  42217. + qh->data_toggle = DWC_OTG_HC_PID_DATA0;
  42218. + else
  42219. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  42220. + }
  42221. +
  42222. + if (halt_status == DWC_OTG_HC_XFER_COMPLETE) {
  42223. + hcint_data_t hcint;
  42224. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  42225. + if (hcint.b.nyet) {
  42226. + /*
  42227. + * Got a NYET on the last transaction of the transfer. It
  42228. + * means that the endpoint should be in the PING state at the
  42229. + * beginning of the next transfer.
  42230. + */
  42231. + qh->ping_state = 1;
  42232. + clear_hc_int(hc_regs, nyet);
  42233. + }
  42234. +
  42235. + }
  42236. +
  42237. +}
  42238. +
  42239. +/**
  42240. + * This function is called from interrupt handlers.
  42241. + * Scans the descriptor list, updates URB's status and
  42242. + * calls completion routine for the URB if it's done.
  42243. + * Releases the channel to be used by other transfers.
  42244. + * In case of Isochronous endpoint the channel is not halted until
  42245. + * the end of the session, i.e. QTD list is empty.
  42246. + * If periodic channel released the FrameList is updated accordingly.
  42247. + *
  42248. + * Calls transaction selection routines to activate pending transfers.
  42249. + *
  42250. + * @param hcd The HCD state structure for the DWC OTG controller.
  42251. + * @param hc Host channel, the transfer is completed on.
  42252. + * @param hc_regs Host channel registers.
  42253. + * @param halt_status Reason the channel is being halted,
  42254. + * or just XferComplete for isochronous transfer
  42255. + */
  42256. +void dwc_otg_hcd_complete_xfer_ddma(dwc_otg_hcd_t * hcd,
  42257. + dwc_hc_t * hc,
  42258. + dwc_otg_hc_regs_t * hc_regs,
  42259. + dwc_otg_halt_status_e halt_status)
  42260. +{
  42261. + uint8_t continue_isoc_xfer = 0;
  42262. + dwc_otg_transaction_type_e tr_type;
  42263. + dwc_otg_qh_t *qh = hc->qh;
  42264. +
  42265. + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
  42266. +
  42267. + complete_isoc_xfer_ddma(hcd, hc, hc_regs, halt_status);
  42268. +
  42269. + /* Release the channel if halted or session completed */
  42270. + if (halt_status != DWC_OTG_HC_XFER_COMPLETE ||
  42271. + DWC_CIRCLEQ_EMPTY(&qh->qtd_list)) {
  42272. +
  42273. + /* Halt the channel if session completed */
  42274. + if (halt_status == DWC_OTG_HC_XFER_COMPLETE) {
  42275. + dwc_otg_hc_halt(hcd->core_if, hc, halt_status);
  42276. + }
  42277. +
  42278. + release_channel_ddma(hcd, qh);
  42279. + dwc_otg_hcd_qh_remove(hcd, qh);
  42280. + } else {
  42281. + /* Keep in assigned schedule to continue transfer */
  42282. + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_assigned,
  42283. + &qh->qh_list_entry);
  42284. + continue_isoc_xfer = 1;
  42285. +
  42286. + }
  42287. + /** @todo Consider the case when period exceeds FrameList size.
  42288. + * Frame Rollover interrupt should be used.
  42289. + */
  42290. + } else {
  42291. + /* Scan descriptor list to complete the URB(s), then release the channel */
  42292. + complete_non_isoc_xfer_ddma(hcd, hc, hc_regs, halt_status);
  42293. +
  42294. + release_channel_ddma(hcd, qh);
  42295. + dwc_otg_hcd_qh_remove(hcd, qh);
  42296. +
  42297. + if (!DWC_CIRCLEQ_EMPTY(&qh->qtd_list)) {
  42298. + /* Add back to inactive non-periodic schedule on normal completion */
  42299. + dwc_otg_hcd_qh_add(hcd, qh);
  42300. + }
  42301. +
  42302. + }
  42303. + tr_type = dwc_otg_hcd_select_transactions(hcd);
  42304. + if (tr_type != DWC_OTG_TRANSACTION_NONE || continue_isoc_xfer) {
  42305. + if (continue_isoc_xfer) {
  42306. + if (tr_type == DWC_OTG_TRANSACTION_NONE) {
  42307. + tr_type = DWC_OTG_TRANSACTION_PERIODIC;
  42308. + } else if (tr_type == DWC_OTG_TRANSACTION_NON_PERIODIC) {
  42309. + tr_type = DWC_OTG_TRANSACTION_ALL;
  42310. + }
  42311. + }
  42312. + dwc_otg_hcd_queue_transactions(hcd, tr_type);
  42313. + }
  42314. +}
  42315. +
  42316. +#endif /* DWC_DEVICE_ONLY */
  42317. --- /dev/null
  42318. +++ b/drivers/usb/host/dwc_otg/dwc_otg_hcd_if.h
  42319. @@ -0,0 +1,417 @@
  42320. +/* ==========================================================================
  42321. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd_if.h $
  42322. + * $Revision: #12 $
  42323. + * $Date: 2011/10/26 $
  42324. + * $Change: 1873028 $
  42325. + *
  42326. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  42327. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  42328. + * otherwise expressly agreed to in writing between Synopsys and you.
  42329. + *
  42330. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  42331. + * any End User Software License Agreement or Agreement for Licensed Product
  42332. + * with Synopsys or any supplement thereto. You are permitted to use and
  42333. + * redistribute this Software in source and binary forms, with or without
  42334. + * modification, provided that redistributions of source code must retain this
  42335. + * notice. You may not view, use, disclose, copy or distribute this file or
  42336. + * any information contained herein except pursuant to this license grant from
  42337. + * Synopsys. If you do not agree with this notice, including the disclaimer
  42338. + * below, then you are not authorized to use the Software.
  42339. + *
  42340. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  42341. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42342. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  42343. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  42344. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  42345. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  42346. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  42347. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  42348. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  42349. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  42350. + * DAMAGE.
  42351. + * ========================================================================== */
  42352. +#ifndef DWC_DEVICE_ONLY
  42353. +#ifndef __DWC_HCD_IF_H__
  42354. +#define __DWC_HCD_IF_H__
  42355. +
  42356. +#include "dwc_otg_core_if.h"
  42357. +
  42358. +/** @file
  42359. + * This file defines DWC_OTG HCD Core API.
  42360. + */
  42361. +
  42362. +struct dwc_otg_hcd;
  42363. +typedef struct dwc_otg_hcd dwc_otg_hcd_t;
  42364. +
  42365. +struct dwc_otg_hcd_urb;
  42366. +typedef struct dwc_otg_hcd_urb dwc_otg_hcd_urb_t;
  42367. +
  42368. +/** @name HCD Function Driver Callbacks */
  42369. +/** @{ */
  42370. +
  42371. +/** This function is called whenever core switches to host mode. */
  42372. +typedef int (*dwc_otg_hcd_start_cb_t) (dwc_otg_hcd_t * hcd);
  42373. +
  42374. +/** This function is called when device has been disconnected */
  42375. +typedef int (*dwc_otg_hcd_disconnect_cb_t) (dwc_otg_hcd_t * hcd);
  42376. +
  42377. +/** Wrapper provides this function to HCD to core, so it can get hub information to which device is connected */
  42378. +typedef int (*dwc_otg_hcd_hub_info_from_urb_cb_t) (dwc_otg_hcd_t * hcd,
  42379. + void *urb_handle,
  42380. + uint32_t * hub_addr,
  42381. + uint32_t * port_addr);
  42382. +/** Via this function HCD core gets device speed */
  42383. +typedef int (*dwc_otg_hcd_speed_from_urb_cb_t) (dwc_otg_hcd_t * hcd,
  42384. + void *urb_handle);
  42385. +
  42386. +/** This function is called when urb is completed */
  42387. +typedef int (*dwc_otg_hcd_complete_urb_cb_t) (dwc_otg_hcd_t * hcd,
  42388. + void *urb_handle,
  42389. + dwc_otg_hcd_urb_t * dwc_otg_urb,
  42390. + int32_t status);
  42391. +
  42392. +/** Via this function HCD core gets b_hnp_enable parameter */
  42393. +typedef int (*dwc_otg_hcd_get_b_hnp_enable) (dwc_otg_hcd_t * hcd);
  42394. +
  42395. +struct dwc_otg_hcd_function_ops {
  42396. + dwc_otg_hcd_start_cb_t start;
  42397. + dwc_otg_hcd_disconnect_cb_t disconnect;
  42398. + dwc_otg_hcd_hub_info_from_urb_cb_t hub_info;
  42399. + dwc_otg_hcd_speed_from_urb_cb_t speed;
  42400. + dwc_otg_hcd_complete_urb_cb_t complete;
  42401. + dwc_otg_hcd_get_b_hnp_enable get_b_hnp_enable;
  42402. +};
  42403. +/** @} */
  42404. +
  42405. +/** @name HCD Core API */
  42406. +/** @{ */
  42407. +/** This function allocates dwc_otg_hcd structure and returns pointer on it. */
  42408. +extern dwc_otg_hcd_t *dwc_otg_hcd_alloc_hcd(void);
  42409. +
  42410. +/** This function should be called to initiate HCD Core.
  42411. + *
  42412. + * @param hcd The HCD
  42413. + * @param core_if The DWC_OTG Core
  42414. + *
  42415. + * Returns -DWC_E_NO_MEMORY if no enough memory.
  42416. + * Returns 0 on success
  42417. + */
  42418. +extern int dwc_otg_hcd_init(dwc_otg_hcd_t * hcd, dwc_otg_core_if_t * core_if);
  42419. +
  42420. +/** Frees HCD
  42421. + *
  42422. + * @param hcd The HCD
  42423. + */
  42424. +extern void dwc_otg_hcd_remove(dwc_otg_hcd_t * hcd);
  42425. +
  42426. +/** This function should be called on every hardware interrupt.
  42427. + *
  42428. + * @param dwc_otg_hcd The HCD
  42429. + *
  42430. + * Returns non zero if interrupt is handled
  42431. + * Return 0 if interrupt is not handled
  42432. + */
  42433. +extern int32_t dwc_otg_hcd_handle_intr(dwc_otg_hcd_t * dwc_otg_hcd);
  42434. +
  42435. +/** This function is used to handle the fast interrupt
  42436. + *
  42437. + */
  42438. +extern void __attribute__ ((naked)) dwc_otg_hcd_handle_fiq(void);
  42439. +
  42440. +/**
  42441. + * Returns private data set by
  42442. + * dwc_otg_hcd_set_priv_data function.
  42443. + *
  42444. + * @param hcd The HCD
  42445. + */
  42446. +extern void *dwc_otg_hcd_get_priv_data(dwc_otg_hcd_t * hcd);
  42447. +
  42448. +/**
  42449. + * Set private data.
  42450. + *
  42451. + * @param hcd The HCD
  42452. + * @param priv_data pointer to be stored in private data
  42453. + */
  42454. +extern void dwc_otg_hcd_set_priv_data(dwc_otg_hcd_t * hcd, void *priv_data);
  42455. +
  42456. +/**
  42457. + * This function initializes the HCD Core.
  42458. + *
  42459. + * @param hcd The HCD
  42460. + * @param fops The Function Driver Operations data structure containing pointers to all callbacks.
  42461. + *
  42462. + * Returns -DWC_E_NO_DEVICE if Core is currently is in device mode.
  42463. + * Returns 0 on success
  42464. + */
  42465. +extern int dwc_otg_hcd_start(dwc_otg_hcd_t * hcd,
  42466. + struct dwc_otg_hcd_function_ops *fops);
  42467. +
  42468. +/**
  42469. + * Halts the DWC_otg host mode operations in a clean manner. USB transfers are
  42470. + * stopped.
  42471. + *
  42472. + * @param hcd The HCD
  42473. + */
  42474. +extern void dwc_otg_hcd_stop(dwc_otg_hcd_t * hcd);
  42475. +
  42476. +/**
  42477. + * Handles hub class-specific requests.
  42478. + *
  42479. + * @param dwc_otg_hcd The HCD
  42480. + * @param typeReq Request Type
  42481. + * @param wValue wValue from control request
  42482. + * @param wIndex wIndex from control request
  42483. + * @param buf data buffer
  42484. + * @param wLength data buffer length
  42485. + *
  42486. + * Returns -DWC_E_INVALID if invalid argument is passed
  42487. + * Returns 0 on success
  42488. + */
  42489. +extern int dwc_otg_hcd_hub_control(dwc_otg_hcd_t * dwc_otg_hcd,
  42490. + uint16_t typeReq, uint16_t wValue,
  42491. + uint16_t wIndex, uint8_t * buf,
  42492. + uint16_t wLength);
  42493. +
  42494. +/**
  42495. + * Returns otg port number.
  42496. + *
  42497. + * @param hcd The HCD
  42498. + */
  42499. +extern uint32_t dwc_otg_hcd_otg_port(dwc_otg_hcd_t * hcd);
  42500. +
  42501. +/**
  42502. + * Returns OTG version - either 1.3 or 2.0.
  42503. + *
  42504. + * @param core_if The core_if structure pointer
  42505. + */
  42506. +extern uint16_t dwc_otg_get_otg_version(dwc_otg_core_if_t * core_if);
  42507. +
  42508. +/**
  42509. + * Returns 1 if currently core is acting as B host, and 0 otherwise.
  42510. + *
  42511. + * @param hcd The HCD
  42512. + */
  42513. +extern uint32_t dwc_otg_hcd_is_b_host(dwc_otg_hcd_t * hcd);
  42514. +
  42515. +/**
  42516. + * Returns current frame number.
  42517. + *
  42518. + * @param hcd The HCD
  42519. + */
  42520. +extern int dwc_otg_hcd_get_frame_number(dwc_otg_hcd_t * hcd);
  42521. +
  42522. +/**
  42523. + * Dumps hcd state.
  42524. + *
  42525. + * @param hcd The HCD
  42526. + */
  42527. +extern void dwc_otg_hcd_dump_state(dwc_otg_hcd_t * hcd);
  42528. +
  42529. +/**
  42530. + * Dump the average frame remaining at SOF. This can be used to
  42531. + * determine average interrupt latency. Frame remaining is also shown for
  42532. + * start transfer and two additional sample points.
  42533. + * Currently this function is not implemented.
  42534. + *
  42535. + * @param hcd The HCD
  42536. + */
  42537. +extern void dwc_otg_hcd_dump_frrem(dwc_otg_hcd_t * hcd);
  42538. +
  42539. +/**
  42540. + * Sends LPM transaction to the local device.
  42541. + *
  42542. + * @param hcd The HCD
  42543. + * @param devaddr Device Address
  42544. + * @param hird Host initiated resume duration
  42545. + * @param bRemoteWake Value of bRemoteWake field in LPM transaction
  42546. + *
  42547. + * Returns negative value if sending LPM transaction was not succeeded.
  42548. + * Returns 0 on success.
  42549. + */
  42550. +extern int dwc_otg_hcd_send_lpm(dwc_otg_hcd_t * hcd, uint8_t devaddr,
  42551. + uint8_t hird, uint8_t bRemoteWake);
  42552. +
  42553. +/* URB interface */
  42554. +
  42555. +/**
  42556. + * Allocates memory for dwc_otg_hcd_urb structure.
  42557. + * Allocated memory should be freed by call of DWC_FREE.
  42558. + *
  42559. + * @param hcd The HCD
  42560. + * @param iso_desc_count Count of ISOC descriptors
  42561. + * @param atomic_alloc Specefies whether to perform atomic allocation.
  42562. + */
  42563. +extern dwc_otg_hcd_urb_t *dwc_otg_hcd_urb_alloc(dwc_otg_hcd_t * hcd,
  42564. + int iso_desc_count,
  42565. + int atomic_alloc);
  42566. +
  42567. +/**
  42568. + * Set pipe information in URB.
  42569. + *
  42570. + * @param hcd_urb DWC_OTG URB
  42571. + * @param devaddr Device Address
  42572. + * @param ep_num Endpoint Number
  42573. + * @param ep_type Endpoint Type
  42574. + * @param ep_dir Endpoint Direction
  42575. + * @param mps Max Packet Size
  42576. + */
  42577. +extern void dwc_otg_hcd_urb_set_pipeinfo(dwc_otg_hcd_urb_t * hcd_urb,
  42578. + uint8_t devaddr, uint8_t ep_num,
  42579. + uint8_t ep_type, uint8_t ep_dir,
  42580. + uint16_t mps);
  42581. +
  42582. +/* Transfer flags */
  42583. +#define URB_GIVEBACK_ASAP 0x1
  42584. +#define URB_SEND_ZERO_PACKET 0x2
  42585. +
  42586. +/**
  42587. + * Sets dwc_otg_hcd_urb parameters.
  42588. + *
  42589. + * @param urb DWC_OTG URB allocated by dwc_otg_hcd_urb_alloc function.
  42590. + * @param urb_handle Unique handle for request, this will be passed back
  42591. + * to function driver in completion callback.
  42592. + * @param buf The buffer for the data
  42593. + * @param dma The DMA buffer for the data
  42594. + * @param buflen Transfer length
  42595. + * @param sp Buffer for setup data
  42596. + * @param sp_dma DMA address of setup data buffer
  42597. + * @param flags Transfer flags
  42598. + * @param interval Polling interval for interrupt or isochronous transfers.
  42599. + */
  42600. +extern void dwc_otg_hcd_urb_set_params(dwc_otg_hcd_urb_t * urb,
  42601. + void *urb_handle, void *buf,
  42602. + dwc_dma_t dma, uint32_t buflen, void *sp,
  42603. + dwc_dma_t sp_dma, uint32_t flags,
  42604. + uint16_t interval);
  42605. +
  42606. +/** Gets status from dwc_otg_hcd_urb
  42607. + *
  42608. + * @param dwc_otg_urb DWC_OTG URB
  42609. + */
  42610. +extern uint32_t dwc_otg_hcd_urb_get_status(dwc_otg_hcd_urb_t * dwc_otg_urb);
  42611. +
  42612. +/** Gets actual length from dwc_otg_hcd_urb
  42613. + *
  42614. + * @param dwc_otg_urb DWC_OTG URB
  42615. + */
  42616. +extern uint32_t dwc_otg_hcd_urb_get_actual_length(dwc_otg_hcd_urb_t *
  42617. + dwc_otg_urb);
  42618. +
  42619. +/** Gets error count from dwc_otg_hcd_urb. Only for ISOC URBs
  42620. + *
  42621. + * @param dwc_otg_urb DWC_OTG URB
  42622. + */
  42623. +extern uint32_t dwc_otg_hcd_urb_get_error_count(dwc_otg_hcd_urb_t *
  42624. + dwc_otg_urb);
  42625. +
  42626. +/** Set ISOC descriptor offset and length
  42627. + *
  42628. + * @param dwc_otg_urb DWC_OTG URB
  42629. + * @param desc_num ISOC descriptor number
  42630. + * @param offset Offset from beginig of buffer.
  42631. + * @param length Transaction length
  42632. + */
  42633. +extern void dwc_otg_hcd_urb_set_iso_desc_params(dwc_otg_hcd_urb_t * dwc_otg_urb,
  42634. + int desc_num, uint32_t offset,
  42635. + uint32_t length);
  42636. +
  42637. +/** Get status of ISOC descriptor, specified by desc_num
  42638. + *
  42639. + * @param dwc_otg_urb DWC_OTG URB
  42640. + * @param desc_num ISOC descriptor number
  42641. + */
  42642. +extern uint32_t dwc_otg_hcd_urb_get_iso_desc_status(dwc_otg_hcd_urb_t *
  42643. + dwc_otg_urb, int desc_num);
  42644. +
  42645. +/** Get actual length of ISOC descriptor, specified by desc_num
  42646. + *
  42647. + * @param dwc_otg_urb DWC_OTG URB
  42648. + * @param desc_num ISOC descriptor number
  42649. + */
  42650. +extern uint32_t dwc_otg_hcd_urb_get_iso_desc_actual_length(dwc_otg_hcd_urb_t *
  42651. + dwc_otg_urb,
  42652. + int desc_num);
  42653. +
  42654. +/** Queue URB. After transfer is completes, the complete callback will be called with the URB status
  42655. + *
  42656. + * @param dwc_otg_hcd The HCD
  42657. + * @param dwc_otg_urb DWC_OTG URB
  42658. + * @param ep_handle Out parameter for returning endpoint handle
  42659. + * @param atomic_alloc Flag to do atomic allocation if needed
  42660. + *
  42661. + * Returns -DWC_E_NO_DEVICE if no device is connected.
  42662. + * Returns -DWC_E_NO_MEMORY if there is no enough memory.
  42663. + * Returns 0 on success.
  42664. + */
  42665. +extern int dwc_otg_hcd_urb_enqueue(dwc_otg_hcd_t * dwc_otg_hcd,
  42666. + dwc_otg_hcd_urb_t * dwc_otg_urb,
  42667. + void **ep_handle, int atomic_alloc);
  42668. +
  42669. +/** De-queue the specified URB
  42670. + *
  42671. + * @param dwc_otg_hcd The HCD
  42672. + * @param dwc_otg_urb DWC_OTG URB
  42673. + */
  42674. +extern int dwc_otg_hcd_urb_dequeue(dwc_otg_hcd_t * dwc_otg_hcd,
  42675. + dwc_otg_hcd_urb_t * dwc_otg_urb);
  42676. +
  42677. +/** Frees resources in the DWC_otg controller related to a given endpoint.
  42678. + * Any URBs for the endpoint must already be dequeued.
  42679. + *
  42680. + * @param hcd The HCD
  42681. + * @param ep_handle Endpoint handle, returned by dwc_otg_hcd_urb_enqueue function
  42682. + * @param retry Number of retries if there are queued transfers.
  42683. + *
  42684. + * Returns -DWC_E_INVALID if invalid arguments are passed.
  42685. + * Returns 0 on success
  42686. + */
  42687. +extern int dwc_otg_hcd_endpoint_disable(dwc_otg_hcd_t * hcd, void *ep_handle,
  42688. + int retry);
  42689. +
  42690. +/* Resets the data toggle in qh structure. This function can be called from
  42691. + * usb_clear_halt routine.
  42692. + *
  42693. + * @param hcd The HCD
  42694. + * @param ep_handle Endpoint handle, returned by dwc_otg_hcd_urb_enqueue function
  42695. + *
  42696. + * Returns -DWC_E_INVALID if invalid arguments are passed.
  42697. + * Returns 0 on success
  42698. + */
  42699. +extern int dwc_otg_hcd_endpoint_reset(dwc_otg_hcd_t * hcd, void *ep_handle);
  42700. +
  42701. +/** Returns 1 if status of specified port is changed and 0 otherwise.
  42702. + *
  42703. + * @param hcd The HCD
  42704. + * @param port Port number
  42705. + */
  42706. +extern int dwc_otg_hcd_is_status_changed(dwc_otg_hcd_t * hcd, int port);
  42707. +
  42708. +/** Call this function to check if bandwidth was allocated for specified endpoint.
  42709. + * Only for ISOC and INTERRUPT endpoints.
  42710. + *
  42711. + * @param hcd The HCD
  42712. + * @param ep_handle Endpoint handle
  42713. + */
  42714. +extern int dwc_otg_hcd_is_bandwidth_allocated(dwc_otg_hcd_t * hcd,
  42715. + void *ep_handle);
  42716. +
  42717. +/** Call this function to check if bandwidth was freed for specified endpoint.
  42718. + *
  42719. + * @param hcd The HCD
  42720. + * @param ep_handle Endpoint handle
  42721. + */
  42722. +extern int dwc_otg_hcd_is_bandwidth_freed(dwc_otg_hcd_t * hcd, void *ep_handle);
  42723. +
  42724. +/** Returns bandwidth allocated for specified endpoint in microseconds.
  42725. + * Only for ISOC and INTERRUPT endpoints.
  42726. + *
  42727. + * @param hcd The HCD
  42728. + * @param ep_handle Endpoint handle
  42729. + */
  42730. +extern uint8_t dwc_otg_hcd_get_ep_bandwidth(dwc_otg_hcd_t * hcd,
  42731. + void *ep_handle);
  42732. +
  42733. +/** @} */
  42734. +
  42735. +#endif /* __DWC_HCD_IF_H__ */
  42736. +#endif /* DWC_DEVICE_ONLY */
  42737. --- /dev/null
  42738. +++ b/drivers/usb/host/dwc_otg/dwc_otg_hcd_intr.c
  42739. @@ -0,0 +1,2727 @@
  42740. +/* ==========================================================================
  42741. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd_intr.c $
  42742. + * $Revision: #89 $
  42743. + * $Date: 2011/10/20 $
  42744. + * $Change: 1869487 $
  42745. + *
  42746. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  42747. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  42748. + * otherwise expressly agreed to in writing between Synopsys and you.
  42749. + *
  42750. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  42751. + * any End User Software License Agreement or Agreement for Licensed Product
  42752. + * with Synopsys or any supplement thereto. You are permitted to use and
  42753. + * redistribute this Software in source and binary forms, with or without
  42754. + * modification, provided that redistributions of source code must retain this
  42755. + * notice. You may not view, use, disclose, copy or distribute this file or
  42756. + * any information contained herein except pursuant to this license grant from
  42757. + * Synopsys. If you do not agree with this notice, including the disclaimer
  42758. + * below, then you are not authorized to use the Software.
  42759. + *
  42760. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  42761. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42762. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  42763. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  42764. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  42765. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  42766. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  42767. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  42768. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  42769. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  42770. + * DAMAGE.
  42771. + * ========================================================================== */
  42772. +#ifndef DWC_DEVICE_ONLY
  42773. +
  42774. +#include "dwc_otg_hcd.h"
  42775. +#include "dwc_otg_regs.h"
  42776. +
  42777. +#include <linux/jiffies.h>
  42778. +#include <asm/fiq.h>
  42779. +
  42780. +
  42781. +extern bool microframe_schedule;
  42782. +
  42783. +/** @file
  42784. + * This file contains the implementation of the HCD Interrupt handlers.
  42785. + */
  42786. +
  42787. +int fiq_done, int_done;
  42788. +
  42789. +#ifdef FIQ_DEBUG
  42790. +char buffer[1000*16];
  42791. +int wptr;
  42792. +void notrace _fiq_print(FIQDBG_T dbg_lvl, char *fmt, ...)
  42793. +{
  42794. + FIQDBG_T dbg_lvl_req = FIQDBG_PORTHUB;
  42795. + va_list args;
  42796. + char text[17];
  42797. + hfnum_data_t hfnum = { .d32 = FIQ_READ(dwc_regs_base + 0x408) };
  42798. +
  42799. + if(dbg_lvl & dbg_lvl_req || dbg_lvl == FIQDBG_ERR)
  42800. + {
  42801. + local_fiq_disable();
  42802. + snprintf(text, 9, "%4d%d:%d ", hfnum.b.frnum/8, hfnum.b.frnum%8, 8 - hfnum.b.frrem/937);
  42803. + va_start(args, fmt);
  42804. + vsnprintf(text+8, 9, fmt, args);
  42805. + va_end(args);
  42806. +
  42807. + memcpy(buffer + wptr, text, 16);
  42808. + wptr = (wptr + 16) % sizeof(buffer);
  42809. + local_fiq_enable();
  42810. + }
  42811. +}
  42812. +#endif
  42813. +
  42814. +/** This function handles interrupts for the HCD. */
  42815. +int32_t dwc_otg_hcd_handle_intr(dwc_otg_hcd_t * dwc_otg_hcd)
  42816. +{
  42817. + int retval = 0;
  42818. + static int last_time;
  42819. + dwc_otg_core_if_t *core_if = dwc_otg_hcd->core_if;
  42820. + gintsts_data_t gintsts;
  42821. + gintmsk_data_t gintmsk;
  42822. + hfnum_data_t hfnum;
  42823. + haintmsk_data_t haintmsk;
  42824. +
  42825. +#ifdef DEBUG
  42826. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  42827. +
  42828. +#endif
  42829. +
  42830. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  42831. + gintmsk.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
  42832. +
  42833. + /* Exit from ISR if core is hibernated */
  42834. + if (core_if->hibernation_suspend == 1) {
  42835. + goto exit_handler_routine;
  42836. + }
  42837. + DWC_SPINLOCK(dwc_otg_hcd->lock);
  42838. + /* Check if HOST Mode */
  42839. + if (dwc_otg_is_host_mode(core_if)) {
  42840. + if (fiq_enable) {
  42841. + local_fiq_disable();
  42842. + fiq_fsm_spin_lock(&dwc_otg_hcd->fiq_state->lock);
  42843. + /* Pull in from the FIQ's disabled mask */
  42844. + gintmsk.d32 = gintmsk.d32 | ~(dwc_otg_hcd->fiq_state->gintmsk_saved.d32);
  42845. + dwc_otg_hcd->fiq_state->gintmsk_saved.d32 = ~0;
  42846. + }
  42847. +
  42848. + if (fiq_fsm_enable && ( 0x0000FFFF & ~(dwc_otg_hcd->fiq_state->haintmsk_saved.b2.chint))) {
  42849. + gintsts.b.hcintr = 1;
  42850. + }
  42851. +
  42852. + /* Danger will robinson: fake a SOF if necessary */
  42853. + if (fiq_fsm_enable && (dwc_otg_hcd->fiq_state->gintmsk_saved.b.sofintr == 1)) {
  42854. + gintsts.b.sofintr = 1;
  42855. + }
  42856. + gintsts.d32 &= gintmsk.d32;
  42857. +
  42858. + if (fiq_enable) {
  42859. + fiq_fsm_spin_unlock(&dwc_otg_hcd->fiq_state->lock);
  42860. + local_fiq_enable();
  42861. + }
  42862. +
  42863. + if (!gintsts.d32) {
  42864. + goto exit_handler_routine;
  42865. + }
  42866. +
  42867. +#ifdef DEBUG
  42868. + // We should be OK doing this because the common interrupts should already have been serviced
  42869. + /* Don't print debug message in the interrupt handler on SOF */
  42870. +#ifndef DEBUG_SOF
  42871. + if (gintsts.d32 != DWC_SOF_INTR_MASK)
  42872. +#endif
  42873. + DWC_DEBUGPL(DBG_HCDI, "\n");
  42874. +#endif
  42875. +
  42876. +#ifdef DEBUG
  42877. +#ifndef DEBUG_SOF
  42878. + if (gintsts.d32 != DWC_SOF_INTR_MASK)
  42879. +#endif
  42880. + DWC_DEBUGPL(DBG_HCDI,
  42881. + "DWC OTG HCD Interrupt Detected gintsts&gintmsk=0x%08x core_if=%p\n",
  42882. + gintsts.d32, core_if);
  42883. +#endif
  42884. + hfnum.d32 = DWC_READ_REG32(&dwc_otg_hcd->core_if->host_if->host_global_regs->hfnum);
  42885. + if (gintsts.b.sofintr) {
  42886. + retval |= dwc_otg_hcd_handle_sof_intr(dwc_otg_hcd);
  42887. + }
  42888. +
  42889. + if (gintsts.b.rxstsqlvl) {
  42890. + retval |=
  42891. + dwc_otg_hcd_handle_rx_status_q_level_intr
  42892. + (dwc_otg_hcd);
  42893. + }
  42894. + if (gintsts.b.nptxfempty) {
  42895. + retval |=
  42896. + dwc_otg_hcd_handle_np_tx_fifo_empty_intr
  42897. + (dwc_otg_hcd);
  42898. + }
  42899. + if (gintsts.b.i2cintr) {
  42900. + /** @todo Implement i2cintr handler. */
  42901. + }
  42902. + if (gintsts.b.portintr) {
  42903. +
  42904. + gintmsk_data_t gintmsk = { .b.portintr = 1};
  42905. + retval |= dwc_otg_hcd_handle_port_intr(dwc_otg_hcd);
  42906. + if (fiq_enable) {
  42907. + local_fiq_disable();
  42908. + fiq_fsm_spin_lock(&dwc_otg_hcd->fiq_state->lock);
  42909. + DWC_MODIFY_REG32(&dwc_otg_hcd->core_if->core_global_regs->gintmsk, 0, gintmsk.d32);
  42910. + fiq_fsm_spin_unlock(&dwc_otg_hcd->fiq_state->lock);
  42911. + local_fiq_enable();
  42912. + } else {
  42913. + DWC_MODIFY_REG32(&dwc_otg_hcd->core_if->core_global_regs->gintmsk, 0, gintmsk.d32);
  42914. + }
  42915. + }
  42916. + if (gintsts.b.hcintr) {
  42917. + retval |= dwc_otg_hcd_handle_hc_intr(dwc_otg_hcd);
  42918. + }
  42919. + if (gintsts.b.ptxfempty) {
  42920. + retval |=
  42921. + dwc_otg_hcd_handle_perio_tx_fifo_empty_intr
  42922. + (dwc_otg_hcd);
  42923. + }
  42924. +#ifdef DEBUG
  42925. +#ifndef DEBUG_SOF
  42926. + if (gintsts.d32 != DWC_SOF_INTR_MASK)
  42927. +#endif
  42928. + {
  42929. + DWC_DEBUGPL(DBG_HCDI,
  42930. + "DWC OTG HCD Finished Servicing Interrupts\n");
  42931. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD gintsts=0x%08x\n",
  42932. + DWC_READ_REG32(&global_regs->gintsts));
  42933. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD gintmsk=0x%08x\n",
  42934. + DWC_READ_REG32(&global_regs->gintmsk));
  42935. + }
  42936. +#endif
  42937. +
  42938. +#ifdef DEBUG
  42939. +#ifndef DEBUG_SOF
  42940. + if (gintsts.d32 != DWC_SOF_INTR_MASK)
  42941. +#endif
  42942. + DWC_DEBUGPL(DBG_HCDI, "\n");
  42943. +#endif
  42944. +
  42945. + }
  42946. +
  42947. +exit_handler_routine:
  42948. + if (fiq_enable) {
  42949. + gintmsk_data_t gintmsk_new;
  42950. + haintmsk_data_t haintmsk_new;
  42951. + local_fiq_disable();
  42952. + fiq_fsm_spin_lock(&dwc_otg_hcd->fiq_state->lock);
  42953. + gintmsk_new.d32 = *(volatile uint32_t *)&dwc_otg_hcd->fiq_state->gintmsk_saved.d32;
  42954. + if(fiq_fsm_enable)
  42955. + haintmsk_new.d32 = *(volatile uint32_t *)&dwc_otg_hcd->fiq_state->haintmsk_saved.d32;
  42956. + else
  42957. + haintmsk_new.d32 = 0x0000FFFF;
  42958. +
  42959. + /* The FIQ could have sneaked another interrupt in. If so, don't clear MPHI */
  42960. + if ((gintmsk_new.d32 == ~0) && (haintmsk_new.d32 == 0x0000FFFF)) {
  42961. + DWC_WRITE_REG32(dwc_otg_hcd->fiq_state->mphi_regs.intstat, (1<<16));
  42962. + if (dwc_otg_hcd->fiq_state->mphi_int_count >= 50) {
  42963. + fiq_print(FIQDBG_INT, dwc_otg_hcd->fiq_state, "MPHI CLR");
  42964. + DWC_WRITE_REG32(dwc_otg_hcd->fiq_state->mphi_regs.ctrl, ((1<<31) + (1<<16)));
  42965. + while (!(DWC_READ_REG32(dwc_otg_hcd->fiq_state->mphi_regs.ctrl) & (1 << 17)))
  42966. + ;
  42967. + DWC_WRITE_REG32(dwc_otg_hcd->fiq_state->mphi_regs.ctrl, (1<<31));
  42968. + dwc_otg_hcd->fiq_state->mphi_int_count = 0;
  42969. + }
  42970. + int_done++;
  42971. + }
  42972. + haintmsk.d32 = DWC_READ_REG32(&core_if->host_if->host_global_regs->haintmsk);
  42973. + /* Re-enable interrupts that the FIQ masked (first time round) */
  42974. + FIQ_WRITE(dwc_otg_hcd->fiq_state->dwc_regs_base + GINTMSK, gintmsk.d32);
  42975. + fiq_fsm_spin_unlock(&dwc_otg_hcd->fiq_state->lock);
  42976. + local_fiq_enable();
  42977. +
  42978. + if ((jiffies / HZ) > last_time) {
  42979. + //dwc_otg_qh_t *qh;
  42980. + //dwc_list_link_t *cur;
  42981. + /* Once a second output the fiq and irq numbers, useful for debug */
  42982. + last_time = jiffies / HZ;
  42983. + // DWC_WARN("np_kick=%d AHC=%d sched_frame=%d cur_frame=%d int_done=%d fiq_done=%d",
  42984. + // dwc_otg_hcd->fiq_state->kick_np_queues, dwc_otg_hcd->available_host_channels,
  42985. + // dwc_otg_hcd->fiq_state->next_sched_frame, hfnum.b.frnum, int_done, dwc_otg_hcd->fiq_state->fiq_done);
  42986. + //printk(KERN_WARNING "Periodic queues:\n");
  42987. + }
  42988. + }
  42989. +
  42990. + DWC_SPINUNLOCK(dwc_otg_hcd->lock);
  42991. + return retval;
  42992. +}
  42993. +
  42994. +#ifdef DWC_TRACK_MISSED_SOFS
  42995. +
  42996. +#warning Compiling code to track missed SOFs
  42997. +#define FRAME_NUM_ARRAY_SIZE 1000
  42998. +/**
  42999. + * This function is for debug only.
  43000. + */
  43001. +static inline void track_missed_sofs(uint16_t curr_frame_number)
  43002. +{
  43003. + static uint16_t frame_num_array[FRAME_NUM_ARRAY_SIZE];
  43004. + static uint16_t last_frame_num_array[FRAME_NUM_ARRAY_SIZE];
  43005. + static int frame_num_idx = 0;
  43006. + static uint16_t last_frame_num = DWC_HFNUM_MAX_FRNUM;
  43007. + static int dumped_frame_num_array = 0;
  43008. +
  43009. + if (frame_num_idx < FRAME_NUM_ARRAY_SIZE) {
  43010. + if (((last_frame_num + 1) & DWC_HFNUM_MAX_FRNUM) !=
  43011. + curr_frame_number) {
  43012. + frame_num_array[frame_num_idx] = curr_frame_number;
  43013. + last_frame_num_array[frame_num_idx++] = last_frame_num;
  43014. + }
  43015. + } else if (!dumped_frame_num_array) {
  43016. + int i;
  43017. + DWC_PRINTF("Frame Last Frame\n");
  43018. + DWC_PRINTF("----- ----------\n");
  43019. + for (i = 0; i < FRAME_NUM_ARRAY_SIZE; i++) {
  43020. + DWC_PRINTF("0x%04x 0x%04x\n",
  43021. + frame_num_array[i], last_frame_num_array[i]);
  43022. + }
  43023. + dumped_frame_num_array = 1;
  43024. + }
  43025. + last_frame_num = curr_frame_number;
  43026. +}
  43027. +#endif
  43028. +
  43029. +/**
  43030. + * Handles the start-of-frame interrupt in host mode. Non-periodic
  43031. + * transactions may be queued to the DWC_otg controller for the current
  43032. + * (micro)frame. Periodic transactions may be queued to the controller for the
  43033. + * next (micro)frame.
  43034. + */
  43035. +int32_t dwc_otg_hcd_handle_sof_intr(dwc_otg_hcd_t * hcd)
  43036. +{
  43037. + hfnum_data_t hfnum;
  43038. + gintsts_data_t gintsts = { .d32 = 0 };
  43039. + dwc_list_link_t *qh_entry;
  43040. + dwc_otg_qh_t *qh;
  43041. + dwc_otg_transaction_type_e tr_type;
  43042. + int did_something = 0;
  43043. + int32_t next_sched_frame = -1;
  43044. +
  43045. + hfnum.d32 =
  43046. + DWC_READ_REG32(&hcd->core_if->host_if->host_global_regs->hfnum);
  43047. +
  43048. +#ifdef DEBUG_SOF
  43049. + DWC_DEBUGPL(DBG_HCD, "--Start of Frame Interrupt--\n");
  43050. +#endif
  43051. + hcd->frame_number = hfnum.b.frnum;
  43052. +
  43053. +#ifdef DEBUG
  43054. + hcd->frrem_accum += hfnum.b.frrem;
  43055. + hcd->frrem_samples++;
  43056. +#endif
  43057. +
  43058. +#ifdef DWC_TRACK_MISSED_SOFS
  43059. + track_missed_sofs(hcd->frame_number);
  43060. +#endif
  43061. + /* Determine whether any periodic QHs should be executed. */
  43062. + qh_entry = DWC_LIST_FIRST(&hcd->periodic_sched_inactive);
  43063. + while (qh_entry != &hcd->periodic_sched_inactive) {
  43064. + qh = DWC_LIST_ENTRY(qh_entry, dwc_otg_qh_t, qh_list_entry);
  43065. + qh_entry = qh_entry->next;
  43066. + if (dwc_frame_num_le(qh->sched_frame, hcd->frame_number)) {
  43067. +
  43068. + /*
  43069. + * Move QH to the ready list to be executed next
  43070. + * (micro)frame.
  43071. + */
  43072. + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_ready,
  43073. + &qh->qh_list_entry);
  43074. +
  43075. + did_something = 1;
  43076. + }
  43077. + else
  43078. + {
  43079. + if(next_sched_frame < 0 || dwc_frame_num_le(qh->sched_frame, next_sched_frame))
  43080. + {
  43081. + next_sched_frame = qh->sched_frame;
  43082. + }
  43083. + }
  43084. + }
  43085. + if (fiq_enable)
  43086. + hcd->fiq_state->next_sched_frame = next_sched_frame;
  43087. +
  43088. + tr_type = dwc_otg_hcd_select_transactions(hcd);
  43089. + if (tr_type != DWC_OTG_TRANSACTION_NONE) {
  43090. + dwc_otg_hcd_queue_transactions(hcd, tr_type);
  43091. + did_something = 1;
  43092. + }
  43093. +
  43094. + /* Clear interrupt - but do not trample on the FIQ sof */
  43095. + if (!fiq_fsm_enable) {
  43096. + gintsts.b.sofintr = 1;
  43097. + DWC_WRITE_REG32(&hcd->core_if->core_global_regs->gintsts, gintsts.d32);
  43098. + }
  43099. + return 1;
  43100. +}
  43101. +
  43102. +/** Handles the Rx Status Queue Level Interrupt, which indicates that there is at
  43103. + * least one packet in the Rx FIFO. The packets are moved from the FIFO to
  43104. + * memory if the DWC_otg controller is operating in Slave mode. */
  43105. +int32_t dwc_otg_hcd_handle_rx_status_q_level_intr(dwc_otg_hcd_t * dwc_otg_hcd)
  43106. +{
  43107. + host_grxsts_data_t grxsts;
  43108. + dwc_hc_t *hc = NULL;
  43109. +
  43110. + DWC_DEBUGPL(DBG_HCD, "--RxStsQ Level Interrupt--\n");
  43111. +
  43112. + grxsts.d32 =
  43113. + DWC_READ_REG32(&dwc_otg_hcd->core_if->core_global_regs->grxstsp);
  43114. +
  43115. + hc = dwc_otg_hcd->hc_ptr_array[grxsts.b.chnum];
  43116. + if (!hc) {
  43117. + DWC_ERROR("Unable to get corresponding channel\n");
  43118. + return 0;
  43119. + }
  43120. +
  43121. + /* Packet Status */
  43122. + DWC_DEBUGPL(DBG_HCDV, " Ch num = %d\n", grxsts.b.chnum);
  43123. + DWC_DEBUGPL(DBG_HCDV, " Count = %d\n", grxsts.b.bcnt);
  43124. + DWC_DEBUGPL(DBG_HCDV, " DPID = %d, hc.dpid = %d\n", grxsts.b.dpid,
  43125. + hc->data_pid_start);
  43126. + DWC_DEBUGPL(DBG_HCDV, " PStatus = %d\n", grxsts.b.pktsts);
  43127. +
  43128. + switch (grxsts.b.pktsts) {
  43129. + case DWC_GRXSTS_PKTSTS_IN:
  43130. + /* Read the data into the host buffer. */
  43131. + if (grxsts.b.bcnt > 0) {
  43132. + dwc_otg_read_packet(dwc_otg_hcd->core_if,
  43133. + hc->xfer_buff, grxsts.b.bcnt);
  43134. +
  43135. + /* Update the HC fields for the next packet received. */
  43136. + hc->xfer_count += grxsts.b.bcnt;
  43137. + hc->xfer_buff += grxsts.b.bcnt;
  43138. + }
  43139. +
  43140. + case DWC_GRXSTS_PKTSTS_IN_XFER_COMP:
  43141. + case DWC_GRXSTS_PKTSTS_DATA_TOGGLE_ERR:
  43142. + case DWC_GRXSTS_PKTSTS_CH_HALTED:
  43143. + /* Handled in interrupt, just ignore data */
  43144. + break;
  43145. + default:
  43146. + DWC_ERROR("RX_STS_Q Interrupt: Unknown status %d\n",
  43147. + grxsts.b.pktsts);
  43148. + break;
  43149. + }
  43150. +
  43151. + return 1;
  43152. +}
  43153. +
  43154. +/** This interrupt occurs when the non-periodic Tx FIFO is half-empty. More
  43155. + * data packets may be written to the FIFO for OUT transfers. More requests
  43156. + * may be written to the non-periodic request queue for IN transfers. This
  43157. + * interrupt is enabled only in Slave mode. */
  43158. +int32_t dwc_otg_hcd_handle_np_tx_fifo_empty_intr(dwc_otg_hcd_t * dwc_otg_hcd)
  43159. +{
  43160. + DWC_DEBUGPL(DBG_HCD, "--Non-Periodic TxFIFO Empty Interrupt--\n");
  43161. + dwc_otg_hcd_queue_transactions(dwc_otg_hcd,
  43162. + DWC_OTG_TRANSACTION_NON_PERIODIC);
  43163. + return 1;
  43164. +}
  43165. +
  43166. +/** This interrupt occurs when the periodic Tx FIFO is half-empty. More data
  43167. + * packets may be written to the FIFO for OUT transfers. More requests may be
  43168. + * written to the periodic request queue for IN transfers. This interrupt is
  43169. + * enabled only in Slave mode. */
  43170. +int32_t dwc_otg_hcd_handle_perio_tx_fifo_empty_intr(dwc_otg_hcd_t * dwc_otg_hcd)
  43171. +{
  43172. + DWC_DEBUGPL(DBG_HCD, "--Periodic TxFIFO Empty Interrupt--\n");
  43173. + dwc_otg_hcd_queue_transactions(dwc_otg_hcd,
  43174. + DWC_OTG_TRANSACTION_PERIODIC);
  43175. + return 1;
  43176. +}
  43177. +
  43178. +/** There are multiple conditions that can cause a port interrupt. This function
  43179. + * determines which interrupt conditions have occurred and handles them
  43180. + * appropriately. */
  43181. +int32_t dwc_otg_hcd_handle_port_intr(dwc_otg_hcd_t * dwc_otg_hcd)
  43182. +{
  43183. + int retval = 0;
  43184. + hprt0_data_t hprt0;
  43185. + hprt0_data_t hprt0_modify;
  43186. +
  43187. + hprt0.d32 = DWC_READ_REG32(dwc_otg_hcd->core_if->host_if->hprt0);
  43188. + hprt0_modify.d32 = DWC_READ_REG32(dwc_otg_hcd->core_if->host_if->hprt0);
  43189. +
  43190. + /* Clear appropriate bits in HPRT0 to clear the interrupt bit in
  43191. + * GINTSTS */
  43192. +
  43193. + hprt0_modify.b.prtena = 0;
  43194. + hprt0_modify.b.prtconndet = 0;
  43195. + hprt0_modify.b.prtenchng = 0;
  43196. + hprt0_modify.b.prtovrcurrchng = 0;
  43197. +
  43198. + /* Port Connect Detected
  43199. + * Set flag and clear if detected */
  43200. + if (dwc_otg_hcd->core_if->hibernation_suspend == 1) {
  43201. + // Dont modify port status if we are in hibernation state
  43202. + hprt0_modify.b.prtconndet = 1;
  43203. + hprt0_modify.b.prtenchng = 1;
  43204. + DWC_WRITE_REG32(dwc_otg_hcd->core_if->host_if->hprt0, hprt0_modify.d32);
  43205. + hprt0.d32 = DWC_READ_REG32(dwc_otg_hcd->core_if->host_if->hprt0);
  43206. + return retval;
  43207. + }
  43208. +
  43209. + if (hprt0.b.prtconndet) {
  43210. + /** @todo - check if steps performed in 'else' block should be perfromed regardles adp */
  43211. + if (dwc_otg_hcd->core_if->adp_enable &&
  43212. + dwc_otg_hcd->core_if->adp.vbuson_timer_started == 1) {
  43213. + DWC_PRINTF("PORT CONNECT DETECTED ----------------\n");
  43214. + DWC_TIMER_CANCEL(dwc_otg_hcd->core_if->adp.vbuson_timer);
  43215. + dwc_otg_hcd->core_if->adp.vbuson_timer_started = 0;
  43216. + /* TODO - check if this is required, as
  43217. + * host initialization was already performed
  43218. + * after initial ADP probing
  43219. + */
  43220. + /*dwc_otg_hcd->core_if->adp.vbuson_timer_started = 0;
  43221. + dwc_otg_core_init(dwc_otg_hcd->core_if);
  43222. + dwc_otg_enable_global_interrupts(dwc_otg_hcd->core_if);
  43223. + cil_hcd_start(dwc_otg_hcd->core_if);*/
  43224. + } else {
  43225. +
  43226. + DWC_DEBUGPL(DBG_HCD, "--Port Interrupt HPRT0=0x%08x "
  43227. + "Port Connect Detected--\n", hprt0.d32);
  43228. + dwc_otg_hcd->flags.b.port_connect_status_change = 1;
  43229. + dwc_otg_hcd->flags.b.port_connect_status = 1;
  43230. + hprt0_modify.b.prtconndet = 1;
  43231. +
  43232. + /* B-Device has connected, Delete the connection timer. */
  43233. + DWC_TIMER_CANCEL(dwc_otg_hcd->conn_timer);
  43234. + }
  43235. + /* The Hub driver asserts a reset when it sees port connect
  43236. + * status change flag */
  43237. + retval |= 1;
  43238. + }
  43239. +
  43240. + /* Port Enable Changed
  43241. + * Clear if detected - Set internal flag if disabled */
  43242. + if (hprt0.b.prtenchng) {
  43243. + DWC_DEBUGPL(DBG_HCD, " --Port Interrupt HPRT0=0x%08x "
  43244. + "Port Enable Changed--\n", hprt0.d32);
  43245. + hprt0_modify.b.prtenchng = 1;
  43246. + if (hprt0.b.prtena == 1) {
  43247. + hfir_data_t hfir;
  43248. + int do_reset = 0;
  43249. + dwc_otg_core_params_t *params =
  43250. + dwc_otg_hcd->core_if->core_params;
  43251. + dwc_otg_core_global_regs_t *global_regs =
  43252. + dwc_otg_hcd->core_if->core_global_regs;
  43253. + dwc_otg_host_if_t *host_if =
  43254. + dwc_otg_hcd->core_if->host_if;
  43255. +
  43256. + /* Every time when port enables calculate
  43257. + * HFIR.FrInterval
  43258. + */
  43259. + hfir.d32 = DWC_READ_REG32(&host_if->host_global_regs->hfir);
  43260. + hfir.b.frint = calc_frame_interval(dwc_otg_hcd->core_if);
  43261. + DWC_WRITE_REG32(&host_if->host_global_regs->hfir, hfir.d32);
  43262. +
  43263. + /* Check if we need to adjust the PHY clock speed for
  43264. + * low power and adjust it */
  43265. + if (params->host_support_fs_ls_low_power) {
  43266. + gusbcfg_data_t usbcfg;
  43267. +
  43268. + usbcfg.d32 =
  43269. + DWC_READ_REG32(&global_regs->gusbcfg);
  43270. +
  43271. + if (hprt0.b.prtspd == DWC_HPRT0_PRTSPD_LOW_SPEED
  43272. + || hprt0.b.prtspd ==
  43273. + DWC_HPRT0_PRTSPD_FULL_SPEED) {
  43274. + /*
  43275. + * Low power
  43276. + */
  43277. + hcfg_data_t hcfg;
  43278. + if (usbcfg.b.phylpwrclksel == 0) {
  43279. + /* Set PHY low power clock select for FS/LS devices */
  43280. + usbcfg.b.phylpwrclksel = 1;
  43281. + DWC_WRITE_REG32
  43282. + (&global_regs->gusbcfg,
  43283. + usbcfg.d32);
  43284. + do_reset = 1;
  43285. + }
  43286. +
  43287. + hcfg.d32 =
  43288. + DWC_READ_REG32
  43289. + (&host_if->host_global_regs->hcfg);
  43290. +
  43291. + if (hprt0.b.prtspd ==
  43292. + DWC_HPRT0_PRTSPD_LOW_SPEED
  43293. + && params->host_ls_low_power_phy_clk
  43294. + ==
  43295. + DWC_HOST_LS_LOW_POWER_PHY_CLK_PARAM_6MHZ)
  43296. + {
  43297. + /* 6 MHZ */
  43298. + DWC_DEBUGPL(DBG_CIL,
  43299. + "FS_PHY programming HCFG to 6 MHz (Low Power)\n");
  43300. + if (hcfg.b.fslspclksel !=
  43301. + DWC_HCFG_6_MHZ) {
  43302. + hcfg.b.fslspclksel =
  43303. + DWC_HCFG_6_MHZ;
  43304. + DWC_WRITE_REG32
  43305. + (&host_if->host_global_regs->hcfg,
  43306. + hcfg.d32);
  43307. + do_reset = 1;
  43308. + }
  43309. + } else {
  43310. + /* 48 MHZ */
  43311. + DWC_DEBUGPL(DBG_CIL,
  43312. + "FS_PHY programming HCFG to 48 MHz ()\n");
  43313. + if (hcfg.b.fslspclksel !=
  43314. + DWC_HCFG_48_MHZ) {
  43315. + hcfg.b.fslspclksel =
  43316. + DWC_HCFG_48_MHZ;
  43317. + DWC_WRITE_REG32
  43318. + (&host_if->host_global_regs->hcfg,
  43319. + hcfg.d32);
  43320. + do_reset = 1;
  43321. + }
  43322. + }
  43323. + } else {
  43324. + /*
  43325. + * Not low power
  43326. + */
  43327. + if (usbcfg.b.phylpwrclksel == 1) {
  43328. + usbcfg.b.phylpwrclksel = 0;
  43329. + DWC_WRITE_REG32
  43330. + (&global_regs->gusbcfg,
  43331. + usbcfg.d32);
  43332. + do_reset = 1;
  43333. + }
  43334. + }
  43335. +
  43336. + if (do_reset) {
  43337. + DWC_TASK_SCHEDULE(dwc_otg_hcd->reset_tasklet);
  43338. + }
  43339. + }
  43340. +
  43341. + if (!do_reset) {
  43342. + /* Port has been enabled set the reset change flag */
  43343. + dwc_otg_hcd->flags.b.port_reset_change = 1;
  43344. + }
  43345. + } else {
  43346. + dwc_otg_hcd->flags.b.port_enable_change = 1;
  43347. + }
  43348. + retval |= 1;
  43349. + }
  43350. +
  43351. + /** Overcurrent Change Interrupt */
  43352. + if (hprt0.b.prtovrcurrchng) {
  43353. + DWC_DEBUGPL(DBG_HCD, " --Port Interrupt HPRT0=0x%08x "
  43354. + "Port Overcurrent Changed--\n", hprt0.d32);
  43355. + dwc_otg_hcd->flags.b.port_over_current_change = 1;
  43356. + hprt0_modify.b.prtovrcurrchng = 1;
  43357. + retval |= 1;
  43358. + }
  43359. +
  43360. + /* Clear Port Interrupts */
  43361. + DWC_WRITE_REG32(dwc_otg_hcd->core_if->host_if->hprt0, hprt0_modify.d32);
  43362. +
  43363. + return retval;
  43364. +}
  43365. +
  43366. +/** This interrupt indicates that one or more host channels has a pending
  43367. + * interrupt. There are multiple conditions that can cause each host channel
  43368. + * interrupt. This function determines which conditions have occurred for each
  43369. + * host channel interrupt and handles them appropriately. */
  43370. +int32_t dwc_otg_hcd_handle_hc_intr(dwc_otg_hcd_t * dwc_otg_hcd)
  43371. +{
  43372. + int i;
  43373. + int retval = 0;
  43374. + haint_data_t haint = { .d32 = 0 } ;
  43375. +
  43376. + /* Clear appropriate bits in HCINTn to clear the interrupt bit in
  43377. + * GINTSTS */
  43378. +
  43379. + if (!fiq_fsm_enable)
  43380. + haint.d32 = dwc_otg_read_host_all_channels_intr(dwc_otg_hcd->core_if);
  43381. +
  43382. + // Overwrite with saved interrupts from fiq handler
  43383. + if(fiq_fsm_enable)
  43384. + {
  43385. + /* check the mask? */
  43386. + local_fiq_disable();
  43387. + fiq_fsm_spin_lock(&dwc_otg_hcd->fiq_state->lock);
  43388. + haint.b2.chint |= ~(dwc_otg_hcd->fiq_state->haintmsk_saved.b2.chint);
  43389. + dwc_otg_hcd->fiq_state->haintmsk_saved.b2.chint = ~0;
  43390. + fiq_fsm_spin_unlock(&dwc_otg_hcd->fiq_state->lock);
  43391. + local_fiq_enable();
  43392. + }
  43393. +
  43394. + for (i = 0; i < dwc_otg_hcd->core_if->core_params->host_channels; i++) {
  43395. + if (haint.b2.chint & (1 << i)) {
  43396. + retval |= dwc_otg_hcd_handle_hc_n_intr(dwc_otg_hcd, i);
  43397. + }
  43398. + }
  43399. +
  43400. + return retval;
  43401. +}
  43402. +
  43403. +/**
  43404. + * Gets the actual length of a transfer after the transfer halts. _halt_status
  43405. + * holds the reason for the halt.
  43406. + *
  43407. + * For IN transfers where halt_status is DWC_OTG_HC_XFER_COMPLETE,
  43408. + * *short_read is set to 1 upon return if less than the requested
  43409. + * number of bytes were transferred. Otherwise, *short_read is set to 0 upon
  43410. + * return. short_read may also be NULL on entry, in which case it remains
  43411. + * unchanged.
  43412. + */
  43413. +static uint32_t get_actual_xfer_length(dwc_hc_t * hc,
  43414. + dwc_otg_hc_regs_t * hc_regs,
  43415. + dwc_otg_qtd_t * qtd,
  43416. + dwc_otg_halt_status_e halt_status,
  43417. + int *short_read)
  43418. +{
  43419. + hctsiz_data_t hctsiz;
  43420. + uint32_t length;
  43421. +
  43422. + if (short_read != NULL) {
  43423. + *short_read = 0;
  43424. + }
  43425. + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
  43426. +
  43427. + if (halt_status == DWC_OTG_HC_XFER_COMPLETE) {
  43428. + if (hc->ep_is_in) {
  43429. + length = hc->xfer_len - hctsiz.b.xfersize;
  43430. + if (short_read != NULL) {
  43431. + *short_read = (hctsiz.b.xfersize != 0);
  43432. + }
  43433. + } else if (hc->qh->do_split) {
  43434. + //length = split_out_xfersize[hc->hc_num];
  43435. + length = qtd->ssplit_out_xfer_count;
  43436. + } else {
  43437. + length = hc->xfer_len;
  43438. + }
  43439. + } else {
  43440. + /*
  43441. + * Must use the hctsiz.pktcnt field to determine how much data
  43442. + * has been transferred. This field reflects the number of
  43443. + * packets that have been transferred via the USB. This is
  43444. + * always an integral number of packets if the transfer was
  43445. + * halted before its normal completion. (Can't use the
  43446. + * hctsiz.xfersize field because that reflects the number of
  43447. + * bytes transferred via the AHB, not the USB).
  43448. + */
  43449. + length =
  43450. + (hc->start_pkt_count - hctsiz.b.pktcnt) * hc->max_packet;
  43451. + }
  43452. +
  43453. + return length;
  43454. +}
  43455. +
  43456. +/**
  43457. + * Updates the state of the URB after a Transfer Complete interrupt on the
  43458. + * host channel. Updates the actual_length field of the URB based on the
  43459. + * number of bytes transferred via the host channel. Sets the URB status
  43460. + * if the data transfer is finished.
  43461. + *
  43462. + * @return 1 if the data transfer specified by the URB is completely finished,
  43463. + * 0 otherwise.
  43464. + */
  43465. +static int update_urb_state_xfer_comp(dwc_hc_t * hc,
  43466. + dwc_otg_hc_regs_t * hc_regs,
  43467. + dwc_otg_hcd_urb_t * urb,
  43468. + dwc_otg_qtd_t * qtd)
  43469. +{
  43470. + int xfer_done = 0;
  43471. + int short_read = 0;
  43472. +
  43473. + int xfer_length;
  43474. +
  43475. + xfer_length = get_actual_xfer_length(hc, hc_regs, qtd,
  43476. + DWC_OTG_HC_XFER_COMPLETE,
  43477. + &short_read);
  43478. +
  43479. + if (urb->actual_length + xfer_length > urb->length) {
  43480. + printk_once(KERN_DEBUG "dwc_otg: DEVICE:%03d : %s:%d:trimming xfer length\n",
  43481. + hc->dev_addr, __func__, __LINE__);
  43482. + xfer_length = urb->length - urb->actual_length;
  43483. + }
  43484. +
  43485. + /* non DWORD-aligned buffer case handling. */
  43486. + if (hc->align_buff && xfer_length && hc->ep_is_in) {
  43487. + dwc_memcpy(urb->buf + urb->actual_length, hc->qh->dw_align_buf,
  43488. + xfer_length);
  43489. + }
  43490. +
  43491. + urb->actual_length += xfer_length;
  43492. +
  43493. + if (xfer_length && (hc->ep_type == DWC_OTG_EP_TYPE_BULK) &&
  43494. + (urb->flags & URB_SEND_ZERO_PACKET)
  43495. + && (urb->actual_length == urb->length)
  43496. + && !(urb->length % hc->max_packet)) {
  43497. + xfer_done = 0;
  43498. + } else if (short_read || urb->actual_length >= urb->length) {
  43499. + xfer_done = 1;
  43500. + urb->status = 0;
  43501. + }
  43502. +
  43503. +#ifdef DEBUG
  43504. + {
  43505. + hctsiz_data_t hctsiz;
  43506. + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
  43507. + DWC_DEBUGPL(DBG_HCDV, "DWC_otg: %s: %s, channel %d\n",
  43508. + __func__, (hc->ep_is_in ? "IN" : "OUT"),
  43509. + hc->hc_num);
  43510. + DWC_DEBUGPL(DBG_HCDV, " hc->xfer_len %d\n", hc->xfer_len);
  43511. + DWC_DEBUGPL(DBG_HCDV, " hctsiz.xfersize %d\n",
  43512. + hctsiz.b.xfersize);
  43513. + DWC_DEBUGPL(DBG_HCDV, " urb->transfer_buffer_length %d\n",
  43514. + urb->length);
  43515. + DWC_DEBUGPL(DBG_HCDV, " urb->actual_length %d\n",
  43516. + urb->actual_length);
  43517. + DWC_DEBUGPL(DBG_HCDV, " short_read %d, xfer_done %d\n",
  43518. + short_read, xfer_done);
  43519. + }
  43520. +#endif
  43521. +
  43522. + return xfer_done;
  43523. +}
  43524. +
  43525. +/*
  43526. + * Save the starting data toggle for the next transfer. The data toggle is
  43527. + * saved in the QH for non-control transfers and it's saved in the QTD for
  43528. + * control transfers.
  43529. + */
  43530. +void dwc_otg_hcd_save_data_toggle(dwc_hc_t * hc,
  43531. + dwc_otg_hc_regs_t * hc_regs, dwc_otg_qtd_t * qtd)
  43532. +{
  43533. + hctsiz_data_t hctsiz;
  43534. + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
  43535. +
  43536. + if (hc->ep_type != DWC_OTG_EP_TYPE_CONTROL) {
  43537. + dwc_otg_qh_t *qh = hc->qh;
  43538. + if (hctsiz.b.pid == DWC_HCTSIZ_DATA0) {
  43539. + qh->data_toggle = DWC_OTG_HC_PID_DATA0;
  43540. + } else {
  43541. + qh->data_toggle = DWC_OTG_HC_PID_DATA1;
  43542. + }
  43543. + } else {
  43544. + if (hctsiz.b.pid == DWC_HCTSIZ_DATA0) {
  43545. + qtd->data_toggle = DWC_OTG_HC_PID_DATA0;
  43546. + } else {
  43547. + qtd->data_toggle = DWC_OTG_HC_PID_DATA1;
  43548. + }
  43549. + }
  43550. +}
  43551. +
  43552. +/**
  43553. + * Updates the state of an Isochronous URB when the transfer is stopped for
  43554. + * any reason. The fields of the current entry in the frame descriptor array
  43555. + * are set based on the transfer state and the input _halt_status. Completes
  43556. + * the Isochronous URB if all the URB frames have been completed.
  43557. + *
  43558. + * @return DWC_OTG_HC_XFER_COMPLETE if there are more frames remaining to be
  43559. + * transferred in the URB. Otherwise return DWC_OTG_HC_XFER_URB_COMPLETE.
  43560. + */
  43561. +static dwc_otg_halt_status_e
  43562. +update_isoc_urb_state(dwc_otg_hcd_t * hcd,
  43563. + dwc_hc_t * hc,
  43564. + dwc_otg_hc_regs_t * hc_regs,
  43565. + dwc_otg_qtd_t * qtd, dwc_otg_halt_status_e halt_status)
  43566. +{
  43567. + dwc_otg_hcd_urb_t *urb = qtd->urb;
  43568. + dwc_otg_halt_status_e ret_val = halt_status;
  43569. + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
  43570. +
  43571. + frame_desc = &urb->iso_descs[qtd->isoc_frame_index];
  43572. + switch (halt_status) {
  43573. + case DWC_OTG_HC_XFER_COMPLETE:
  43574. + frame_desc->status = 0;
  43575. + frame_desc->actual_length =
  43576. + get_actual_xfer_length(hc, hc_regs, qtd, halt_status, NULL);
  43577. +
  43578. + /* non DWORD-aligned buffer case handling. */
  43579. + if (hc->align_buff && frame_desc->actual_length && hc->ep_is_in) {
  43580. + dwc_memcpy(urb->buf + frame_desc->offset + qtd->isoc_split_offset,
  43581. + hc->qh->dw_align_buf, frame_desc->actual_length);
  43582. + }
  43583. +
  43584. + break;
  43585. + case DWC_OTG_HC_XFER_FRAME_OVERRUN:
  43586. + urb->error_count++;
  43587. + if (hc->ep_is_in) {
  43588. + frame_desc->status = -DWC_E_NO_STREAM_RES;
  43589. + } else {
  43590. + frame_desc->status = -DWC_E_COMMUNICATION;
  43591. + }
  43592. + frame_desc->actual_length = 0;
  43593. + break;
  43594. + case DWC_OTG_HC_XFER_BABBLE_ERR:
  43595. + urb->error_count++;
  43596. + frame_desc->status = -DWC_E_OVERFLOW;
  43597. + /* Don't need to update actual_length in this case. */
  43598. + break;
  43599. + case DWC_OTG_HC_XFER_XACT_ERR:
  43600. + urb->error_count++;
  43601. + frame_desc->status = -DWC_E_PROTOCOL;
  43602. + frame_desc->actual_length =
  43603. + get_actual_xfer_length(hc, hc_regs, qtd, halt_status, NULL);
  43604. +
  43605. + /* non DWORD-aligned buffer case handling. */
  43606. + if (hc->align_buff && frame_desc->actual_length && hc->ep_is_in) {
  43607. + dwc_memcpy(urb->buf + frame_desc->offset + qtd->isoc_split_offset,
  43608. + hc->qh->dw_align_buf, frame_desc->actual_length);
  43609. + }
  43610. + /* Skip whole frame */
  43611. + if (hc->qh->do_split && (hc->ep_type == DWC_OTG_EP_TYPE_ISOC) &&
  43612. + hc->ep_is_in && hcd->core_if->dma_enable) {
  43613. + qtd->complete_split = 0;
  43614. + qtd->isoc_split_offset = 0;
  43615. + }
  43616. +
  43617. + break;
  43618. + default:
  43619. + DWC_ASSERT(1, "Unhandled _halt_status (%d)\n", halt_status);
  43620. + break;
  43621. + }
  43622. + if (++qtd->isoc_frame_index == urb->packet_count) {
  43623. + /*
  43624. + * urb->status is not used for isoc transfers.
  43625. + * The individual frame_desc statuses are used instead.
  43626. + */
  43627. + hcd->fops->complete(hcd, urb->priv, urb, 0);
  43628. + ret_val = DWC_OTG_HC_XFER_URB_COMPLETE;
  43629. + } else {
  43630. + ret_val = DWC_OTG_HC_XFER_COMPLETE;
  43631. + }
  43632. + return ret_val;
  43633. +}
  43634. +
  43635. +/**
  43636. + * Frees the first QTD in the QH's list if free_qtd is 1. For non-periodic
  43637. + * QHs, removes the QH from the active non-periodic schedule. If any QTDs are
  43638. + * still linked to the QH, the QH is added to the end of the inactive
  43639. + * non-periodic schedule. For periodic QHs, removes the QH from the periodic
  43640. + * schedule if no more QTDs are linked to the QH.
  43641. + */
  43642. +static void deactivate_qh(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh, int free_qtd)
  43643. +{
  43644. + int continue_split = 0;
  43645. + dwc_otg_qtd_t *qtd;
  43646. +
  43647. + DWC_DEBUGPL(DBG_HCDV, " %s(%p,%p,%d)\n", __func__, hcd, qh, free_qtd);
  43648. +
  43649. + qtd = DWC_CIRCLEQ_FIRST(&qh->qtd_list);
  43650. +
  43651. + if (qtd->complete_split) {
  43652. + continue_split = 1;
  43653. + } else if (qtd->isoc_split_pos == DWC_HCSPLIT_XACTPOS_MID ||
  43654. + qtd->isoc_split_pos == DWC_HCSPLIT_XACTPOS_END) {
  43655. + continue_split = 1;
  43656. + }
  43657. +
  43658. + if (free_qtd) {
  43659. + dwc_otg_hcd_qtd_remove_and_free(hcd, qtd, qh);
  43660. + continue_split = 0;
  43661. + }
  43662. +
  43663. + qh->channel = NULL;
  43664. + dwc_otg_hcd_qh_deactivate(hcd, qh, continue_split);
  43665. +}
  43666. +
  43667. +/**
  43668. + * Releases a host channel for use by other transfers. Attempts to select and
  43669. + * queue more transactions since at least one host channel is available.
  43670. + *
  43671. + * @param hcd The HCD state structure.
  43672. + * @param hc The host channel to release.
  43673. + * @param qtd The QTD associated with the host channel. This QTD may be freed
  43674. + * if the transfer is complete or an error has occurred.
  43675. + * @param halt_status Reason the channel is being released. This status
  43676. + * determines the actions taken by this function.
  43677. + */
  43678. +static void release_channel(dwc_otg_hcd_t * hcd,
  43679. + dwc_hc_t * hc,
  43680. + dwc_otg_qtd_t * qtd,
  43681. + dwc_otg_halt_status_e halt_status)
  43682. +{
  43683. + dwc_otg_transaction_type_e tr_type;
  43684. + int free_qtd;
  43685. + dwc_irqflags_t flags;
  43686. + dwc_spinlock_t *channel_lock = hcd->channel_lock;
  43687. +
  43688. + int hog_port = 0;
  43689. +
  43690. + DWC_DEBUGPL(DBG_HCDV, " %s: channel %d, halt_status %d, xfer_len %d\n",
  43691. + __func__, hc->hc_num, halt_status, hc->xfer_len);
  43692. +
  43693. + if(fiq_fsm_enable && hc->do_split) {
  43694. + if(!hc->ep_is_in && hc->ep_type == UE_ISOCHRONOUS) {
  43695. + if(hc->xact_pos == DWC_HCSPLIT_XACTPOS_MID ||
  43696. + hc->xact_pos == DWC_HCSPLIT_XACTPOS_BEGIN) {
  43697. + hog_port = 0;
  43698. + }
  43699. + }
  43700. + }
  43701. +
  43702. + switch (halt_status) {
  43703. + case DWC_OTG_HC_XFER_URB_COMPLETE:
  43704. + free_qtd = 1;
  43705. + break;
  43706. + case DWC_OTG_HC_XFER_AHB_ERR:
  43707. + case DWC_OTG_HC_XFER_STALL:
  43708. + case DWC_OTG_HC_XFER_BABBLE_ERR:
  43709. + free_qtd = 1;
  43710. + break;
  43711. + case DWC_OTG_HC_XFER_XACT_ERR:
  43712. + if (qtd->error_count >= 3) {
  43713. + DWC_DEBUGPL(DBG_HCDV,
  43714. + " Complete URB with transaction error\n");
  43715. + free_qtd = 1;
  43716. + qtd->urb->status = -DWC_E_PROTOCOL;
  43717. + hcd->fops->complete(hcd, qtd->urb->priv,
  43718. + qtd->urb, -DWC_E_PROTOCOL);
  43719. + } else {
  43720. + free_qtd = 0;
  43721. + }
  43722. + break;
  43723. + case DWC_OTG_HC_XFER_URB_DEQUEUE:
  43724. + /*
  43725. + * The QTD has already been removed and the QH has been
  43726. + * deactivated. Don't want to do anything except release the
  43727. + * host channel and try to queue more transfers.
  43728. + */
  43729. + goto cleanup;
  43730. + case DWC_OTG_HC_XFER_NO_HALT_STATUS:
  43731. + free_qtd = 0;
  43732. + break;
  43733. + case DWC_OTG_HC_XFER_PERIODIC_INCOMPLETE:
  43734. + DWC_DEBUGPL(DBG_HCDV,
  43735. + " Complete URB with I/O error\n");
  43736. + free_qtd = 1;
  43737. + qtd->urb->status = -DWC_E_IO;
  43738. + hcd->fops->complete(hcd, qtd->urb->priv,
  43739. + qtd->urb, -DWC_E_IO);
  43740. + break;
  43741. + default:
  43742. + free_qtd = 0;
  43743. + break;
  43744. + }
  43745. +
  43746. + deactivate_qh(hcd, hc->qh, free_qtd);
  43747. +
  43748. +cleanup:
  43749. + /*
  43750. + * Release the host channel for use by other transfers. The cleanup
  43751. + * function clears the channel interrupt enables and conditions, so
  43752. + * there's no need to clear the Channel Halted interrupt separately.
  43753. + */
  43754. + if (fiq_fsm_enable && hcd->fiq_state->channel[hc->hc_num].fsm != FIQ_PASSTHROUGH)
  43755. + dwc_otg_cleanup_fiq_channel(hcd, hc->hc_num);
  43756. + dwc_otg_hc_cleanup(hcd->core_if, hc);
  43757. + DWC_CIRCLEQ_INSERT_TAIL(&hcd->free_hc_list, hc, hc_list_entry);
  43758. +
  43759. + if (!microframe_schedule) {
  43760. + switch (hc->ep_type) {
  43761. + case DWC_OTG_EP_TYPE_CONTROL:
  43762. + case DWC_OTG_EP_TYPE_BULK:
  43763. + hcd->non_periodic_channels--;
  43764. + break;
  43765. +
  43766. + default:
  43767. + /*
  43768. + * Don't release reservations for periodic channels here.
  43769. + * That's done when a periodic transfer is descheduled (i.e.
  43770. + * when the QH is removed from the periodic schedule).
  43771. + */
  43772. + break;
  43773. + }
  43774. + } else {
  43775. +
  43776. + DWC_SPINLOCK_IRQSAVE(channel_lock, &flags);
  43777. + hcd->available_host_channels++;
  43778. + fiq_print(FIQDBG_INT, hcd->fiq_state, "AHC = %d ", hcd->available_host_channels);
  43779. + DWC_SPINUNLOCK_IRQRESTORE(channel_lock, flags);
  43780. + }
  43781. +
  43782. + /* Try to queue more transfers now that there's a free channel. */
  43783. + tr_type = dwc_otg_hcd_select_transactions(hcd);
  43784. + if (tr_type != DWC_OTG_TRANSACTION_NONE) {
  43785. + dwc_otg_hcd_queue_transactions(hcd, tr_type);
  43786. + }
  43787. +}
  43788. +
  43789. +/**
  43790. + * Halts a host channel. If the channel cannot be halted immediately because
  43791. + * the request queue is full, this function ensures that the FIFO empty
  43792. + * interrupt for the appropriate queue is enabled so that the halt request can
  43793. + * be queued when there is space in the request queue.
  43794. + *
  43795. + * This function may also be called in DMA mode. In that case, the channel is
  43796. + * simply released since the core always halts the channel automatically in
  43797. + * DMA mode.
  43798. + */
  43799. +static void halt_channel(dwc_otg_hcd_t * hcd,
  43800. + dwc_hc_t * hc,
  43801. + dwc_otg_qtd_t * qtd, dwc_otg_halt_status_e halt_status)
  43802. +{
  43803. + if (hcd->core_if->dma_enable) {
  43804. + release_channel(hcd, hc, qtd, halt_status);
  43805. + return;
  43806. + }
  43807. +
  43808. + /* Slave mode processing... */
  43809. + dwc_otg_hc_halt(hcd->core_if, hc, halt_status);
  43810. +
  43811. + if (hc->halt_on_queue) {
  43812. + gintmsk_data_t gintmsk = {.d32 = 0 };
  43813. + dwc_otg_core_global_regs_t *global_regs;
  43814. + global_regs = hcd->core_if->core_global_regs;
  43815. +
  43816. + if (hc->ep_type == DWC_OTG_EP_TYPE_CONTROL ||
  43817. + hc->ep_type == DWC_OTG_EP_TYPE_BULK) {
  43818. + /*
  43819. + * Make sure the Non-periodic Tx FIFO empty interrupt
  43820. + * is enabled so that the non-periodic schedule will
  43821. + * be processed.
  43822. + */
  43823. + gintmsk.b.nptxfempty = 1;
  43824. + if (fiq_enable) {
  43825. + local_fiq_disable();
  43826. + fiq_fsm_spin_lock(&hcd->fiq_state->lock);
  43827. + DWC_MODIFY_REG32(&global_regs->gintmsk, 0, gintmsk.d32);
  43828. + fiq_fsm_spin_unlock(&hcd->fiq_state->lock);
  43829. + local_fiq_enable();
  43830. + } else {
  43831. + DWC_MODIFY_REG32(&global_regs->gintmsk, 0, gintmsk.d32);
  43832. + }
  43833. + } else {
  43834. + /*
  43835. + * Move the QH from the periodic queued schedule to
  43836. + * the periodic assigned schedule. This allows the
  43837. + * halt to be queued when the periodic schedule is
  43838. + * processed.
  43839. + */
  43840. + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_assigned,
  43841. + &hc->qh->qh_list_entry);
  43842. +
  43843. + /*
  43844. + * Make sure the Periodic Tx FIFO Empty interrupt is
  43845. + * enabled so that the periodic schedule will be
  43846. + * processed.
  43847. + */
  43848. + gintmsk.b.ptxfempty = 1;
  43849. + if (fiq_enable) {
  43850. + local_fiq_disable();
  43851. + fiq_fsm_spin_lock(&hcd->fiq_state->lock);
  43852. + DWC_MODIFY_REG32(&global_regs->gintmsk, 0, gintmsk.d32);
  43853. + fiq_fsm_spin_unlock(&hcd->fiq_state->lock);
  43854. + local_fiq_enable();
  43855. + } else {
  43856. + DWC_MODIFY_REG32(&global_regs->gintmsk, 0, gintmsk.d32);
  43857. + }
  43858. + }
  43859. + }
  43860. +}
  43861. +
  43862. +/**
  43863. + * Performs common cleanup for non-periodic transfers after a Transfer
  43864. + * Complete interrupt. This function should be called after any endpoint type
  43865. + * specific handling is finished to release the host channel.
  43866. + */
  43867. +static void complete_non_periodic_xfer(dwc_otg_hcd_t * hcd,
  43868. + dwc_hc_t * hc,
  43869. + dwc_otg_hc_regs_t * hc_regs,
  43870. + dwc_otg_qtd_t * qtd,
  43871. + dwc_otg_halt_status_e halt_status)
  43872. +{
  43873. + hcint_data_t hcint;
  43874. +
  43875. + qtd->error_count = 0;
  43876. +
  43877. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  43878. + if (hcint.b.nyet) {
  43879. + /*
  43880. + * Got a NYET on the last transaction of the transfer. This
  43881. + * means that the endpoint should be in the PING state at the
  43882. + * beginning of the next transfer.
  43883. + */
  43884. + hc->qh->ping_state = 1;
  43885. + clear_hc_int(hc_regs, nyet);
  43886. + }
  43887. +
  43888. + /*
  43889. + * Always halt and release the host channel to make it available for
  43890. + * more transfers. There may still be more phases for a control
  43891. + * transfer or more data packets for a bulk transfer at this point,
  43892. + * but the host channel is still halted. A channel will be reassigned
  43893. + * to the transfer when the non-periodic schedule is processed after
  43894. + * the channel is released. This allows transactions to be queued
  43895. + * properly via dwc_otg_hcd_queue_transactions, which also enables the
  43896. + * Tx FIFO Empty interrupt if necessary.
  43897. + */
  43898. + if (hc->ep_is_in) {
  43899. + /*
  43900. + * IN transfers in Slave mode require an explicit disable to
  43901. + * halt the channel. (In DMA mode, this call simply releases
  43902. + * the channel.)
  43903. + */
  43904. + halt_channel(hcd, hc, qtd, halt_status);
  43905. + } else {
  43906. + /*
  43907. + * The channel is automatically disabled by the core for OUT
  43908. + * transfers in Slave mode.
  43909. + */
  43910. + release_channel(hcd, hc, qtd, halt_status);
  43911. + }
  43912. +}
  43913. +
  43914. +/**
  43915. + * Performs common cleanup for periodic transfers after a Transfer Complete
  43916. + * interrupt. This function should be called after any endpoint type specific
  43917. + * handling is finished to release the host channel.
  43918. + */
  43919. +static void complete_periodic_xfer(dwc_otg_hcd_t * hcd,
  43920. + dwc_hc_t * hc,
  43921. + dwc_otg_hc_regs_t * hc_regs,
  43922. + dwc_otg_qtd_t * qtd,
  43923. + dwc_otg_halt_status_e halt_status)
  43924. +{
  43925. + hctsiz_data_t hctsiz;
  43926. + qtd->error_count = 0;
  43927. +
  43928. + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
  43929. + if (!hc->ep_is_in || hctsiz.b.pktcnt == 0) {
  43930. + /* Core halts channel in these cases. */
  43931. + release_channel(hcd, hc, qtd, halt_status);
  43932. + } else {
  43933. + /* Flush any outstanding requests from the Tx queue. */
  43934. + halt_channel(hcd, hc, qtd, halt_status);
  43935. + }
  43936. +}
  43937. +
  43938. +static int32_t handle_xfercomp_isoc_split_in(dwc_otg_hcd_t * hcd,
  43939. + dwc_hc_t * hc,
  43940. + dwc_otg_hc_regs_t * hc_regs,
  43941. + dwc_otg_qtd_t * qtd)
  43942. +{
  43943. + uint32_t len;
  43944. + struct dwc_otg_hcd_iso_packet_desc *frame_desc;
  43945. + frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
  43946. +
  43947. + len = get_actual_xfer_length(hc, hc_regs, qtd,
  43948. + DWC_OTG_HC_XFER_COMPLETE, NULL);
  43949. +
  43950. + if (!len) {
  43951. + qtd->complete_split = 0;
  43952. + qtd->isoc_split_offset = 0;
  43953. + return 0;
  43954. + }
  43955. + frame_desc->actual_length += len;
  43956. +
  43957. + if (hc->align_buff && len)
  43958. + dwc_memcpy(qtd->urb->buf + frame_desc->offset +
  43959. + qtd->isoc_split_offset, hc->qh->dw_align_buf, len);
  43960. + qtd->isoc_split_offset += len;
  43961. +
  43962. + if (frame_desc->length == frame_desc->actual_length) {
  43963. + frame_desc->status = 0;
  43964. + qtd->isoc_frame_index++;
  43965. + qtd->complete_split = 0;
  43966. + qtd->isoc_split_offset = 0;
  43967. + }
  43968. +
  43969. + if (qtd->isoc_frame_index == qtd->urb->packet_count) {
  43970. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
  43971. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
  43972. + } else {
  43973. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
  43974. + }
  43975. +
  43976. + return 1; /* Indicates that channel released */
  43977. +}
  43978. +
  43979. +/**
  43980. + * Handles a host channel Transfer Complete interrupt. This handler may be
  43981. + * called in either DMA mode or Slave mode.
  43982. + */
  43983. +static int32_t handle_hc_xfercomp_intr(dwc_otg_hcd_t * hcd,
  43984. + dwc_hc_t * hc,
  43985. + dwc_otg_hc_regs_t * hc_regs,
  43986. + dwc_otg_qtd_t * qtd)
  43987. +{
  43988. + int urb_xfer_done;
  43989. + dwc_otg_halt_status_e halt_status = DWC_OTG_HC_XFER_COMPLETE;
  43990. + dwc_otg_hcd_urb_t *urb = qtd->urb;
  43991. + int pipe_type = dwc_otg_hcd_get_pipe_type(&urb->pipe_info);
  43992. +
  43993. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  43994. + "Transfer Complete--\n", hc->hc_num);
  43995. +
  43996. + if (hcd->core_if->dma_desc_enable) {
  43997. + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs, halt_status);
  43998. + if (pipe_type == UE_ISOCHRONOUS) {
  43999. + /* Do not disable the interrupt, just clear it */
  44000. + clear_hc_int(hc_regs, xfercomp);
  44001. + return 1;
  44002. + }
  44003. + goto handle_xfercomp_done;
  44004. + }
  44005. +
  44006. + /*
  44007. + * Handle xfer complete on CSPLIT.
  44008. + */
  44009. +
  44010. + if (hc->qh->do_split) {
  44011. + if ((hc->ep_type == DWC_OTG_EP_TYPE_ISOC) && hc->ep_is_in
  44012. + && hcd->core_if->dma_enable) {
  44013. + if (qtd->complete_split
  44014. + && handle_xfercomp_isoc_split_in(hcd, hc, hc_regs,
  44015. + qtd))
  44016. + goto handle_xfercomp_done;
  44017. + } else {
  44018. + qtd->complete_split = 0;
  44019. + }
  44020. + }
  44021. +
  44022. + /* Update the QTD and URB states. */
  44023. + switch (pipe_type) {
  44024. + case UE_CONTROL:
  44025. + switch (qtd->control_phase) {
  44026. + case DWC_OTG_CONTROL_SETUP:
  44027. + if (urb->length > 0) {
  44028. + qtd->control_phase = DWC_OTG_CONTROL_DATA;
  44029. + } else {
  44030. + qtd->control_phase = DWC_OTG_CONTROL_STATUS;
  44031. + }
  44032. + DWC_DEBUGPL(DBG_HCDV,
  44033. + " Control setup transaction done\n");
  44034. + halt_status = DWC_OTG_HC_XFER_COMPLETE;
  44035. + break;
  44036. + case DWC_OTG_CONTROL_DATA:{
  44037. + urb_xfer_done =
  44038. + update_urb_state_xfer_comp(hc, hc_regs, urb,
  44039. + qtd);
  44040. + if (urb_xfer_done) {
  44041. + qtd->control_phase =
  44042. + DWC_OTG_CONTROL_STATUS;
  44043. + DWC_DEBUGPL(DBG_HCDV,
  44044. + " Control data transfer done\n");
  44045. + } else {
  44046. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  44047. + }
  44048. + halt_status = DWC_OTG_HC_XFER_COMPLETE;
  44049. + break;
  44050. + }
  44051. + case DWC_OTG_CONTROL_STATUS:
  44052. + DWC_DEBUGPL(DBG_HCDV, " Control transfer complete\n");
  44053. + if (urb->status == -DWC_E_IN_PROGRESS) {
  44054. + urb->status = 0;
  44055. + }
  44056. + hcd->fops->complete(hcd, urb->priv, urb, urb->status);
  44057. + halt_status = DWC_OTG_HC_XFER_URB_COMPLETE;
  44058. + break;
  44059. + }
  44060. +
  44061. + complete_non_periodic_xfer(hcd, hc, hc_regs, qtd, halt_status);
  44062. + break;
  44063. + case UE_BULK:
  44064. + DWC_DEBUGPL(DBG_HCDV, " Bulk transfer complete\n");
  44065. + urb_xfer_done =
  44066. + update_urb_state_xfer_comp(hc, hc_regs, urb, qtd);
  44067. + if (urb_xfer_done) {
  44068. + hcd->fops->complete(hcd, urb->priv, urb, urb->status);
  44069. + halt_status = DWC_OTG_HC_XFER_URB_COMPLETE;
  44070. + } else {
  44071. + halt_status = DWC_OTG_HC_XFER_COMPLETE;
  44072. + }
  44073. +
  44074. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  44075. + complete_non_periodic_xfer(hcd, hc, hc_regs, qtd, halt_status);
  44076. + break;
  44077. + case UE_INTERRUPT:
  44078. + DWC_DEBUGPL(DBG_HCDV, " Interrupt transfer complete\n");
  44079. + urb_xfer_done =
  44080. + update_urb_state_xfer_comp(hc, hc_regs, urb, qtd);
  44081. +
  44082. + /*
  44083. + * Interrupt URB is done on the first transfer complete
  44084. + * interrupt.
  44085. + */
  44086. + if (urb_xfer_done) {
  44087. + hcd->fops->complete(hcd, urb->priv, urb, urb->status);
  44088. + halt_status = DWC_OTG_HC_XFER_URB_COMPLETE;
  44089. + } else {
  44090. + halt_status = DWC_OTG_HC_XFER_COMPLETE;
  44091. + }
  44092. +
  44093. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  44094. + complete_periodic_xfer(hcd, hc, hc_regs, qtd, halt_status);
  44095. + break;
  44096. + case UE_ISOCHRONOUS:
  44097. + DWC_DEBUGPL(DBG_HCDV, " Isochronous transfer complete\n");
  44098. + if (qtd->isoc_split_pos == DWC_HCSPLIT_XACTPOS_ALL) {
  44099. + halt_status =
  44100. + update_isoc_urb_state(hcd, hc, hc_regs, qtd,
  44101. + DWC_OTG_HC_XFER_COMPLETE);
  44102. + }
  44103. + complete_periodic_xfer(hcd, hc, hc_regs, qtd, halt_status);
  44104. + break;
  44105. + }
  44106. +
  44107. +handle_xfercomp_done:
  44108. + disable_hc_int(hc_regs, xfercompl);
  44109. +
  44110. + return 1;
  44111. +}
  44112. +
  44113. +/**
  44114. + * Handles a host channel STALL interrupt. This handler may be called in
  44115. + * either DMA mode or Slave mode.
  44116. + */
  44117. +static int32_t handle_hc_stall_intr(dwc_otg_hcd_t * hcd,
  44118. + dwc_hc_t * hc,
  44119. + dwc_otg_hc_regs_t * hc_regs,
  44120. + dwc_otg_qtd_t * qtd)
  44121. +{
  44122. + dwc_otg_hcd_urb_t *urb = qtd->urb;
  44123. + int pipe_type = dwc_otg_hcd_get_pipe_type(&urb->pipe_info);
  44124. +
  44125. + DWC_DEBUGPL(DBG_HCD, "--Host Channel %d Interrupt: "
  44126. + "STALL Received--\n", hc->hc_num);
  44127. +
  44128. + if (hcd->core_if->dma_desc_enable) {
  44129. + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs, DWC_OTG_HC_XFER_STALL);
  44130. + goto handle_stall_done;
  44131. + }
  44132. +
  44133. + if (pipe_type == UE_CONTROL) {
  44134. + hcd->fops->complete(hcd, urb->priv, urb, -DWC_E_PIPE);
  44135. + }
  44136. +
  44137. + if (pipe_type == UE_BULK || pipe_type == UE_INTERRUPT) {
  44138. + hcd->fops->complete(hcd, urb->priv, urb, -DWC_E_PIPE);
  44139. + /*
  44140. + * USB protocol requires resetting the data toggle for bulk
  44141. + * and interrupt endpoints when a CLEAR_FEATURE(ENDPOINT_HALT)
  44142. + * setup command is issued to the endpoint. Anticipate the
  44143. + * CLEAR_FEATURE command since a STALL has occurred and reset
  44144. + * the data toggle now.
  44145. + */
  44146. + hc->qh->data_toggle = 0;
  44147. + }
  44148. +
  44149. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_STALL);
  44150. +
  44151. +handle_stall_done:
  44152. + disable_hc_int(hc_regs, stall);
  44153. +
  44154. + return 1;
  44155. +}
  44156. +
  44157. +/*
  44158. + * Updates the state of the URB when a transfer has been stopped due to an
  44159. + * abnormal condition before the transfer completes. Modifies the
  44160. + * actual_length field of the URB to reflect the number of bytes that have
  44161. + * actually been transferred via the host channel.
  44162. + */
  44163. +static void update_urb_state_xfer_intr(dwc_hc_t * hc,
  44164. + dwc_otg_hc_regs_t * hc_regs,
  44165. + dwc_otg_hcd_urb_t * urb,
  44166. + dwc_otg_qtd_t * qtd,
  44167. + dwc_otg_halt_status_e halt_status)
  44168. +{
  44169. + uint32_t bytes_transferred = get_actual_xfer_length(hc, hc_regs, qtd,
  44170. + halt_status, NULL);
  44171. +
  44172. + if (urb->actual_length + bytes_transferred > urb->length) {
  44173. + printk_once(KERN_DEBUG "dwc_otg: DEVICE:%03d : %s:%d:trimming xfer length\n",
  44174. + hc->dev_addr, __func__, __LINE__);
  44175. + bytes_transferred = urb->length - urb->actual_length;
  44176. + }
  44177. +
  44178. + /* non DWORD-aligned buffer case handling. */
  44179. + if (hc->align_buff && bytes_transferred && hc->ep_is_in) {
  44180. + dwc_memcpy(urb->buf + urb->actual_length, hc->qh->dw_align_buf,
  44181. + bytes_transferred);
  44182. + }
  44183. +
  44184. + urb->actual_length += bytes_transferred;
  44185. +
  44186. +#ifdef DEBUG
  44187. + {
  44188. + hctsiz_data_t hctsiz;
  44189. + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
  44190. + DWC_DEBUGPL(DBG_HCDV, "DWC_otg: %s: %s, channel %d\n",
  44191. + __func__, (hc->ep_is_in ? "IN" : "OUT"),
  44192. + hc->hc_num);
  44193. + DWC_DEBUGPL(DBG_HCDV, " hc->start_pkt_count %d\n",
  44194. + hc->start_pkt_count);
  44195. + DWC_DEBUGPL(DBG_HCDV, " hctsiz.pktcnt %d\n", hctsiz.b.pktcnt);
  44196. + DWC_DEBUGPL(DBG_HCDV, " hc->max_packet %d\n", hc->max_packet);
  44197. + DWC_DEBUGPL(DBG_HCDV, " bytes_transferred %d\n",
  44198. + bytes_transferred);
  44199. + DWC_DEBUGPL(DBG_HCDV, " urb->actual_length %d\n",
  44200. + urb->actual_length);
  44201. + DWC_DEBUGPL(DBG_HCDV, " urb->transfer_buffer_length %d\n",
  44202. + urb->length);
  44203. + }
  44204. +#endif
  44205. +}
  44206. +
  44207. +/**
  44208. + * Handles a host channel NAK interrupt. This handler may be called in either
  44209. + * DMA mode or Slave mode.
  44210. + */
  44211. +static int32_t handle_hc_nak_intr(dwc_otg_hcd_t * hcd,
  44212. + dwc_hc_t * hc,
  44213. + dwc_otg_hc_regs_t * hc_regs,
  44214. + dwc_otg_qtd_t * qtd)
  44215. +{
  44216. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  44217. + "NAK Received--\n", hc->hc_num);
  44218. +
  44219. + /*
  44220. + * When we get bulk NAKs then remember this so we holdoff on this qh until
  44221. + * the beginning of the next frame
  44222. + */
  44223. + switch(dwc_otg_hcd_get_pipe_type(&qtd->urb->pipe_info)) {
  44224. + case UE_BULK:
  44225. + case UE_CONTROL:
  44226. + if (nak_holdoff && qtd->qh->do_split)
  44227. + hc->qh->nak_frame = dwc_otg_hcd_get_frame_number(hcd);
  44228. + }
  44229. +
  44230. + /*
  44231. + * Handle NAK for IN/OUT SSPLIT/CSPLIT transfers, bulk, control, and
  44232. + * interrupt. Re-start the SSPLIT transfer.
  44233. + */
  44234. + if (hc->do_split) {
  44235. + if (hc->complete_split) {
  44236. + qtd->error_count = 0;
  44237. + }
  44238. + qtd->complete_split = 0;
  44239. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NAK);
  44240. + goto handle_nak_done;
  44241. + }
  44242. +
  44243. + switch (dwc_otg_hcd_get_pipe_type(&qtd->urb->pipe_info)) {
  44244. + case UE_CONTROL:
  44245. + case UE_BULK:
  44246. + if (hcd->core_if->dma_enable && hc->ep_is_in) {
  44247. + /*
  44248. + * NAK interrupts are enabled on bulk/control IN
  44249. + * transfers in DMA mode for the sole purpose of
  44250. + * resetting the error count after a transaction error
  44251. + * occurs. The core will continue transferring data.
  44252. + * Disable other interrupts unmasked for the same
  44253. + * reason.
  44254. + */
  44255. + disable_hc_int(hc_regs, datatglerr);
  44256. + disable_hc_int(hc_regs, ack);
  44257. + qtd->error_count = 0;
  44258. + goto handle_nak_done;
  44259. + }
  44260. +
  44261. + /*
  44262. + * NAK interrupts normally occur during OUT transfers in DMA
  44263. + * or Slave mode. For IN transfers, more requests will be
  44264. + * queued as request queue space is available.
  44265. + */
  44266. + qtd->error_count = 0;
  44267. +
  44268. + if (!hc->qh->ping_state) {
  44269. + update_urb_state_xfer_intr(hc, hc_regs,
  44270. + qtd->urb, qtd,
  44271. + DWC_OTG_HC_XFER_NAK);
  44272. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  44273. +
  44274. + if (hc->speed == DWC_OTG_EP_SPEED_HIGH)
  44275. + hc->qh->ping_state = 1;
  44276. + }
  44277. +
  44278. + /*
  44279. + * Halt the channel so the transfer can be re-started from
  44280. + * the appropriate point or the PING protocol will
  44281. + * start/continue.
  44282. + */
  44283. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NAK);
  44284. + break;
  44285. + case UE_INTERRUPT:
  44286. + qtd->error_count = 0;
  44287. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NAK);
  44288. + break;
  44289. + case UE_ISOCHRONOUS:
  44290. + /* Should never get called for isochronous transfers. */
  44291. + DWC_ASSERT(1, "NACK interrupt for ISOC transfer\n");
  44292. + break;
  44293. + }
  44294. +
  44295. +handle_nak_done:
  44296. + disable_hc_int(hc_regs, nak);
  44297. +
  44298. + return 1;
  44299. +}
  44300. +
  44301. +/**
  44302. + * Handles a host channel ACK interrupt. This interrupt is enabled when
  44303. + * performing the PING protocol in Slave mode, when errors occur during
  44304. + * either Slave mode or DMA mode, and during Start Split transactions.
  44305. + */
  44306. +static int32_t handle_hc_ack_intr(dwc_otg_hcd_t * hcd,
  44307. + dwc_hc_t * hc,
  44308. + dwc_otg_hc_regs_t * hc_regs,
  44309. + dwc_otg_qtd_t * qtd)
  44310. +{
  44311. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  44312. + "ACK Received--\n", hc->hc_num);
  44313. +
  44314. + if (hc->do_split) {
  44315. + /*
  44316. + * Handle ACK on SSPLIT.
  44317. + * ACK should not occur in CSPLIT.
  44318. + */
  44319. + if (!hc->ep_is_in && hc->data_pid_start != DWC_OTG_HC_PID_SETUP) {
  44320. + qtd->ssplit_out_xfer_count = hc->xfer_len;
  44321. + }
  44322. + if (!(hc->ep_type == DWC_OTG_EP_TYPE_ISOC && !hc->ep_is_in)) {
  44323. + /* Don't need complete for isochronous out transfers. */
  44324. + qtd->complete_split = 1;
  44325. + }
  44326. +
  44327. + /* ISOC OUT */
  44328. + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC && !hc->ep_is_in) {
  44329. + switch (hc->xact_pos) {
  44330. + case DWC_HCSPLIT_XACTPOS_ALL:
  44331. + break;
  44332. + case DWC_HCSPLIT_XACTPOS_END:
  44333. + qtd->isoc_split_pos = DWC_HCSPLIT_XACTPOS_ALL;
  44334. + qtd->isoc_split_offset = 0;
  44335. + break;
  44336. + case DWC_HCSPLIT_XACTPOS_BEGIN:
  44337. + case DWC_HCSPLIT_XACTPOS_MID:
  44338. + /*
  44339. + * For BEGIN or MID, calculate the length for
  44340. + * the next microframe to determine the correct
  44341. + * SSPLIT token, either MID or END.
  44342. + */
  44343. + {
  44344. + struct dwc_otg_hcd_iso_packet_desc
  44345. + *frame_desc;
  44346. +
  44347. + frame_desc =
  44348. + &qtd->urb->
  44349. + iso_descs[qtd->isoc_frame_index];
  44350. + qtd->isoc_split_offset += 188;
  44351. +
  44352. + if ((frame_desc->length -
  44353. + qtd->isoc_split_offset) <= 188) {
  44354. + qtd->isoc_split_pos =
  44355. + DWC_HCSPLIT_XACTPOS_END;
  44356. + } else {
  44357. + qtd->isoc_split_pos =
  44358. + DWC_HCSPLIT_XACTPOS_MID;
  44359. + }
  44360. +
  44361. + }
  44362. + break;
  44363. + }
  44364. + } else {
  44365. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_ACK);
  44366. + }
  44367. + } else {
  44368. + /*
  44369. + * An unmasked ACK on a non-split DMA transaction is
  44370. + * for the sole purpose of resetting error counts. Disable other
  44371. + * interrupts unmasked for the same reason.
  44372. + */
  44373. + if(hcd->core_if->dma_enable) {
  44374. + disable_hc_int(hc_regs, datatglerr);
  44375. + disable_hc_int(hc_regs, nak);
  44376. + }
  44377. + qtd->error_count = 0;
  44378. +
  44379. + if (hc->qh->ping_state) {
  44380. + hc->qh->ping_state = 0;
  44381. + /*
  44382. + * Halt the channel so the transfer can be re-started
  44383. + * from the appropriate point. This only happens in
  44384. + * Slave mode. In DMA mode, the ping_state is cleared
  44385. + * when the transfer is started because the core
  44386. + * automatically executes the PING, then the transfer.
  44387. + */
  44388. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_ACK);
  44389. + }
  44390. + }
  44391. +
  44392. + /*
  44393. + * If the ACK occurred when _not_ in the PING state, let the channel
  44394. + * continue transferring data after clearing the error count.
  44395. + */
  44396. +
  44397. + disable_hc_int(hc_regs, ack);
  44398. +
  44399. + return 1;
  44400. +}
  44401. +
  44402. +/**
  44403. + * Handles a host channel NYET interrupt. This interrupt should only occur on
  44404. + * Bulk and Control OUT endpoints and for complete split transactions. If a
  44405. + * NYET occurs at the same time as a Transfer Complete interrupt, it is
  44406. + * handled in the xfercomp interrupt handler, not here. This handler may be
  44407. + * called in either DMA mode or Slave mode.
  44408. + */
  44409. +static int32_t handle_hc_nyet_intr(dwc_otg_hcd_t * hcd,
  44410. + dwc_hc_t * hc,
  44411. + dwc_otg_hc_regs_t * hc_regs,
  44412. + dwc_otg_qtd_t * qtd)
  44413. +{
  44414. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  44415. + "NYET Received--\n", hc->hc_num);
  44416. +
  44417. + /*
  44418. + * NYET on CSPLIT
  44419. + * re-do the CSPLIT immediately on non-periodic
  44420. + */
  44421. + if (hc->do_split && hc->complete_split) {
  44422. + if (hc->ep_is_in && (hc->ep_type == DWC_OTG_EP_TYPE_ISOC)
  44423. + && hcd->core_if->dma_enable) {
  44424. + qtd->complete_split = 0;
  44425. + qtd->isoc_split_offset = 0;
  44426. + if (++qtd->isoc_frame_index == qtd->urb->packet_count) {
  44427. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
  44428. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
  44429. + }
  44430. + else
  44431. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
  44432. + goto handle_nyet_done;
  44433. + }
  44434. +
  44435. + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
  44436. + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
  44437. + int frnum = dwc_otg_hcd_get_frame_number(hcd);
  44438. +
  44439. + // With the FIQ running we only ever see the failed NYET
  44440. + if (dwc_full_frame_num(frnum) !=
  44441. + dwc_full_frame_num(hc->qh->sched_frame) ||
  44442. + fiq_fsm_enable) {
  44443. + /*
  44444. + * No longer in the same full speed frame.
  44445. + * Treat this as a transaction error.
  44446. + */
  44447. +#if 0
  44448. + /** @todo Fix system performance so this can
  44449. + * be treated as an error. Right now complete
  44450. + * splits cannot be scheduled precisely enough
  44451. + * due to other system activity, so this error
  44452. + * occurs regularly in Slave mode.
  44453. + */
  44454. + qtd->error_count++;
  44455. +#endif
  44456. + qtd->complete_split = 0;
  44457. + halt_channel(hcd, hc, qtd,
  44458. + DWC_OTG_HC_XFER_XACT_ERR);
  44459. + /** @todo add support for isoc release */
  44460. + goto handle_nyet_done;
  44461. + }
  44462. + }
  44463. +
  44464. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NYET);
  44465. + goto handle_nyet_done;
  44466. + }
  44467. +
  44468. + hc->qh->ping_state = 1;
  44469. + qtd->error_count = 0;
  44470. +
  44471. + update_urb_state_xfer_intr(hc, hc_regs, qtd->urb, qtd,
  44472. + DWC_OTG_HC_XFER_NYET);
  44473. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  44474. +
  44475. + /*
  44476. + * Halt the channel and re-start the transfer so the PING
  44477. + * protocol will start.
  44478. + */
  44479. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NYET);
  44480. +
  44481. +handle_nyet_done:
  44482. + disable_hc_int(hc_regs, nyet);
  44483. + return 1;
  44484. +}
  44485. +
  44486. +/**
  44487. + * Handles a host channel babble interrupt. This handler may be called in
  44488. + * either DMA mode or Slave mode.
  44489. + */
  44490. +static int32_t handle_hc_babble_intr(dwc_otg_hcd_t * hcd,
  44491. + dwc_hc_t * hc,
  44492. + dwc_otg_hc_regs_t * hc_regs,
  44493. + dwc_otg_qtd_t * qtd)
  44494. +{
  44495. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  44496. + "Babble Error--\n", hc->hc_num);
  44497. +
  44498. + if (hcd->core_if->dma_desc_enable) {
  44499. + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs,
  44500. + DWC_OTG_HC_XFER_BABBLE_ERR);
  44501. + goto handle_babble_done;
  44502. + }
  44503. +
  44504. + if (hc->ep_type != DWC_OTG_EP_TYPE_ISOC) {
  44505. + hcd->fops->complete(hcd, qtd->urb->priv,
  44506. + qtd->urb, -DWC_E_OVERFLOW);
  44507. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_BABBLE_ERR);
  44508. + } else {
  44509. + dwc_otg_halt_status_e halt_status;
  44510. + halt_status = update_isoc_urb_state(hcd, hc, hc_regs, qtd,
  44511. + DWC_OTG_HC_XFER_BABBLE_ERR);
  44512. + halt_channel(hcd, hc, qtd, halt_status);
  44513. + }
  44514. +
  44515. +handle_babble_done:
  44516. + disable_hc_int(hc_regs, bblerr);
  44517. + return 1;
  44518. +}
  44519. +
  44520. +/**
  44521. + * Handles a host channel AHB error interrupt. This handler is only called in
  44522. + * DMA mode.
  44523. + */
  44524. +static int32_t handle_hc_ahberr_intr(dwc_otg_hcd_t * hcd,
  44525. + dwc_hc_t * hc,
  44526. + dwc_otg_hc_regs_t * hc_regs,
  44527. + dwc_otg_qtd_t * qtd)
  44528. +{
  44529. + hcchar_data_t hcchar;
  44530. + hcsplt_data_t hcsplt;
  44531. + hctsiz_data_t hctsiz;
  44532. + uint32_t hcdma;
  44533. + char *pipetype, *speed;
  44534. +
  44535. + dwc_otg_hcd_urb_t *urb = qtd->urb;
  44536. +
  44537. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  44538. + "AHB Error--\n", hc->hc_num);
  44539. +
  44540. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  44541. + hcsplt.d32 = DWC_READ_REG32(&hc_regs->hcsplt);
  44542. + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
  44543. + hcdma = DWC_READ_REG32(&hc_regs->hcdma);
  44544. +
  44545. + DWC_ERROR("AHB ERROR, Channel %d\n", hc->hc_num);
  44546. + DWC_ERROR(" hcchar 0x%08x, hcsplt 0x%08x\n", hcchar.d32, hcsplt.d32);
  44547. + DWC_ERROR(" hctsiz 0x%08x, hcdma 0x%08x\n", hctsiz.d32, hcdma);
  44548. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD URB Enqueue\n");
  44549. + DWC_ERROR(" Device address: %d\n",
  44550. + dwc_otg_hcd_get_dev_addr(&urb->pipe_info));
  44551. + DWC_ERROR(" Endpoint: %d, %s\n",
  44552. + dwc_otg_hcd_get_ep_num(&urb->pipe_info),
  44553. + (dwc_otg_hcd_is_pipe_in(&urb->pipe_info) ? "IN" : "OUT"));
  44554. +
  44555. + switch (dwc_otg_hcd_get_pipe_type(&urb->pipe_info)) {
  44556. + case UE_CONTROL:
  44557. + pipetype = "CONTROL";
  44558. + break;
  44559. + case UE_BULK:
  44560. + pipetype = "BULK";
  44561. + break;
  44562. + case UE_INTERRUPT:
  44563. + pipetype = "INTERRUPT";
  44564. + break;
  44565. + case UE_ISOCHRONOUS:
  44566. + pipetype = "ISOCHRONOUS";
  44567. + break;
  44568. + default:
  44569. + pipetype = "UNKNOWN";
  44570. + break;
  44571. + }
  44572. +
  44573. + DWC_ERROR(" Endpoint type: %s\n", pipetype);
  44574. +
  44575. + switch (hc->speed) {
  44576. + case DWC_OTG_EP_SPEED_HIGH:
  44577. + speed = "HIGH";
  44578. + break;
  44579. + case DWC_OTG_EP_SPEED_FULL:
  44580. + speed = "FULL";
  44581. + break;
  44582. + case DWC_OTG_EP_SPEED_LOW:
  44583. + speed = "LOW";
  44584. + break;
  44585. + default:
  44586. + speed = "UNKNOWN";
  44587. + break;
  44588. + };
  44589. +
  44590. + DWC_ERROR(" Speed: %s\n", speed);
  44591. +
  44592. + DWC_ERROR(" Max packet size: %d\n",
  44593. + dwc_otg_hcd_get_mps(&urb->pipe_info));
  44594. + DWC_ERROR(" Data buffer length: %d\n", urb->length);
  44595. + DWC_ERROR(" Transfer buffer: %p, Transfer DMA: %p\n",
  44596. + urb->buf, (void *)urb->dma);
  44597. + DWC_ERROR(" Setup buffer: %p, Setup DMA: %p\n",
  44598. + urb->setup_packet, (void *)urb->setup_dma);
  44599. + DWC_ERROR(" Interval: %d\n", urb->interval);
  44600. +
  44601. + /* Core haltes the channel for Descriptor DMA mode */
  44602. + if (hcd->core_if->dma_desc_enable) {
  44603. + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs,
  44604. + DWC_OTG_HC_XFER_AHB_ERR);
  44605. + goto handle_ahberr_done;
  44606. + }
  44607. +
  44608. + hcd->fops->complete(hcd, urb->priv, urb, -DWC_E_IO);
  44609. +
  44610. + /*
  44611. + * Force a channel halt. Don't call halt_channel because that won't
  44612. + * write to the HCCHARn register in DMA mode to force the halt.
  44613. + */
  44614. + dwc_otg_hc_halt(hcd->core_if, hc, DWC_OTG_HC_XFER_AHB_ERR);
  44615. +handle_ahberr_done:
  44616. + disable_hc_int(hc_regs, ahberr);
  44617. + return 1;
  44618. +}
  44619. +
  44620. +/**
  44621. + * Handles a host channel transaction error interrupt. This handler may be
  44622. + * called in either DMA mode or Slave mode.
  44623. + */
  44624. +static int32_t handle_hc_xacterr_intr(dwc_otg_hcd_t * hcd,
  44625. + dwc_hc_t * hc,
  44626. + dwc_otg_hc_regs_t * hc_regs,
  44627. + dwc_otg_qtd_t * qtd)
  44628. +{
  44629. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  44630. + "Transaction Error--\n", hc->hc_num);
  44631. +
  44632. + if (hcd->core_if->dma_desc_enable) {
  44633. + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs,
  44634. + DWC_OTG_HC_XFER_XACT_ERR);
  44635. + goto handle_xacterr_done;
  44636. + }
  44637. +
  44638. + switch (dwc_otg_hcd_get_pipe_type(&qtd->urb->pipe_info)) {
  44639. + case UE_CONTROL:
  44640. + case UE_BULK:
  44641. + qtd->error_count++;
  44642. + if (!hc->qh->ping_state) {
  44643. +
  44644. + update_urb_state_xfer_intr(hc, hc_regs,
  44645. + qtd->urb, qtd,
  44646. + DWC_OTG_HC_XFER_XACT_ERR);
  44647. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  44648. + if (!hc->ep_is_in && hc->speed == DWC_OTG_EP_SPEED_HIGH) {
  44649. + hc->qh->ping_state = 1;
  44650. + }
  44651. + }
  44652. +
  44653. + /*
  44654. + * Halt the channel so the transfer can be re-started from
  44655. + * the appropriate point or the PING protocol will start.
  44656. + */
  44657. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_XACT_ERR);
  44658. + break;
  44659. + case UE_INTERRUPT:
  44660. + qtd->error_count++;
  44661. + if (hc->do_split && hc->complete_split) {
  44662. + qtd->complete_split = 0;
  44663. + }
  44664. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_XACT_ERR);
  44665. + break;
  44666. + case UE_ISOCHRONOUS:
  44667. + {
  44668. + dwc_otg_halt_status_e halt_status;
  44669. + halt_status =
  44670. + update_isoc_urb_state(hcd, hc, hc_regs, qtd,
  44671. + DWC_OTG_HC_XFER_XACT_ERR);
  44672. +
  44673. + halt_channel(hcd, hc, qtd, halt_status);
  44674. + }
  44675. + break;
  44676. + }
  44677. +handle_xacterr_done:
  44678. + disable_hc_int(hc_regs, xacterr);
  44679. +
  44680. + return 1;
  44681. +}
  44682. +
  44683. +/**
  44684. + * Handles a host channel frame overrun interrupt. This handler may be called
  44685. + * in either DMA mode or Slave mode.
  44686. + */
  44687. +static int32_t handle_hc_frmovrun_intr(dwc_otg_hcd_t * hcd,
  44688. + dwc_hc_t * hc,
  44689. + dwc_otg_hc_regs_t * hc_regs,
  44690. + dwc_otg_qtd_t * qtd)
  44691. +{
  44692. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  44693. + "Frame Overrun--\n", hc->hc_num);
  44694. +
  44695. + switch (dwc_otg_hcd_get_pipe_type(&qtd->urb->pipe_info)) {
  44696. + case UE_CONTROL:
  44697. + case UE_BULK:
  44698. + break;
  44699. + case UE_INTERRUPT:
  44700. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_FRAME_OVERRUN);
  44701. + break;
  44702. + case UE_ISOCHRONOUS:
  44703. + {
  44704. + dwc_otg_halt_status_e halt_status;
  44705. + halt_status =
  44706. + update_isoc_urb_state(hcd, hc, hc_regs, qtd,
  44707. + DWC_OTG_HC_XFER_FRAME_OVERRUN);
  44708. +
  44709. + halt_channel(hcd, hc, qtd, halt_status);
  44710. + }
  44711. + break;
  44712. + }
  44713. +
  44714. + disable_hc_int(hc_regs, frmovrun);
  44715. +
  44716. + return 1;
  44717. +}
  44718. +
  44719. +/**
  44720. + * Handles a host channel data toggle error interrupt. This handler may be
  44721. + * called in either DMA mode or Slave mode.
  44722. + */
  44723. +static int32_t handle_hc_datatglerr_intr(dwc_otg_hcd_t * hcd,
  44724. + dwc_hc_t * hc,
  44725. + dwc_otg_hc_regs_t * hc_regs,
  44726. + dwc_otg_qtd_t * qtd)
  44727. +{
  44728. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  44729. + "Data Toggle Error on %s transfer--\n",
  44730. + hc->hc_num, (hc->ep_is_in ? "IN" : "OUT"));
  44731. +
  44732. + /* Data toggles on split transactions cause the hc to halt.
  44733. + * restart transfer */
  44734. + if(hc->qh->do_split)
  44735. + {
  44736. + qtd->error_count++;
  44737. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  44738. + update_urb_state_xfer_intr(hc, hc_regs,
  44739. + qtd->urb, qtd, DWC_OTG_HC_XFER_XACT_ERR);
  44740. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_XACT_ERR);
  44741. + } else if (hc->ep_is_in) {
  44742. + /* An unmasked data toggle error on a non-split DMA transaction is
  44743. + * for the sole purpose of resetting error counts. Disable other
  44744. + * interrupts unmasked for the same reason.
  44745. + */
  44746. + if(hcd->core_if->dma_enable) {
  44747. + disable_hc_int(hc_regs, ack);
  44748. + disable_hc_int(hc_regs, nak);
  44749. + }
  44750. + qtd->error_count = 0;
  44751. + }
  44752. +
  44753. + disable_hc_int(hc_regs, datatglerr);
  44754. +
  44755. + return 1;
  44756. +}
  44757. +
  44758. +#ifdef DEBUG
  44759. +/**
  44760. + * This function is for debug only. It checks that a valid halt status is set
  44761. + * and that HCCHARn.chdis is clear. If there's a problem, corrective action is
  44762. + * taken and a warning is issued.
  44763. + * @return 1 if halt status is ok, 0 otherwise.
  44764. + */
  44765. +static inline int halt_status_ok(dwc_otg_hcd_t * hcd,
  44766. + dwc_hc_t * hc,
  44767. + dwc_otg_hc_regs_t * hc_regs,
  44768. + dwc_otg_qtd_t * qtd)
  44769. +{
  44770. + hcchar_data_t hcchar;
  44771. + hctsiz_data_t hctsiz;
  44772. + hcint_data_t hcint;
  44773. + hcintmsk_data_t hcintmsk;
  44774. + hcsplt_data_t hcsplt;
  44775. +
  44776. + if (hc->halt_status == DWC_OTG_HC_XFER_NO_HALT_STATUS) {
  44777. + /*
  44778. + * This code is here only as a check. This condition should
  44779. + * never happen. Ignore the halt if it does occur.
  44780. + */
  44781. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  44782. + hctsiz.d32 = DWC_READ_REG32(&hc_regs->hctsiz);
  44783. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  44784. + hcintmsk.d32 = DWC_READ_REG32(&hc_regs->hcintmsk);
  44785. + hcsplt.d32 = DWC_READ_REG32(&hc_regs->hcsplt);
  44786. + DWC_WARN
  44787. + ("%s: hc->halt_status == DWC_OTG_HC_XFER_NO_HALT_STATUS, "
  44788. + "channel %d, hcchar 0x%08x, hctsiz 0x%08x, "
  44789. + "hcint 0x%08x, hcintmsk 0x%08x, "
  44790. + "hcsplt 0x%08x, qtd->complete_split %d\n", __func__,
  44791. + hc->hc_num, hcchar.d32, hctsiz.d32, hcint.d32,
  44792. + hcintmsk.d32, hcsplt.d32, qtd->complete_split);
  44793. +
  44794. + DWC_WARN("%s: no halt status, channel %d, ignoring interrupt\n",
  44795. + __func__, hc->hc_num);
  44796. + DWC_WARN("\n");
  44797. + clear_hc_int(hc_regs, chhltd);
  44798. + return 0;
  44799. + }
  44800. +
  44801. + /*
  44802. + * This code is here only as a check. hcchar.chdis should
  44803. + * never be set when the halt interrupt occurs. Halt the
  44804. + * channel again if it does occur.
  44805. + */
  44806. + hcchar.d32 = DWC_READ_REG32(&hc_regs->hcchar);
  44807. + if (hcchar.b.chdis) {
  44808. + DWC_WARN("%s: hcchar.chdis set unexpectedly, "
  44809. + "hcchar 0x%08x, trying to halt again\n",
  44810. + __func__, hcchar.d32);
  44811. + clear_hc_int(hc_regs, chhltd);
  44812. + hc->halt_pending = 0;
  44813. + halt_channel(hcd, hc, qtd, hc->halt_status);
  44814. + return 0;
  44815. + }
  44816. +
  44817. + return 1;
  44818. +}
  44819. +#endif
  44820. +
  44821. +/**
  44822. + * Handles a host Channel Halted interrupt in DMA mode. This handler
  44823. + * determines the reason the channel halted and proceeds accordingly.
  44824. + */
  44825. +static void handle_hc_chhltd_intr_dma(dwc_otg_hcd_t * hcd,
  44826. + dwc_hc_t * hc,
  44827. + dwc_otg_hc_regs_t * hc_regs,
  44828. + dwc_otg_qtd_t * qtd)
  44829. +{
  44830. + int out_nak_enh = 0;
  44831. + hcint_data_t hcint;
  44832. + hcintmsk_data_t hcintmsk;
  44833. + /* For core with OUT NAK enhancement, the flow for high-
  44834. + * speed CONTROL/BULK OUT is handled a little differently.
  44835. + */
  44836. + if (hcd->core_if->snpsid >= OTG_CORE_REV_2_71a) {
  44837. + if (hc->speed == DWC_OTG_EP_SPEED_HIGH && !hc->ep_is_in &&
  44838. + (hc->ep_type == DWC_OTG_EP_TYPE_CONTROL ||
  44839. + hc->ep_type == DWC_OTG_EP_TYPE_BULK)) {
  44840. + out_nak_enh = 1;
  44841. + }
  44842. + }
  44843. +
  44844. + if (hc->halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE ||
  44845. + (hc->halt_status == DWC_OTG_HC_XFER_AHB_ERR
  44846. + && !hcd->core_if->dma_desc_enable)) {
  44847. + /*
  44848. + * Just release the channel. A dequeue can happen on a
  44849. + * transfer timeout. In the case of an AHB Error, the channel
  44850. + * was forced to halt because there's no way to gracefully
  44851. + * recover.
  44852. + */
  44853. + if (hcd->core_if->dma_desc_enable)
  44854. + dwc_otg_hcd_complete_xfer_ddma(hcd, hc, hc_regs,
  44855. + hc->halt_status);
  44856. + else
  44857. + release_channel(hcd, hc, qtd, hc->halt_status);
  44858. + return;
  44859. + }
  44860. +
  44861. + /* Read the HCINTn register to determine the cause for the halt. */
  44862. +
  44863. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  44864. + hcintmsk.d32 = DWC_READ_REG32(&hc_regs->hcintmsk);
  44865. +
  44866. + if (hcint.b.xfercomp) {
  44867. + /** @todo This is here because of a possible hardware bug. Spec
  44868. + * says that on SPLIT-ISOC OUT transfers in DMA mode that a HALT
  44869. + * interrupt w/ACK bit set should occur, but I only see the
  44870. + * XFERCOMP bit, even with it masked out. This is a workaround
  44871. + * for that behavior. Should fix this when hardware is fixed.
  44872. + */
  44873. + if (hc->ep_type == DWC_OTG_EP_TYPE_ISOC && !hc->ep_is_in) {
  44874. + handle_hc_ack_intr(hcd, hc, hc_regs, qtd);
  44875. + }
  44876. + handle_hc_xfercomp_intr(hcd, hc, hc_regs, qtd);
  44877. + } else if (hcint.b.stall) {
  44878. + handle_hc_stall_intr(hcd, hc, hc_regs, qtd);
  44879. + } else if (hcint.b.xacterr && !hcd->core_if->dma_desc_enable) {
  44880. + if (out_nak_enh) {
  44881. + if (hcint.b.nyet || hcint.b.nak || hcint.b.ack) {
  44882. + DWC_DEBUGPL(DBG_HCD, "XactErr with NYET/NAK/ACK\n");
  44883. + qtd->error_count = 0;
  44884. + } else {
  44885. + DWC_DEBUGPL(DBG_HCD, "XactErr without NYET/NAK/ACK\n");
  44886. + }
  44887. + }
  44888. +
  44889. + /*
  44890. + * Must handle xacterr before nak or ack. Could get a xacterr
  44891. + * at the same time as either of these on a BULK/CONTROL OUT
  44892. + * that started with a PING. The xacterr takes precedence.
  44893. + */
  44894. + handle_hc_xacterr_intr(hcd, hc, hc_regs, qtd);
  44895. + } else if (hcint.b.xcs_xact && hcd->core_if->dma_desc_enable) {
  44896. + handle_hc_xacterr_intr(hcd, hc, hc_regs, qtd);
  44897. + } else if (hcint.b.ahberr && hcd->core_if->dma_desc_enable) {
  44898. + handle_hc_ahberr_intr(hcd, hc, hc_regs, qtd);
  44899. + } else if (hcint.b.bblerr) {
  44900. + handle_hc_babble_intr(hcd, hc, hc_regs, qtd);
  44901. + } else if (hcint.b.frmovrun) {
  44902. + handle_hc_frmovrun_intr(hcd, hc, hc_regs, qtd);
  44903. + } else if (hcint.b.datatglerr) {
  44904. + handle_hc_datatglerr_intr(hcd, hc, hc_regs, qtd);
  44905. + } else if (!out_nak_enh) {
  44906. + if (hcint.b.nyet) {
  44907. + /*
  44908. + * Must handle nyet before nak or ack. Could get a nyet at the
  44909. + * same time as either of those on a BULK/CONTROL OUT that
  44910. + * started with a PING. The nyet takes precedence.
  44911. + */
  44912. + handle_hc_nyet_intr(hcd, hc, hc_regs, qtd);
  44913. + } else if (hcint.b.nak && !hcintmsk.b.nak) {
  44914. + /*
  44915. + * If nak is not masked, it's because a non-split IN transfer
  44916. + * is in an error state. In that case, the nak is handled by
  44917. + * the nak interrupt handler, not here. Handle nak here for
  44918. + * BULK/CONTROL OUT transfers, which halt on a NAK to allow
  44919. + * rewinding the buffer pointer.
  44920. + */
  44921. + handle_hc_nak_intr(hcd, hc, hc_regs, qtd);
  44922. + } else if (hcint.b.ack && !hcintmsk.b.ack) {
  44923. + /*
  44924. + * If ack is not masked, it's because a non-split IN transfer
  44925. + * is in an error state. In that case, the ack is handled by
  44926. + * the ack interrupt handler, not here. Handle ack here for
  44927. + * split transfers. Start splits halt on ACK.
  44928. + */
  44929. + handle_hc_ack_intr(hcd, hc, hc_regs, qtd);
  44930. + } else {
  44931. + if (hc->ep_type == DWC_OTG_EP_TYPE_INTR ||
  44932. + hc->ep_type == DWC_OTG_EP_TYPE_ISOC) {
  44933. + /*
  44934. + * A periodic transfer halted with no other channel
  44935. + * interrupts set. Assume it was halted by the core
  44936. + * because it could not be completed in its scheduled
  44937. + * (micro)frame.
  44938. + */
  44939. +#ifdef DEBUG
  44940. + DWC_PRINTF
  44941. + ("%s: Halt channel %d (assume incomplete periodic transfer)\n",
  44942. + __func__, hc->hc_num);
  44943. +#endif
  44944. + halt_channel(hcd, hc, qtd,
  44945. + DWC_OTG_HC_XFER_PERIODIC_INCOMPLETE);
  44946. + } else {
  44947. + DWC_ERROR
  44948. + ("%s: Channel %d, DMA Mode -- ChHltd set, but reason "
  44949. + "for halting is unknown, hcint 0x%08x, intsts 0x%08x\n",
  44950. + __func__, hc->hc_num, hcint.d32,
  44951. + DWC_READ_REG32(&hcd->
  44952. + core_if->core_global_regs->
  44953. + gintsts));
  44954. + /* Failthrough: use 3-strikes rule */
  44955. + qtd->error_count++;
  44956. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  44957. + update_urb_state_xfer_intr(hc, hc_regs,
  44958. + qtd->urb, qtd, DWC_OTG_HC_XFER_XACT_ERR);
  44959. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_XACT_ERR);
  44960. + }
  44961. +
  44962. + }
  44963. + } else {
  44964. + DWC_PRINTF("NYET/NAK/ACK/other in non-error case, 0x%08x\n",
  44965. + hcint.d32);
  44966. + /* Failthrough: use 3-strikes rule */
  44967. + qtd->error_count++;
  44968. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  44969. + update_urb_state_xfer_intr(hc, hc_regs,
  44970. + qtd->urb, qtd, DWC_OTG_HC_XFER_XACT_ERR);
  44971. + halt_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_XACT_ERR);
  44972. + }
  44973. +}
  44974. +
  44975. +/**
  44976. + * Handles a host channel Channel Halted interrupt.
  44977. + *
  44978. + * In slave mode, this handler is called only when the driver specifically
  44979. + * requests a halt. This occurs during handling other host channel interrupts
  44980. + * (e.g. nak, xacterr, stall, nyet, etc.).
  44981. + *
  44982. + * In DMA mode, this is the interrupt that occurs when the core has finished
  44983. + * processing a transfer on a channel. Other host channel interrupts (except
  44984. + * ahberr) are disabled in DMA mode.
  44985. + */
  44986. +static int32_t handle_hc_chhltd_intr(dwc_otg_hcd_t * hcd,
  44987. + dwc_hc_t * hc,
  44988. + dwc_otg_hc_regs_t * hc_regs,
  44989. + dwc_otg_qtd_t * qtd)
  44990. +{
  44991. + DWC_DEBUGPL(DBG_HCDI, "--Host Channel %d Interrupt: "
  44992. + "Channel Halted--\n", hc->hc_num);
  44993. +
  44994. + if (hcd->core_if->dma_enable) {
  44995. + handle_hc_chhltd_intr_dma(hcd, hc, hc_regs, qtd);
  44996. + } else {
  44997. +#ifdef DEBUG
  44998. + if (!halt_status_ok(hcd, hc, hc_regs, qtd)) {
  44999. + return 1;
  45000. + }
  45001. +#endif
  45002. + release_channel(hcd, hc, qtd, hc->halt_status);
  45003. + }
  45004. +
  45005. + return 1;
  45006. +}
  45007. +
  45008. +
  45009. +/**
  45010. + * dwc_otg_fiq_unmangle_isoc() - Update the iso_frame_desc structure on
  45011. + * FIQ transfer completion
  45012. + * @hcd: Pointer to dwc_otg_hcd struct
  45013. + * @num: Host channel number
  45014. + *
  45015. + * 1. Un-mangle the status as recorded in each iso_frame_desc status
  45016. + * 2. Copy it from the dwc_otg_urb into the real URB
  45017. + */
  45018. +void dwc_otg_fiq_unmangle_isoc(dwc_otg_hcd_t *hcd, dwc_otg_qh_t *qh, dwc_otg_qtd_t *qtd, uint32_t num)
  45019. +{
  45020. + struct dwc_otg_hcd_urb *dwc_urb = qtd->urb;
  45021. + int nr_frames = dwc_urb->packet_count;
  45022. + int i;
  45023. + hcint_data_t frame_hcint;
  45024. +
  45025. + for (i = 0; i < nr_frames; i++) {
  45026. + frame_hcint.d32 = dwc_urb->iso_descs[i].status;
  45027. + if (frame_hcint.b.xfercomp) {
  45028. + dwc_urb->iso_descs[i].status = 0;
  45029. + dwc_urb->actual_length += dwc_urb->iso_descs[i].actual_length;
  45030. + } else if (frame_hcint.b.frmovrun) {
  45031. + if (qh->ep_is_in)
  45032. + dwc_urb->iso_descs[i].status = -DWC_E_NO_STREAM_RES;
  45033. + else
  45034. + dwc_urb->iso_descs[i].status = -DWC_E_COMMUNICATION;
  45035. + dwc_urb->error_count++;
  45036. + dwc_urb->iso_descs[i].actual_length = 0;
  45037. + } else if (frame_hcint.b.xacterr) {
  45038. + dwc_urb->iso_descs[i].status = -DWC_E_PROTOCOL;
  45039. + dwc_urb->error_count++;
  45040. + dwc_urb->iso_descs[i].actual_length = 0;
  45041. + } else if (frame_hcint.b.bblerr) {
  45042. + dwc_urb->iso_descs[i].status = -DWC_E_OVERFLOW;
  45043. + dwc_urb->error_count++;
  45044. + dwc_urb->iso_descs[i].actual_length = 0;
  45045. + } else {
  45046. + /* Something went wrong */
  45047. + dwc_urb->iso_descs[i].status = -1;
  45048. + dwc_urb->iso_descs[i].actual_length = 0;
  45049. + dwc_urb->error_count++;
  45050. + }
  45051. + }
  45052. + qh->sched_frame = dwc_frame_num_inc(qh->sched_frame, qh->interval * (nr_frames - 1));
  45053. +
  45054. + //printk_ratelimited(KERN_INFO "%s: HS isochronous of %d/%d frames with %d errors complete\n",
  45055. + // __FUNCTION__, i, dwc_urb->packet_count, dwc_urb->error_count);
  45056. +}
  45057. +
  45058. +/**
  45059. + * dwc_otg_fiq_unsetup_per_dma() - Remove data from bounce buffers for split transactions
  45060. + * @hcd: Pointer to dwc_otg_hcd struct
  45061. + * @num: Host channel number
  45062. + *
  45063. + * Copies data from the FIQ bounce buffers into the URB's transfer buffer. Does not modify URB state.
  45064. + * Returns total length of data or -1 if the buffers were not used.
  45065. + *
  45066. + */
  45067. +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)
  45068. +{
  45069. + dwc_hc_t *hc = qh->channel;
  45070. + struct fiq_dma_blob *blob = hcd->fiq_dmab;
  45071. + struct fiq_channel_state *st = &hcd->fiq_state->channel[num];
  45072. + uint8_t *ptr = NULL;
  45073. + int index = 0, len = 0;
  45074. + int i = 0;
  45075. + if (hc->ep_is_in) {
  45076. + /* Copy data out of the DMA bounce buffers to the URB's buffer.
  45077. + * The align_buf is ignored as this is ignored on FSM enqueue. */
  45078. + ptr = qtd->urb->buf;
  45079. + if (qh->ep_type == UE_ISOCHRONOUS) {
  45080. + /* Isoc IN transactions - grab the offset of the iso_frame_desc into the URB transfer buffer */
  45081. + index = qtd->isoc_frame_index;
  45082. + ptr += qtd->urb->iso_descs[index].offset;
  45083. + } else {
  45084. + /* Need to increment by actual_length for interrupt IN */
  45085. + ptr += qtd->urb->actual_length;
  45086. + }
  45087. +
  45088. + for (i = 0; i < st->dma_info.index; i++) {
  45089. + len += st->dma_info.slot_len[i];
  45090. + dwc_memcpy(ptr, &blob->channel[num].index[i].buf[0], st->dma_info.slot_len[i]);
  45091. + ptr += st->dma_info.slot_len[i];
  45092. + }
  45093. + return len;
  45094. + } else {
  45095. + /* OUT endpoints - nothing to do. */
  45096. + return -1;
  45097. + }
  45098. +
  45099. +}
  45100. +/**
  45101. + * dwc_otg_hcd_handle_hc_fsm() - handle an unmasked channel interrupt
  45102. + * from a channel handled in the FIQ
  45103. + * @hcd: Pointer to dwc_otg_hcd struct
  45104. + * @num: Host channel number
  45105. + *
  45106. + * If a host channel interrupt was received by the IRQ and this was a channel
  45107. + * used by the FIQ, the execution flow for transfer completion is substantially
  45108. + * different from the normal (messy) path. This function and its friends handles
  45109. + * channel cleanup and transaction completion from a FIQ transaction.
  45110. + */
  45111. +void dwc_otg_hcd_handle_hc_fsm(dwc_otg_hcd_t *hcd, uint32_t num)
  45112. +{
  45113. + struct fiq_channel_state *st = &hcd->fiq_state->channel[num];
  45114. + dwc_hc_t *hc = hcd->hc_ptr_array[num];
  45115. + dwc_otg_qtd_t *qtd = DWC_CIRCLEQ_FIRST(&hc->qh->qtd_list);
  45116. + dwc_otg_qh_t *qh = hc->qh;
  45117. + dwc_otg_hc_regs_t *hc_regs = hcd->core_if->host_if->hc_regs[num];
  45118. + hcint_data_t hcint = hcd->fiq_state->channel[num].hcint_copy;
  45119. + int hostchannels = 0;
  45120. + fiq_print(FIQDBG_INT, hcd->fiq_state, "OUT %01d %01d ", num , st->fsm);
  45121. +
  45122. + hostchannels = hcd->available_host_channels;
  45123. + switch (st->fsm) {
  45124. + case FIQ_TEST:
  45125. + break;
  45126. +
  45127. + case FIQ_DEQUEUE_ISSUED:
  45128. + /* hc_halt was called. QTD no longer exists. */
  45129. + /* TODO: for a nonperiodic split transaction, need to issue a
  45130. + * CLEAR_TT_BUFFER hub command if we were in the start-split phase.
  45131. + */
  45132. + release_channel(hcd, hc, NULL, hc->halt_status);
  45133. + break;
  45134. +
  45135. + case FIQ_NP_SPLIT_DONE:
  45136. + /* Nonperiodic transaction complete. */
  45137. + if (!hc->ep_is_in) {
  45138. + qtd->ssplit_out_xfer_count = hc->xfer_len;
  45139. + }
  45140. + if (hcint.b.xfercomp) {
  45141. + handle_hc_xfercomp_intr(hcd, hc, hc_regs, qtd);
  45142. + } else if (hcint.b.nak) {
  45143. + handle_hc_nak_intr(hcd, hc, hc_regs, qtd);
  45144. + }
  45145. + break;
  45146. +
  45147. + case FIQ_NP_SPLIT_HS_ABORTED:
  45148. + /* A HS abort is a 3-strikes on the HS bus at any point in the transaction.
  45149. + * Normally a CLEAR_TT_BUFFER hub command would be required: we can't do that
  45150. + * because there's no guarantee which order a non-periodic split happened in.
  45151. + * We could end up clearing a perfectly good transaction out of the buffer.
  45152. + */
  45153. + if (hcint.b.xacterr) {
  45154. + qtd->error_count += st->nr_errors;
  45155. + handle_hc_xacterr_intr(hcd, hc, hc_regs, qtd);
  45156. + } else if (hcint.b.ahberr) {
  45157. + handle_hc_ahberr_intr(hcd, hc, hc_regs, qtd);
  45158. + } else {
  45159. + local_fiq_disable();
  45160. + BUG();
  45161. + }
  45162. + break;
  45163. +
  45164. + case FIQ_NP_SPLIT_LS_ABORTED:
  45165. + /* A few cases can cause this - either an unknown state on a SSPLIT or
  45166. + * STALL/data toggle error response on a CSPLIT */
  45167. + if (hcint.b.stall) {
  45168. + handle_hc_stall_intr(hcd, hc, hc_regs, qtd);
  45169. + } else if (hcint.b.datatglerr) {
  45170. + handle_hc_datatglerr_intr(hcd, hc, hc_regs, qtd);
  45171. + } else if (hcint.b.bblerr) {
  45172. + handle_hc_babble_intr(hcd, hc, hc_regs, qtd);
  45173. + } else if (hcint.b.ahberr) {
  45174. + handle_hc_ahberr_intr(hcd, hc, hc_regs, qtd);
  45175. + } else {
  45176. + local_fiq_disable();
  45177. + BUG();
  45178. + }
  45179. + break;
  45180. +
  45181. + case FIQ_PER_SPLIT_DONE:
  45182. + /* Isoc IN or Interrupt IN/OUT */
  45183. +
  45184. + /* Flow control here is different from the normal execution by the driver.
  45185. + * We need to completely ignore most of the driver's method of handling
  45186. + * split transactions and do it ourselves.
  45187. + */
  45188. + if (hc->ep_type == UE_INTERRUPT) {
  45189. + if (hcint.b.nak) {
  45190. + handle_hc_nak_intr(hcd, hc, hc_regs, qtd);
  45191. + } else if (hc->ep_is_in) {
  45192. + int len;
  45193. + len = dwc_otg_fiq_unsetup_per_dma(hcd, hc->qh, qtd, num);
  45194. + //printk(KERN_NOTICE "FIQ Transaction: hc=%d len=%d urb_len = %d\n", num, len, qtd->urb->length);
  45195. + qtd->urb->actual_length += len;
  45196. + if (qtd->urb->actual_length >= qtd->urb->length) {
  45197. + qtd->urb->status = 0;
  45198. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, qtd->urb->status);
  45199. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
  45200. + } else {
  45201. + /* Interrupt transfer not complete yet - is it a short read? */
  45202. + if (len < hc->max_packet) {
  45203. + /* Interrupt transaction complete */
  45204. + qtd->urb->status = 0;
  45205. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, qtd->urb->status);
  45206. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
  45207. + } else {
  45208. + /* Further transactions required */
  45209. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_COMPLETE);
  45210. + }
  45211. + }
  45212. + } else {
  45213. + /* Interrupt OUT complete. */
  45214. + dwc_otg_hcd_save_data_toggle(hc, hc_regs, qtd);
  45215. + qtd->urb->actual_length += hc->xfer_len;
  45216. + if (qtd->urb->actual_length >= qtd->urb->length) {
  45217. + qtd->urb->status = 0;
  45218. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, qtd->urb->status);
  45219. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
  45220. + } else {
  45221. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_COMPLETE);
  45222. + }
  45223. + }
  45224. + } else {
  45225. + /* ISOC IN complete. */
  45226. + struct dwc_otg_hcd_iso_packet_desc *frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
  45227. + int len = 0;
  45228. + /* Record errors, update qtd. */
  45229. + if (st->nr_errors) {
  45230. + frame_desc->actual_length = 0;
  45231. + frame_desc->status = -DWC_E_PROTOCOL;
  45232. + } else {
  45233. + frame_desc->status = 0;
  45234. + /* Unswizzle dma */
  45235. + len = dwc_otg_fiq_unsetup_per_dma(hcd, qh, qtd, num);
  45236. + frame_desc->actual_length = len;
  45237. + }
  45238. + qtd->isoc_frame_index++;
  45239. + if (qtd->isoc_frame_index == qtd->urb->packet_count) {
  45240. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
  45241. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
  45242. + } else {
  45243. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_COMPLETE);
  45244. + }
  45245. + }
  45246. + break;
  45247. +
  45248. + case FIQ_PER_ISO_OUT_DONE: {
  45249. + struct dwc_otg_hcd_iso_packet_desc *frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
  45250. + /* Record errors, update qtd. */
  45251. + if (st->nr_errors) {
  45252. + frame_desc->actual_length = 0;
  45253. + frame_desc->status = -DWC_E_PROTOCOL;
  45254. + } else {
  45255. + frame_desc->status = 0;
  45256. + frame_desc->actual_length = frame_desc->length;
  45257. + }
  45258. + qtd->isoc_frame_index++;
  45259. + qtd->isoc_split_offset = 0;
  45260. + if (qtd->isoc_frame_index == qtd->urb->packet_count) {
  45261. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
  45262. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
  45263. + } else {
  45264. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_COMPLETE);
  45265. + }
  45266. + }
  45267. + break;
  45268. +
  45269. + case FIQ_PER_SPLIT_NYET_ABORTED:
  45270. + /* Doh. lost the data. */
  45271. + printk_ratelimited(KERN_INFO "Transfer to device %d endpoint 0x%x frame %d failed "
  45272. + "- FIQ reported NYET. Data may have been lost.\n",
  45273. + hc->dev_addr, hc->ep_num, dwc_otg_hcd_get_frame_number(hcd) >> 3);
  45274. + if (hc->ep_type == UE_ISOCHRONOUS) {
  45275. + struct dwc_otg_hcd_iso_packet_desc *frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
  45276. + /* Record errors, update qtd. */
  45277. + frame_desc->actual_length = 0;
  45278. + frame_desc->status = -DWC_E_PROTOCOL;
  45279. + qtd->isoc_frame_index++;
  45280. + qtd->isoc_split_offset = 0;
  45281. + if (qtd->isoc_frame_index == qtd->urb->packet_count) {
  45282. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
  45283. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
  45284. + } else {
  45285. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_COMPLETE);
  45286. + }
  45287. + } else {
  45288. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
  45289. + }
  45290. + break;
  45291. +
  45292. + case FIQ_HS_ISOC_DONE:
  45293. + /* The FIQ has performed a whole pile of isochronous transactions.
  45294. + * The status is recorded as the interrupt state should the transaction
  45295. + * fail.
  45296. + */
  45297. + dwc_otg_fiq_unmangle_isoc(hcd, qh, qtd, num);
  45298. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
  45299. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
  45300. + break;
  45301. +
  45302. + case FIQ_PER_SPLIT_LS_ABORTED:
  45303. + if (hcint.b.xacterr) {
  45304. + /* Hub has responded with an ERR packet. Device
  45305. + * has been unplugged or the port has been disabled.
  45306. + * TODO: need to issue a reset to the hub port. */
  45307. + qtd->error_count += 3;
  45308. + handle_hc_xacterr_intr(hcd, hc, hc_regs, qtd);
  45309. + } else if (hcint.b.stall) {
  45310. + handle_hc_stall_intr(hcd, hc, hc_regs, qtd);
  45311. + } else if (hcint.b.bblerr) {
  45312. + handle_hc_babble_intr(hcd, hc, hc_regs, qtd);
  45313. + } else {
  45314. + printk_ratelimited(KERN_INFO "Transfer to device %d endpoint 0x%x failed "
  45315. + "- FIQ reported FSM=%d. Data may have been lost.\n",
  45316. + st->fsm, hc->dev_addr, hc->ep_num);
  45317. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
  45318. + }
  45319. + break;
  45320. +
  45321. + case FIQ_PER_SPLIT_HS_ABORTED:
  45322. + /* Either the SSPLIT phase suffered transaction errors or something
  45323. + * unexpected happened.
  45324. + */
  45325. + qtd->error_count += 3;
  45326. + handle_hc_xacterr_intr(hcd, hc, hc_regs, qtd);
  45327. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
  45328. + break;
  45329. +
  45330. + case FIQ_PER_SPLIT_TIMEOUT:
  45331. + /* Couldn't complete in the nominated frame */
  45332. + printk(KERN_INFO "Transfer to device %d endpoint 0x%x frame %d failed "
  45333. + "- FIQ timed out. Data may have been lost.\n",
  45334. + hc->dev_addr, hc->ep_num, dwc_otg_hcd_get_frame_number(hcd) >> 3);
  45335. + if (hc->ep_type == UE_ISOCHRONOUS) {
  45336. + struct dwc_otg_hcd_iso_packet_desc *frame_desc = &qtd->urb->iso_descs[qtd->isoc_frame_index];
  45337. + /* Record errors, update qtd. */
  45338. + frame_desc->actual_length = 0;
  45339. + if (hc->ep_is_in) {
  45340. + frame_desc->status = -DWC_E_NO_STREAM_RES;
  45341. + } else {
  45342. + frame_desc->status = -DWC_E_COMMUNICATION;
  45343. + }
  45344. + qtd->isoc_frame_index++;
  45345. + if (qtd->isoc_frame_index == qtd->urb->packet_count) {
  45346. + hcd->fops->complete(hcd, qtd->urb->priv, qtd->urb, 0);
  45347. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_URB_COMPLETE);
  45348. + } else {
  45349. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_COMPLETE);
  45350. + }
  45351. + } else {
  45352. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
  45353. + }
  45354. + break;
  45355. +
  45356. + default:
  45357. + DWC_WARN("Unexpected state received on hc=%d fsm=%d on transfer to device %d ep 0x%x",
  45358. + hc->hc_num, st->fsm, hc->dev_addr, hc->ep_num);
  45359. + qtd->error_count++;
  45360. + release_channel(hcd, hc, qtd, DWC_OTG_HC_XFER_NO_HALT_STATUS);
  45361. + }
  45362. + return;
  45363. +}
  45364. +
  45365. +/** Handles interrupt for a specific Host Channel */
  45366. +int32_t dwc_otg_hcd_handle_hc_n_intr(dwc_otg_hcd_t * dwc_otg_hcd, uint32_t num)
  45367. +{
  45368. + int retval = 0;
  45369. + hcint_data_t hcint;
  45370. + hcintmsk_data_t hcintmsk;
  45371. + dwc_hc_t *hc;
  45372. + dwc_otg_hc_regs_t *hc_regs;
  45373. + dwc_otg_qtd_t *qtd;
  45374. +
  45375. + DWC_DEBUGPL(DBG_HCDV, "--Host Channel Interrupt--, Channel %d\n", num);
  45376. +
  45377. + hc = dwc_otg_hcd->hc_ptr_array[num];
  45378. + hc_regs = dwc_otg_hcd->core_if->host_if->hc_regs[num];
  45379. + if(hc->halt_status == DWC_OTG_HC_XFER_URB_DEQUEUE) {
  45380. + /* We are responding to a channel disable. Driver
  45381. + * state is cleared - our qtd has gone away.
  45382. + */
  45383. + release_channel(dwc_otg_hcd, hc, NULL, hc->halt_status);
  45384. + return 1;
  45385. + }
  45386. + qtd = DWC_CIRCLEQ_FIRST(&hc->qh->qtd_list);
  45387. +
  45388. + /*
  45389. + * FSM mode: Check to see if this is a HC interrupt from a channel handled by the FIQ.
  45390. + * Execution path is fundamentally different for the channels after a FIQ has completed
  45391. + * a split transaction.
  45392. + */
  45393. + if (fiq_fsm_enable) {
  45394. + switch (dwc_otg_hcd->fiq_state->channel[num].fsm) {
  45395. + case FIQ_PASSTHROUGH:
  45396. + break;
  45397. + case FIQ_PASSTHROUGH_ERRORSTATE:
  45398. + /* Hook into the error count */
  45399. + fiq_print(FIQDBG_ERR, dwc_otg_hcd->fiq_state, "HCDERR%02d", num);
  45400. + if (!dwc_otg_hcd->fiq_state->channel[num].nr_errors) {
  45401. + qtd->error_count = 0;
  45402. + fiq_print(FIQDBG_ERR, dwc_otg_hcd->fiq_state, "RESET ");
  45403. + }
  45404. + break;
  45405. + default:
  45406. + dwc_otg_hcd_handle_hc_fsm(dwc_otg_hcd, num);
  45407. + return 1;
  45408. + }
  45409. + }
  45410. +
  45411. + hcint.d32 = DWC_READ_REG32(&hc_regs->hcint);
  45412. + hcintmsk.d32 = DWC_READ_REG32(&hc_regs->hcintmsk);
  45413. + hcint.d32 = hcint.d32 & hcintmsk.d32;
  45414. + if (!dwc_otg_hcd->core_if->dma_enable) {
  45415. + if (hcint.b.chhltd && hcint.d32 != 0x2) {
  45416. + hcint.b.chhltd = 0;
  45417. + }
  45418. + }
  45419. +
  45420. + if (hcint.b.xfercomp) {
  45421. + retval |=
  45422. + handle_hc_xfercomp_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  45423. + /*
  45424. + * If NYET occurred at same time as Xfer Complete, the NYET is
  45425. + * handled by the Xfer Complete interrupt handler. Don't want
  45426. + * to call the NYET interrupt handler in this case.
  45427. + */
  45428. + hcint.b.nyet = 0;
  45429. + }
  45430. + if (hcint.b.chhltd) {
  45431. + retval |= handle_hc_chhltd_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  45432. + }
  45433. + if (hcint.b.ahberr) {
  45434. + retval |= handle_hc_ahberr_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  45435. + }
  45436. + if (hcint.b.stall) {
  45437. + retval |= handle_hc_stall_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  45438. + }
  45439. + if (hcint.b.nak) {
  45440. + retval |= handle_hc_nak_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  45441. + }
  45442. + if (hcint.b.ack) {
  45443. + if(!hcint.b.chhltd)
  45444. + retval |= handle_hc_ack_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  45445. + }
  45446. + if (hcint.b.nyet) {
  45447. + retval |= handle_hc_nyet_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  45448. + }
  45449. + if (hcint.b.xacterr) {
  45450. + retval |= handle_hc_xacterr_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  45451. + }
  45452. + if (hcint.b.bblerr) {
  45453. + retval |= handle_hc_babble_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  45454. + }
  45455. + if (hcint.b.frmovrun) {
  45456. + retval |=
  45457. + handle_hc_frmovrun_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  45458. + }
  45459. + if (hcint.b.datatglerr) {
  45460. + retval |=
  45461. + handle_hc_datatglerr_intr(dwc_otg_hcd, hc, hc_regs, qtd);
  45462. + }
  45463. +
  45464. + return retval;
  45465. +}
  45466. +#endif /* DWC_DEVICE_ONLY */
  45467. --- /dev/null
  45468. +++ b/drivers/usb/host/dwc_otg/dwc_otg_hcd_linux.c
  45469. @@ -0,0 +1,1005 @@
  45470. +
  45471. +/* ==========================================================================
  45472. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd_linux.c $
  45473. + * $Revision: #20 $
  45474. + * $Date: 2011/10/26 $
  45475. + * $Change: 1872981 $
  45476. + *
  45477. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  45478. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  45479. + * otherwise expressly agreed to in writing between Synopsys and you.
  45480. + *
  45481. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  45482. + * any End User Software License Agreement or Agreement for Licensed Product
  45483. + * with Synopsys or any supplement thereto. You are permitted to use and
  45484. + * redistribute this Software in source and binary forms, with or without
  45485. + * modification, provided that redistributions of source code must retain this
  45486. + * notice. You may not view, use, disclose, copy or distribute this file or
  45487. + * any information contained herein except pursuant to this license grant from
  45488. + * Synopsys. If you do not agree with this notice, including the disclaimer
  45489. + * below, then you are not authorized to use the Software.
  45490. + *
  45491. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  45492. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  45493. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  45494. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  45495. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  45496. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  45497. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  45498. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  45499. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  45500. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  45501. + * DAMAGE.
  45502. + * ========================================================================== */
  45503. +#ifndef DWC_DEVICE_ONLY
  45504. +
  45505. +/**
  45506. + * @file
  45507. + *
  45508. + * This file contains the implementation of the HCD. In Linux, the HCD
  45509. + * implements the hc_driver API.
  45510. + */
  45511. +#include <linux/kernel.h>
  45512. +#include <linux/module.h>
  45513. +#include <linux/moduleparam.h>
  45514. +#include <linux/init.h>
  45515. +#include <linux/device.h>
  45516. +#include <linux/errno.h>
  45517. +#include <linux/list.h>
  45518. +#include <linux/interrupt.h>
  45519. +#include <linux/string.h>
  45520. +#include <linux/dma-mapping.h>
  45521. +#include <linux/version.h>
  45522. +#include <asm/io.h>
  45523. +#include <asm/fiq.h>
  45524. +#include <linux/usb.h>
  45525. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,35)
  45526. +#include <../drivers/usb/core/hcd.h>
  45527. +#else
  45528. +#include <linux/usb/hcd.h>
  45529. +#endif
  45530. +#include <asm/bug.h>
  45531. +
  45532. +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30))
  45533. +#define USB_URB_EP_LINKING 1
  45534. +#else
  45535. +#define USB_URB_EP_LINKING 0
  45536. +#endif
  45537. +
  45538. +#include "dwc_otg_hcd_if.h"
  45539. +#include "dwc_otg_dbg.h"
  45540. +#include "dwc_otg_driver.h"
  45541. +#include "dwc_otg_hcd.h"
  45542. +
  45543. +extern unsigned char _dwc_otg_fiq_stub, _dwc_otg_fiq_stub_end;
  45544. +
  45545. +/**
  45546. + * Gets the endpoint number from a _bEndpointAddress argument. The endpoint is
  45547. + * qualified with its direction (possible 32 endpoints per device).
  45548. + */
  45549. +#define dwc_ep_addr_to_endpoint(_bEndpointAddress_) ((_bEndpointAddress_ & USB_ENDPOINT_NUMBER_MASK) | \
  45550. + ((_bEndpointAddress_ & USB_DIR_IN) != 0) << 4)
  45551. +
  45552. +static const char dwc_otg_hcd_name[] = "dwc_otg_hcd";
  45553. +
  45554. +extern bool fiq_enable;
  45555. +
  45556. +/** @name Linux HC Driver API Functions */
  45557. +/** @{ */
  45558. +/* manage i/o requests, device state */
  45559. +static int dwc_otg_urb_enqueue(struct usb_hcd *hcd,
  45560. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  45561. + struct usb_host_endpoint *ep,
  45562. +#endif
  45563. + struct urb *urb, gfp_t mem_flags);
  45564. +
  45565. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30)
  45566. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  45567. +static int dwc_otg_urb_dequeue(struct usb_hcd *hcd, struct urb *urb);
  45568. +#endif
  45569. +#else /* kernels at or post 2.6.30 */
  45570. +static int dwc_otg_urb_dequeue(struct usb_hcd *hcd,
  45571. + struct urb *urb, int status);
  45572. +#endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30) */
  45573. +
  45574. +static void endpoint_disable(struct usb_hcd *hcd, struct usb_host_endpoint *ep);
  45575. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30)
  45576. +static void endpoint_reset(struct usb_hcd *hcd, struct usb_host_endpoint *ep);
  45577. +#endif
  45578. +static irqreturn_t dwc_otg_hcd_irq(struct usb_hcd *hcd);
  45579. +extern int hcd_start(struct usb_hcd *hcd);
  45580. +extern void hcd_stop(struct usb_hcd *hcd);
  45581. +static int get_frame_number(struct usb_hcd *hcd);
  45582. +extern int hub_status_data(struct usb_hcd *hcd, char *buf);
  45583. +extern int hub_control(struct usb_hcd *hcd,
  45584. + u16 typeReq,
  45585. + u16 wValue, u16 wIndex, char *buf, u16 wLength);
  45586. +
  45587. +struct wrapper_priv_data {
  45588. + dwc_otg_hcd_t *dwc_otg_hcd;
  45589. +};
  45590. +
  45591. +/** @} */
  45592. +
  45593. +static struct hc_driver dwc_otg_hc_driver = {
  45594. +
  45595. + .description = dwc_otg_hcd_name,
  45596. + .product_desc = "DWC OTG Controller",
  45597. + .hcd_priv_size = sizeof(struct wrapper_priv_data),
  45598. +
  45599. + .irq = dwc_otg_hcd_irq,
  45600. +
  45601. + .flags = HCD_MEMORY | HCD_USB2,
  45602. +
  45603. + //.reset =
  45604. + .start = hcd_start,
  45605. + //.suspend =
  45606. + //.resume =
  45607. + .stop = hcd_stop,
  45608. +
  45609. + .urb_enqueue = dwc_otg_urb_enqueue,
  45610. + .urb_dequeue = dwc_otg_urb_dequeue,
  45611. + .endpoint_disable = endpoint_disable,
  45612. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30)
  45613. + .endpoint_reset = endpoint_reset,
  45614. +#endif
  45615. + .get_frame_number = get_frame_number,
  45616. +
  45617. + .hub_status_data = hub_status_data,
  45618. + .hub_control = hub_control,
  45619. + //.bus_suspend =
  45620. + //.bus_resume =
  45621. +};
  45622. +
  45623. +/** Gets the dwc_otg_hcd from a struct usb_hcd */
  45624. +static inline dwc_otg_hcd_t *hcd_to_dwc_otg_hcd(struct usb_hcd *hcd)
  45625. +{
  45626. + struct wrapper_priv_data *p;
  45627. + p = (struct wrapper_priv_data *)(hcd->hcd_priv);
  45628. + return p->dwc_otg_hcd;
  45629. +}
  45630. +
  45631. +/** Gets the struct usb_hcd that contains a dwc_otg_hcd_t. */
  45632. +static inline struct usb_hcd *dwc_otg_hcd_to_hcd(dwc_otg_hcd_t * dwc_otg_hcd)
  45633. +{
  45634. + return dwc_otg_hcd_get_priv_data(dwc_otg_hcd);
  45635. +}
  45636. +
  45637. +/** Gets the usb_host_endpoint associated with an URB. */
  45638. +inline struct usb_host_endpoint *dwc_urb_to_endpoint(struct urb *urb)
  45639. +{
  45640. + struct usb_device *dev = urb->dev;
  45641. + int ep_num = usb_pipeendpoint(urb->pipe);
  45642. +
  45643. + if (usb_pipein(urb->pipe))
  45644. + return dev->ep_in[ep_num];
  45645. + else
  45646. + return dev->ep_out[ep_num];
  45647. +}
  45648. +
  45649. +static int _disconnect(dwc_otg_hcd_t * hcd)
  45650. +{
  45651. + struct usb_hcd *usb_hcd = dwc_otg_hcd_to_hcd(hcd);
  45652. +
  45653. + usb_hcd->self.is_b_host = 0;
  45654. + return 0;
  45655. +}
  45656. +
  45657. +static int _start(dwc_otg_hcd_t * hcd)
  45658. +{
  45659. + struct usb_hcd *usb_hcd = dwc_otg_hcd_to_hcd(hcd);
  45660. +
  45661. + usb_hcd->self.is_b_host = dwc_otg_hcd_is_b_host(hcd);
  45662. + hcd_start(usb_hcd);
  45663. +
  45664. + return 0;
  45665. +}
  45666. +
  45667. +static int _hub_info(dwc_otg_hcd_t * hcd, void *urb_handle, uint32_t * hub_addr,
  45668. + uint32_t * port_addr)
  45669. +{
  45670. + struct urb *urb = (struct urb *)urb_handle;
  45671. + struct usb_bus *bus;
  45672. +#if 1 //GRAYG - temporary
  45673. + if (NULL == urb_handle)
  45674. + DWC_ERROR("**** %s - NULL URB handle\n", __func__);//GRAYG
  45675. + if (NULL == urb->dev)
  45676. + DWC_ERROR("**** %s - URB has no device\n", __func__);//GRAYG
  45677. + if (NULL == port_addr)
  45678. + DWC_ERROR("**** %s - NULL port_address\n", __func__);//GRAYG
  45679. +#endif
  45680. + if (urb->dev->tt) {
  45681. + if (NULL == urb->dev->tt->hub) {
  45682. + DWC_ERROR("**** %s - (URB's transactor has no TT - giving no hub)\n",
  45683. + __func__); //GRAYG
  45684. + //*hub_addr = (u8)usb_pipedevice(urb->pipe); //GRAYG
  45685. + *hub_addr = 0; //GRAYG
  45686. + // we probably shouldn't have a transaction translator if
  45687. + // there's no associated hub?
  45688. + } else {
  45689. + bus = hcd_to_bus(dwc_otg_hcd_to_hcd(hcd));
  45690. + if (urb->dev->tt->hub == bus->root_hub)
  45691. + *hub_addr = 0;
  45692. + else
  45693. + *hub_addr = urb->dev->tt->hub->devnum;
  45694. + }
  45695. + *port_addr = urb->dev->tt->multi ? urb->dev->ttport : 1;
  45696. + } else {
  45697. + *hub_addr = 0;
  45698. + *port_addr = urb->dev->ttport;
  45699. + }
  45700. + return 0;
  45701. +}
  45702. +
  45703. +static int _speed(dwc_otg_hcd_t * hcd, void *urb_handle)
  45704. +{
  45705. + struct urb *urb = (struct urb *)urb_handle;
  45706. + return urb->dev->speed;
  45707. +}
  45708. +
  45709. +static int _get_b_hnp_enable(dwc_otg_hcd_t * hcd)
  45710. +{
  45711. + struct usb_hcd *usb_hcd = dwc_otg_hcd_to_hcd(hcd);
  45712. + return usb_hcd->self.b_hnp_enable;
  45713. +}
  45714. +
  45715. +static void allocate_bus_bandwidth(struct usb_hcd *hcd, uint32_t bw,
  45716. + struct urb *urb)
  45717. +{
  45718. + hcd_to_bus(hcd)->bandwidth_allocated += bw / urb->interval;
  45719. + if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
  45720. + hcd_to_bus(hcd)->bandwidth_isoc_reqs++;
  45721. + } else {
  45722. + hcd_to_bus(hcd)->bandwidth_int_reqs++;
  45723. + }
  45724. +}
  45725. +
  45726. +static void free_bus_bandwidth(struct usb_hcd *hcd, uint32_t bw,
  45727. + struct urb *urb)
  45728. +{
  45729. + hcd_to_bus(hcd)->bandwidth_allocated -= bw / urb->interval;
  45730. + if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
  45731. + hcd_to_bus(hcd)->bandwidth_isoc_reqs--;
  45732. + } else {
  45733. + hcd_to_bus(hcd)->bandwidth_int_reqs--;
  45734. + }
  45735. +}
  45736. +
  45737. +/**
  45738. + * Sets the final status of an URB and returns it to the device driver. Any
  45739. + * required cleanup of the URB is performed. The HCD lock should be held on
  45740. + * entry.
  45741. + */
  45742. +static int _complete(dwc_otg_hcd_t * hcd, void *urb_handle,
  45743. + dwc_otg_hcd_urb_t * dwc_otg_urb, int32_t status)
  45744. +{
  45745. + struct urb *urb = (struct urb *)urb_handle;
  45746. + urb_tq_entry_t *new_entry;
  45747. + int rc = 0;
  45748. + if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
  45749. + DWC_PRINTF("%s: urb %p, device %d, ep %d %s, status=%d\n",
  45750. + __func__, urb, usb_pipedevice(urb->pipe),
  45751. + usb_pipeendpoint(urb->pipe),
  45752. + usb_pipein(urb->pipe) ? "IN" : "OUT", status);
  45753. + if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
  45754. + int i;
  45755. + for (i = 0; i < urb->number_of_packets; i++) {
  45756. + DWC_PRINTF(" ISO Desc %d status: %d\n",
  45757. + i, urb->iso_frame_desc[i].status);
  45758. + }
  45759. + }
  45760. + }
  45761. + new_entry = DWC_ALLOC_ATOMIC(sizeof(urb_tq_entry_t));
  45762. + urb->actual_length = dwc_otg_hcd_urb_get_actual_length(dwc_otg_urb);
  45763. + /* Convert status value. */
  45764. + switch (status) {
  45765. + case -DWC_E_PROTOCOL:
  45766. + status = -EPROTO;
  45767. + break;
  45768. + case -DWC_E_IN_PROGRESS:
  45769. + status = -EINPROGRESS;
  45770. + break;
  45771. + case -DWC_E_PIPE:
  45772. + status = -EPIPE;
  45773. + break;
  45774. + case -DWC_E_IO:
  45775. + status = -EIO;
  45776. + break;
  45777. + case -DWC_E_TIMEOUT:
  45778. + status = -ETIMEDOUT;
  45779. + break;
  45780. + case -DWC_E_OVERFLOW:
  45781. + status = -EOVERFLOW;
  45782. + break;
  45783. + case -DWC_E_SHUTDOWN:
  45784. + status = -ESHUTDOWN;
  45785. + break;
  45786. + default:
  45787. + if (status) {
  45788. + DWC_PRINTF("Uknown urb status %d\n", status);
  45789. +
  45790. + }
  45791. + }
  45792. +
  45793. + if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
  45794. + int i;
  45795. +
  45796. + urb->error_count = dwc_otg_hcd_urb_get_error_count(dwc_otg_urb);
  45797. + for (i = 0; i < urb->number_of_packets; ++i) {
  45798. + urb->iso_frame_desc[i].actual_length =
  45799. + dwc_otg_hcd_urb_get_iso_desc_actual_length
  45800. + (dwc_otg_urb, i);
  45801. + urb->iso_frame_desc[i].status =
  45802. + dwc_otg_hcd_urb_get_iso_desc_status(dwc_otg_urb, i);
  45803. + }
  45804. + }
  45805. +
  45806. + urb->status = status;
  45807. + urb->hcpriv = NULL;
  45808. + if (!status) {
  45809. + if ((urb->transfer_flags & URB_SHORT_NOT_OK) &&
  45810. + (urb->actual_length < urb->transfer_buffer_length)) {
  45811. + urb->status = -EREMOTEIO;
  45812. + }
  45813. + }
  45814. +
  45815. + if ((usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) ||
  45816. + (usb_pipetype(urb->pipe) == PIPE_INTERRUPT)) {
  45817. + struct usb_host_endpoint *ep = dwc_urb_to_endpoint(urb);
  45818. + if (ep) {
  45819. + free_bus_bandwidth(dwc_otg_hcd_to_hcd(hcd),
  45820. + dwc_otg_hcd_get_ep_bandwidth(hcd,
  45821. + ep->hcpriv),
  45822. + urb);
  45823. + }
  45824. + }
  45825. + DWC_FREE(dwc_otg_urb);
  45826. + if (!new_entry) {
  45827. + DWC_ERROR("dwc_otg_hcd: complete: cannot allocate URB TQ entry\n");
  45828. + urb->status = -EPROTO;
  45829. + /* don't schedule the tasklet -
  45830. + * directly return the packet here with error. */
  45831. +#if USB_URB_EP_LINKING
  45832. + usb_hcd_unlink_urb_from_ep(dwc_otg_hcd_to_hcd(hcd), urb);
  45833. +#endif
  45834. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  45835. + usb_hcd_giveback_urb(dwc_otg_hcd_to_hcd(hcd), urb);
  45836. +#else
  45837. + usb_hcd_giveback_urb(dwc_otg_hcd_to_hcd(hcd), urb, urb->status);
  45838. +#endif
  45839. + } else {
  45840. + new_entry->urb = urb;
  45841. +#if USB_URB_EP_LINKING
  45842. + rc = usb_hcd_check_unlink_urb(dwc_otg_hcd_to_hcd(hcd), urb, urb->status);
  45843. + if(0 == rc) {
  45844. + usb_hcd_unlink_urb_from_ep(dwc_otg_hcd_to_hcd(hcd), urb);
  45845. + }
  45846. +#endif
  45847. + if(0 == rc) {
  45848. + DWC_TAILQ_INSERT_TAIL(&hcd->completed_urb_list, new_entry,
  45849. + urb_tq_entries);
  45850. + DWC_TASK_HI_SCHEDULE(hcd->completion_tasklet);
  45851. + }
  45852. + }
  45853. + return 0;
  45854. +}
  45855. +
  45856. +static struct dwc_otg_hcd_function_ops hcd_fops = {
  45857. + .start = _start,
  45858. + .disconnect = _disconnect,
  45859. + .hub_info = _hub_info,
  45860. + .speed = _speed,
  45861. + .complete = _complete,
  45862. + .get_b_hnp_enable = _get_b_hnp_enable,
  45863. +};
  45864. +
  45865. +static struct fiq_handler fh = {
  45866. + .name = "usb_fiq",
  45867. +};
  45868. +
  45869. +static void hcd_init_fiq(void *cookie)
  45870. +{
  45871. + dwc_otg_device_t *otg_dev = cookie;
  45872. + dwc_otg_hcd_t *dwc_otg_hcd = otg_dev->hcd;
  45873. + struct pt_regs regs;
  45874. + int irq;
  45875. +
  45876. + if (claim_fiq(&fh)) {
  45877. + DWC_ERROR("Can't claim FIQ");
  45878. + BUG();
  45879. + }
  45880. + DWC_WARN("FIQ on core %d at 0x%08x",
  45881. + smp_processor_id(),
  45882. + (fiq_fsm_enable ? (int)&dwc_otg_fiq_fsm : (int)&dwc_otg_fiq_nop));
  45883. + DWC_WARN("FIQ ASM at 0x%08x length %d", (int)&_dwc_otg_fiq_stub, (int)(&_dwc_otg_fiq_stub_end - &_dwc_otg_fiq_stub));
  45884. + set_fiq_handler((void *) &_dwc_otg_fiq_stub, &_dwc_otg_fiq_stub_end - &_dwc_otg_fiq_stub);
  45885. + memset(&regs,0,sizeof(regs));
  45886. +
  45887. + regs.ARM_r8 = (long) dwc_otg_hcd->fiq_state;
  45888. + if (fiq_fsm_enable) {
  45889. + regs.ARM_r9 = dwc_otg_hcd->core_if->core_params->host_channels;
  45890. + //regs.ARM_r10 = dwc_otg_hcd->dma;
  45891. + regs.ARM_fp = (long) dwc_otg_fiq_fsm;
  45892. + } else {
  45893. + regs.ARM_fp = (long) dwc_otg_fiq_nop;
  45894. + }
  45895. +
  45896. + regs.ARM_sp = (long) dwc_otg_hcd->fiq_stack + (sizeof(struct fiq_stack) - 4);
  45897. +
  45898. +// __show_regs(&regs);
  45899. + set_fiq_regs(&regs);
  45900. +
  45901. + //Set the mphi periph to the required registers
  45902. + dwc_otg_hcd->fiq_state->mphi_regs.base = otg_dev->os_dep.mphi_base;
  45903. + dwc_otg_hcd->fiq_state->mphi_regs.ctrl = otg_dev->os_dep.mphi_base + 0x4c;
  45904. + dwc_otg_hcd->fiq_state->mphi_regs.outdda = otg_dev->os_dep.mphi_base + 0x28;
  45905. + dwc_otg_hcd->fiq_state->mphi_regs.outddb = otg_dev->os_dep.mphi_base + 0x2c;
  45906. + dwc_otg_hcd->fiq_state->mphi_regs.intstat = otg_dev->os_dep.mphi_base + 0x50;
  45907. + dwc_otg_hcd->fiq_state->dwc_regs_base = otg_dev->os_dep.base;
  45908. + DWC_WARN("MPHI regs_base at 0x%08x", (int)dwc_otg_hcd->fiq_state->mphi_regs.base);
  45909. + //Enable mphi peripheral
  45910. + writel((1<<31),dwc_otg_hcd->fiq_state->mphi_regs.ctrl);
  45911. +#ifdef DEBUG
  45912. + if (readl(dwc_otg_hcd->fiq_state->mphi_regs.ctrl) & 0x80000000)
  45913. + DWC_WARN("MPHI periph has been enabled");
  45914. + else
  45915. + DWC_WARN("MPHI periph has NOT been enabled");
  45916. +#endif
  45917. + // Enable FIQ interrupt from USB peripheral
  45918. +#ifdef CONFIG_MULTI_IRQ_HANDLER
  45919. + irq = platform_get_irq(otg_dev->os_dep.platformdev, 1);
  45920. +#else
  45921. + irq = INTERRUPT_VC_USB;
  45922. +#endif
  45923. + if (irq < 0) {
  45924. + DWC_ERROR("Can't get FIQ irq");
  45925. + return;
  45926. + }
  45927. + enable_fiq(irq);
  45928. + local_fiq_enable();
  45929. +}
  45930. +
  45931. +/**
  45932. + * Initializes the HCD. This function allocates memory for and initializes the
  45933. + * static parts of the usb_hcd and dwc_otg_hcd structures. It also registers the
  45934. + * USB bus with the core and calls the hc_driver->start() function. It returns
  45935. + * a negative error on failure.
  45936. + */
  45937. +int hcd_init(dwc_bus_dev_t *_dev)
  45938. +{
  45939. + struct usb_hcd *hcd = NULL;
  45940. + dwc_otg_hcd_t *dwc_otg_hcd = NULL;
  45941. + dwc_otg_device_t *otg_dev = DWC_OTG_BUSDRVDATA(_dev);
  45942. + int retval = 0;
  45943. + u64 dmamask;
  45944. +
  45945. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD INIT otg_dev=%p\n", otg_dev);
  45946. +
  45947. + /* Set device flags indicating whether the HCD supports DMA. */
  45948. + if (dwc_otg_is_dma_enable(otg_dev->core_if))
  45949. + dmamask = DMA_BIT_MASK(32);
  45950. + else
  45951. + dmamask = 0;
  45952. +
  45953. +#if defined(LM_INTERFACE) || defined(PLATFORM_INTERFACE)
  45954. + dma_set_mask(&_dev->dev, dmamask);
  45955. + dma_set_coherent_mask(&_dev->dev, dmamask);
  45956. +#elif defined(PCI_INTERFACE)
  45957. + pci_set_dma_mask(_dev, dmamask);
  45958. + pci_set_consistent_dma_mask(_dev, dmamask);
  45959. +#endif
  45960. +
  45961. + /*
  45962. + * Allocate memory for the base HCD plus the DWC OTG HCD.
  45963. + * Initialize the base HCD.
  45964. + */
  45965. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30)
  45966. + hcd = usb_create_hcd(&dwc_otg_hc_driver, &_dev->dev, _dev->dev.bus_id);
  45967. +#else
  45968. + hcd = usb_create_hcd(&dwc_otg_hc_driver, &_dev->dev, dev_name(&_dev->dev));
  45969. + hcd->has_tt = 1;
  45970. +// hcd->uses_new_polling = 1;
  45971. +// hcd->poll_rh = 0;
  45972. +#endif
  45973. + if (!hcd) {
  45974. + retval = -ENOMEM;
  45975. + goto error1;
  45976. + }
  45977. +
  45978. + hcd->regs = otg_dev->os_dep.base;
  45979. +
  45980. +
  45981. + /* Initialize the DWC OTG HCD. */
  45982. + dwc_otg_hcd = dwc_otg_hcd_alloc_hcd();
  45983. + if (!dwc_otg_hcd) {
  45984. + goto error2;
  45985. + }
  45986. + ((struct wrapper_priv_data *)(hcd->hcd_priv))->dwc_otg_hcd =
  45987. + dwc_otg_hcd;
  45988. + otg_dev->hcd = dwc_otg_hcd;
  45989. + otg_dev->hcd->otg_dev = otg_dev;
  45990. +
  45991. + if (dwc_otg_hcd_init(dwc_otg_hcd, otg_dev->core_if)) {
  45992. + goto error2;
  45993. + }
  45994. +
  45995. + if (fiq_enable) {
  45996. + if (num_online_cpus() > 1) {
  45997. + /* bcm2709: can run the FIQ on a separate core to IRQs */
  45998. + smp_call_function_single(1, hcd_init_fiq, otg_dev, 1);
  45999. + } else {
  46000. + smp_call_function_single(0, hcd_init_fiq, otg_dev, 1);
  46001. + }
  46002. + }
  46003. +
  46004. + hcd->self.otg_port = dwc_otg_hcd_otg_port(dwc_otg_hcd);
  46005. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,33) //don't support for LM(with 2.6.20.1 kernel)
  46006. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,35) //version field absent later
  46007. + hcd->self.otg_version = dwc_otg_get_otg_version(otg_dev->core_if);
  46008. +#endif
  46009. + /* Don't support SG list at this point */
  46010. + hcd->self.sg_tablesize = 0;
  46011. +#endif
  46012. + /*
  46013. + * Finish generic HCD initialization and start the HCD. This function
  46014. + * allocates the DMA buffer pool, registers the USB bus, requests the
  46015. + * IRQ line, and calls hcd_start method.
  46016. + */
  46017. +#ifdef PLATFORM_INTERFACE
  46018. + retval = usb_add_hcd(hcd, platform_get_irq(_dev, fiq_enable ? 0 : 1), IRQF_SHARED);
  46019. +#else
  46020. + retval = usb_add_hcd(hcd, _dev->irq, IRQF_SHARED);
  46021. +#endif
  46022. + if (retval < 0) {
  46023. + goto error2;
  46024. + }
  46025. +
  46026. + dwc_otg_hcd_set_priv_data(dwc_otg_hcd, hcd);
  46027. + return 0;
  46028. +
  46029. +error2:
  46030. + usb_put_hcd(hcd);
  46031. +error1:
  46032. + return retval;
  46033. +}
  46034. +
  46035. +/**
  46036. + * Removes the HCD.
  46037. + * Frees memory and resources associated with the HCD and deregisters the bus.
  46038. + */
  46039. +void hcd_remove(dwc_bus_dev_t *_dev)
  46040. +{
  46041. + dwc_otg_device_t *otg_dev = DWC_OTG_BUSDRVDATA(_dev);
  46042. + dwc_otg_hcd_t *dwc_otg_hcd;
  46043. + struct usb_hcd *hcd;
  46044. +
  46045. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD REMOVE otg_dev=%p\n", otg_dev);
  46046. +
  46047. + if (!otg_dev) {
  46048. + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev NULL!\n", __func__);
  46049. + return;
  46050. + }
  46051. +
  46052. + dwc_otg_hcd = otg_dev->hcd;
  46053. +
  46054. + if (!dwc_otg_hcd) {
  46055. + DWC_DEBUGPL(DBG_ANY, "%s: otg_dev->hcd NULL!\n", __func__);
  46056. + return;
  46057. + }
  46058. +
  46059. + hcd = dwc_otg_hcd_to_hcd(dwc_otg_hcd);
  46060. +
  46061. + if (!hcd) {
  46062. + DWC_DEBUGPL(DBG_ANY,
  46063. + "%s: dwc_otg_hcd_to_hcd(dwc_otg_hcd) NULL!\n",
  46064. + __func__);
  46065. + return;
  46066. + }
  46067. + usb_remove_hcd(hcd);
  46068. + dwc_otg_hcd_set_priv_data(dwc_otg_hcd, NULL);
  46069. + dwc_otg_hcd_remove(dwc_otg_hcd);
  46070. + usb_put_hcd(hcd);
  46071. +}
  46072. +
  46073. +/* =========================================================================
  46074. + * Linux HC Driver Functions
  46075. + * ========================================================================= */
  46076. +
  46077. +/** Initializes the DWC_otg controller and its root hub and prepares it for host
  46078. + * mode operation. Activates the root port. Returns 0 on success and a negative
  46079. + * error code on failure. */
  46080. +int hcd_start(struct usb_hcd *hcd)
  46081. +{
  46082. + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  46083. + struct usb_bus *bus;
  46084. +
  46085. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD START\n");
  46086. + bus = hcd_to_bus(hcd);
  46087. +
  46088. + hcd->state = HC_STATE_RUNNING;
  46089. + if (dwc_otg_hcd_start(dwc_otg_hcd, &hcd_fops)) {
  46090. + return 0;
  46091. + }
  46092. +
  46093. + /* Initialize and connect root hub if one is not already attached */
  46094. + if (bus->root_hub) {
  46095. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD Has Root Hub\n");
  46096. + /* Inform the HUB driver to resume. */
  46097. + usb_hcd_resume_root_hub(hcd);
  46098. + }
  46099. +
  46100. + return 0;
  46101. +}
  46102. +
  46103. +/**
  46104. + * Halts the DWC_otg host mode operations in a clean manner. USB transfers are
  46105. + * stopped.
  46106. + */
  46107. +void hcd_stop(struct usb_hcd *hcd)
  46108. +{
  46109. + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  46110. +
  46111. + dwc_otg_hcd_stop(dwc_otg_hcd);
  46112. +}
  46113. +
  46114. +/** Returns the current frame number. */
  46115. +static int get_frame_number(struct usb_hcd *hcd)
  46116. +{
  46117. + hprt0_data_t hprt0;
  46118. + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  46119. + hprt0.d32 = DWC_READ_REG32(dwc_otg_hcd->core_if->host_if->hprt0);
  46120. + if (hprt0.b.prtspd == DWC_HPRT0_PRTSPD_HIGH_SPEED)
  46121. + return dwc_otg_hcd_get_frame_number(dwc_otg_hcd) >> 3;
  46122. + else
  46123. + return dwc_otg_hcd_get_frame_number(dwc_otg_hcd);
  46124. +}
  46125. +
  46126. +#ifdef DEBUG
  46127. +static void dump_urb_info(struct urb *urb, char *fn_name)
  46128. +{
  46129. + DWC_PRINTF("%s, urb %p\n", fn_name, urb);
  46130. + DWC_PRINTF(" Device address: %d\n", usb_pipedevice(urb->pipe));
  46131. + DWC_PRINTF(" Endpoint: %d, %s\n", usb_pipeendpoint(urb->pipe),
  46132. + (usb_pipein(urb->pipe) ? "IN" : "OUT"));
  46133. + DWC_PRINTF(" Endpoint type: %s\n", ( {
  46134. + char *pipetype;
  46135. + switch (usb_pipetype(urb->pipe)) {
  46136. +case PIPE_CONTROL:
  46137. +pipetype = "CONTROL"; break; case PIPE_BULK:
  46138. +pipetype = "BULK"; break; case PIPE_INTERRUPT:
  46139. +pipetype = "INTERRUPT"; break; case PIPE_ISOCHRONOUS:
  46140. +pipetype = "ISOCHRONOUS"; break; default:
  46141. + pipetype = "UNKNOWN"; break;};
  46142. + pipetype;}
  46143. + )) ;
  46144. + DWC_PRINTF(" Speed: %s\n", ( {
  46145. + char *speed; switch (urb->dev->speed) {
  46146. +case USB_SPEED_HIGH:
  46147. +speed = "HIGH"; break; case USB_SPEED_FULL:
  46148. +speed = "FULL"; break; case USB_SPEED_LOW:
  46149. +speed = "LOW"; break; default:
  46150. + speed = "UNKNOWN"; break;};
  46151. + speed;}
  46152. + )) ;
  46153. + DWC_PRINTF(" Max packet size: %d\n",
  46154. + usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe)));
  46155. + DWC_PRINTF(" Data buffer length: %d\n", urb->transfer_buffer_length);
  46156. + DWC_PRINTF(" Transfer buffer: %p, Transfer DMA: %p\n",
  46157. + urb->transfer_buffer, (void *)urb->transfer_dma);
  46158. + DWC_PRINTF(" Setup buffer: %p, Setup DMA: %p\n",
  46159. + urb->setup_packet, (void *)urb->setup_dma);
  46160. + DWC_PRINTF(" Interval: %d\n", urb->interval);
  46161. + if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
  46162. + int i;
  46163. + for (i = 0; i < urb->number_of_packets; i++) {
  46164. + DWC_PRINTF(" ISO Desc %d:\n", i);
  46165. + DWC_PRINTF(" offset: %d, length %d\n",
  46166. + urb->iso_frame_desc[i].offset,
  46167. + urb->iso_frame_desc[i].length);
  46168. + }
  46169. + }
  46170. +}
  46171. +#endif
  46172. +
  46173. +/** Starts processing a USB transfer request specified by a USB Request Block
  46174. + * (URB). mem_flags indicates the type of memory allocation to use while
  46175. + * processing this URB. */
  46176. +static int dwc_otg_urb_enqueue(struct usb_hcd *hcd,
  46177. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  46178. + struct usb_host_endpoint *ep,
  46179. +#endif
  46180. + struct urb *urb, gfp_t mem_flags)
  46181. +{
  46182. + int retval = 0;
  46183. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,28)
  46184. + struct usb_host_endpoint *ep = urb->ep;
  46185. +#endif
  46186. + dwc_irqflags_t irqflags;
  46187. + void **ref_ep_hcpriv = &ep->hcpriv;
  46188. + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  46189. + dwc_otg_hcd_urb_t *dwc_otg_urb;
  46190. + int i;
  46191. + int alloc_bandwidth = 0;
  46192. + uint8_t ep_type = 0;
  46193. + uint32_t flags = 0;
  46194. + void *buf;
  46195. +
  46196. +#ifdef DEBUG
  46197. + if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
  46198. + dump_urb_info(urb, "dwc_otg_urb_enqueue");
  46199. + }
  46200. +#endif
  46201. +
  46202. + if (!urb->transfer_buffer && urb->transfer_buffer_length)
  46203. + return -EINVAL;
  46204. +
  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. + !(usb_pipein(urb->pipe))));
  46246. +
  46247. + buf = urb->transfer_buffer;
  46248. + if (hcd->self.uses_dma && !buf && urb->transfer_buffer_length) {
  46249. + /*
  46250. + * Calculate virtual address from physical address,
  46251. + * because some class driver may not fill transfer_buffer.
  46252. + * In Buffer DMA mode virual address is used,
  46253. + * when handling non DWORD aligned buffers.
  46254. + */
  46255. + buf = (void *)__bus_to_virt((unsigned long)urb->transfer_dma);
  46256. + dev_warn_once(&urb->dev->dev,
  46257. + "USB transfer_buffer was NULL, will use __bus_to_virt(%pad)=%p\n",
  46258. + &urb->transfer_dma, buf);
  46259. + }
  46260. +
  46261. + if (!(urb->transfer_flags & URB_NO_INTERRUPT))
  46262. + flags |= URB_GIVEBACK_ASAP;
  46263. + if (urb->transfer_flags & URB_ZERO_PACKET)
  46264. + flags |= URB_SEND_ZERO_PACKET;
  46265. +
  46266. + dwc_otg_hcd_urb_set_params(dwc_otg_urb, urb, buf,
  46267. + urb->transfer_dma,
  46268. + urb->transfer_buffer_length,
  46269. + urb->setup_packet,
  46270. + urb->setup_dma, flags, urb->interval);
  46271. +
  46272. + for (i = 0; i < urb->number_of_packets; ++i) {
  46273. + dwc_otg_hcd_urb_set_iso_desc_params(dwc_otg_urb, i,
  46274. + urb->
  46275. + iso_frame_desc[i].offset,
  46276. + urb->
  46277. + iso_frame_desc[i].length);
  46278. + }
  46279. +
  46280. + DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &irqflags);
  46281. + urb->hcpriv = dwc_otg_urb;
  46282. +#if USB_URB_EP_LINKING
  46283. + retval = usb_hcd_link_urb_to_ep(hcd, urb);
  46284. + if (0 == retval)
  46285. +#endif
  46286. + {
  46287. + retval = dwc_otg_hcd_urb_enqueue(dwc_otg_hcd, dwc_otg_urb,
  46288. + /*(dwc_otg_qh_t **)*/
  46289. + ref_ep_hcpriv, 1);
  46290. + if (0 == retval) {
  46291. + if (alloc_bandwidth) {
  46292. + allocate_bus_bandwidth(hcd,
  46293. + dwc_otg_hcd_get_ep_bandwidth(
  46294. + dwc_otg_hcd, *ref_ep_hcpriv),
  46295. + urb);
  46296. + }
  46297. + } else {
  46298. + DWC_DEBUGPL(DBG_HCD, "DWC OTG dwc_otg_hcd_urb_enqueue failed rc %d\n", retval);
  46299. +#if USB_URB_EP_LINKING
  46300. + usb_hcd_unlink_urb_from_ep(hcd, urb);
  46301. +#endif
  46302. + DWC_FREE(dwc_otg_urb);
  46303. + urb->hcpriv = NULL;
  46304. + if (retval == -DWC_E_NO_DEVICE)
  46305. + retval = -ENODEV;
  46306. + }
  46307. + }
  46308. +#if USB_URB_EP_LINKING
  46309. + else
  46310. + {
  46311. + DWC_FREE(dwc_otg_urb);
  46312. + urb->hcpriv = NULL;
  46313. + }
  46314. +#endif
  46315. + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, irqflags);
  46316. + return retval;
  46317. +}
  46318. +
  46319. +/** Aborts/cancels a USB transfer request. Always returns 0 to indicate
  46320. + * success. */
  46321. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  46322. +static int dwc_otg_urb_dequeue(struct usb_hcd *hcd, struct urb *urb)
  46323. +#else
  46324. +static int dwc_otg_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
  46325. +#endif
  46326. +{
  46327. + dwc_irqflags_t flags;
  46328. + dwc_otg_hcd_t *dwc_otg_hcd;
  46329. + int rc;
  46330. +
  46331. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD URB Dequeue\n");
  46332. +
  46333. + dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  46334. +
  46335. +#ifdef DEBUG
  46336. + if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
  46337. + dump_urb_info(urb, "dwc_otg_urb_dequeue");
  46338. + }
  46339. +#endif
  46340. +
  46341. + DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &flags);
  46342. + rc = usb_hcd_check_unlink_urb(hcd, urb, status);
  46343. + if (0 == rc) {
  46344. + if(urb->hcpriv != NULL) {
  46345. + dwc_otg_hcd_urb_dequeue(dwc_otg_hcd,
  46346. + (dwc_otg_hcd_urb_t *)urb->hcpriv);
  46347. +
  46348. + DWC_FREE(urb->hcpriv);
  46349. + urb->hcpriv = NULL;
  46350. + }
  46351. + }
  46352. +
  46353. + if (0 == rc) {
  46354. + /* Higher layer software sets URB status. */
  46355. +#if USB_URB_EP_LINKING
  46356. + usb_hcd_unlink_urb_from_ep(hcd, urb);
  46357. +#endif
  46358. + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, flags);
  46359. +
  46360. +
  46361. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  46362. + usb_hcd_giveback_urb(hcd, urb);
  46363. +#else
  46364. + usb_hcd_giveback_urb(hcd, urb, status);
  46365. +#endif
  46366. + if (CHK_DEBUG_LEVEL(DBG_HCDV | DBG_HCD_URB)) {
  46367. + DWC_PRINTF("Called usb_hcd_giveback_urb() \n");
  46368. + DWC_PRINTF(" 1urb->status = %d\n", urb->status);
  46369. + }
  46370. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD URB Dequeue OK\n");
  46371. + } else {
  46372. + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, flags);
  46373. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD URB Dequeue failed - rc %d\n",
  46374. + rc);
  46375. + }
  46376. +
  46377. + return rc;
  46378. +}
  46379. +
  46380. +/* Frees resources in the DWC_otg controller related to a given endpoint. Also
  46381. + * clears state in the HCD related to the endpoint. Any URBs for the endpoint
  46382. + * must already be dequeued. */
  46383. +static void endpoint_disable(struct usb_hcd *hcd, struct usb_host_endpoint *ep)
  46384. +{
  46385. + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  46386. +
  46387. + DWC_DEBUGPL(DBG_HCD,
  46388. + "DWC OTG HCD EP DISABLE: _bEndpointAddress=0x%02x, "
  46389. + "endpoint=%d\n", ep->desc.bEndpointAddress,
  46390. + dwc_ep_addr_to_endpoint(ep->desc.bEndpointAddress));
  46391. + dwc_otg_hcd_endpoint_disable(dwc_otg_hcd, ep->hcpriv, 250);
  46392. + ep->hcpriv = NULL;
  46393. +}
  46394. +
  46395. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30)
  46396. +/* Resets endpoint specific parameter values, in current version used to reset
  46397. + * the data toggle(as a WA). This function can be called from usb_clear_halt routine */
  46398. +static void endpoint_reset(struct usb_hcd *hcd, struct usb_host_endpoint *ep)
  46399. +{
  46400. + dwc_irqflags_t flags;
  46401. + struct usb_device *udev = NULL;
  46402. + int epnum = usb_endpoint_num(&ep->desc);
  46403. + int is_out = usb_endpoint_dir_out(&ep->desc);
  46404. + int is_control = usb_endpoint_xfer_control(&ep->desc);
  46405. + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  46406. + struct device *dev = DWC_OTG_OS_GETDEV(dwc_otg_hcd->otg_dev->os_dep);
  46407. +
  46408. + if (dev)
  46409. + udev = to_usb_device(dev);
  46410. + else
  46411. + return;
  46412. +
  46413. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD EP RESET: Endpoint Num=0x%02d\n", epnum);
  46414. +
  46415. + DWC_SPINLOCK_IRQSAVE(dwc_otg_hcd->lock, &flags);
  46416. + usb_settoggle(udev, epnum, is_out, 0);
  46417. + if (is_control)
  46418. + usb_settoggle(udev, epnum, !is_out, 0);
  46419. +
  46420. + if (ep->hcpriv) {
  46421. + dwc_otg_hcd_endpoint_reset(dwc_otg_hcd, ep->hcpriv);
  46422. + }
  46423. + DWC_SPINUNLOCK_IRQRESTORE(dwc_otg_hcd->lock, flags);
  46424. +}
  46425. +#endif
  46426. +
  46427. +/** Handles host mode interrupts for the DWC_otg controller. Returns IRQ_NONE if
  46428. + * there was no interrupt to handle. Returns IRQ_HANDLED if there was a valid
  46429. + * interrupt.
  46430. + *
  46431. + * This function is called by the USB core when an interrupt occurs */
  46432. +static irqreturn_t dwc_otg_hcd_irq(struct usb_hcd *hcd)
  46433. +{
  46434. + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  46435. + int32_t retval = dwc_otg_hcd_handle_intr(dwc_otg_hcd);
  46436. + if (retval != 0) {
  46437. + S3C2410X_CLEAR_EINTPEND();
  46438. + }
  46439. + return IRQ_RETVAL(retval);
  46440. +}
  46441. +
  46442. +/** Creates Status Change bitmap for the root hub and root port. The bitmap is
  46443. + * returned in buf. Bit 0 is the status change indicator for the root hub. Bit 1
  46444. + * is the status change indicator for the single root port. Returns 1 if either
  46445. + * change indicator is 1, otherwise returns 0. */
  46446. +int hub_status_data(struct usb_hcd *hcd, char *buf)
  46447. +{
  46448. + dwc_otg_hcd_t *dwc_otg_hcd = hcd_to_dwc_otg_hcd(hcd);
  46449. +
  46450. + buf[0] = 0;
  46451. + buf[0] |= (dwc_otg_hcd_is_status_changed(dwc_otg_hcd, 1)) << 1;
  46452. +
  46453. + return (buf[0] != 0);
  46454. +}
  46455. +
  46456. +/** Handles hub class-specific requests. */
  46457. +int hub_control(struct usb_hcd *hcd,
  46458. + u16 typeReq, u16 wValue, u16 wIndex, char *buf, u16 wLength)
  46459. +{
  46460. + int retval;
  46461. +
  46462. + retval = dwc_otg_hcd_hub_control(hcd_to_dwc_otg_hcd(hcd),
  46463. + typeReq, wValue, wIndex, buf, wLength);
  46464. +
  46465. + switch (retval) {
  46466. + case -DWC_E_INVALID:
  46467. + retval = -EINVAL;
  46468. + break;
  46469. + }
  46470. +
  46471. + return retval;
  46472. +}
  46473. +
  46474. +#endif /* DWC_DEVICE_ONLY */
  46475. --- /dev/null
  46476. +++ b/drivers/usb/host/dwc_otg/dwc_otg_hcd_queue.c
  46477. @@ -0,0 +1,963 @@
  46478. +/* ==========================================================================
  46479. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_hcd_queue.c $
  46480. + * $Revision: #44 $
  46481. + * $Date: 2011/10/26 $
  46482. + * $Change: 1873028 $
  46483. + *
  46484. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  46485. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  46486. + * otherwise expressly agreed to in writing between Synopsys and you.
  46487. + *
  46488. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  46489. + * any End User Software License Agreement or Agreement for Licensed Product
  46490. + * with Synopsys or any supplement thereto. You are permitted to use and
  46491. + * redistribute this Software in source and binary forms, with or without
  46492. + * modification, provided that redistributions of source code must retain this
  46493. + * notice. You may not view, use, disclose, copy or distribute this file or
  46494. + * any information contained herein except pursuant to this license grant from
  46495. + * Synopsys. If you do not agree with this notice, including the disclaimer
  46496. + * below, then you are not authorized to use the Software.
  46497. + *
  46498. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  46499. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  46500. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  46501. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  46502. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  46503. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  46504. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  46505. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  46506. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  46507. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  46508. + * DAMAGE.
  46509. + * ========================================================================== */
  46510. +#ifndef DWC_DEVICE_ONLY
  46511. +
  46512. +/**
  46513. + * @file
  46514. + *
  46515. + * This file contains the functions to manage Queue Heads and Queue
  46516. + * Transfer Descriptors.
  46517. + */
  46518. +
  46519. +#include "dwc_otg_hcd.h"
  46520. +#include "dwc_otg_regs.h"
  46521. +
  46522. +extern bool microframe_schedule;
  46523. +
  46524. +/**
  46525. + * Free each QTD in the QH's QTD-list then free the QH. QH should already be
  46526. + * removed from a list. QTD list should already be empty if called from URB
  46527. + * Dequeue.
  46528. + *
  46529. + * @param hcd HCD instance.
  46530. + * @param qh The QH to free.
  46531. + */
  46532. +void dwc_otg_hcd_qh_free(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  46533. +{
  46534. + dwc_otg_qtd_t *qtd, *qtd_tmp;
  46535. + dwc_irqflags_t flags;
  46536. + uint32_t buf_size = 0;
  46537. + uint8_t *align_buf_virt = NULL;
  46538. + dwc_dma_t align_buf_dma;
  46539. + struct device *dev = dwc_otg_hcd_to_dev(hcd);
  46540. +
  46541. + /* Free each QTD in the QTD list */
  46542. + DWC_SPINLOCK_IRQSAVE(hcd->lock, &flags);
  46543. + DWC_CIRCLEQ_FOREACH_SAFE(qtd, qtd_tmp, &qh->qtd_list, qtd_list_entry) {
  46544. + DWC_CIRCLEQ_REMOVE(&qh->qtd_list, qtd, qtd_list_entry);
  46545. + dwc_otg_hcd_qtd_free(qtd);
  46546. + }
  46547. +
  46548. + if (hcd->core_if->dma_desc_enable) {
  46549. + dwc_otg_hcd_qh_free_ddma(hcd, qh);
  46550. + } else if (qh->dw_align_buf) {
  46551. + if (qh->ep_type == UE_ISOCHRONOUS) {
  46552. + buf_size = 4096;
  46553. + } else {
  46554. + buf_size = hcd->core_if->core_params->max_transfer_size;
  46555. + }
  46556. + align_buf_virt = qh->dw_align_buf;
  46557. + align_buf_dma = qh->dw_align_buf_dma;
  46558. + }
  46559. +
  46560. + DWC_FREE(qh);
  46561. + DWC_SPINUNLOCK_IRQRESTORE(hcd->lock, flags);
  46562. + if (align_buf_virt)
  46563. + DWC_DMA_FREE(dev, buf_size, align_buf_virt, align_buf_dma);
  46564. + return;
  46565. +}
  46566. +
  46567. +#define BitStuffTime(bytecount) ((8 * 7* bytecount) / 6)
  46568. +#define HS_HOST_DELAY 5 /* nanoseconds */
  46569. +#define FS_LS_HOST_DELAY 1000 /* nanoseconds */
  46570. +#define HUB_LS_SETUP 333 /* nanoseconds */
  46571. +#define NS_TO_US(ns) ((ns + 500) / 1000)
  46572. + /* convert & round nanoseconds to microseconds */
  46573. +
  46574. +static uint32_t calc_bus_time(int speed, int is_in, int is_isoc, int bytecount)
  46575. +{
  46576. + unsigned long retval;
  46577. +
  46578. + switch (speed) {
  46579. + case USB_SPEED_HIGH:
  46580. + if (is_isoc) {
  46581. + retval =
  46582. + ((38 * 8 * 2083) +
  46583. + (2083 * (3 + BitStuffTime(bytecount)))) / 1000 +
  46584. + HS_HOST_DELAY;
  46585. + } else {
  46586. + retval =
  46587. + ((55 * 8 * 2083) +
  46588. + (2083 * (3 + BitStuffTime(bytecount)))) / 1000 +
  46589. + HS_HOST_DELAY;
  46590. + }
  46591. + break;
  46592. + case USB_SPEED_FULL:
  46593. + if (is_isoc) {
  46594. + retval =
  46595. + (8354 * (31 + 10 * BitStuffTime(bytecount))) / 1000;
  46596. + if (is_in) {
  46597. + retval = 7268 + FS_LS_HOST_DELAY + retval;
  46598. + } else {
  46599. + retval = 6265 + FS_LS_HOST_DELAY + retval;
  46600. + }
  46601. + } else {
  46602. + retval =
  46603. + (8354 * (31 + 10 * BitStuffTime(bytecount))) / 1000;
  46604. + retval = 9107 + FS_LS_HOST_DELAY + retval;
  46605. + }
  46606. + break;
  46607. + case USB_SPEED_LOW:
  46608. + if (is_in) {
  46609. + retval =
  46610. + (67667 * (31 + 10 * BitStuffTime(bytecount))) /
  46611. + 1000;
  46612. + retval =
  46613. + 64060 + (2 * HUB_LS_SETUP) + FS_LS_HOST_DELAY +
  46614. + retval;
  46615. + } else {
  46616. + retval =
  46617. + (66700 * (31 + 10 * BitStuffTime(bytecount))) /
  46618. + 1000;
  46619. + retval =
  46620. + 64107 + (2 * HUB_LS_SETUP) + FS_LS_HOST_DELAY +
  46621. + retval;
  46622. + }
  46623. + break;
  46624. + default:
  46625. + DWC_WARN("Unknown device speed\n");
  46626. + retval = -1;
  46627. + }
  46628. +
  46629. + return NS_TO_US(retval);
  46630. +}
  46631. +
  46632. +/**
  46633. + * Initializes a QH structure.
  46634. + *
  46635. + * @param hcd The HCD state structure for the DWC OTG controller.
  46636. + * @param qh The QH to init.
  46637. + * @param urb Holds the information about the device/endpoint that we need
  46638. + * to initialize the QH.
  46639. + */
  46640. +#define SCHEDULE_SLOP 10
  46641. +void qh_init(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh, dwc_otg_hcd_urb_t * urb)
  46642. +{
  46643. + char *speed, *type;
  46644. + int dev_speed;
  46645. + uint32_t hub_addr, hub_port;
  46646. +
  46647. + dwc_memset(qh, 0, sizeof(dwc_otg_qh_t));
  46648. +
  46649. + /* Initialize QH */
  46650. + qh->ep_type = dwc_otg_hcd_get_pipe_type(&urb->pipe_info);
  46651. + qh->ep_is_in = dwc_otg_hcd_is_pipe_in(&urb->pipe_info) ? 1 : 0;
  46652. +
  46653. + qh->data_toggle = DWC_OTG_HC_PID_DATA0;
  46654. + qh->maxp = dwc_otg_hcd_get_mps(&urb->pipe_info);
  46655. + DWC_CIRCLEQ_INIT(&qh->qtd_list);
  46656. + DWC_LIST_INIT(&qh->qh_list_entry);
  46657. + qh->channel = NULL;
  46658. +
  46659. + /* FS/LS Enpoint on HS Hub
  46660. + * NOT virtual root hub */
  46661. + dev_speed = hcd->fops->speed(hcd, urb->priv);
  46662. +
  46663. + hcd->fops->hub_info(hcd, urb->priv, &hub_addr, &hub_port);
  46664. + qh->do_split = 0;
  46665. + if (microframe_schedule)
  46666. + qh->speed = dev_speed;
  46667. +
  46668. + qh->nak_frame = 0xffff;
  46669. +
  46670. + if (((dev_speed == USB_SPEED_LOW) ||
  46671. + (dev_speed == USB_SPEED_FULL)) &&
  46672. + (hub_addr != 0 && hub_addr != 1)) {
  46673. + DWC_DEBUGPL(DBG_HCD,
  46674. + "QH init: EP %d: TT found at hub addr %d, for port %d\n",
  46675. + dwc_otg_hcd_get_ep_num(&urb->pipe_info), hub_addr,
  46676. + hub_port);
  46677. + qh->do_split = 1;
  46678. + qh->skip_count = 0;
  46679. + }
  46680. +
  46681. + if (qh->ep_type == UE_INTERRUPT || qh->ep_type == UE_ISOCHRONOUS) {
  46682. + /* Compute scheduling parameters once and save them. */
  46683. + hprt0_data_t hprt;
  46684. +
  46685. + /** @todo Account for split transfers in the bus time. */
  46686. + int bytecount =
  46687. + dwc_hb_mult(qh->maxp) * dwc_max_packet(qh->maxp);
  46688. +
  46689. + qh->usecs =
  46690. + calc_bus_time((qh->do_split ? USB_SPEED_HIGH : dev_speed),
  46691. + qh->ep_is_in, (qh->ep_type == UE_ISOCHRONOUS),
  46692. + bytecount);
  46693. + /* Start in a slightly future (micro)frame. */
  46694. + qh->sched_frame = dwc_frame_num_inc(hcd->frame_number,
  46695. + SCHEDULE_SLOP);
  46696. + qh->interval = urb->interval;
  46697. +
  46698. +#if 0
  46699. + /* Increase interrupt polling rate for debugging. */
  46700. + if (qh->ep_type == UE_INTERRUPT) {
  46701. + qh->interval = 8;
  46702. + }
  46703. +#endif
  46704. + hprt.d32 = DWC_READ_REG32(hcd->core_if->host_if->hprt0);
  46705. + if ((hprt.b.prtspd == DWC_HPRT0_PRTSPD_HIGH_SPEED) &&
  46706. + ((dev_speed == USB_SPEED_LOW) ||
  46707. + (dev_speed == USB_SPEED_FULL))) {
  46708. + qh->interval *= 8;
  46709. + qh->sched_frame |= 0x7;
  46710. + qh->start_split_frame = qh->sched_frame;
  46711. + }
  46712. +
  46713. + }
  46714. +
  46715. + DWC_DEBUGPL(DBG_HCD, "DWC OTG HCD QH Initialized\n");
  46716. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - qh = %p\n", qh);
  46717. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - Device Address = %d\n",
  46718. + dwc_otg_hcd_get_dev_addr(&urb->pipe_info));
  46719. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - Endpoint %d, %s\n",
  46720. + dwc_otg_hcd_get_ep_num(&urb->pipe_info),
  46721. + dwc_otg_hcd_is_pipe_in(&urb->pipe_info) ? "IN" : "OUT");
  46722. + switch (dev_speed) {
  46723. + case USB_SPEED_LOW:
  46724. + qh->dev_speed = DWC_OTG_EP_SPEED_LOW;
  46725. + speed = "low";
  46726. + break;
  46727. + case USB_SPEED_FULL:
  46728. + qh->dev_speed = DWC_OTG_EP_SPEED_FULL;
  46729. + speed = "full";
  46730. + break;
  46731. + case USB_SPEED_HIGH:
  46732. + qh->dev_speed = DWC_OTG_EP_SPEED_HIGH;
  46733. + speed = "high";
  46734. + break;
  46735. + default:
  46736. + speed = "?";
  46737. + break;
  46738. + }
  46739. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - Speed = %s\n", speed);
  46740. +
  46741. + switch (qh->ep_type) {
  46742. + case UE_ISOCHRONOUS:
  46743. + type = "isochronous";
  46744. + break;
  46745. + case UE_INTERRUPT:
  46746. + type = "interrupt";
  46747. + break;
  46748. + case UE_CONTROL:
  46749. + type = "control";
  46750. + break;
  46751. + case UE_BULK:
  46752. + type = "bulk";
  46753. + break;
  46754. + default:
  46755. + type = "?";
  46756. + break;
  46757. + }
  46758. +
  46759. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - Type = %s\n", type);
  46760. +
  46761. +#ifdef DEBUG
  46762. + if (qh->ep_type == UE_INTERRUPT) {
  46763. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - usecs = %d\n",
  46764. + qh->usecs);
  46765. + DWC_DEBUGPL(DBG_HCDV, "DWC OTG HCD QH - interval = %d\n",
  46766. + qh->interval);
  46767. + }
  46768. +#endif
  46769. +
  46770. +}
  46771. +
  46772. +/**
  46773. + * This function allocates and initializes a QH.
  46774. + *
  46775. + * @param hcd The HCD state structure for the DWC OTG controller.
  46776. + * @param urb Holds the information about the device/endpoint that we need
  46777. + * to initialize the QH.
  46778. + * @param atomic_alloc Flag to do atomic allocation if needed
  46779. + *
  46780. + * @return Returns pointer to the newly allocated QH, or NULL on error. */
  46781. +dwc_otg_qh_t *dwc_otg_hcd_qh_create(dwc_otg_hcd_t * hcd,
  46782. + dwc_otg_hcd_urb_t * urb, int atomic_alloc)
  46783. +{
  46784. + dwc_otg_qh_t *qh;
  46785. +
  46786. + /* Allocate memory */
  46787. + /** @todo add memflags argument */
  46788. + qh = dwc_otg_hcd_qh_alloc(atomic_alloc);
  46789. + if (qh == NULL) {
  46790. + DWC_ERROR("qh allocation failed");
  46791. + return NULL;
  46792. + }
  46793. +
  46794. + qh_init(hcd, qh, urb);
  46795. +
  46796. + if (hcd->core_if->dma_desc_enable
  46797. + && (dwc_otg_hcd_qh_init_ddma(hcd, qh) < 0)) {
  46798. + dwc_otg_hcd_qh_free(hcd, qh);
  46799. + return NULL;
  46800. + }
  46801. +
  46802. + return qh;
  46803. +}
  46804. +
  46805. +/* microframe_schedule=0 start */
  46806. +
  46807. +/**
  46808. + * Checks that a channel is available for a periodic transfer.
  46809. + *
  46810. + * @return 0 if successful, negative error code otherise.
  46811. + */
  46812. +static int periodic_channel_available(dwc_otg_hcd_t * hcd)
  46813. +{
  46814. + /*
  46815. + * Currently assuming that there is a dedicated host channnel for each
  46816. + * periodic transaction plus at least one host channel for
  46817. + * non-periodic transactions.
  46818. + */
  46819. + int status;
  46820. + int num_channels;
  46821. +
  46822. + num_channels = hcd->core_if->core_params->host_channels;
  46823. + if ((hcd->periodic_channels + hcd->non_periodic_channels < num_channels)
  46824. + && (hcd->periodic_channels < num_channels - 1)) {
  46825. + status = 0;
  46826. + } else {
  46827. + DWC_INFO("%s: Total channels: %d, Periodic: %d, Non-periodic: %d\n",
  46828. + __func__, num_channels, hcd->periodic_channels, hcd->non_periodic_channels); //NOTICE
  46829. + status = -DWC_E_NO_SPACE;
  46830. + }
  46831. +
  46832. + return status;
  46833. +}
  46834. +
  46835. +/**
  46836. + * Checks that there is sufficient bandwidth for the specified QH in the
  46837. + * periodic schedule. For simplicity, this calculation assumes that all the
  46838. + * transfers in the periodic schedule may occur in the same (micro)frame.
  46839. + *
  46840. + * @param hcd The HCD state structure for the DWC OTG controller.
  46841. + * @param qh QH containing periodic bandwidth required.
  46842. + *
  46843. + * @return 0 if successful, negative error code otherwise.
  46844. + */
  46845. +static int check_periodic_bandwidth(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  46846. +{
  46847. + int status;
  46848. + int16_t max_claimed_usecs;
  46849. +
  46850. + status = 0;
  46851. +
  46852. + if ((qh->dev_speed == DWC_OTG_EP_SPEED_HIGH) || qh->do_split) {
  46853. + /*
  46854. + * High speed mode.
  46855. + * Max periodic usecs is 80% x 125 usec = 100 usec.
  46856. + */
  46857. +
  46858. + max_claimed_usecs = 100 - qh->usecs;
  46859. + } else {
  46860. + /*
  46861. + * Full speed mode.
  46862. + * Max periodic usecs is 90% x 1000 usec = 900 usec.
  46863. + */
  46864. + max_claimed_usecs = 900 - qh->usecs;
  46865. + }
  46866. +
  46867. + if (hcd->periodic_usecs > max_claimed_usecs) {
  46868. + DWC_INFO("%s: already claimed usecs %d, required usecs %d\n", __func__, hcd->periodic_usecs, qh->usecs); //NOTICE
  46869. + status = -DWC_E_NO_SPACE;
  46870. + }
  46871. +
  46872. + return status;
  46873. +}
  46874. +
  46875. +/* microframe_schedule=0 end */
  46876. +
  46877. +/**
  46878. + * Microframe scheduler
  46879. + * track the total use in hcd->frame_usecs
  46880. + * keep each qh use in qh->frame_usecs
  46881. + * when surrendering the qh then donate the time back
  46882. + */
  46883. +const unsigned short max_uframe_usecs[]={ 100, 100, 100, 100, 100, 100, 30, 0 };
  46884. +
  46885. +/*
  46886. + * called from dwc_otg_hcd.c:dwc_otg_hcd_init
  46887. + */
  46888. +int init_hcd_usecs(dwc_otg_hcd_t *_hcd)
  46889. +{
  46890. + int i;
  46891. + for (i=0; i<8; i++) {
  46892. + _hcd->frame_usecs[i] = max_uframe_usecs[i];
  46893. + }
  46894. + return 0;
  46895. +}
  46896. +
  46897. +static int find_single_uframe(dwc_otg_hcd_t * _hcd, dwc_otg_qh_t * _qh)
  46898. +{
  46899. + int i;
  46900. + unsigned short utime;
  46901. + int t_left;
  46902. + int ret;
  46903. + int done;
  46904. +
  46905. + ret = -1;
  46906. + utime = _qh->usecs;
  46907. + t_left = utime;
  46908. + i = 0;
  46909. + done = 0;
  46910. + while (done == 0) {
  46911. + /* At the start _hcd->frame_usecs[i] = max_uframe_usecs[i]; */
  46912. + if (utime <= _hcd->frame_usecs[i]) {
  46913. + _hcd->frame_usecs[i] -= utime;
  46914. + _qh->frame_usecs[i] += utime;
  46915. + t_left -= utime;
  46916. + ret = i;
  46917. + done = 1;
  46918. + return ret;
  46919. + } else {
  46920. + i++;
  46921. + if (i == 8) {
  46922. + done = 1;
  46923. + ret = -1;
  46924. + }
  46925. + }
  46926. + }
  46927. + return ret;
  46928. + }
  46929. +
  46930. +/*
  46931. + * use this for FS apps that can span multiple uframes
  46932. + */
  46933. +static int find_multi_uframe(dwc_otg_hcd_t * _hcd, dwc_otg_qh_t * _qh)
  46934. +{
  46935. + int i;
  46936. + int j;
  46937. + unsigned short utime;
  46938. + int t_left;
  46939. + int ret;
  46940. + int done;
  46941. + unsigned short xtime;
  46942. +
  46943. + ret = -1;
  46944. + utime = _qh->usecs;
  46945. + t_left = utime;
  46946. + i = 0;
  46947. + done = 0;
  46948. +loop:
  46949. + while (done == 0) {
  46950. + if(_hcd->frame_usecs[i] <= 0) {
  46951. + i++;
  46952. + if (i == 8) {
  46953. + done = 1;
  46954. + ret = -1;
  46955. + }
  46956. + goto loop;
  46957. + }
  46958. +
  46959. + /*
  46960. + * we need n consecutive slots
  46961. + * so use j as a start slot j plus j+1 must be enough time (for now)
  46962. + */
  46963. + xtime= _hcd->frame_usecs[i];
  46964. + for (j = i+1 ; j < 8 ; j++ ) {
  46965. + /*
  46966. + * if we add this frame remaining time to xtime we may
  46967. + * be OK, if not we need to test j for a complete frame
  46968. + */
  46969. + if ((xtime+_hcd->frame_usecs[j]) < utime) {
  46970. + if (_hcd->frame_usecs[j] < max_uframe_usecs[j]) {
  46971. + j = 8;
  46972. + ret = -1;
  46973. + continue;
  46974. + }
  46975. + }
  46976. + if (xtime >= utime) {
  46977. + ret = i;
  46978. + j = 8; /* stop loop with a good value ret */
  46979. + continue;
  46980. + }
  46981. + /* add the frame time to x time */
  46982. + xtime += _hcd->frame_usecs[j];
  46983. + /* we must have a fully available next frame or break */
  46984. + if ((xtime < utime)
  46985. + && (_hcd->frame_usecs[j] == max_uframe_usecs[j])) {
  46986. + ret = -1;
  46987. + j = 8; /* stop loop with a bad value ret */
  46988. + continue;
  46989. + }
  46990. + }
  46991. + if (ret >= 0) {
  46992. + t_left = utime;
  46993. + for (j = i; (t_left>0) && (j < 8); j++ ) {
  46994. + t_left -= _hcd->frame_usecs[j];
  46995. + if ( t_left <= 0 ) {
  46996. + _qh->frame_usecs[j] += _hcd->frame_usecs[j] + t_left;
  46997. + _hcd->frame_usecs[j]= -t_left;
  46998. + ret = i;
  46999. + done = 1;
  47000. + } else {
  47001. + _qh->frame_usecs[j] += _hcd->frame_usecs[j];
  47002. + _hcd->frame_usecs[j] = 0;
  47003. + }
  47004. + }
  47005. + } else {
  47006. + i++;
  47007. + if (i == 8) {
  47008. + done = 1;
  47009. + ret = -1;
  47010. + }
  47011. + }
  47012. + }
  47013. + return ret;
  47014. +}
  47015. +
  47016. +static int find_uframe(dwc_otg_hcd_t * _hcd, dwc_otg_qh_t * _qh)
  47017. +{
  47018. + int ret;
  47019. + ret = -1;
  47020. +
  47021. + if (_qh->speed == USB_SPEED_HIGH) {
  47022. + /* if this is a hs transaction we need a full frame */
  47023. + ret = find_single_uframe(_hcd, _qh);
  47024. + } else {
  47025. + /* if this is a fs transaction we may need a sequence of frames */
  47026. + ret = find_multi_uframe(_hcd, _qh);
  47027. + }
  47028. + return ret;
  47029. +}
  47030. +
  47031. +/**
  47032. + * Checks that the max transfer size allowed in a host channel is large enough
  47033. + * to handle the maximum data transfer in a single (micro)frame for a periodic
  47034. + * transfer.
  47035. + *
  47036. + * @param hcd The HCD state structure for the DWC OTG controller.
  47037. + * @param qh QH for a periodic endpoint.
  47038. + *
  47039. + * @return 0 if successful, negative error code otherwise.
  47040. + */
  47041. +static int check_max_xfer_size(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  47042. +{
  47043. + int status;
  47044. + uint32_t max_xfer_size;
  47045. + uint32_t max_channel_xfer_size;
  47046. +
  47047. + status = 0;
  47048. +
  47049. + max_xfer_size = dwc_max_packet(qh->maxp) * dwc_hb_mult(qh->maxp);
  47050. + max_channel_xfer_size = hcd->core_if->core_params->max_transfer_size;
  47051. +
  47052. + if (max_xfer_size > max_channel_xfer_size) {
  47053. + DWC_INFO("%s: Periodic xfer length %d > " "max xfer length for channel %d\n",
  47054. + __func__, max_xfer_size, max_channel_xfer_size); //NOTICE
  47055. + status = -DWC_E_NO_SPACE;
  47056. + }
  47057. +
  47058. + return status;
  47059. +}
  47060. +
  47061. +
  47062. +
  47063. +/**
  47064. + * Schedules an interrupt or isochronous transfer in the periodic schedule.
  47065. + *
  47066. + * @param hcd The HCD state structure for the DWC OTG controller.
  47067. + * @param qh QH for the periodic transfer. The QH should already contain the
  47068. + * scheduling information.
  47069. + *
  47070. + * @return 0 if successful, negative error code otherwise.
  47071. + */
  47072. +static int schedule_periodic(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  47073. +{
  47074. + int status = 0;
  47075. +
  47076. + if (microframe_schedule) {
  47077. + int frame;
  47078. + status = find_uframe(hcd, qh);
  47079. + frame = -1;
  47080. + if (status == 0) {
  47081. + frame = 7;
  47082. + } else {
  47083. + if (status > 0 )
  47084. + frame = status-1;
  47085. + }
  47086. +
  47087. + /* Set the new frame up */
  47088. + if (frame > -1) {
  47089. + qh->sched_frame &= ~0x7;
  47090. + qh->sched_frame |= (frame & 7);
  47091. + }
  47092. +
  47093. + if (status != -1)
  47094. + status = 0;
  47095. + } else {
  47096. + status = periodic_channel_available(hcd);
  47097. + if (status) {
  47098. + DWC_INFO("%s: No host channel available for periodic " "transfer.\n", __func__); //NOTICE
  47099. + return status;
  47100. + }
  47101. +
  47102. + status = check_periodic_bandwidth(hcd, qh);
  47103. + }
  47104. + if (status) {
  47105. + DWC_INFO("%s: Insufficient periodic bandwidth for "
  47106. + "periodic transfer.\n", __func__);
  47107. + return status;
  47108. + }
  47109. + status = check_max_xfer_size(hcd, qh);
  47110. + if (status) {
  47111. + DWC_INFO("%s: Channel max transfer size too small "
  47112. + "for periodic transfer.\n", __func__);
  47113. + return status;
  47114. + }
  47115. +
  47116. + if (hcd->core_if->dma_desc_enable) {
  47117. + /* Don't rely on SOF and start in ready schedule */
  47118. + DWC_LIST_INSERT_TAIL(&hcd->periodic_sched_ready, &qh->qh_list_entry);
  47119. + }
  47120. + else {
  47121. + if(fiq_enable && (DWC_LIST_EMPTY(&hcd->periodic_sched_inactive) || dwc_frame_num_le(qh->sched_frame, hcd->fiq_state->next_sched_frame)))
  47122. + {
  47123. + hcd->fiq_state->next_sched_frame = qh->sched_frame;
  47124. +
  47125. + }
  47126. + /* Always start in the inactive schedule. */
  47127. + DWC_LIST_INSERT_TAIL(&hcd->periodic_sched_inactive, &qh->qh_list_entry);
  47128. + }
  47129. +
  47130. + if (!microframe_schedule) {
  47131. + /* Reserve the periodic channel. */
  47132. + hcd->periodic_channels++;
  47133. + }
  47134. +
  47135. + /* Update claimed usecs per (micro)frame. */
  47136. + hcd->periodic_usecs += qh->usecs;
  47137. +
  47138. + return status;
  47139. +}
  47140. +
  47141. +
  47142. +/**
  47143. + * This function adds a QH to either the non periodic or periodic schedule if
  47144. + * it is not already in the schedule. If the QH is already in the schedule, no
  47145. + * action is taken.
  47146. + *
  47147. + * @return 0 if successful, negative error code otherwise.
  47148. + */
  47149. +int dwc_otg_hcd_qh_add(dwc_otg_hcd_t * hcd, dwc_otg_qh_t * qh)
  47150. +{
  47151. + int status = 0;
  47152. + gintmsk_data_t intr_mask = {.d32 = 0 };
  47153. +
  47154. + if (!DWC_LIST_EMPTY(&qh->qh_list_entry)) {
  47155. + /* QH already in a schedule. */
  47156. + return status;
  47157. + }
  47158. +
  47159. + /* Add the new QH to the appropriate schedule */
  47160. + if (dwc_qh_is_non_per(qh)) {
  47161. + /* Always start in the inactive schedule. */
  47162. + DWC_LIST_INSERT_TAIL(&hcd->non_periodic_sched_inactive,
  47163. + &qh->qh_list_entry);
  47164. + //hcd->fiq_state->kick_np_queues = 1;
  47165. + } else {
  47166. + status = schedule_periodic(hcd, qh);
  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. + }
  47274. + } else {
  47275. + uint16_t frame_number = dwc_otg_hcd_get_frame_number(hcd);
  47276. +
  47277. + if (qh->do_split) {
  47278. + /* Schedule the next continuing periodic split transfer */
  47279. + if (sched_next_periodic_split) {
  47280. +
  47281. + qh->sched_frame = frame_number;
  47282. +
  47283. + if (dwc_frame_num_le(frame_number,
  47284. + dwc_frame_num_inc
  47285. + (qh->start_split_frame,
  47286. + 1))) {
  47287. + /*
  47288. + * Allow one frame to elapse after start
  47289. + * split microframe before scheduling
  47290. + * complete split, but DONT if we are
  47291. + * doing the next start split in the
  47292. + * same frame for an ISOC out.
  47293. + */
  47294. + if ((qh->ep_type != UE_ISOCHRONOUS) ||
  47295. + (qh->ep_is_in != 0)) {
  47296. + qh->sched_frame =
  47297. + dwc_frame_num_inc(qh->sched_frame, 1);
  47298. + }
  47299. + }
  47300. + } else {
  47301. + qh->sched_frame =
  47302. + dwc_frame_num_inc(qh->start_split_frame,
  47303. + qh->interval);
  47304. + if (dwc_frame_num_le
  47305. + (qh->sched_frame, frame_number)) {
  47306. + qh->sched_frame = frame_number;
  47307. + }
  47308. + qh->sched_frame |= 0x7;
  47309. + qh->start_split_frame = qh->sched_frame;
  47310. + }
  47311. + } else {
  47312. + qh->sched_frame =
  47313. + dwc_frame_num_inc(qh->sched_frame, qh->interval);
  47314. + if (dwc_frame_num_le(qh->sched_frame, frame_number)) {
  47315. + qh->sched_frame = frame_number;
  47316. + }
  47317. + }
  47318. +
  47319. + if (DWC_CIRCLEQ_EMPTY(&qh->qtd_list)) {
  47320. + dwc_otg_hcd_qh_remove(hcd, qh);
  47321. + } else {
  47322. + /*
  47323. + * Remove from periodic_sched_queued and move to
  47324. + * appropriate queue.
  47325. + */
  47326. + if ((microframe_schedule && dwc_frame_num_le(qh->sched_frame, frame_number)) ||
  47327. + (!microframe_schedule && qh->sched_frame == frame_number)) {
  47328. + DWC_LIST_MOVE_HEAD(&hcd->periodic_sched_ready,
  47329. + &qh->qh_list_entry);
  47330. + } else {
  47331. + if(fiq_enable && !dwc_frame_num_le(hcd->fiq_state->next_sched_frame, qh->sched_frame))
  47332. + {
  47333. + hcd->fiq_state->next_sched_frame = qh->sched_frame;
  47334. + }
  47335. +
  47336. + DWC_LIST_MOVE_HEAD
  47337. + (&hcd->periodic_sched_inactive,
  47338. + &qh->qh_list_entry);
  47339. + }
  47340. + }
  47341. + }
  47342. +}
  47343. +
  47344. +/**
  47345. + * This function allocates and initializes a QTD.
  47346. + *
  47347. + * @param urb The URB to create a QTD from. Each URB-QTD pair will end up
  47348. + * pointing to each other so each pair should have a unique correlation.
  47349. + * @param atomic_alloc Flag to do atomic alloc if needed
  47350. + *
  47351. + * @return Returns pointer to the newly allocated QTD, or NULL on error. */
  47352. +dwc_otg_qtd_t *dwc_otg_hcd_qtd_create(dwc_otg_hcd_urb_t * urb, int atomic_alloc)
  47353. +{
  47354. + dwc_otg_qtd_t *qtd;
  47355. +
  47356. + qtd = dwc_otg_hcd_qtd_alloc(atomic_alloc);
  47357. + if (qtd == NULL) {
  47358. + return NULL;
  47359. + }
  47360. +
  47361. + dwc_otg_hcd_qtd_init(qtd, urb);
  47362. + return qtd;
  47363. +}
  47364. +
  47365. +/**
  47366. + * Initializes a QTD structure.
  47367. + *
  47368. + * @param qtd The QTD to initialize.
  47369. + * @param urb The URB to use for initialization. */
  47370. +void dwc_otg_hcd_qtd_init(dwc_otg_qtd_t * qtd, dwc_otg_hcd_urb_t * urb)
  47371. +{
  47372. + dwc_memset(qtd, 0, sizeof(dwc_otg_qtd_t));
  47373. + qtd->urb = urb;
  47374. + if (dwc_otg_hcd_get_pipe_type(&urb->pipe_info) == UE_CONTROL) {
  47375. + /*
  47376. + * The only time the QTD data toggle is used is on the data
  47377. + * phase of control transfers. This phase always starts with
  47378. + * DATA1.
  47379. + */
  47380. + qtd->data_toggle = DWC_OTG_HC_PID_DATA1;
  47381. + qtd->control_phase = DWC_OTG_CONTROL_SETUP;
  47382. + }
  47383. +
  47384. + /* start split */
  47385. + qtd->complete_split = 0;
  47386. + qtd->isoc_split_pos = DWC_HCSPLIT_XACTPOS_ALL;
  47387. + qtd->isoc_split_offset = 0;
  47388. + qtd->in_process = 0;
  47389. +
  47390. + /* Store the qtd ptr in the urb to reference what QTD. */
  47391. + urb->qtd = qtd;
  47392. + return;
  47393. +}
  47394. +
  47395. +/**
  47396. + * This function adds a QTD to the QTD-list of a QH. It will find the correct
  47397. + * QH to place the QTD into. If it does not find a QH, then it will create a
  47398. + * new QH. If the QH to which the QTD is added is not currently scheduled, it
  47399. + * is placed into the proper schedule based on its EP type.
  47400. + * HCD lock must be held and interrupts must be disabled on entry
  47401. + *
  47402. + * @param[in] qtd The QTD to add
  47403. + * @param[in] hcd The DWC HCD structure
  47404. + * @param[out] qh out parameter to return queue head
  47405. + * @param atomic_alloc Flag to do atomic alloc if needed
  47406. + *
  47407. + * @return 0 if successful, negative error code otherwise.
  47408. + */
  47409. +int dwc_otg_hcd_qtd_add(dwc_otg_qtd_t * qtd,
  47410. + dwc_otg_hcd_t * hcd, dwc_otg_qh_t ** qh, int atomic_alloc)
  47411. +{
  47412. + int retval = 0;
  47413. + dwc_otg_hcd_urb_t *urb = qtd->urb;
  47414. +
  47415. + /*
  47416. + * Get the QH which holds the QTD-list to insert to. Create QH if it
  47417. + * doesn't exist.
  47418. + */
  47419. + if (*qh == NULL) {
  47420. + *qh = dwc_otg_hcd_qh_create(hcd, urb, atomic_alloc);
  47421. + if (*qh == NULL) {
  47422. + retval = -DWC_E_NO_MEMORY;
  47423. + goto done;
  47424. + } else {
  47425. + if (fiq_enable)
  47426. + hcd->fiq_state->kick_np_queues = 1;
  47427. + }
  47428. + }
  47429. + retval = dwc_otg_hcd_qh_add(hcd, *qh);
  47430. + if (retval == 0) {
  47431. + DWC_CIRCLEQ_INSERT_TAIL(&((*qh)->qtd_list), qtd,
  47432. + qtd_list_entry);
  47433. + qtd->qh = *qh;
  47434. + }
  47435. +done:
  47436. +
  47437. + return retval;
  47438. +}
  47439. +
  47440. +#endif /* DWC_DEVICE_ONLY */
  47441. --- /dev/null
  47442. +++ b/drivers/usb/host/dwc_otg/dwc_otg_os_dep.h
  47443. @@ -0,0 +1,188 @@
  47444. +#ifndef _DWC_OS_DEP_H_
  47445. +#define _DWC_OS_DEP_H_
  47446. +
  47447. +/**
  47448. + * @file
  47449. + *
  47450. + * This file contains OS dependent structures.
  47451. + *
  47452. + */
  47453. +
  47454. +#include <linux/kernel.h>
  47455. +#include <linux/module.h>
  47456. +#include <linux/moduleparam.h>
  47457. +#include <linux/init.h>
  47458. +#include <linux/device.h>
  47459. +#include <linux/errno.h>
  47460. +#include <linux/types.h>
  47461. +#include <linux/slab.h>
  47462. +#include <linux/list.h>
  47463. +#include <linux/interrupt.h>
  47464. +#include <linux/ctype.h>
  47465. +#include <linux/string.h>
  47466. +#include <linux/dma-mapping.h>
  47467. +#include <linux/jiffies.h>
  47468. +#include <linux/delay.h>
  47469. +#include <linux/timer.h>
  47470. +#include <linux/workqueue.h>
  47471. +#include <linux/stat.h>
  47472. +#include <linux/pci.h>
  47473. +
  47474. +#include <linux/version.h>
  47475. +
  47476. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
  47477. +# include <linux/irq.h>
  47478. +#endif
  47479. +
  47480. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,21)
  47481. +# include <linux/usb/ch9.h>
  47482. +#else
  47483. +# include <linux/usb_ch9.h>
  47484. +#endif
  47485. +
  47486. +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
  47487. +# include <linux/usb/gadget.h>
  47488. +#else
  47489. +# include <linux/usb_gadget.h>
  47490. +#endif
  47491. +
  47492. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
  47493. +# include <asm/irq.h>
  47494. +#endif
  47495. +
  47496. +#ifdef PCI_INTERFACE
  47497. +# include <asm/io.h>
  47498. +#endif
  47499. +
  47500. +#ifdef LM_INTERFACE
  47501. +# include <asm/unaligned.h>
  47502. +# include <asm/sizes.h>
  47503. +# include <asm/param.h>
  47504. +# include <asm/io.h>
  47505. +# if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30))
  47506. +# include <asm/arch/hardware.h>
  47507. +# include <asm/arch/lm.h>
  47508. +# include <asm/arch/irqs.h>
  47509. +# include <asm/arch/regs-irq.h>
  47510. +# else
  47511. +/* in 2.6.31, at least, we seem to have lost the generic LM infrastructure -
  47512. + here we assume that the machine architecture provides definitions
  47513. + in its own header
  47514. +*/
  47515. +# include <mach/lm.h>
  47516. +# include <mach/hardware.h>
  47517. +# endif
  47518. +#endif
  47519. +
  47520. +#ifdef PLATFORM_INTERFACE
  47521. +#include <linux/platform_device.h>
  47522. +#include <asm/mach/map.h>
  47523. +#endif
  47524. +
  47525. +/** The OS page size */
  47526. +#define DWC_OS_PAGE_SIZE PAGE_SIZE
  47527. +
  47528. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,14)
  47529. +typedef int gfp_t;
  47530. +#endif
  47531. +
  47532. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18)
  47533. +# define IRQF_SHARED SA_SHIRQ
  47534. +#endif
  47535. +
  47536. +typedef struct os_dependent {
  47537. + /** Base address returned from ioremap() */
  47538. + void *base;
  47539. +
  47540. + /** Register offset for Diagnostic API */
  47541. + uint32_t reg_offset;
  47542. +
  47543. + /** Base address for MPHI peripheral */
  47544. + void *mphi_base;
  47545. +
  47546. +#ifdef LM_INTERFACE
  47547. + struct lm_device *lmdev;
  47548. +#elif defined(PCI_INTERFACE)
  47549. + struct pci_dev *pcidev;
  47550. +
  47551. + /** Start address of a PCI region */
  47552. + resource_size_t rsrc_start;
  47553. +
  47554. + /** Length address of a PCI region */
  47555. + resource_size_t rsrc_len;
  47556. +#elif defined(PLATFORM_INTERFACE)
  47557. + struct platform_device *platformdev;
  47558. +#endif
  47559. +
  47560. +} os_dependent_t;
  47561. +
  47562. +#ifdef __cplusplus
  47563. +}
  47564. +#endif
  47565. +
  47566. +
  47567. +
  47568. +/* Type for the our device on the chosen bus */
  47569. +#if defined(LM_INTERFACE)
  47570. +typedef struct lm_device dwc_bus_dev_t;
  47571. +#elif defined(PCI_INTERFACE)
  47572. +typedef struct pci_dev dwc_bus_dev_t;
  47573. +#elif defined(PLATFORM_INTERFACE)
  47574. +typedef struct platform_device dwc_bus_dev_t;
  47575. +#endif
  47576. +
  47577. +/* Helper macro to retrieve drvdata from the device on the chosen bus */
  47578. +#if defined(LM_INTERFACE)
  47579. +#define DWC_OTG_BUSDRVDATA(_dev) lm_get_drvdata(_dev)
  47580. +#elif defined(PCI_INTERFACE)
  47581. +#define DWC_OTG_BUSDRVDATA(_dev) pci_get_drvdata(_dev)
  47582. +#elif defined(PLATFORM_INTERFACE)
  47583. +#define DWC_OTG_BUSDRVDATA(_dev) platform_get_drvdata(_dev)
  47584. +#endif
  47585. +
  47586. +/**
  47587. + * Helper macro returning the otg_device structure of a given struct device
  47588. + *
  47589. + * c.f. static dwc_otg_device_t *dwc_otg_drvdev(struct device *_dev)
  47590. + */
  47591. +#ifdef LM_INTERFACE
  47592. +#define DWC_OTG_GETDRVDEV(_var, _dev) do { \
  47593. + struct lm_device *lm_dev = \
  47594. + container_of(_dev, struct lm_device, dev); \
  47595. + _var = lm_get_drvdata(lm_dev); \
  47596. + } while (0)
  47597. +
  47598. +#elif defined(PCI_INTERFACE)
  47599. +#define DWC_OTG_GETDRVDEV(_var, _dev) do { \
  47600. + _var = dev_get_drvdata(_dev); \
  47601. + } while (0)
  47602. +
  47603. +#elif defined(PLATFORM_INTERFACE)
  47604. +#define DWC_OTG_GETDRVDEV(_var, _dev) do { \
  47605. + struct platform_device *platform_dev = \
  47606. + container_of(_dev, struct platform_device, dev); \
  47607. + _var = platform_get_drvdata(platform_dev); \
  47608. + } while (0)
  47609. +#endif
  47610. +
  47611. +
  47612. +/**
  47613. + * Helper macro returning the struct dev of the given struct os_dependent
  47614. + *
  47615. + * c.f. static struct device *dwc_otg_getdev(struct os_dependent *osdep)
  47616. + */
  47617. +#ifdef LM_INTERFACE
  47618. +#define DWC_OTG_OS_GETDEV(_osdep) \
  47619. + ((_osdep).lmdev == NULL? NULL: &(_osdep).lmdev->dev)
  47620. +#elif defined(PCI_INTERFACE)
  47621. +#define DWC_OTG_OS_GETDEV(_osdep) \
  47622. + ((_osdep).pci_dev == NULL? NULL: &(_osdep).pci_dev->dev)
  47623. +#elif defined(PLATFORM_INTERFACE)
  47624. +#define DWC_OTG_OS_GETDEV(_osdep) \
  47625. + ((_osdep).platformdev == NULL? NULL: &(_osdep).platformdev->dev)
  47626. +#endif
  47627. +
  47628. +
  47629. +
  47630. +
  47631. +#endif /* _DWC_OS_DEP_H_ */
  47632. --- /dev/null
  47633. +++ b/drivers/usb/host/dwc_otg/dwc_otg_pcd.c
  47634. @@ -0,0 +1,2725 @@
  47635. +/* ==========================================================================
  47636. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_pcd.c $
  47637. + * $Revision: #101 $
  47638. + * $Date: 2012/08/10 $
  47639. + * $Change: 2047372 $
  47640. + *
  47641. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  47642. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  47643. + * otherwise expressly agreed to in writing between Synopsys and you.
  47644. + *
  47645. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  47646. + * any End User Software License Agreement or Agreement for Licensed Product
  47647. + * with Synopsys or any supplement thereto. You are permitted to use and
  47648. + * redistribute this Software in source and binary forms, with or without
  47649. + * modification, provided that redistributions of source code must retain this
  47650. + * notice. You may not view, use, disclose, copy or distribute this file or
  47651. + * any information contained herein except pursuant to this license grant from
  47652. + * Synopsys. If you do not agree with this notice, including the disclaimer
  47653. + * below, then you are not authorized to use the Software.
  47654. + *
  47655. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  47656. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  47657. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  47658. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  47659. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  47660. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  47661. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  47662. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  47663. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  47664. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  47665. + * DAMAGE.
  47666. + * ========================================================================== */
  47667. +#ifndef DWC_HOST_ONLY
  47668. +
  47669. +/** @file
  47670. + * This file implements PCD Core. All code in this file is portable and doesn't
  47671. + * use any OS specific functions.
  47672. + * PCD Core provides Interface, defined in <code><dwc_otg_pcd_if.h></code>
  47673. + * header file, which can be used to implement OS specific PCD interface.
  47674. + *
  47675. + * An important function of the PCD is managing interrupts generated
  47676. + * by the DWC_otg controller. The implementation of the DWC_otg device
  47677. + * mode interrupt service routines is in dwc_otg_pcd_intr.c.
  47678. + *
  47679. + * @todo Add Device Mode test modes (Test J mode, Test K mode, etc).
  47680. + * @todo Does it work when the request size is greater than DEPTSIZ
  47681. + * transfer size
  47682. + *
  47683. + */
  47684. +
  47685. +#include "dwc_otg_pcd.h"
  47686. +
  47687. +#ifdef DWC_UTE_CFI
  47688. +#include "dwc_otg_cfi.h"
  47689. +
  47690. +extern int init_cfi(cfiobject_t * cfiobj);
  47691. +#endif
  47692. +
  47693. +/**
  47694. + * Choose endpoint from ep arrays using usb_ep structure.
  47695. + */
  47696. +static dwc_otg_pcd_ep_t *get_ep_from_handle(dwc_otg_pcd_t * pcd, void *handle)
  47697. +{
  47698. + int i;
  47699. + if (pcd->ep0.priv == handle) {
  47700. + return &pcd->ep0;
  47701. + }
  47702. + for (i = 0; i < MAX_EPS_CHANNELS - 1; i++) {
  47703. + if (pcd->in_ep[i].priv == handle)
  47704. + return &pcd->in_ep[i];
  47705. + if (pcd->out_ep[i].priv == handle)
  47706. + return &pcd->out_ep[i];
  47707. + }
  47708. +
  47709. + return NULL;
  47710. +}
  47711. +
  47712. +/**
  47713. + * This function completes a request. It call's the request call back.
  47714. + */
  47715. +void dwc_otg_request_done(dwc_otg_pcd_ep_t * ep, dwc_otg_pcd_request_t * req,
  47716. + int32_t status)
  47717. +{
  47718. + unsigned stopped = ep->stopped;
  47719. +
  47720. + DWC_DEBUGPL(DBG_PCDV, "%s(ep %p req %p)\n", __func__, ep, req);
  47721. + DWC_CIRCLEQ_REMOVE_INIT(&ep->queue, req, queue_entry);
  47722. +
  47723. + /* don't modify queue heads during completion callback */
  47724. + ep->stopped = 1;
  47725. + /* spin_unlock/spin_lock now done in fops->complete() */
  47726. + ep->pcd->fops->complete(ep->pcd, ep->priv, req->priv, status,
  47727. + req->actual);
  47728. +
  47729. + if (ep->pcd->request_pending > 0) {
  47730. + --ep->pcd->request_pending;
  47731. + }
  47732. +
  47733. + ep->stopped = stopped;
  47734. + DWC_FREE(req);
  47735. +}
  47736. +
  47737. +/**
  47738. + * This function terminates all the requsts in the EP request queue.
  47739. + */
  47740. +void dwc_otg_request_nuke(dwc_otg_pcd_ep_t * ep)
  47741. +{
  47742. + dwc_otg_pcd_request_t *req;
  47743. +
  47744. + ep->stopped = 1;
  47745. +
  47746. + /* called with irqs blocked?? */
  47747. + while (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  47748. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  47749. + dwc_otg_request_done(ep, req, -DWC_E_SHUTDOWN);
  47750. + }
  47751. +}
  47752. +
  47753. +void dwc_otg_pcd_start(dwc_otg_pcd_t * pcd,
  47754. + const struct dwc_otg_pcd_function_ops *fops)
  47755. +{
  47756. + pcd->fops = fops;
  47757. +}
  47758. +
  47759. +/**
  47760. + * PCD Callback function for initializing the PCD when switching to
  47761. + * device mode.
  47762. + *
  47763. + * @param p void pointer to the <code>dwc_otg_pcd_t</code>
  47764. + */
  47765. +static int32_t dwc_otg_pcd_start_cb(void *p)
  47766. +{
  47767. + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) p;
  47768. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  47769. +
  47770. + /*
  47771. + * Initialized the Core for Device mode.
  47772. + */
  47773. + if (dwc_otg_is_device_mode(core_if)) {
  47774. + dwc_otg_core_dev_init(core_if);
  47775. + /* Set core_if's lock pointer to the pcd->lock */
  47776. + core_if->lock = pcd->lock;
  47777. + }
  47778. + return 1;
  47779. +}
  47780. +
  47781. +/** CFI-specific buffer allocation function for EP */
  47782. +#ifdef DWC_UTE_CFI
  47783. +uint8_t *cfiw_ep_alloc_buffer(dwc_otg_pcd_t * pcd, void *pep, dwc_dma_t * addr,
  47784. + size_t buflen, int flags)
  47785. +{
  47786. + dwc_otg_pcd_ep_t *ep;
  47787. + ep = get_ep_from_handle(pcd, pep);
  47788. + if (!ep) {
  47789. + DWC_WARN("bad ep\n");
  47790. + return -DWC_E_INVALID;
  47791. + }
  47792. +
  47793. + return pcd->cfi->ops.ep_alloc_buf(pcd->cfi, pcd, ep, addr, buflen,
  47794. + flags);
  47795. +}
  47796. +#else
  47797. +uint8_t *cfiw_ep_alloc_buffer(dwc_otg_pcd_t * pcd, void *pep, dwc_dma_t * addr,
  47798. + size_t buflen, int flags);
  47799. +#endif
  47800. +
  47801. +/**
  47802. + * PCD Callback function for notifying the PCD when resuming from
  47803. + * suspend.
  47804. + *
  47805. + * @param p void pointer to the <code>dwc_otg_pcd_t</code>
  47806. + */
  47807. +static int32_t dwc_otg_pcd_resume_cb(void *p)
  47808. +{
  47809. + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) p;
  47810. +
  47811. + if (pcd->fops->resume) {
  47812. + pcd->fops->resume(pcd);
  47813. + }
  47814. +
  47815. + /* Stop the SRP timeout timer. */
  47816. + if ((GET_CORE_IF(pcd)->core_params->phy_type != DWC_PHY_TYPE_PARAM_FS)
  47817. + || (!GET_CORE_IF(pcd)->core_params->i2c_enable)) {
  47818. + if (GET_CORE_IF(pcd)->srp_timer_started) {
  47819. + GET_CORE_IF(pcd)->srp_timer_started = 0;
  47820. + DWC_TIMER_CANCEL(GET_CORE_IF(pcd)->srp_timer);
  47821. + }
  47822. + }
  47823. + return 1;
  47824. +}
  47825. +
  47826. +/**
  47827. + * PCD Callback function for notifying the PCD device is suspended.
  47828. + *
  47829. + * @param p void pointer to the <code>dwc_otg_pcd_t</code>
  47830. + */
  47831. +static int32_t dwc_otg_pcd_suspend_cb(void *p)
  47832. +{
  47833. + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) p;
  47834. +
  47835. + if (pcd->fops->suspend) {
  47836. + DWC_SPINUNLOCK(pcd->lock);
  47837. + pcd->fops->suspend(pcd);
  47838. + DWC_SPINLOCK(pcd->lock);
  47839. + }
  47840. +
  47841. + return 1;
  47842. +}
  47843. +
  47844. +/**
  47845. + * PCD Callback function for stopping the PCD when switching to Host
  47846. + * mode.
  47847. + *
  47848. + * @param p void pointer to the <code>dwc_otg_pcd_t</code>
  47849. + */
  47850. +static int32_t dwc_otg_pcd_stop_cb(void *p)
  47851. +{
  47852. + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) p;
  47853. + extern void dwc_otg_pcd_stop(dwc_otg_pcd_t * _pcd);
  47854. +
  47855. + dwc_otg_pcd_stop(pcd);
  47856. + return 1;
  47857. +}
  47858. +
  47859. +/**
  47860. + * PCD Callback structure for handling mode switching.
  47861. + */
  47862. +static dwc_otg_cil_callbacks_t pcd_callbacks = {
  47863. + .start = dwc_otg_pcd_start_cb,
  47864. + .stop = dwc_otg_pcd_stop_cb,
  47865. + .suspend = dwc_otg_pcd_suspend_cb,
  47866. + .resume_wakeup = dwc_otg_pcd_resume_cb,
  47867. + .p = 0, /* Set at registration */
  47868. +};
  47869. +
  47870. +/**
  47871. + * This function allocates a DMA Descriptor chain for the Endpoint
  47872. + * buffer to be used for a transfer to/from the specified endpoint.
  47873. + */
  47874. +dwc_otg_dev_dma_desc_t *dwc_otg_ep_alloc_desc_chain(struct device *dev,
  47875. + dwc_dma_t * dma_desc_addr,
  47876. + uint32_t count)
  47877. +{
  47878. + return DWC_DMA_ALLOC_ATOMIC(dev, count * sizeof(dwc_otg_dev_dma_desc_t),
  47879. + dma_desc_addr);
  47880. +}
  47881. +
  47882. +/**
  47883. + * This function frees a DMA Descriptor chain that was allocated by ep_alloc_desc.
  47884. + */
  47885. +void dwc_otg_ep_free_desc_chain(struct device *dev,
  47886. + dwc_otg_dev_dma_desc_t * desc_addr,
  47887. + uint32_t dma_desc_addr, uint32_t count)
  47888. +{
  47889. + DWC_DMA_FREE(dev, count * sizeof(dwc_otg_dev_dma_desc_t), desc_addr,
  47890. + dma_desc_addr);
  47891. +}
  47892. +
  47893. +#ifdef DWC_EN_ISOC
  47894. +
  47895. +/**
  47896. + * This function initializes a descriptor chain for Isochronous transfer
  47897. + *
  47898. + * @param core_if Programming view of DWC_otg controller.
  47899. + * @param dwc_ep The EP to start the transfer on.
  47900. + *
  47901. + */
  47902. +void dwc_otg_iso_ep_start_ddma_transfer(dwc_otg_core_if_t * core_if,
  47903. + dwc_ep_t * dwc_ep)
  47904. +{
  47905. +
  47906. + dsts_data_t dsts = {.d32 = 0 };
  47907. + depctl_data_t depctl = {.d32 = 0 };
  47908. + volatile uint32_t *addr;
  47909. + int i, j;
  47910. + uint32_t len;
  47911. +
  47912. + if (dwc_ep->is_in)
  47913. + dwc_ep->desc_cnt = dwc_ep->buf_proc_intrvl / dwc_ep->bInterval;
  47914. + else
  47915. + dwc_ep->desc_cnt =
  47916. + dwc_ep->buf_proc_intrvl * dwc_ep->pkt_per_frm /
  47917. + dwc_ep->bInterval;
  47918. +
  47919. + /** Allocate descriptors for double buffering */
  47920. + dwc_ep->iso_desc_addr =
  47921. + dwc_otg_ep_alloc_desc_chain(&dwc_ep->iso_dma_desc_addr,
  47922. + dwc_ep->desc_cnt * 2);
  47923. + if (dwc_ep->desc_addr) {
  47924. + DWC_WARN("%s, can't allocate DMA descriptor chain\n", __func__);
  47925. + return;
  47926. + }
  47927. +
  47928. + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  47929. +
  47930. + /** ISO OUT EP */
  47931. + if (dwc_ep->is_in == 0) {
  47932. + dev_dma_desc_sts_t sts = {.d32 = 0 };
  47933. + dwc_otg_dev_dma_desc_t *dma_desc = dwc_ep->iso_desc_addr;
  47934. + dma_addr_t dma_ad;
  47935. + uint32_t data_per_desc;
  47936. + dwc_otg_dev_out_ep_regs_t *out_regs =
  47937. + core_if->dev_if->out_ep_regs[dwc_ep->num];
  47938. + int offset;
  47939. +
  47940. + addr = &core_if->dev_if->out_ep_regs[dwc_ep->num]->doepctl;
  47941. + dma_ad = (dma_addr_t) DWC_READ_REG32(&(out_regs->doepdma));
  47942. +
  47943. + /** Buffer 0 descriptors setup */
  47944. + dma_ad = dwc_ep->dma_addr0;
  47945. +
  47946. + sts.b_iso_out.bs = BS_HOST_READY;
  47947. + sts.b_iso_out.rxsts = 0;
  47948. + sts.b_iso_out.l = 0;
  47949. + sts.b_iso_out.sp = 0;
  47950. + sts.b_iso_out.ioc = 0;
  47951. + sts.b_iso_out.pid = 0;
  47952. + sts.b_iso_out.framenum = 0;
  47953. +
  47954. + offset = 0;
  47955. + for (i = 0; i < dwc_ep->desc_cnt - dwc_ep->pkt_per_frm;
  47956. + i += dwc_ep->pkt_per_frm) {
  47957. +
  47958. + for (j = 0; j < dwc_ep->pkt_per_frm; ++j) {
  47959. + uint32_t len = (j + 1) * dwc_ep->maxpacket;
  47960. + if (len > dwc_ep->data_per_frame)
  47961. + data_per_desc =
  47962. + dwc_ep->data_per_frame -
  47963. + j * dwc_ep->maxpacket;
  47964. + else
  47965. + data_per_desc = dwc_ep->maxpacket;
  47966. + len = data_per_desc % 4;
  47967. + if (len)
  47968. + data_per_desc += 4 - len;
  47969. +
  47970. + sts.b_iso_out.rxbytes = data_per_desc;
  47971. + dma_desc->buf = dma_ad;
  47972. + dma_desc->status.d32 = sts.d32;
  47973. +
  47974. + offset += data_per_desc;
  47975. + dma_desc++;
  47976. + dma_ad += data_per_desc;
  47977. + }
  47978. + }
  47979. +
  47980. + for (j = 0; j < dwc_ep->pkt_per_frm - 1; ++j) {
  47981. + uint32_t len = (j + 1) * dwc_ep->maxpacket;
  47982. + if (len > dwc_ep->data_per_frame)
  47983. + data_per_desc =
  47984. + dwc_ep->data_per_frame -
  47985. + j * dwc_ep->maxpacket;
  47986. + else
  47987. + data_per_desc = dwc_ep->maxpacket;
  47988. + len = data_per_desc % 4;
  47989. + if (len)
  47990. + data_per_desc += 4 - len;
  47991. + sts.b_iso_out.rxbytes = data_per_desc;
  47992. + dma_desc->buf = dma_ad;
  47993. + dma_desc->status.d32 = sts.d32;
  47994. +
  47995. + offset += data_per_desc;
  47996. + dma_desc++;
  47997. + dma_ad += data_per_desc;
  47998. + }
  47999. +
  48000. + sts.b_iso_out.ioc = 1;
  48001. + len = (j + 1) * dwc_ep->maxpacket;
  48002. + if (len > dwc_ep->data_per_frame)
  48003. + data_per_desc =
  48004. + dwc_ep->data_per_frame - j * dwc_ep->maxpacket;
  48005. + else
  48006. + data_per_desc = dwc_ep->maxpacket;
  48007. + len = data_per_desc % 4;
  48008. + if (len)
  48009. + data_per_desc += 4 - len;
  48010. + sts.b_iso_out.rxbytes = data_per_desc;
  48011. +
  48012. + dma_desc->buf = dma_ad;
  48013. + dma_desc->status.d32 = sts.d32;
  48014. + dma_desc++;
  48015. +
  48016. + /** Buffer 1 descriptors setup */
  48017. + sts.b_iso_out.ioc = 0;
  48018. + dma_ad = dwc_ep->dma_addr1;
  48019. +
  48020. + offset = 0;
  48021. + for (i = 0; i < dwc_ep->desc_cnt - dwc_ep->pkt_per_frm;
  48022. + i += dwc_ep->pkt_per_frm) {
  48023. + for (j = 0; j < dwc_ep->pkt_per_frm; ++j) {
  48024. + uint32_t len = (j + 1) * dwc_ep->maxpacket;
  48025. + if (len > dwc_ep->data_per_frame)
  48026. + data_per_desc =
  48027. + dwc_ep->data_per_frame -
  48028. + j * dwc_ep->maxpacket;
  48029. + else
  48030. + data_per_desc = dwc_ep->maxpacket;
  48031. + len = data_per_desc % 4;
  48032. + if (len)
  48033. + data_per_desc += 4 - len;
  48034. +
  48035. + data_per_desc =
  48036. + sts.b_iso_out.rxbytes = data_per_desc;
  48037. + dma_desc->buf = dma_ad;
  48038. + dma_desc->status.d32 = sts.d32;
  48039. +
  48040. + offset += data_per_desc;
  48041. + dma_desc++;
  48042. + dma_ad += data_per_desc;
  48043. + }
  48044. + }
  48045. + for (j = 0; j < dwc_ep->pkt_per_frm - 1; ++j) {
  48046. + data_per_desc =
  48047. + ((j + 1) * dwc_ep->maxpacket >
  48048. + dwc_ep->data_per_frame) ? dwc_ep->data_per_frame -
  48049. + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
  48050. + data_per_desc +=
  48051. + (data_per_desc % 4) ? (4 - data_per_desc % 4) : 0;
  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. + sts.b_iso_out.ioc = 1;
  48062. + sts.b_iso_out.l = 1;
  48063. + data_per_desc =
  48064. + ((j + 1) * dwc_ep->maxpacket >
  48065. + dwc_ep->data_per_frame) ? dwc_ep->data_per_frame -
  48066. + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
  48067. + data_per_desc +=
  48068. + (data_per_desc % 4) ? (4 - data_per_desc % 4) : 0;
  48069. + sts.b_iso_out.rxbytes = data_per_desc;
  48070. +
  48071. + dma_desc->buf = dma_ad;
  48072. + dma_desc->status.d32 = sts.d32;
  48073. +
  48074. + dwc_ep->next_frame = 0;
  48075. +
  48076. + /** Write dma_ad into DOEPDMA register */
  48077. + DWC_WRITE_REG32(&(out_regs->doepdma),
  48078. + (uint32_t) dwc_ep->iso_dma_desc_addr);
  48079. +
  48080. + }
  48081. + /** ISO IN EP */
  48082. + else {
  48083. + dev_dma_desc_sts_t sts = {.d32 = 0 };
  48084. + dwc_otg_dev_dma_desc_t *dma_desc = dwc_ep->iso_desc_addr;
  48085. + dma_addr_t dma_ad;
  48086. + dwc_otg_dev_in_ep_regs_t *in_regs =
  48087. + core_if->dev_if->in_ep_regs[dwc_ep->num];
  48088. + unsigned int frmnumber;
  48089. + fifosize_data_t txfifosize, rxfifosize;
  48090. +
  48091. + txfifosize.d32 =
  48092. + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[dwc_ep->num]->
  48093. + dtxfsts);
  48094. + rxfifosize.d32 =
  48095. + DWC_READ_REG32(&core_if->core_global_regs->grxfsiz);
  48096. +
  48097. + addr = &core_if->dev_if->in_ep_regs[dwc_ep->num]->diepctl;
  48098. +
  48099. + dma_ad = dwc_ep->dma_addr0;
  48100. +
  48101. + dsts.d32 =
  48102. + DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  48103. +
  48104. + sts.b_iso_in.bs = BS_HOST_READY;
  48105. + sts.b_iso_in.txsts = 0;
  48106. + sts.b_iso_in.sp =
  48107. + (dwc_ep->data_per_frame % dwc_ep->maxpacket) ? 1 : 0;
  48108. + sts.b_iso_in.ioc = 0;
  48109. + sts.b_iso_in.pid = dwc_ep->pkt_per_frm;
  48110. +
  48111. + frmnumber = dwc_ep->next_frame;
  48112. +
  48113. + sts.b_iso_in.framenum = frmnumber;
  48114. + sts.b_iso_in.txbytes = dwc_ep->data_per_frame;
  48115. + sts.b_iso_in.l = 0;
  48116. +
  48117. + /** Buffer 0 descriptors setup */
  48118. + for (i = 0; i < dwc_ep->desc_cnt - 1; i++) {
  48119. + dma_desc->buf = dma_ad;
  48120. + dma_desc->status.d32 = sts.d32;
  48121. + dma_desc++;
  48122. +
  48123. + dma_ad += dwc_ep->data_per_frame;
  48124. + sts.b_iso_in.framenum += dwc_ep->bInterval;
  48125. + }
  48126. +
  48127. + sts.b_iso_in.ioc = 1;
  48128. + dma_desc->buf = dma_ad;
  48129. + dma_desc->status.d32 = sts.d32;
  48130. + ++dma_desc;
  48131. +
  48132. + /** Buffer 1 descriptors setup */
  48133. + sts.b_iso_in.ioc = 0;
  48134. + dma_ad = dwc_ep->dma_addr1;
  48135. +
  48136. + for (i = 0; i < dwc_ep->desc_cnt - dwc_ep->pkt_per_frm;
  48137. + i += dwc_ep->pkt_per_frm) {
  48138. + dma_desc->buf = dma_ad;
  48139. + dma_desc->status.d32 = sts.d32;
  48140. + dma_desc++;
  48141. +
  48142. + dma_ad += dwc_ep->data_per_frame;
  48143. + sts.b_iso_in.framenum += dwc_ep->bInterval;
  48144. +
  48145. + sts.b_iso_in.ioc = 0;
  48146. + }
  48147. + sts.b_iso_in.ioc = 1;
  48148. + sts.b_iso_in.l = 1;
  48149. +
  48150. + dma_desc->buf = dma_ad;
  48151. + dma_desc->status.d32 = sts.d32;
  48152. +
  48153. + dwc_ep->next_frame = sts.b_iso_in.framenum + dwc_ep->bInterval;
  48154. +
  48155. + /** Write dma_ad into diepdma register */
  48156. + DWC_WRITE_REG32(&(in_regs->diepdma),
  48157. + (uint32_t) dwc_ep->iso_dma_desc_addr);
  48158. + }
  48159. + /** Enable endpoint, clear nak */
  48160. + depctl.d32 = 0;
  48161. + depctl.b.epena = 1;
  48162. + depctl.b.usbactep = 1;
  48163. + depctl.b.cnak = 1;
  48164. +
  48165. + DWC_MODIFY_REG32(addr, depctl.d32, depctl.d32);
  48166. + depctl.d32 = DWC_READ_REG32(addr);
  48167. +}
  48168. +
  48169. +/**
  48170. + * This function initializes a descriptor chain for Isochronous transfer
  48171. + *
  48172. + * @param core_if Programming view of DWC_otg controller.
  48173. + * @param ep The EP to start the transfer on.
  48174. + *
  48175. + */
  48176. +void dwc_otg_iso_ep_start_buf_transfer(dwc_otg_core_if_t * core_if,
  48177. + dwc_ep_t * ep)
  48178. +{
  48179. + depctl_data_t depctl = {.d32 = 0 };
  48180. + volatile uint32_t *addr;
  48181. +
  48182. + if (ep->is_in) {
  48183. + addr = &core_if->dev_if->in_ep_regs[ep->num]->diepctl;
  48184. + } else {
  48185. + addr = &core_if->dev_if->out_ep_regs[ep->num]->doepctl;
  48186. + }
  48187. +
  48188. + if (core_if->dma_enable == 0 || core_if->dma_desc_enable != 0) {
  48189. + return;
  48190. + } else {
  48191. + deptsiz_data_t deptsiz = {.d32 = 0 };
  48192. +
  48193. + ep->xfer_len =
  48194. + ep->data_per_frame * ep->buf_proc_intrvl / ep->bInterval;
  48195. + ep->pkt_cnt =
  48196. + (ep->xfer_len - 1 + ep->maxpacket) / ep->maxpacket;
  48197. + ep->xfer_count = 0;
  48198. + ep->xfer_buff =
  48199. + (ep->proc_buf_num) ? ep->xfer_buff1 : ep->xfer_buff0;
  48200. + ep->dma_addr =
  48201. + (ep->proc_buf_num) ? ep->dma_addr1 : ep->dma_addr0;
  48202. +
  48203. + if (ep->is_in) {
  48204. + /* Program the transfer size and packet count
  48205. + * as follows: xfersize = N * maxpacket +
  48206. + * short_packet pktcnt = N + (short_packet
  48207. + * exist ? 1 : 0)
  48208. + */
  48209. + deptsiz.b.mc = ep->pkt_per_frm;
  48210. + deptsiz.b.xfersize = ep->xfer_len;
  48211. + deptsiz.b.pktcnt =
  48212. + (ep->xfer_len - 1 + ep->maxpacket) / ep->maxpacket;
  48213. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
  48214. + dieptsiz, deptsiz.d32);
  48215. +
  48216. + /* Write the DMA register */
  48217. + DWC_WRITE_REG32(&
  48218. + (core_if->dev_if->in_ep_regs[ep->num]->
  48219. + diepdma), (uint32_t) ep->dma_addr);
  48220. +
  48221. + } else {
  48222. + deptsiz.b.pktcnt =
  48223. + (ep->xfer_len + (ep->maxpacket - 1)) /
  48224. + ep->maxpacket;
  48225. + deptsiz.b.xfersize = deptsiz.b.pktcnt * ep->maxpacket;
  48226. +
  48227. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[ep->num]->
  48228. + doeptsiz, deptsiz.d32);
  48229. +
  48230. + /* Write the DMA register */
  48231. + DWC_WRITE_REG32(&
  48232. + (core_if->dev_if->out_ep_regs[ep->num]->
  48233. + doepdma), (uint32_t) ep->dma_addr);
  48234. +
  48235. + }
  48236. + /** Enable endpoint, clear nak */
  48237. + depctl.d32 = 0;
  48238. + depctl.b.epena = 1;
  48239. + depctl.b.cnak = 1;
  48240. +
  48241. + DWC_MODIFY_REG32(addr, depctl.d32, depctl.d32);
  48242. + }
  48243. +}
  48244. +
  48245. +/**
  48246. + * This function does the setup for a data transfer for an EP and
  48247. + * starts the transfer. For an IN transfer, the packets will be
  48248. + * loaded into the appropriate Tx FIFO in the ISR. For OUT transfers,
  48249. + * the packets are unloaded from the Rx FIFO in the ISR.
  48250. + *
  48251. + * @param core_if Programming view of DWC_otg controller.
  48252. + * @param ep The EP to start the transfer on.
  48253. + */
  48254. +
  48255. +static void dwc_otg_iso_ep_start_transfer(dwc_otg_core_if_t * core_if,
  48256. + dwc_ep_t * ep)
  48257. +{
  48258. + if (core_if->dma_enable) {
  48259. + if (core_if->dma_desc_enable) {
  48260. + if (ep->is_in) {
  48261. + ep->desc_cnt = ep->pkt_cnt / ep->pkt_per_frm;
  48262. + } else {
  48263. + ep->desc_cnt = ep->pkt_cnt;
  48264. + }
  48265. + dwc_otg_iso_ep_start_ddma_transfer(core_if, ep);
  48266. + } else {
  48267. + if (core_if->pti_enh_enable) {
  48268. + dwc_otg_iso_ep_start_buf_transfer(core_if, ep);
  48269. + } else {
  48270. + ep->cur_pkt_addr =
  48271. + (ep->proc_buf_num) ? ep->xfer_buff1 : ep->
  48272. + xfer_buff0;
  48273. + ep->cur_pkt_dma_addr =
  48274. + (ep->proc_buf_num) ? ep->dma_addr1 : ep->
  48275. + dma_addr0;
  48276. + dwc_otg_iso_ep_start_frm_transfer(core_if, ep);
  48277. + }
  48278. + }
  48279. + } else {
  48280. + ep->cur_pkt_addr =
  48281. + (ep->proc_buf_num) ? ep->xfer_buff1 : ep->xfer_buff0;
  48282. + ep->cur_pkt_dma_addr =
  48283. + (ep->proc_buf_num) ? ep->dma_addr1 : ep->dma_addr0;
  48284. + dwc_otg_iso_ep_start_frm_transfer(core_if, ep);
  48285. + }
  48286. +}
  48287. +
  48288. +/**
  48289. + * This function stops transfer for an EP and
  48290. + * resets the ep's variables.
  48291. + *
  48292. + * @param core_if Programming view of DWC_otg controller.
  48293. + * @param ep The EP to start the transfer on.
  48294. + */
  48295. +
  48296. +void dwc_otg_iso_ep_stop_transfer(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  48297. +{
  48298. + depctl_data_t depctl = {.d32 = 0 };
  48299. + volatile uint32_t *addr;
  48300. +
  48301. + if (ep->is_in == 1) {
  48302. + addr = &core_if->dev_if->in_ep_regs[ep->num]->diepctl;
  48303. + } else {
  48304. + addr = &core_if->dev_if->out_ep_regs[ep->num]->doepctl;
  48305. + }
  48306. +
  48307. + /* disable the ep */
  48308. + depctl.d32 = DWC_READ_REG32(addr);
  48309. +
  48310. + depctl.b.epdis = 1;
  48311. + depctl.b.snak = 1;
  48312. +
  48313. + DWC_WRITE_REG32(addr, depctl.d32);
  48314. +
  48315. + if (core_if->dma_desc_enable &&
  48316. + ep->iso_desc_addr && ep->iso_dma_desc_addr) {
  48317. + dwc_otg_ep_free_desc_chain(ep->iso_desc_addr,
  48318. + ep->iso_dma_desc_addr,
  48319. + ep->desc_cnt * 2);
  48320. + }
  48321. +
  48322. + /* reset varibales */
  48323. + ep->dma_addr0 = 0;
  48324. + ep->dma_addr1 = 0;
  48325. + ep->xfer_buff0 = 0;
  48326. + ep->xfer_buff1 = 0;
  48327. + ep->data_per_frame = 0;
  48328. + ep->data_pattern_frame = 0;
  48329. + ep->sync_frame = 0;
  48330. + ep->buf_proc_intrvl = 0;
  48331. + ep->bInterval = 0;
  48332. + ep->proc_buf_num = 0;
  48333. + ep->pkt_per_frm = 0;
  48334. + ep->pkt_per_frm = 0;
  48335. + ep->desc_cnt = 0;
  48336. + ep->iso_desc_addr = 0;
  48337. + ep->iso_dma_desc_addr = 0;
  48338. +}
  48339. +
  48340. +int dwc_otg_pcd_iso_ep_start(dwc_otg_pcd_t * pcd, void *ep_handle,
  48341. + uint8_t * buf0, uint8_t * buf1, dwc_dma_t dma0,
  48342. + dwc_dma_t dma1, int sync_frame, int dp_frame,
  48343. + int data_per_frame, int start_frame,
  48344. + int buf_proc_intrvl, void *req_handle,
  48345. + int atomic_alloc)
  48346. +{
  48347. + dwc_otg_pcd_ep_t *ep;
  48348. + dwc_irqflags_t flags = 0;
  48349. + dwc_ep_t *dwc_ep;
  48350. + int32_t frm_data;
  48351. + dsts_data_t dsts;
  48352. + dwc_otg_core_if_t *core_if;
  48353. +
  48354. + ep = get_ep_from_handle(pcd, ep_handle);
  48355. +
  48356. + if (!ep || !ep->desc || ep->dwc_ep.num == 0) {
  48357. + DWC_WARN("bad ep\n");
  48358. + return -DWC_E_INVALID;
  48359. + }
  48360. +
  48361. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  48362. + core_if = GET_CORE_IF(pcd);
  48363. + dwc_ep = &ep->dwc_ep;
  48364. +
  48365. + if (ep->iso_req_handle) {
  48366. + DWC_WARN("ISO request in progress\n");
  48367. + }
  48368. +
  48369. + dwc_ep->dma_addr0 = dma0;
  48370. + dwc_ep->dma_addr1 = dma1;
  48371. +
  48372. + dwc_ep->xfer_buff0 = buf0;
  48373. + dwc_ep->xfer_buff1 = buf1;
  48374. +
  48375. + dwc_ep->data_per_frame = data_per_frame;
  48376. +
  48377. + /** @todo - pattern data support is to be implemented in the future */
  48378. + dwc_ep->data_pattern_frame = dp_frame;
  48379. + dwc_ep->sync_frame = sync_frame;
  48380. +
  48381. + dwc_ep->buf_proc_intrvl = buf_proc_intrvl;
  48382. +
  48383. + dwc_ep->bInterval = 1 << (ep->desc->bInterval - 1);
  48384. +
  48385. + dwc_ep->proc_buf_num = 0;
  48386. +
  48387. + dwc_ep->pkt_per_frm = 0;
  48388. + frm_data = ep->dwc_ep.data_per_frame;
  48389. + while (frm_data > 0) {
  48390. + dwc_ep->pkt_per_frm++;
  48391. + frm_data -= ep->dwc_ep.maxpacket;
  48392. + }
  48393. +
  48394. + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  48395. +
  48396. + if (start_frame == -1) {
  48397. + dwc_ep->next_frame = dsts.b.soffn + 1;
  48398. + if (dwc_ep->bInterval != 1) {
  48399. + dwc_ep->next_frame =
  48400. + dwc_ep->next_frame + (dwc_ep->bInterval - 1 -
  48401. + dwc_ep->next_frame %
  48402. + dwc_ep->bInterval);
  48403. + }
  48404. + } else {
  48405. + dwc_ep->next_frame = start_frame;
  48406. + }
  48407. +
  48408. + if (!core_if->pti_enh_enable) {
  48409. + dwc_ep->pkt_cnt =
  48410. + dwc_ep->buf_proc_intrvl * dwc_ep->pkt_per_frm /
  48411. + dwc_ep->bInterval;
  48412. + } else {
  48413. + dwc_ep->pkt_cnt =
  48414. + (dwc_ep->data_per_frame *
  48415. + (dwc_ep->buf_proc_intrvl / dwc_ep->bInterval)
  48416. + - 1 + dwc_ep->maxpacket) / dwc_ep->maxpacket;
  48417. + }
  48418. +
  48419. + if (core_if->dma_desc_enable) {
  48420. + dwc_ep->desc_cnt =
  48421. + dwc_ep->buf_proc_intrvl * dwc_ep->pkt_per_frm /
  48422. + dwc_ep->bInterval;
  48423. + }
  48424. +
  48425. + if (atomic_alloc) {
  48426. + dwc_ep->pkt_info =
  48427. + DWC_ALLOC_ATOMIC(sizeof(iso_pkt_info_t) * dwc_ep->pkt_cnt);
  48428. + } else {
  48429. + dwc_ep->pkt_info =
  48430. + DWC_ALLOC(sizeof(iso_pkt_info_t) * dwc_ep->pkt_cnt);
  48431. + }
  48432. + if (!dwc_ep->pkt_info) {
  48433. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  48434. + return -DWC_E_NO_MEMORY;
  48435. + }
  48436. + if (core_if->pti_enh_enable) {
  48437. + dwc_memset(dwc_ep->pkt_info, 0,
  48438. + sizeof(iso_pkt_info_t) * dwc_ep->pkt_cnt);
  48439. + }
  48440. +
  48441. + dwc_ep->cur_pkt = 0;
  48442. + ep->iso_req_handle = req_handle;
  48443. +
  48444. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  48445. + dwc_otg_iso_ep_start_transfer(core_if, dwc_ep);
  48446. + return 0;
  48447. +}
  48448. +
  48449. +int dwc_otg_pcd_iso_ep_stop(dwc_otg_pcd_t * pcd, void *ep_handle,
  48450. + void *req_handle)
  48451. +{
  48452. + dwc_irqflags_t flags = 0;
  48453. + dwc_otg_pcd_ep_t *ep;
  48454. + dwc_ep_t *dwc_ep;
  48455. +
  48456. + ep = get_ep_from_handle(pcd, ep_handle);
  48457. + if (!ep || !ep->desc || ep->dwc_ep.num == 0) {
  48458. + DWC_WARN("bad ep\n");
  48459. + return -DWC_E_INVALID;
  48460. + }
  48461. + dwc_ep = &ep->dwc_ep;
  48462. +
  48463. + dwc_otg_iso_ep_stop_transfer(GET_CORE_IF(pcd), dwc_ep);
  48464. +
  48465. + DWC_FREE(dwc_ep->pkt_info);
  48466. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  48467. + if (ep->iso_req_handle != req_handle) {
  48468. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  48469. + return -DWC_E_INVALID;
  48470. + }
  48471. +
  48472. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  48473. +
  48474. + ep->iso_req_handle = 0;
  48475. + return 0;
  48476. +}
  48477. +
  48478. +/**
  48479. + * This function is used for perodical data exchnage between PCD and gadget drivers.
  48480. + * for Isochronous EPs
  48481. + *
  48482. + * - Every time a sync period completes this function is called to
  48483. + * perform data exchange between PCD and gadget
  48484. + */
  48485. +void dwc_otg_iso_buffer_done(dwc_otg_pcd_t * pcd, dwc_otg_pcd_ep_t * ep,
  48486. + void *req_handle)
  48487. +{
  48488. + int i;
  48489. + dwc_ep_t *dwc_ep;
  48490. +
  48491. + dwc_ep = &ep->dwc_ep;
  48492. +
  48493. + DWC_SPINUNLOCK(ep->pcd->lock);
  48494. + pcd->fops->isoc_complete(pcd, ep->priv, ep->iso_req_handle,
  48495. + dwc_ep->proc_buf_num ^ 0x1);
  48496. + DWC_SPINLOCK(ep->pcd->lock);
  48497. +
  48498. + for (i = 0; i < dwc_ep->pkt_cnt; ++i) {
  48499. + dwc_ep->pkt_info[i].status = 0;
  48500. + dwc_ep->pkt_info[i].offset = 0;
  48501. + dwc_ep->pkt_info[i].length = 0;
  48502. + }
  48503. +}
  48504. +
  48505. +int dwc_otg_pcd_get_iso_packet_count(dwc_otg_pcd_t * pcd, void *ep_handle,
  48506. + void *iso_req_handle)
  48507. +{
  48508. + dwc_otg_pcd_ep_t *ep;
  48509. + dwc_ep_t *dwc_ep;
  48510. +
  48511. + ep = get_ep_from_handle(pcd, ep_handle);
  48512. + if (!ep->desc || ep->dwc_ep.num == 0) {
  48513. + DWC_WARN("bad ep\n");
  48514. + return -DWC_E_INVALID;
  48515. + }
  48516. + dwc_ep = &ep->dwc_ep;
  48517. +
  48518. + return dwc_ep->pkt_cnt;
  48519. +}
  48520. +
  48521. +void dwc_otg_pcd_get_iso_packet_params(dwc_otg_pcd_t * pcd, void *ep_handle,
  48522. + void *iso_req_handle, int packet,
  48523. + int *status, int *actual, int *offset)
  48524. +{
  48525. + dwc_otg_pcd_ep_t *ep;
  48526. + dwc_ep_t *dwc_ep;
  48527. +
  48528. + ep = get_ep_from_handle(pcd, ep_handle);
  48529. + if (!ep)
  48530. + DWC_WARN("bad ep\n");
  48531. +
  48532. + dwc_ep = &ep->dwc_ep;
  48533. +
  48534. + *status = dwc_ep->pkt_info[packet].status;
  48535. + *actual = dwc_ep->pkt_info[packet].length;
  48536. + *offset = dwc_ep->pkt_info[packet].offset;
  48537. +}
  48538. +
  48539. +#endif /* DWC_EN_ISOC */
  48540. +
  48541. +static void dwc_otg_pcd_init_ep(dwc_otg_pcd_t * pcd, dwc_otg_pcd_ep_t * pcd_ep,
  48542. + uint32_t is_in, uint32_t ep_num)
  48543. +{
  48544. + /* Init EP structure */
  48545. + pcd_ep->desc = 0;
  48546. + pcd_ep->pcd = pcd;
  48547. + pcd_ep->stopped = 1;
  48548. + pcd_ep->queue_sof = 0;
  48549. +
  48550. + /* Init DWC ep structure */
  48551. + pcd_ep->dwc_ep.is_in = is_in;
  48552. + pcd_ep->dwc_ep.num = ep_num;
  48553. + pcd_ep->dwc_ep.active = 0;
  48554. + pcd_ep->dwc_ep.tx_fifo_num = 0;
  48555. + /* Control until ep is actvated */
  48556. + pcd_ep->dwc_ep.type = DWC_OTG_EP_TYPE_CONTROL;
  48557. + pcd_ep->dwc_ep.maxpacket = MAX_PACKET_SIZE;
  48558. + pcd_ep->dwc_ep.dma_addr = 0;
  48559. + pcd_ep->dwc_ep.start_xfer_buff = 0;
  48560. + pcd_ep->dwc_ep.xfer_buff = 0;
  48561. + pcd_ep->dwc_ep.xfer_len = 0;
  48562. + pcd_ep->dwc_ep.xfer_count = 0;
  48563. + pcd_ep->dwc_ep.sent_zlp = 0;
  48564. + pcd_ep->dwc_ep.total_len = 0;
  48565. + pcd_ep->dwc_ep.desc_addr = 0;
  48566. + pcd_ep->dwc_ep.dma_desc_addr = 0;
  48567. + DWC_CIRCLEQ_INIT(&pcd_ep->queue);
  48568. +}
  48569. +
  48570. +/**
  48571. + * Initialize ep's
  48572. + */
  48573. +static void dwc_otg_pcd_reinit(dwc_otg_pcd_t * pcd)
  48574. +{
  48575. + int i;
  48576. + uint32_t hwcfg1;
  48577. + dwc_otg_pcd_ep_t *ep;
  48578. + int in_ep_cntr, out_ep_cntr;
  48579. + uint32_t num_in_eps = (GET_CORE_IF(pcd))->dev_if->num_in_eps;
  48580. + uint32_t num_out_eps = (GET_CORE_IF(pcd))->dev_if->num_out_eps;
  48581. +
  48582. + /**
  48583. + * Initialize the EP0 structure.
  48584. + */
  48585. + ep = &pcd->ep0;
  48586. + dwc_otg_pcd_init_ep(pcd, ep, 0, 0);
  48587. +
  48588. + in_ep_cntr = 0;
  48589. + hwcfg1 = (GET_CORE_IF(pcd))->hwcfg1.d32 >> 3;
  48590. + for (i = 1; in_ep_cntr < num_in_eps; i++) {
  48591. + if ((hwcfg1 & 0x1) == 0) {
  48592. + dwc_otg_pcd_ep_t *ep = &pcd->in_ep[in_ep_cntr];
  48593. + in_ep_cntr++;
  48594. + /**
  48595. + * @todo NGS: Add direction to EP, based on contents
  48596. + * of HWCFG1. Need a copy of HWCFG1 in pcd structure?
  48597. + * sprintf(";r
  48598. + */
  48599. + dwc_otg_pcd_init_ep(pcd, ep, 1 /* IN */ , i);
  48600. +
  48601. + DWC_CIRCLEQ_INIT(&ep->queue);
  48602. + }
  48603. + hwcfg1 >>= 2;
  48604. + }
  48605. +
  48606. + out_ep_cntr = 0;
  48607. + hwcfg1 = (GET_CORE_IF(pcd))->hwcfg1.d32 >> 2;
  48608. + for (i = 1; out_ep_cntr < num_out_eps; i++) {
  48609. + if ((hwcfg1 & 0x1) == 0) {
  48610. + dwc_otg_pcd_ep_t *ep = &pcd->out_ep[out_ep_cntr];
  48611. + out_ep_cntr++;
  48612. + /**
  48613. + * @todo NGS: Add direction to EP, based on contents
  48614. + * of HWCFG1. Need a copy of HWCFG1 in pcd structure?
  48615. + * sprintf(";r
  48616. + */
  48617. + dwc_otg_pcd_init_ep(pcd, ep, 0 /* OUT */ , i);
  48618. + DWC_CIRCLEQ_INIT(&ep->queue);
  48619. + }
  48620. + hwcfg1 >>= 2;
  48621. + }
  48622. +
  48623. + pcd->ep0state = EP0_DISCONNECT;
  48624. + pcd->ep0.dwc_ep.maxpacket = MAX_EP0_SIZE;
  48625. + pcd->ep0.dwc_ep.type = DWC_OTG_EP_TYPE_CONTROL;
  48626. +}
  48627. +
  48628. +/**
  48629. + * This function is called when the SRP timer expires. The SRP should
  48630. + * complete within 6 seconds.
  48631. + */
  48632. +static void srp_timeout(void *ptr)
  48633. +{
  48634. + gotgctl_data_t gotgctl;
  48635. + dwc_otg_core_if_t *core_if = (dwc_otg_core_if_t *) ptr;
  48636. + volatile uint32_t *addr = &core_if->core_global_regs->gotgctl;
  48637. +
  48638. + gotgctl.d32 = DWC_READ_REG32(addr);
  48639. +
  48640. + core_if->srp_timer_started = 0;
  48641. +
  48642. + if (core_if->adp_enable) {
  48643. + if (gotgctl.b.bsesvld == 0) {
  48644. + gpwrdn_data_t gpwrdn = {.d32 = 0 };
  48645. + DWC_PRINTF("SRP Timeout BSESSVLD = 0\n");
  48646. + /* Power off the core */
  48647. + if (core_if->power_down == 2) {
  48648. + gpwrdn.b.pwrdnswtch = 1;
  48649. + DWC_MODIFY_REG32(&core_if->
  48650. + core_global_regs->gpwrdn,
  48651. + gpwrdn.d32, 0);
  48652. + }
  48653. +
  48654. + gpwrdn.d32 = 0;
  48655. + gpwrdn.b.pmuintsel = 1;
  48656. + gpwrdn.b.pmuactv = 1;
  48657. + DWC_MODIFY_REG32(&core_if->core_global_regs->gpwrdn, 0,
  48658. + gpwrdn.d32);
  48659. + dwc_otg_adp_probe_start(core_if);
  48660. + } else {
  48661. + DWC_PRINTF("SRP Timeout BSESSVLD = 1\n");
  48662. + core_if->op_state = B_PERIPHERAL;
  48663. + dwc_otg_core_init(core_if);
  48664. + dwc_otg_enable_global_interrupts(core_if);
  48665. + cil_pcd_start(core_if);
  48666. + }
  48667. + }
  48668. +
  48669. + if ((core_if->core_params->phy_type == DWC_PHY_TYPE_PARAM_FS) &&
  48670. + (core_if->core_params->i2c_enable)) {
  48671. + DWC_PRINTF("SRP Timeout\n");
  48672. +
  48673. + if ((core_if->srp_success) && (gotgctl.b.bsesvld)) {
  48674. + if (core_if->pcd_cb && core_if->pcd_cb->resume_wakeup) {
  48675. + core_if->pcd_cb->resume_wakeup(core_if->pcd_cb->p);
  48676. + }
  48677. +
  48678. + /* Clear Session Request */
  48679. + gotgctl.d32 = 0;
  48680. + gotgctl.b.sesreq = 1;
  48681. + DWC_MODIFY_REG32(&core_if->core_global_regs->gotgctl,
  48682. + gotgctl.d32, 0);
  48683. +
  48684. + core_if->srp_success = 0;
  48685. + } else {
  48686. + __DWC_ERROR("Device not connected/responding\n");
  48687. + gotgctl.b.sesreq = 0;
  48688. + DWC_WRITE_REG32(addr, gotgctl.d32);
  48689. + }
  48690. + } else if (gotgctl.b.sesreq) {
  48691. + DWC_PRINTF("SRP Timeout\n");
  48692. +
  48693. + __DWC_ERROR("Device not connected/responding\n");
  48694. + gotgctl.b.sesreq = 0;
  48695. + DWC_WRITE_REG32(addr, gotgctl.d32);
  48696. + } else {
  48697. + DWC_PRINTF(" SRP GOTGCTL=%0x\n", gotgctl.d32);
  48698. + }
  48699. +}
  48700. +
  48701. +/**
  48702. + * Tasklet
  48703. + *
  48704. + */
  48705. +extern void start_next_request(dwc_otg_pcd_ep_t * ep);
  48706. +
  48707. +static void start_xfer_tasklet_func(void *data)
  48708. +{
  48709. + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) data;
  48710. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  48711. +
  48712. + int i;
  48713. + depctl_data_t diepctl;
  48714. +
  48715. + DWC_DEBUGPL(DBG_PCDV, "Start xfer tasklet\n");
  48716. +
  48717. + diepctl.d32 = DWC_READ_REG32(&core_if->dev_if->in_ep_regs[0]->diepctl);
  48718. +
  48719. + if (pcd->ep0.queue_sof) {
  48720. + pcd->ep0.queue_sof = 0;
  48721. + start_next_request(&pcd->ep0);
  48722. + // break;
  48723. + }
  48724. +
  48725. + for (i = 0; i < core_if->dev_if->num_in_eps; i++) {
  48726. + depctl_data_t diepctl;
  48727. + diepctl.d32 =
  48728. + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[i]->diepctl);
  48729. +
  48730. + if (pcd->in_ep[i].queue_sof) {
  48731. + pcd->in_ep[i].queue_sof = 0;
  48732. + start_next_request(&pcd->in_ep[i]);
  48733. + // break;
  48734. + }
  48735. + }
  48736. +
  48737. + return;
  48738. +}
  48739. +
  48740. +/**
  48741. + * This function initialized the PCD portion of the driver.
  48742. + *
  48743. + */
  48744. +dwc_otg_pcd_t *dwc_otg_pcd_init(dwc_otg_device_t *otg_dev)
  48745. +{
  48746. + struct device *dev = &otg_dev->os_dep.platformdev->dev;
  48747. + dwc_otg_core_if_t *core_if = otg_dev->core_if;
  48748. + dwc_otg_pcd_t *pcd = NULL;
  48749. + dwc_otg_dev_if_t *dev_if;
  48750. + int i;
  48751. +
  48752. + /*
  48753. + * Allocate PCD structure
  48754. + */
  48755. + pcd = DWC_ALLOC(sizeof(dwc_otg_pcd_t));
  48756. +
  48757. + if (pcd == NULL) {
  48758. + return NULL;
  48759. + }
  48760. +
  48761. +#if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_SPINLOCK))
  48762. + DWC_SPINLOCK_ALLOC_LINUX_DEBUG(pcd->lock);
  48763. +#else
  48764. + pcd->lock = DWC_SPINLOCK_ALLOC();
  48765. +#endif
  48766. + DWC_DEBUGPL(DBG_HCDV, "Init of PCD %p given core_if %p\n",
  48767. + pcd, core_if);//GRAYG
  48768. + if (!pcd->lock) {
  48769. + DWC_ERROR("Could not allocate lock for pcd");
  48770. + DWC_FREE(pcd);
  48771. + return NULL;
  48772. + }
  48773. + /* Set core_if's lock pointer to hcd->lock */
  48774. + core_if->lock = pcd->lock;
  48775. + pcd->core_if = core_if;
  48776. +
  48777. + dev_if = core_if->dev_if;
  48778. + dev_if->isoc_ep = NULL;
  48779. +
  48780. + if (core_if->hwcfg4.b.ded_fifo_en) {
  48781. + DWC_PRINTF("Dedicated Tx FIFOs mode\n");
  48782. + } else {
  48783. + DWC_PRINTF("Shared Tx FIFO mode\n");
  48784. + }
  48785. +
  48786. + /*
  48787. + * Initialized the Core for Device mode here if there is nod ADP support.
  48788. + * Otherwise it will be done later in dwc_otg_adp_start routine.
  48789. + */
  48790. + if (dwc_otg_is_device_mode(core_if) /*&& !core_if->adp_enable*/) {
  48791. + dwc_otg_core_dev_init(core_if);
  48792. + }
  48793. +
  48794. + /*
  48795. + * Register the PCD Callbacks.
  48796. + */
  48797. + dwc_otg_cil_register_pcd_callbacks(core_if, &pcd_callbacks, pcd);
  48798. +
  48799. + /*
  48800. + * Initialize the DMA buffer for SETUP packets
  48801. + */
  48802. + if (GET_CORE_IF(pcd)->dma_enable) {
  48803. + pcd->setup_pkt =
  48804. + DWC_DMA_ALLOC(dev, sizeof(*pcd->setup_pkt) * 5,
  48805. + &pcd->setup_pkt_dma_handle);
  48806. + if (pcd->setup_pkt == NULL) {
  48807. + DWC_FREE(pcd);
  48808. + return NULL;
  48809. + }
  48810. +
  48811. + pcd->status_buf =
  48812. + DWC_DMA_ALLOC(dev, sizeof(uint16_t),
  48813. + &pcd->status_buf_dma_handle);
  48814. + if (pcd->status_buf == NULL) {
  48815. + DWC_DMA_FREE(dev, sizeof(*pcd->setup_pkt) * 5,
  48816. + pcd->setup_pkt, pcd->setup_pkt_dma_handle);
  48817. + DWC_FREE(pcd);
  48818. + return NULL;
  48819. + }
  48820. +
  48821. + if (GET_CORE_IF(pcd)->dma_desc_enable) {
  48822. + dev_if->setup_desc_addr[0] =
  48823. + dwc_otg_ep_alloc_desc_chain(dev,
  48824. + &dev_if->dma_setup_desc_addr[0], 1);
  48825. + dev_if->setup_desc_addr[1] =
  48826. + dwc_otg_ep_alloc_desc_chain(dev,
  48827. + &dev_if->dma_setup_desc_addr[1], 1);
  48828. + dev_if->in_desc_addr =
  48829. + dwc_otg_ep_alloc_desc_chain(dev,
  48830. + &dev_if->dma_in_desc_addr, 1);
  48831. + dev_if->out_desc_addr =
  48832. + dwc_otg_ep_alloc_desc_chain(dev,
  48833. + &dev_if->dma_out_desc_addr, 1);
  48834. + pcd->data_terminated = 0;
  48835. +
  48836. + if (dev_if->setup_desc_addr[0] == 0
  48837. + || dev_if->setup_desc_addr[1] == 0
  48838. + || dev_if->in_desc_addr == 0
  48839. + || dev_if->out_desc_addr == 0) {
  48840. +
  48841. + if (dev_if->out_desc_addr)
  48842. + dwc_otg_ep_free_desc_chain(dev,
  48843. + dev_if->out_desc_addr,
  48844. + dev_if->dma_out_desc_addr, 1);
  48845. + if (dev_if->in_desc_addr)
  48846. + dwc_otg_ep_free_desc_chain(dev,
  48847. + dev_if->in_desc_addr,
  48848. + dev_if->dma_in_desc_addr, 1);
  48849. + if (dev_if->setup_desc_addr[1])
  48850. + dwc_otg_ep_free_desc_chain(dev,
  48851. + dev_if->setup_desc_addr[1],
  48852. + dev_if->dma_setup_desc_addr[1], 1);
  48853. + if (dev_if->setup_desc_addr[0])
  48854. + dwc_otg_ep_free_desc_chain(dev,
  48855. + dev_if->setup_desc_addr[0],
  48856. + dev_if->dma_setup_desc_addr[0], 1);
  48857. +
  48858. + DWC_DMA_FREE(dev, sizeof(*pcd->setup_pkt) * 5,
  48859. + pcd->setup_pkt,
  48860. + pcd->setup_pkt_dma_handle);
  48861. + DWC_DMA_FREE(dev, sizeof(*pcd->status_buf),
  48862. + pcd->status_buf,
  48863. + pcd->status_buf_dma_handle);
  48864. +
  48865. + DWC_FREE(pcd);
  48866. +
  48867. + return NULL;
  48868. + }
  48869. + }
  48870. + } else {
  48871. + pcd->setup_pkt = DWC_ALLOC(sizeof(*pcd->setup_pkt) * 5);
  48872. + if (pcd->setup_pkt == NULL) {
  48873. + DWC_FREE(pcd);
  48874. + return NULL;
  48875. + }
  48876. +
  48877. + pcd->status_buf = DWC_ALLOC(sizeof(uint16_t));
  48878. + if (pcd->status_buf == NULL) {
  48879. + DWC_FREE(pcd->setup_pkt);
  48880. + DWC_FREE(pcd);
  48881. + return NULL;
  48882. + }
  48883. + }
  48884. +
  48885. + dwc_otg_pcd_reinit(pcd);
  48886. +
  48887. + /* Allocate the cfi object for the PCD */
  48888. +#ifdef DWC_UTE_CFI
  48889. + pcd->cfi = DWC_ALLOC(sizeof(cfiobject_t));
  48890. + if (NULL == pcd->cfi)
  48891. + goto fail;
  48892. + if (init_cfi(pcd->cfi)) {
  48893. + CFI_INFO("%s: Failed to init the CFI object\n", __func__);
  48894. + goto fail;
  48895. + }
  48896. +#endif
  48897. +
  48898. + /* Initialize tasklets */
  48899. + pcd->start_xfer_tasklet = DWC_TASK_ALLOC("xfer_tasklet",
  48900. + start_xfer_tasklet_func, pcd);
  48901. + pcd->test_mode_tasklet = DWC_TASK_ALLOC("test_mode_tasklet",
  48902. + do_test_mode, pcd);
  48903. +
  48904. + /* Initialize SRP timer */
  48905. + core_if->srp_timer = DWC_TIMER_ALLOC("SRP TIMER", srp_timeout, core_if);
  48906. +
  48907. + if (core_if->core_params->dev_out_nak) {
  48908. + /**
  48909. + * Initialize xfer timeout timer. Implemented for
  48910. + * 2.93a feature "Device DDMA OUT NAK Enhancement"
  48911. + */
  48912. + for(i = 0; i < MAX_EPS_CHANNELS; i++) {
  48913. + pcd->core_if->ep_xfer_timer[i] =
  48914. + DWC_TIMER_ALLOC("ep timer", ep_xfer_timeout,
  48915. + &pcd->core_if->ep_xfer_info[i]);
  48916. + }
  48917. + }
  48918. +
  48919. + return pcd;
  48920. +#ifdef DWC_UTE_CFI
  48921. +fail:
  48922. +#endif
  48923. + if (pcd->setup_pkt)
  48924. + DWC_FREE(pcd->setup_pkt);
  48925. + if (pcd->status_buf)
  48926. + DWC_FREE(pcd->status_buf);
  48927. +#ifdef DWC_UTE_CFI
  48928. + if (pcd->cfi)
  48929. + DWC_FREE(pcd->cfi);
  48930. +#endif
  48931. + if (pcd)
  48932. + DWC_FREE(pcd);
  48933. + return NULL;
  48934. +
  48935. +}
  48936. +
  48937. +/**
  48938. + * Remove PCD specific data
  48939. + */
  48940. +void dwc_otg_pcd_remove(dwc_otg_pcd_t * pcd)
  48941. +{
  48942. + dwc_otg_dev_if_t *dev_if = GET_CORE_IF(pcd)->dev_if;
  48943. + struct device *dev = dwc_otg_pcd_to_dev(pcd);
  48944. + int i;
  48945. +
  48946. + if (pcd->core_if->core_params->dev_out_nak) {
  48947. + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  48948. + DWC_TIMER_CANCEL(pcd->core_if->ep_xfer_timer[i]);
  48949. + pcd->core_if->ep_xfer_info[i].state = 0;
  48950. + }
  48951. + }
  48952. +
  48953. + if (GET_CORE_IF(pcd)->dma_enable) {
  48954. + DWC_DMA_FREE(dev, sizeof(*pcd->setup_pkt) * 5, pcd->setup_pkt,
  48955. + pcd->setup_pkt_dma_handle);
  48956. + DWC_DMA_FREE(dev, sizeof(uint16_t), pcd->status_buf,
  48957. + pcd->status_buf_dma_handle);
  48958. + if (GET_CORE_IF(pcd)->dma_desc_enable) {
  48959. + dwc_otg_ep_free_desc_chain(dev,
  48960. + dev_if->setup_desc_addr[0],
  48961. + dev_if->dma_setup_desc_addr
  48962. + [0], 1);
  48963. + dwc_otg_ep_free_desc_chain(dev,
  48964. + dev_if->setup_desc_addr[1],
  48965. + dev_if->dma_setup_desc_addr
  48966. + [1], 1);
  48967. + dwc_otg_ep_free_desc_chain(dev,
  48968. + dev_if->in_desc_addr,
  48969. + dev_if->dma_in_desc_addr, 1);
  48970. + dwc_otg_ep_free_desc_chain(dev,
  48971. + dev_if->out_desc_addr,
  48972. + dev_if->dma_out_desc_addr,
  48973. + 1);
  48974. + }
  48975. + } else {
  48976. + DWC_FREE(pcd->setup_pkt);
  48977. + DWC_FREE(pcd->status_buf);
  48978. + }
  48979. + DWC_SPINLOCK_FREE(pcd->lock);
  48980. + /* Set core_if's lock pointer to NULL */
  48981. + pcd->core_if->lock = NULL;
  48982. +
  48983. + DWC_TASK_FREE(pcd->start_xfer_tasklet);
  48984. + DWC_TASK_FREE(pcd->test_mode_tasklet);
  48985. + if (pcd->core_if->core_params->dev_out_nak) {
  48986. + for (i = 0; i < MAX_EPS_CHANNELS; i++) {
  48987. + if (pcd->core_if->ep_xfer_timer[i]) {
  48988. + DWC_TIMER_FREE(pcd->core_if->ep_xfer_timer[i]);
  48989. + }
  48990. + }
  48991. + }
  48992. +
  48993. +/* Release the CFI object's dynamic memory */
  48994. +#ifdef DWC_UTE_CFI
  48995. + if (pcd->cfi->ops.release) {
  48996. + pcd->cfi->ops.release(pcd->cfi);
  48997. + }
  48998. +#endif
  48999. +
  49000. + DWC_FREE(pcd);
  49001. +}
  49002. +
  49003. +/**
  49004. + * Returns whether registered pcd is dual speed or not
  49005. + */
  49006. +uint32_t dwc_otg_pcd_is_dualspeed(dwc_otg_pcd_t * pcd)
  49007. +{
  49008. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  49009. +
  49010. + if ((core_if->core_params->speed == DWC_SPEED_PARAM_FULL) ||
  49011. + ((core_if->hwcfg2.b.hs_phy_type == 2) &&
  49012. + (core_if->hwcfg2.b.fs_phy_type == 1) &&
  49013. + (core_if->core_params->ulpi_fs_ls))) {
  49014. + return 0;
  49015. + }
  49016. +
  49017. + return 1;
  49018. +}
  49019. +
  49020. +/**
  49021. + * Returns whether registered pcd is OTG capable or not
  49022. + */
  49023. +uint32_t dwc_otg_pcd_is_otg(dwc_otg_pcd_t * pcd)
  49024. +{
  49025. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  49026. + gusbcfg_data_t usbcfg = {.d32 = 0 };
  49027. +
  49028. + usbcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->gusbcfg);
  49029. + if (!usbcfg.b.srpcap || !usbcfg.b.hnpcap) {
  49030. + return 0;
  49031. + }
  49032. +
  49033. + return 1;
  49034. +}
  49035. +
  49036. +/**
  49037. + * This function assigns periodic Tx FIFO to an periodic EP
  49038. + * in shared Tx FIFO mode
  49039. + */
  49040. +static uint32_t assign_tx_fifo(dwc_otg_core_if_t * core_if)
  49041. +{
  49042. + uint32_t TxMsk = 1;
  49043. + int i;
  49044. +
  49045. + for (i = 0; i < core_if->hwcfg4.b.num_in_eps; ++i) {
  49046. + if ((TxMsk & core_if->tx_msk) == 0) {
  49047. + core_if->tx_msk |= TxMsk;
  49048. + return i + 1;
  49049. + }
  49050. + TxMsk <<= 1;
  49051. + }
  49052. + return 0;
  49053. +}
  49054. +
  49055. +/**
  49056. + * This function assigns periodic Tx FIFO to an periodic EP
  49057. + * in shared Tx FIFO mode
  49058. + */
  49059. +static uint32_t assign_perio_tx_fifo(dwc_otg_core_if_t * core_if)
  49060. +{
  49061. + uint32_t PerTxMsk = 1;
  49062. + int i;
  49063. + for (i = 0; i < core_if->hwcfg4.b.num_dev_perio_in_ep; ++i) {
  49064. + if ((PerTxMsk & core_if->p_tx_msk) == 0) {
  49065. + core_if->p_tx_msk |= PerTxMsk;
  49066. + return i + 1;
  49067. + }
  49068. + PerTxMsk <<= 1;
  49069. + }
  49070. + return 0;
  49071. +}
  49072. +
  49073. +/**
  49074. + * This function releases periodic Tx FIFO
  49075. + * in shared Tx FIFO mode
  49076. + */
  49077. +static void release_perio_tx_fifo(dwc_otg_core_if_t * core_if,
  49078. + uint32_t fifo_num)
  49079. +{
  49080. + core_if->p_tx_msk =
  49081. + (core_if->p_tx_msk & (1 << (fifo_num - 1))) ^ core_if->p_tx_msk;
  49082. +}
  49083. +
  49084. +/**
  49085. + * This function releases periodic Tx FIFO
  49086. + * in shared Tx FIFO mode
  49087. + */
  49088. +static void release_tx_fifo(dwc_otg_core_if_t * core_if, uint32_t fifo_num)
  49089. +{
  49090. + core_if->tx_msk =
  49091. + (core_if->tx_msk & (1 << (fifo_num - 1))) ^ core_if->tx_msk;
  49092. +}
  49093. +
  49094. +/**
  49095. + * This function is being called from gadget
  49096. + * to enable PCD endpoint.
  49097. + */
  49098. +int dwc_otg_pcd_ep_enable(dwc_otg_pcd_t * pcd,
  49099. + const uint8_t * ep_desc, void *usb_ep)
  49100. +{
  49101. + int num, dir;
  49102. + dwc_otg_pcd_ep_t *ep = NULL;
  49103. + const usb_endpoint_descriptor_t *desc;
  49104. + dwc_irqflags_t flags;
  49105. + fifosize_data_t dptxfsiz = {.d32 = 0 };
  49106. + gdfifocfg_data_t gdfifocfg = {.d32 = 0 };
  49107. + gdfifocfg_data_t gdfifocfgbase = {.d32 = 0 };
  49108. + int retval = 0;
  49109. + int i, epcount;
  49110. + struct device *dev = dwc_otg_pcd_to_dev(pcd);
  49111. +
  49112. + desc = (const usb_endpoint_descriptor_t *)ep_desc;
  49113. +
  49114. + if (!desc) {
  49115. + pcd->ep0.priv = usb_ep;
  49116. + ep = &pcd->ep0;
  49117. + retval = -DWC_E_INVALID;
  49118. + goto out;
  49119. + }
  49120. +
  49121. + num = UE_GET_ADDR(desc->bEndpointAddress);
  49122. + dir = UE_GET_DIR(desc->bEndpointAddress);
  49123. +
  49124. + if (!desc->wMaxPacketSize) {
  49125. + DWC_WARN("bad maxpacketsize\n");
  49126. + retval = -DWC_E_INVALID;
  49127. + goto out;
  49128. + }
  49129. +
  49130. + if (dir == UE_DIR_IN) {
  49131. + epcount = pcd->core_if->dev_if->num_in_eps;
  49132. + for (i = 0; i < epcount; i++) {
  49133. + if (num == pcd->in_ep[i].dwc_ep.num) {
  49134. + ep = &pcd->in_ep[i];
  49135. + break;
  49136. + }
  49137. + }
  49138. + } else {
  49139. + epcount = pcd->core_if->dev_if->num_out_eps;
  49140. + for (i = 0; i < epcount; i++) {
  49141. + if (num == pcd->out_ep[i].dwc_ep.num) {
  49142. + ep = &pcd->out_ep[i];
  49143. + break;
  49144. + }
  49145. + }
  49146. + }
  49147. +
  49148. + if (!ep) {
  49149. + DWC_WARN("bad address\n");
  49150. + retval = -DWC_E_INVALID;
  49151. + goto out;
  49152. + }
  49153. +
  49154. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  49155. +
  49156. + ep->desc = desc;
  49157. + ep->priv = usb_ep;
  49158. +
  49159. + /*
  49160. + * Activate the EP
  49161. + */
  49162. + ep->stopped = 0;
  49163. +
  49164. + ep->dwc_ep.is_in = (dir == UE_DIR_IN);
  49165. + ep->dwc_ep.maxpacket = UGETW(desc->wMaxPacketSize);
  49166. +
  49167. + ep->dwc_ep.type = desc->bmAttributes & UE_XFERTYPE;
  49168. +
  49169. + if (ep->dwc_ep.is_in) {
  49170. + if (!GET_CORE_IF(pcd)->en_multiple_tx_fifo) {
  49171. + ep->dwc_ep.tx_fifo_num = 0;
  49172. +
  49173. + if (ep->dwc_ep.type == UE_ISOCHRONOUS) {
  49174. + /*
  49175. + * if ISOC EP then assign a Periodic Tx FIFO.
  49176. + */
  49177. + ep->dwc_ep.tx_fifo_num =
  49178. + assign_perio_tx_fifo(GET_CORE_IF(pcd));
  49179. + }
  49180. + } else {
  49181. + /*
  49182. + * if Dedicated FIFOs mode is on then assign a Tx FIFO.
  49183. + */
  49184. + ep->dwc_ep.tx_fifo_num =
  49185. + assign_tx_fifo(GET_CORE_IF(pcd));
  49186. + }
  49187. +
  49188. + /* Calculating EP info controller base address */
  49189. + if (ep->dwc_ep.tx_fifo_num
  49190. + && GET_CORE_IF(pcd)->en_multiple_tx_fifo) {
  49191. + gdfifocfg.d32 =
  49192. + DWC_READ_REG32(&GET_CORE_IF(pcd)->
  49193. + core_global_regs->gdfifocfg);
  49194. + gdfifocfgbase.d32 = gdfifocfg.d32 >> 16;
  49195. + dptxfsiz.d32 =
  49196. + (DWC_READ_REG32
  49197. + (&GET_CORE_IF(pcd)->core_global_regs->
  49198. + dtxfsiz[ep->dwc_ep.tx_fifo_num - 1]) >> 16);
  49199. + gdfifocfg.b.epinfobase =
  49200. + gdfifocfgbase.d32 + dptxfsiz.d32;
  49201. + if (GET_CORE_IF(pcd)->snpsid <= OTG_CORE_REV_2_94a) {
  49202. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->
  49203. + core_global_regs->gdfifocfg,
  49204. + gdfifocfg.d32);
  49205. + }
  49206. + }
  49207. + }
  49208. + /* Set initial data PID. */
  49209. + if (ep->dwc_ep.type == UE_BULK) {
  49210. + ep->dwc_ep.data_pid_start = 0;
  49211. + }
  49212. +
  49213. + /* Alloc DMA Descriptors */
  49214. + if (GET_CORE_IF(pcd)->dma_desc_enable) {
  49215. +#ifndef DWC_UTE_PER_IO
  49216. + if (ep->dwc_ep.type != UE_ISOCHRONOUS) {
  49217. +#endif
  49218. + ep->dwc_ep.desc_addr =
  49219. + dwc_otg_ep_alloc_desc_chain(dev,
  49220. + &ep->dwc_ep.dma_desc_addr,
  49221. + MAX_DMA_DESC_CNT);
  49222. + if (!ep->dwc_ep.desc_addr) {
  49223. + DWC_WARN("%s, can't allocate DMA descriptor\n",
  49224. + __func__);
  49225. + retval = -DWC_E_SHUTDOWN;
  49226. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  49227. + goto out;
  49228. + }
  49229. +#ifndef DWC_UTE_PER_IO
  49230. + }
  49231. +#endif
  49232. + }
  49233. +
  49234. + DWC_DEBUGPL(DBG_PCD, "Activate %s: type=%d, mps=%d desc=%p\n",
  49235. + (ep->dwc_ep.is_in ? "IN" : "OUT"),
  49236. + ep->dwc_ep.type, ep->dwc_ep.maxpacket, ep->desc);
  49237. +#ifdef DWC_UTE_PER_IO
  49238. + ep->dwc_ep.xiso_bInterval = 1 << (ep->desc->bInterval - 1);
  49239. +#endif
  49240. + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
  49241. + ep->dwc_ep.bInterval = 1 << (ep->desc->bInterval - 1);
  49242. + ep->dwc_ep.frame_num = 0xFFFFFFFF;
  49243. + }
  49244. +
  49245. + dwc_otg_ep_activate(GET_CORE_IF(pcd), &ep->dwc_ep);
  49246. +
  49247. +#ifdef DWC_UTE_CFI
  49248. + if (pcd->cfi->ops.ep_enable) {
  49249. + pcd->cfi->ops.ep_enable(pcd->cfi, pcd, ep);
  49250. + }
  49251. +#endif
  49252. +
  49253. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  49254. +
  49255. +out:
  49256. + return retval;
  49257. +}
  49258. +
  49259. +/**
  49260. + * This function is being called from gadget
  49261. + * to disable PCD endpoint.
  49262. + */
  49263. +int dwc_otg_pcd_ep_disable(dwc_otg_pcd_t * pcd, void *ep_handle)
  49264. +{
  49265. + dwc_otg_pcd_ep_t *ep;
  49266. + dwc_irqflags_t flags;
  49267. + dwc_otg_dev_dma_desc_t *desc_addr;
  49268. + dwc_dma_t dma_desc_addr;
  49269. + gdfifocfg_data_t gdfifocfgbase = {.d32 = 0 };
  49270. + gdfifocfg_data_t gdfifocfg = {.d32 = 0 };
  49271. + fifosize_data_t dptxfsiz = {.d32 = 0 };
  49272. + struct device *dev = dwc_otg_pcd_to_dev(pcd);
  49273. +
  49274. + ep = get_ep_from_handle(pcd, ep_handle);
  49275. +
  49276. + if (!ep || !ep->desc) {
  49277. + DWC_DEBUGPL(DBG_PCD, "bad ep address\n");
  49278. + return -DWC_E_INVALID;
  49279. + }
  49280. +
  49281. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  49282. +
  49283. + dwc_otg_request_nuke(ep);
  49284. +
  49285. + dwc_otg_ep_deactivate(GET_CORE_IF(pcd), &ep->dwc_ep);
  49286. + if (pcd->core_if->core_params->dev_out_nak) {
  49287. + DWC_TIMER_CANCEL(pcd->core_if->ep_xfer_timer[ep->dwc_ep.num]);
  49288. + pcd->core_if->ep_xfer_info[ep->dwc_ep.num].state = 0;
  49289. + }
  49290. + ep->desc = NULL;
  49291. + ep->stopped = 1;
  49292. +
  49293. + gdfifocfg.d32 =
  49294. + DWC_READ_REG32(&GET_CORE_IF(pcd)->core_global_regs->gdfifocfg);
  49295. + gdfifocfgbase.d32 = gdfifocfg.d32 >> 16;
  49296. +
  49297. + if (ep->dwc_ep.is_in) {
  49298. + if (GET_CORE_IF(pcd)->en_multiple_tx_fifo) {
  49299. + /* Flush the Tx FIFO */
  49300. + dwc_otg_flush_tx_fifo(GET_CORE_IF(pcd),
  49301. + ep->dwc_ep.tx_fifo_num);
  49302. + }
  49303. + release_perio_tx_fifo(GET_CORE_IF(pcd), ep->dwc_ep.tx_fifo_num);
  49304. + release_tx_fifo(GET_CORE_IF(pcd), ep->dwc_ep.tx_fifo_num);
  49305. + if (GET_CORE_IF(pcd)->en_multiple_tx_fifo) {
  49306. + /* Decreasing EPinfo Base Addr */
  49307. + dptxfsiz.d32 =
  49308. + (DWC_READ_REG32
  49309. + (&GET_CORE_IF(pcd)->
  49310. + core_global_regs->dtxfsiz[ep->dwc_ep.tx_fifo_num-1]) >> 16);
  49311. + gdfifocfg.b.epinfobase = gdfifocfgbase.d32 - dptxfsiz.d32;
  49312. + if (GET_CORE_IF(pcd)->snpsid <= OTG_CORE_REV_2_94a) {
  49313. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gdfifocfg,
  49314. + gdfifocfg.d32);
  49315. + }
  49316. + }
  49317. + }
  49318. +
  49319. + /* Free DMA Descriptors */
  49320. + if (GET_CORE_IF(pcd)->dma_desc_enable) {
  49321. + if (ep->dwc_ep.type != UE_ISOCHRONOUS) {
  49322. + desc_addr = ep->dwc_ep.desc_addr;
  49323. + dma_desc_addr = ep->dwc_ep.dma_desc_addr;
  49324. +
  49325. + /* Cannot call dma_free_coherent() with IRQs disabled */
  49326. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  49327. + dwc_otg_ep_free_desc_chain(dev, desc_addr, dma_desc_addr,
  49328. + MAX_DMA_DESC_CNT);
  49329. +
  49330. + goto out_unlocked;
  49331. + }
  49332. + }
  49333. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  49334. +
  49335. +out_unlocked:
  49336. + DWC_DEBUGPL(DBG_PCD, "%d %s disabled\n", ep->dwc_ep.num,
  49337. + ep->dwc_ep.is_in ? "IN" : "OUT");
  49338. + return 0;
  49339. +
  49340. +}
  49341. +
  49342. +/******************************************************************************/
  49343. +#ifdef DWC_UTE_PER_IO
  49344. +
  49345. +/**
  49346. + * Free the request and its extended parts
  49347. + *
  49348. + */
  49349. +void dwc_pcd_xiso_ereq_free(dwc_otg_pcd_ep_t * ep, dwc_otg_pcd_request_t * req)
  49350. +{
  49351. + DWC_FREE(req->ext_req.per_io_frame_descs);
  49352. + DWC_FREE(req);
  49353. +}
  49354. +
  49355. +/**
  49356. + * Start the next request in the endpoint's queue.
  49357. + *
  49358. + */
  49359. +int dwc_otg_pcd_xiso_start_next_request(dwc_otg_pcd_t * pcd,
  49360. + dwc_otg_pcd_ep_t * ep)
  49361. +{
  49362. + int i;
  49363. + dwc_otg_pcd_request_t *req = NULL;
  49364. + dwc_ep_t *dwcep = NULL;
  49365. + struct dwc_iso_xreq_port *ereq = NULL;
  49366. + struct dwc_iso_pkt_desc_port *ddesc_iso;
  49367. + uint16_t nat;
  49368. + depctl_data_t diepctl;
  49369. +
  49370. + dwcep = &ep->dwc_ep;
  49371. +
  49372. + if (dwcep->xiso_active_xfers > 0) {
  49373. +#if 0 //Disable this to decrease s/w overhead that is crucial for Isoc transfers
  49374. + DWC_WARN("There are currently active transfers for EP%d \
  49375. + (active=%d; queued=%d)", dwcep->num, dwcep->xiso_active_xfers,
  49376. + dwcep->xiso_queued_xfers);
  49377. +#endif
  49378. + return 0;
  49379. + }
  49380. +
  49381. + nat = UGETW(ep->desc->wMaxPacketSize);
  49382. + nat = (nat >> 11) & 0x03;
  49383. +
  49384. + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  49385. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  49386. + ereq = &req->ext_req;
  49387. + ep->stopped = 0;
  49388. +
  49389. + /* Get the frame number */
  49390. + dwcep->xiso_frame_num =
  49391. + dwc_otg_get_frame_number(GET_CORE_IF(pcd));
  49392. + DWC_DEBUG("FRM_NUM=%d", dwcep->xiso_frame_num);
  49393. +
  49394. + ddesc_iso = ereq->per_io_frame_descs;
  49395. +
  49396. + if (dwcep->is_in) {
  49397. + /* Setup DMA Descriptor chain for IN Isoc request */
  49398. + for (i = 0; i < ereq->pio_pkt_count; i++) {
  49399. + //if ((i % (nat + 1)) == 0)
  49400. + if ( i > 0 )
  49401. + dwcep->xiso_frame_num =
  49402. + (dwcep->xiso_bInterval +
  49403. + dwcep->xiso_frame_num) & 0x3FFF;
  49404. + dwcep->desc_addr[i].buf =
  49405. + req->dma + ddesc_iso[i].offset;
  49406. + dwcep->desc_addr[i].status.b_iso_in.txbytes =
  49407. + ddesc_iso[i].length;
  49408. + dwcep->desc_addr[i].status.b_iso_in.framenum =
  49409. + dwcep->xiso_frame_num;
  49410. + dwcep->desc_addr[i].status.b_iso_in.bs =
  49411. + BS_HOST_READY;
  49412. + dwcep->desc_addr[i].status.b_iso_in.txsts = 0;
  49413. + dwcep->desc_addr[i].status.b_iso_in.sp =
  49414. + (ddesc_iso[i].length %
  49415. + dwcep->maxpacket) ? 1 : 0;
  49416. + dwcep->desc_addr[i].status.b_iso_in.ioc = 0;
  49417. + dwcep->desc_addr[i].status.b_iso_in.pid = nat + 1;
  49418. + dwcep->desc_addr[i].status.b_iso_in.l = 0;
  49419. +
  49420. + /* Process the last descriptor */
  49421. + if (i == ereq->pio_pkt_count - 1) {
  49422. + dwcep->desc_addr[i].status.b_iso_in.ioc = 1;
  49423. + dwcep->desc_addr[i].status.b_iso_in.l = 1;
  49424. + }
  49425. + }
  49426. +
  49427. + /* Setup and start the transfer for this endpoint */
  49428. + dwcep->xiso_active_xfers++;
  49429. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->dev_if->
  49430. + in_ep_regs[dwcep->num]->diepdma,
  49431. + dwcep->dma_desc_addr);
  49432. + diepctl.d32 = 0;
  49433. + diepctl.b.epena = 1;
  49434. + diepctl.b.cnak = 1;
  49435. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->dev_if->
  49436. + in_ep_regs[dwcep->num]->diepctl, 0,
  49437. + diepctl.d32);
  49438. + } else {
  49439. + /* Setup DMA Descriptor chain for OUT Isoc request */
  49440. + for (i = 0; i < ereq->pio_pkt_count; i++) {
  49441. + //if ((i % (nat + 1)) == 0)
  49442. + dwcep->xiso_frame_num = (dwcep->xiso_bInterval +
  49443. + dwcep->xiso_frame_num) & 0x3FFF;
  49444. + dwcep->desc_addr[i].buf =
  49445. + req->dma + ddesc_iso[i].offset;
  49446. + dwcep->desc_addr[i].status.b_iso_out.rxbytes =
  49447. + ddesc_iso[i].length;
  49448. + dwcep->desc_addr[i].status.b_iso_out.framenum =
  49449. + dwcep->xiso_frame_num;
  49450. + dwcep->desc_addr[i].status.b_iso_out.bs =
  49451. + BS_HOST_READY;
  49452. + dwcep->desc_addr[i].status.b_iso_out.rxsts = 0;
  49453. + dwcep->desc_addr[i].status.b_iso_out.sp =
  49454. + (ddesc_iso[i].length %
  49455. + dwcep->maxpacket) ? 1 : 0;
  49456. + dwcep->desc_addr[i].status.b_iso_out.ioc = 0;
  49457. + dwcep->desc_addr[i].status.b_iso_out.pid = nat + 1;
  49458. + dwcep->desc_addr[i].status.b_iso_out.l = 0;
  49459. +
  49460. + /* Process the last descriptor */
  49461. + if (i == ereq->pio_pkt_count - 1) {
  49462. + dwcep->desc_addr[i].status.b_iso_out.ioc = 1;
  49463. + dwcep->desc_addr[i].status.b_iso_out.l = 1;
  49464. + }
  49465. + }
  49466. +
  49467. + /* Setup and start the transfer for this endpoint */
  49468. + dwcep->xiso_active_xfers++;
  49469. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->
  49470. + dev_if->out_ep_regs[dwcep->num]->
  49471. + doepdma, dwcep->dma_desc_addr);
  49472. + diepctl.d32 = 0;
  49473. + diepctl.b.epena = 1;
  49474. + diepctl.b.cnak = 1;
  49475. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->
  49476. + dev_if->out_ep_regs[dwcep->num]->
  49477. + doepctl, 0, diepctl.d32);
  49478. + }
  49479. +
  49480. + } else {
  49481. + ep->stopped = 1;
  49482. + }
  49483. +
  49484. + return 0;
  49485. +}
  49486. +
  49487. +/**
  49488. + * - Remove the request from the queue
  49489. + */
  49490. +void complete_xiso_ep(dwc_otg_pcd_ep_t * ep)
  49491. +{
  49492. + dwc_otg_pcd_request_t *req = NULL;
  49493. + struct dwc_iso_xreq_port *ereq = NULL;
  49494. + struct dwc_iso_pkt_desc_port *ddesc_iso = NULL;
  49495. + dwc_ep_t *dwcep = NULL;
  49496. + int i;
  49497. +
  49498. + //DWC_DEBUG();
  49499. + dwcep = &ep->dwc_ep;
  49500. +
  49501. + /* Get the first pending request from the queue */
  49502. + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  49503. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  49504. + if (!req) {
  49505. + DWC_PRINTF("complete_ep 0x%p, req = NULL!\n", ep);
  49506. + return;
  49507. + }
  49508. + dwcep->xiso_active_xfers--;
  49509. + dwcep->xiso_queued_xfers--;
  49510. + /* Remove this request from the queue */
  49511. + DWC_CIRCLEQ_REMOVE_INIT(&ep->queue, req, queue_entry);
  49512. + } else {
  49513. + DWC_PRINTF("complete_ep 0x%p, ep->queue empty!\n", ep);
  49514. + return;
  49515. + }
  49516. +
  49517. + ep->stopped = 1;
  49518. + ereq = &req->ext_req;
  49519. + ddesc_iso = ereq->per_io_frame_descs;
  49520. +
  49521. + if (dwcep->xiso_active_xfers < 0) {
  49522. + DWC_WARN("EP#%d (xiso_active_xfers=%d)", dwcep->num,
  49523. + dwcep->xiso_active_xfers);
  49524. + }
  49525. +
  49526. + /* Fill the Isoc descs of portable extended req from dma descriptors */
  49527. + for (i = 0; i < ereq->pio_pkt_count; i++) {
  49528. + if (dwcep->is_in) { /* IN endpoints */
  49529. + ddesc_iso[i].actual_length = ddesc_iso[i].length -
  49530. + dwcep->desc_addr[i].status.b_iso_in.txbytes;
  49531. + ddesc_iso[i].status =
  49532. + dwcep->desc_addr[i].status.b_iso_in.txsts;
  49533. + } else { /* OUT endpoints */
  49534. + ddesc_iso[i].actual_length = ddesc_iso[i].length -
  49535. + dwcep->desc_addr[i].status.b_iso_out.rxbytes;
  49536. + ddesc_iso[i].status =
  49537. + dwcep->desc_addr[i].status.b_iso_out.rxsts;
  49538. + }
  49539. + }
  49540. +
  49541. + DWC_SPINUNLOCK(ep->pcd->lock);
  49542. +
  49543. + /* Call the completion function in the non-portable logic */
  49544. + ep->pcd->fops->xisoc_complete(ep->pcd, ep->priv, req->priv, 0,
  49545. + &req->ext_req);
  49546. +
  49547. + DWC_SPINLOCK(ep->pcd->lock);
  49548. +
  49549. + /* Free the request - specific freeing needed for extended request object */
  49550. + dwc_pcd_xiso_ereq_free(ep, req);
  49551. +
  49552. + /* Start the next request */
  49553. + dwc_otg_pcd_xiso_start_next_request(ep->pcd, ep);
  49554. +
  49555. + return;
  49556. +}
  49557. +
  49558. +/**
  49559. + * Create and initialize the Isoc pkt descriptors of the extended request.
  49560. + *
  49561. + */
  49562. +static int dwc_otg_pcd_xiso_create_pkt_descs(dwc_otg_pcd_request_t * req,
  49563. + void *ereq_nonport,
  49564. + int atomic_alloc)
  49565. +{
  49566. + struct dwc_iso_xreq_port *ereq = NULL;
  49567. + struct dwc_iso_xreq_port *req_mapped = NULL;
  49568. + struct dwc_iso_pkt_desc_port *ipds = NULL; /* To be created in this function */
  49569. + uint32_t pkt_count;
  49570. + int i;
  49571. +
  49572. + ereq = &req->ext_req;
  49573. + req_mapped = (struct dwc_iso_xreq_port *)ereq_nonport;
  49574. + pkt_count = req_mapped->pio_pkt_count;
  49575. +
  49576. + /* Create the isoc descs */
  49577. + if (atomic_alloc) {
  49578. + ipds = DWC_ALLOC_ATOMIC(sizeof(*ipds) * pkt_count);
  49579. + } else {
  49580. + ipds = DWC_ALLOC(sizeof(*ipds) * pkt_count);
  49581. + }
  49582. +
  49583. + if (!ipds) {
  49584. + DWC_ERROR("Failed to allocate isoc descriptors");
  49585. + return -DWC_E_NO_MEMORY;
  49586. + }
  49587. +
  49588. + /* Initialize the extended request fields */
  49589. + ereq->per_io_frame_descs = ipds;
  49590. + ereq->error_count = 0;
  49591. + ereq->pio_alloc_pkt_count = pkt_count;
  49592. + ereq->pio_pkt_count = pkt_count;
  49593. + ereq->tr_sub_flags = req_mapped->tr_sub_flags;
  49594. +
  49595. + /* Init the Isoc descriptors */
  49596. + for (i = 0; i < pkt_count; i++) {
  49597. + ipds[i].length = req_mapped->per_io_frame_descs[i].length;
  49598. + ipds[i].offset = req_mapped->per_io_frame_descs[i].offset;
  49599. + ipds[i].status = req_mapped->per_io_frame_descs[i].status; /* 0 */
  49600. + ipds[i].actual_length =
  49601. + req_mapped->per_io_frame_descs[i].actual_length;
  49602. + }
  49603. +
  49604. + return 0;
  49605. +}
  49606. +
  49607. +static void prn_ext_request(struct dwc_iso_xreq_port *ereq)
  49608. +{
  49609. + struct dwc_iso_pkt_desc_port *xfd = NULL;
  49610. + int i;
  49611. +
  49612. + DWC_DEBUG("per_io_frame_descs=%p", ereq->per_io_frame_descs);
  49613. + DWC_DEBUG("tr_sub_flags=%d", ereq->tr_sub_flags);
  49614. + DWC_DEBUG("error_count=%d", ereq->error_count);
  49615. + DWC_DEBUG("pio_alloc_pkt_count=%d", ereq->pio_alloc_pkt_count);
  49616. + DWC_DEBUG("pio_pkt_count=%d", ereq->pio_pkt_count);
  49617. + DWC_DEBUG("res=%d", ereq->res);
  49618. +
  49619. + for (i = 0; i < ereq->pio_pkt_count; i++) {
  49620. + xfd = &ereq->per_io_frame_descs[0];
  49621. + DWC_DEBUG("FD #%d", i);
  49622. +
  49623. + DWC_DEBUG("xfd->actual_length=%d", xfd->actual_length);
  49624. + DWC_DEBUG("xfd->length=%d", xfd->length);
  49625. + DWC_DEBUG("xfd->offset=%d", xfd->offset);
  49626. + DWC_DEBUG("xfd->status=%d", xfd->status);
  49627. + }
  49628. +}
  49629. +
  49630. +/**
  49631. + *
  49632. + */
  49633. +int dwc_otg_pcd_xiso_ep_queue(dwc_otg_pcd_t * pcd, void *ep_handle,
  49634. + uint8_t * buf, dwc_dma_t dma_buf, uint32_t buflen,
  49635. + int zero, void *req_handle, int atomic_alloc,
  49636. + void *ereq_nonport)
  49637. +{
  49638. + dwc_otg_pcd_request_t *req = NULL;
  49639. + dwc_otg_pcd_ep_t *ep;
  49640. + dwc_irqflags_t flags;
  49641. + int res;
  49642. +
  49643. + ep = get_ep_from_handle(pcd, ep_handle);
  49644. + if (!ep) {
  49645. + DWC_WARN("bad ep\n");
  49646. + return -DWC_E_INVALID;
  49647. + }
  49648. +
  49649. + /* We support this extension only for DDMA mode */
  49650. + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC)
  49651. + if (!GET_CORE_IF(pcd)->dma_desc_enable)
  49652. + return -DWC_E_INVALID;
  49653. +
  49654. + /* Create a dwc_otg_pcd_request_t object */
  49655. + if (atomic_alloc) {
  49656. + req = DWC_ALLOC_ATOMIC(sizeof(*req));
  49657. + } else {
  49658. + req = DWC_ALLOC(sizeof(*req));
  49659. + }
  49660. +
  49661. + if (!req) {
  49662. + return -DWC_E_NO_MEMORY;
  49663. + }
  49664. +
  49665. + /* Create the Isoc descs for this request which shall be the exact match
  49666. + * of the structure sent to us from the non-portable logic */
  49667. + res =
  49668. + dwc_otg_pcd_xiso_create_pkt_descs(req, ereq_nonport, atomic_alloc);
  49669. + if (res) {
  49670. + DWC_WARN("Failed to init the Isoc descriptors");
  49671. + DWC_FREE(req);
  49672. + return res;
  49673. + }
  49674. +
  49675. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  49676. +
  49677. + DWC_CIRCLEQ_INIT_ENTRY(req, queue_entry);
  49678. + req->buf = buf;
  49679. + req->dma = dma_buf;
  49680. + req->length = buflen;
  49681. + req->sent_zlp = zero;
  49682. + req->priv = req_handle;
  49683. +
  49684. + //DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  49685. + ep->dwc_ep.dma_addr = dma_buf;
  49686. + ep->dwc_ep.start_xfer_buff = buf;
  49687. + ep->dwc_ep.xfer_buff = buf;
  49688. + ep->dwc_ep.xfer_len = 0;
  49689. + ep->dwc_ep.xfer_count = 0;
  49690. + ep->dwc_ep.sent_zlp = 0;
  49691. + ep->dwc_ep.total_len = buflen;
  49692. +
  49693. + /* Add this request to the tail */
  49694. + DWC_CIRCLEQ_INSERT_TAIL(&ep->queue, req, queue_entry);
  49695. + ep->dwc_ep.xiso_queued_xfers++;
  49696. +
  49697. +//DWC_DEBUG("CP_0");
  49698. +//DWC_DEBUG("req->ext_req.tr_sub_flags=%d", req->ext_req.tr_sub_flags);
  49699. +//prn_ext_request((struct dwc_iso_xreq_port *) ereq_nonport);
  49700. +//prn_ext_request(&req->ext_req);
  49701. +
  49702. + //DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  49703. +
  49704. + /* If the req->status == ASAP then check if there is any active transfer
  49705. + * for this endpoint. If no active transfers, then get the first entry
  49706. + * from the queue and start that transfer
  49707. + */
  49708. + if (req->ext_req.tr_sub_flags == DWC_EREQ_TF_ASAP) {
  49709. + res = dwc_otg_pcd_xiso_start_next_request(pcd, ep);
  49710. + if (res) {
  49711. + DWC_WARN("Failed to start the next Isoc transfer");
  49712. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  49713. + DWC_FREE(req);
  49714. + return res;
  49715. + }
  49716. + }
  49717. +
  49718. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  49719. + return 0;
  49720. +}
  49721. +
  49722. +#endif
  49723. +/* END ifdef DWC_UTE_PER_IO ***************************************************/
  49724. +int dwc_otg_pcd_ep_queue(dwc_otg_pcd_t * pcd, void *ep_handle,
  49725. + uint8_t * buf, dwc_dma_t dma_buf, uint32_t buflen,
  49726. + int zero, void *req_handle, int atomic_alloc)
  49727. +{
  49728. + struct device *dev = dwc_otg_pcd_to_dev(pcd);
  49729. + dwc_irqflags_t flags;
  49730. + dwc_otg_pcd_request_t *req;
  49731. + dwc_otg_pcd_ep_t *ep;
  49732. + uint32_t max_transfer;
  49733. +
  49734. + ep = get_ep_from_handle(pcd, ep_handle);
  49735. + if (!ep || (!ep->desc && ep->dwc_ep.num != 0)) {
  49736. + DWC_WARN("bad ep\n");
  49737. + return -DWC_E_INVALID;
  49738. + }
  49739. +
  49740. + if (atomic_alloc) {
  49741. + req = DWC_ALLOC_ATOMIC(sizeof(*req));
  49742. + } else {
  49743. + req = DWC_ALLOC(sizeof(*req));
  49744. + }
  49745. +
  49746. + if (!req) {
  49747. + return -DWC_E_NO_MEMORY;
  49748. + }
  49749. + DWC_CIRCLEQ_INIT_ENTRY(req, queue_entry);
  49750. + if (!GET_CORE_IF(pcd)->core_params->opt) {
  49751. + if (ep->dwc_ep.num != 0) {
  49752. + DWC_ERROR("queue req %p, len %d buf %p\n",
  49753. + req_handle, buflen, buf);
  49754. + }
  49755. + }
  49756. +
  49757. + req->buf = buf;
  49758. + req->dma = dma_buf;
  49759. + req->length = buflen;
  49760. + req->sent_zlp = zero;
  49761. + req->priv = req_handle;
  49762. + req->dw_align_buf = NULL;
  49763. + if ((dma_buf & 0x3) && GET_CORE_IF(pcd)->dma_enable
  49764. + && !GET_CORE_IF(pcd)->dma_desc_enable)
  49765. + req->dw_align_buf = DWC_DMA_ALLOC(dev, buflen,
  49766. + &req->dw_align_buf_dma);
  49767. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  49768. +
  49769. + /*
  49770. + * After adding request to the queue for IN ISOC wait for In Token Received
  49771. + * when TX FIFO is empty interrupt and for OUT ISOC wait for OUT Token
  49772. + * Received when EP is disabled interrupt to obtain starting microframe
  49773. + * (odd/even) start transfer
  49774. + */
  49775. + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
  49776. + if (req != 0) {
  49777. + depctl_data_t depctl = {.d32 =
  49778. + DWC_READ_REG32(&pcd->core_if->dev_if->
  49779. + in_ep_regs[ep->dwc_ep.num]->
  49780. + diepctl) };
  49781. + ++pcd->request_pending;
  49782. +
  49783. + DWC_CIRCLEQ_INSERT_TAIL(&ep->queue, req, queue_entry);
  49784. + if (ep->dwc_ep.is_in) {
  49785. + depctl.b.cnak = 1;
  49786. + DWC_WRITE_REG32(&pcd->core_if->dev_if->
  49787. + in_ep_regs[ep->dwc_ep.num]->
  49788. + diepctl, depctl.d32);
  49789. + }
  49790. +
  49791. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  49792. + }
  49793. + return 0;
  49794. + }
  49795. +
  49796. + /*
  49797. + * For EP0 IN without premature status, zlp is required?
  49798. + */
  49799. + if (ep->dwc_ep.num == 0 && ep->dwc_ep.is_in) {
  49800. + DWC_DEBUGPL(DBG_PCDV, "%d-OUT ZLP\n", ep->dwc_ep.num);
  49801. + //_req->zero = 1;
  49802. + }
  49803. +
  49804. + /* Start the transfer */
  49805. + if (DWC_CIRCLEQ_EMPTY(&ep->queue) && !ep->stopped) {
  49806. + /* EP0 Transfer? */
  49807. + if (ep->dwc_ep.num == 0) {
  49808. + switch (pcd->ep0state) {
  49809. + case EP0_IN_DATA_PHASE:
  49810. + DWC_DEBUGPL(DBG_PCD,
  49811. + "%s ep0: EP0_IN_DATA_PHASE\n",
  49812. + __func__);
  49813. + break;
  49814. +
  49815. + case EP0_OUT_DATA_PHASE:
  49816. + DWC_DEBUGPL(DBG_PCD,
  49817. + "%s ep0: EP0_OUT_DATA_PHASE\n",
  49818. + __func__);
  49819. + if (pcd->request_config) {
  49820. + /* Complete STATUS PHASE */
  49821. + ep->dwc_ep.is_in = 1;
  49822. + pcd->ep0state = EP0_IN_STATUS_PHASE;
  49823. + }
  49824. + break;
  49825. +
  49826. + case EP0_IN_STATUS_PHASE:
  49827. + DWC_DEBUGPL(DBG_PCD,
  49828. + "%s ep0: EP0_IN_STATUS_PHASE\n",
  49829. + __func__);
  49830. + break;
  49831. +
  49832. + default:
  49833. + DWC_DEBUGPL(DBG_ANY, "ep0: odd state %d\n",
  49834. + pcd->ep0state);
  49835. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  49836. + return -DWC_E_SHUTDOWN;
  49837. + }
  49838. +
  49839. + ep->dwc_ep.dma_addr = dma_buf;
  49840. + ep->dwc_ep.start_xfer_buff = buf;
  49841. + ep->dwc_ep.xfer_buff = buf;
  49842. + ep->dwc_ep.xfer_len = buflen;
  49843. + ep->dwc_ep.xfer_count = 0;
  49844. + ep->dwc_ep.sent_zlp = 0;
  49845. + ep->dwc_ep.total_len = ep->dwc_ep.xfer_len;
  49846. +
  49847. + if (zero) {
  49848. + if ((ep->dwc_ep.xfer_len %
  49849. + ep->dwc_ep.maxpacket == 0)
  49850. + && (ep->dwc_ep.xfer_len != 0)) {
  49851. + ep->dwc_ep.sent_zlp = 1;
  49852. + }
  49853. +
  49854. + }
  49855. +
  49856. + dwc_otg_ep0_start_transfer(GET_CORE_IF(pcd),
  49857. + &ep->dwc_ep);
  49858. + } // non-ep0 endpoints
  49859. + else {
  49860. +#ifdef DWC_UTE_CFI
  49861. + if (ep->dwc_ep.buff_mode != BM_STANDARD) {
  49862. + /* store the request length */
  49863. + ep->dwc_ep.cfi_req_len = buflen;
  49864. + pcd->cfi->ops.build_descriptors(pcd->cfi, pcd,
  49865. + ep, req);
  49866. + } else {
  49867. +#endif
  49868. + max_transfer =
  49869. + GET_CORE_IF(ep->pcd)->core_params->
  49870. + max_transfer_size;
  49871. +
  49872. + /* Setup and start the Transfer */
  49873. + if (req->dw_align_buf){
  49874. + if (ep->dwc_ep.is_in)
  49875. + dwc_memcpy(req->dw_align_buf,
  49876. + buf, buflen);
  49877. + ep->dwc_ep.dma_addr =
  49878. + req->dw_align_buf_dma;
  49879. + ep->dwc_ep.start_xfer_buff =
  49880. + req->dw_align_buf;
  49881. + ep->dwc_ep.xfer_buff =
  49882. + req->dw_align_buf;
  49883. + } else {
  49884. + ep->dwc_ep.dma_addr = dma_buf;
  49885. + ep->dwc_ep.start_xfer_buff = buf;
  49886. + ep->dwc_ep.xfer_buff = buf;
  49887. + }
  49888. + ep->dwc_ep.xfer_len = 0;
  49889. + ep->dwc_ep.xfer_count = 0;
  49890. + ep->dwc_ep.sent_zlp = 0;
  49891. + ep->dwc_ep.total_len = buflen;
  49892. +
  49893. + ep->dwc_ep.maxxfer = max_transfer;
  49894. + if (GET_CORE_IF(pcd)->dma_desc_enable) {
  49895. + uint32_t out_max_xfer =
  49896. + DDMA_MAX_TRANSFER_SIZE -
  49897. + (DDMA_MAX_TRANSFER_SIZE % 4);
  49898. + if (ep->dwc_ep.is_in) {
  49899. + if (ep->dwc_ep.maxxfer >
  49900. + DDMA_MAX_TRANSFER_SIZE) {
  49901. + ep->dwc_ep.maxxfer =
  49902. + DDMA_MAX_TRANSFER_SIZE;
  49903. + }
  49904. + } else {
  49905. + if (ep->dwc_ep.maxxfer >
  49906. + out_max_xfer) {
  49907. + ep->dwc_ep.maxxfer =
  49908. + out_max_xfer;
  49909. + }
  49910. + }
  49911. + }
  49912. + if (ep->dwc_ep.maxxfer < ep->dwc_ep.total_len) {
  49913. + ep->dwc_ep.maxxfer -=
  49914. + (ep->dwc_ep.maxxfer %
  49915. + ep->dwc_ep.maxpacket);
  49916. + }
  49917. +
  49918. + if (zero) {
  49919. + if ((ep->dwc_ep.total_len %
  49920. + ep->dwc_ep.maxpacket == 0)
  49921. + && (ep->dwc_ep.total_len != 0)) {
  49922. + ep->dwc_ep.sent_zlp = 1;
  49923. + }
  49924. + }
  49925. +#ifdef DWC_UTE_CFI
  49926. + }
  49927. +#endif
  49928. + dwc_otg_ep_start_transfer(GET_CORE_IF(pcd),
  49929. + &ep->dwc_ep);
  49930. + }
  49931. + }
  49932. +
  49933. + if (req != 0) {
  49934. + ++pcd->request_pending;
  49935. + DWC_CIRCLEQ_INSERT_TAIL(&ep->queue, req, queue_entry);
  49936. + if (ep->dwc_ep.is_in && ep->stopped
  49937. + && !(GET_CORE_IF(pcd)->dma_enable)) {
  49938. + /** @todo NGS Create a function for this. */
  49939. + diepmsk_data_t diepmsk = {.d32 = 0 };
  49940. + diepmsk.b.intktxfemp = 1;
  49941. + if (GET_CORE_IF(pcd)->multiproc_int_enable) {
  49942. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->
  49943. + dev_if->dev_global_regs->diepeachintmsk
  49944. + [ep->dwc_ep.num], 0,
  49945. + diepmsk.d32);
  49946. + } else {
  49947. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->
  49948. + dev_if->dev_global_regs->
  49949. + diepmsk, 0, diepmsk.d32);
  49950. + }
  49951. +
  49952. + }
  49953. + }
  49954. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  49955. +
  49956. + return 0;
  49957. +}
  49958. +
  49959. +int dwc_otg_pcd_ep_dequeue(dwc_otg_pcd_t * pcd, void *ep_handle,
  49960. + void *req_handle)
  49961. +{
  49962. + dwc_irqflags_t flags;
  49963. + dwc_otg_pcd_request_t *req;
  49964. + dwc_otg_pcd_ep_t *ep;
  49965. +
  49966. + ep = get_ep_from_handle(pcd, ep_handle);
  49967. + if (!ep || (!ep->desc && ep->dwc_ep.num != 0)) {
  49968. + DWC_WARN("bad argument\n");
  49969. + return -DWC_E_INVALID;
  49970. + }
  49971. +
  49972. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  49973. +
  49974. + /* make sure it's actually queued on this endpoint */
  49975. + DWC_CIRCLEQ_FOREACH(req, &ep->queue, queue_entry) {
  49976. + if (req->priv == (void *)req_handle) {
  49977. + break;
  49978. + }
  49979. + }
  49980. +
  49981. + if (req->priv != (void *)req_handle) {
  49982. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  49983. + return -DWC_E_INVALID;
  49984. + }
  49985. +
  49986. + if (!DWC_CIRCLEQ_EMPTY_ENTRY(req, queue_entry)) {
  49987. + dwc_otg_request_done(ep, req, -DWC_E_RESTART);
  49988. + } else {
  49989. + req = NULL;
  49990. + }
  49991. +
  49992. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  49993. +
  49994. + return req ? 0 : -DWC_E_SHUTDOWN;
  49995. +
  49996. +}
  49997. +
  49998. +/**
  49999. + * dwc_otg_pcd_ep_wedge - sets the halt feature and ignores clear requests
  50000. + *
  50001. + * Use this to stall an endpoint and ignore CLEAR_FEATURE(HALT_ENDPOINT)
  50002. + * requests. If the gadget driver clears the halt status, it will
  50003. + * automatically unwedge the endpoint.
  50004. + *
  50005. + * Returns zero on success, else negative DWC error code.
  50006. + */
  50007. +int dwc_otg_pcd_ep_wedge(dwc_otg_pcd_t * pcd, void *ep_handle)
  50008. +{
  50009. + dwc_otg_pcd_ep_t *ep;
  50010. + dwc_irqflags_t flags;
  50011. + int retval = 0;
  50012. +
  50013. + ep = get_ep_from_handle(pcd, ep_handle);
  50014. +
  50015. + if ((!ep->desc && ep != &pcd->ep0) ||
  50016. + (ep->desc && (ep->desc->bmAttributes == UE_ISOCHRONOUS))) {
  50017. + DWC_WARN("%s, bad ep\n", __func__);
  50018. + return -DWC_E_INVALID;
  50019. + }
  50020. +
  50021. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  50022. + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  50023. + DWC_WARN("%d %s XFer In process\n", ep->dwc_ep.num,
  50024. + ep->dwc_ep.is_in ? "IN" : "OUT");
  50025. + retval = -DWC_E_AGAIN;
  50026. + } else {
  50027. + /* This code needs to be reviewed */
  50028. + if (ep->dwc_ep.is_in == 1 && GET_CORE_IF(pcd)->dma_desc_enable) {
  50029. + dtxfsts_data_t txstatus;
  50030. + fifosize_data_t txfifosize;
  50031. +
  50032. + txfifosize.d32 =
  50033. + DWC_READ_REG32(&GET_CORE_IF(pcd)->
  50034. + core_global_regs->dtxfsiz[ep->dwc_ep.
  50035. + tx_fifo_num]);
  50036. + txstatus.d32 =
  50037. + DWC_READ_REG32(&GET_CORE_IF(pcd)->
  50038. + dev_if->in_ep_regs[ep->dwc_ep.num]->
  50039. + dtxfsts);
  50040. +
  50041. + if (txstatus.b.txfspcavail < txfifosize.b.depth) {
  50042. + DWC_WARN("%s() Data In Tx Fifo\n", __func__);
  50043. + retval = -DWC_E_AGAIN;
  50044. + } else {
  50045. + if (ep->dwc_ep.num == 0) {
  50046. + pcd->ep0state = EP0_STALL;
  50047. + }
  50048. +
  50049. + ep->stopped = 1;
  50050. + dwc_otg_ep_set_stall(GET_CORE_IF(pcd),
  50051. + &ep->dwc_ep);
  50052. + }
  50053. + } else {
  50054. + if (ep->dwc_ep.num == 0) {
  50055. + pcd->ep0state = EP0_STALL;
  50056. + }
  50057. +
  50058. + ep->stopped = 1;
  50059. + dwc_otg_ep_set_stall(GET_CORE_IF(pcd), &ep->dwc_ep);
  50060. + }
  50061. + }
  50062. +
  50063. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  50064. +
  50065. + return retval;
  50066. +}
  50067. +
  50068. +int dwc_otg_pcd_ep_halt(dwc_otg_pcd_t * pcd, void *ep_handle, int value)
  50069. +{
  50070. + dwc_otg_pcd_ep_t *ep;
  50071. + dwc_irqflags_t flags;
  50072. + int retval = 0;
  50073. +
  50074. + ep = get_ep_from_handle(pcd, ep_handle);
  50075. +
  50076. + if (!ep || (!ep->desc && ep != &pcd->ep0) ||
  50077. + (ep->desc && (ep->desc->bmAttributes == UE_ISOCHRONOUS))) {
  50078. + DWC_WARN("%s, bad ep\n", __func__);
  50079. + return -DWC_E_INVALID;
  50080. + }
  50081. +
  50082. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  50083. + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  50084. + DWC_WARN("%d %s XFer In process\n", ep->dwc_ep.num,
  50085. + ep->dwc_ep.is_in ? "IN" : "OUT");
  50086. + retval = -DWC_E_AGAIN;
  50087. + } else if (value == 0) {
  50088. + dwc_otg_ep_clear_stall(GET_CORE_IF(pcd), &ep->dwc_ep);
  50089. + } else if (value == 1) {
  50090. + if (ep->dwc_ep.is_in == 1 && GET_CORE_IF(pcd)->dma_desc_enable) {
  50091. + dtxfsts_data_t txstatus;
  50092. + fifosize_data_t txfifosize;
  50093. +
  50094. + txfifosize.d32 =
  50095. + DWC_READ_REG32(&GET_CORE_IF(pcd)->core_global_regs->
  50096. + dtxfsiz[ep->dwc_ep.tx_fifo_num]);
  50097. + txstatus.d32 =
  50098. + DWC_READ_REG32(&GET_CORE_IF(pcd)->dev_if->
  50099. + in_ep_regs[ep->dwc_ep.num]->dtxfsts);
  50100. +
  50101. + if (txstatus.b.txfspcavail < txfifosize.b.depth) {
  50102. + DWC_WARN("%s() Data In Tx Fifo\n", __func__);
  50103. + retval = -DWC_E_AGAIN;
  50104. + } else {
  50105. + if (ep->dwc_ep.num == 0) {
  50106. + pcd->ep0state = EP0_STALL;
  50107. + }
  50108. +
  50109. + ep->stopped = 1;
  50110. + dwc_otg_ep_set_stall(GET_CORE_IF(pcd),
  50111. + &ep->dwc_ep);
  50112. + }
  50113. + } else {
  50114. + if (ep->dwc_ep.num == 0) {
  50115. + pcd->ep0state = EP0_STALL;
  50116. + }
  50117. +
  50118. + ep->stopped = 1;
  50119. + dwc_otg_ep_set_stall(GET_CORE_IF(pcd), &ep->dwc_ep);
  50120. + }
  50121. + } else if (value == 2) {
  50122. + ep->dwc_ep.stall_clear_flag = 0;
  50123. + } else if (value == 3) {
  50124. + ep->dwc_ep.stall_clear_flag = 1;
  50125. + }
  50126. +
  50127. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  50128. +
  50129. + return retval;
  50130. +}
  50131. +
  50132. +/**
  50133. + * This function initiates remote wakeup of the host from suspend state.
  50134. + */
  50135. +void dwc_otg_pcd_rem_wkup_from_suspend(dwc_otg_pcd_t * pcd, int set)
  50136. +{
  50137. + dctl_data_t dctl = { 0 };
  50138. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  50139. + dsts_data_t dsts;
  50140. +
  50141. + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  50142. + if (!dsts.b.suspsts) {
  50143. + DWC_WARN("Remote wakeup while is not in suspend state\n");
  50144. + }
  50145. + /* Check if DEVICE_REMOTE_WAKEUP feature enabled */
  50146. + if (pcd->remote_wakeup_enable) {
  50147. + if (set) {
  50148. +
  50149. + if (core_if->adp_enable) {
  50150. + gpwrdn_data_t gpwrdn;
  50151. +
  50152. + dwc_otg_adp_probe_stop(core_if);
  50153. +
  50154. + /* Mask SRP detected interrupt from Power Down Logic */
  50155. + gpwrdn.d32 = 0;
  50156. + gpwrdn.b.srp_det_msk = 1;
  50157. + DWC_MODIFY_REG32(&core_if->
  50158. + core_global_regs->gpwrdn,
  50159. + gpwrdn.d32, 0);
  50160. +
  50161. + /* Disable Power Down Logic */
  50162. + gpwrdn.d32 = 0;
  50163. + gpwrdn.b.pmuactv = 1;
  50164. + DWC_MODIFY_REG32(&core_if->
  50165. + core_global_regs->gpwrdn,
  50166. + gpwrdn.d32, 0);
  50167. +
  50168. + /*
  50169. + * Initialize the Core for Device mode.
  50170. + */
  50171. + core_if->op_state = B_PERIPHERAL;
  50172. + dwc_otg_core_init(core_if);
  50173. + dwc_otg_enable_global_interrupts(core_if);
  50174. + cil_pcd_start(core_if);
  50175. +
  50176. + dwc_otg_initiate_srp(core_if);
  50177. + }
  50178. +
  50179. + dctl.b.rmtwkupsig = 1;
  50180. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  50181. + dctl, 0, dctl.d32);
  50182. + DWC_DEBUGPL(DBG_PCD, "Set Remote Wakeup\n");
  50183. +
  50184. + dwc_mdelay(2);
  50185. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  50186. + dctl, dctl.d32, 0);
  50187. + DWC_DEBUGPL(DBG_PCD, "Clear Remote Wakeup\n");
  50188. + }
  50189. + } else {
  50190. + DWC_DEBUGPL(DBG_PCD, "Remote Wakeup is disabled\n");
  50191. + }
  50192. +}
  50193. +
  50194. +#ifdef CONFIG_USB_DWC_OTG_LPM
  50195. +/**
  50196. + * This function initiates remote wakeup of the host from L1 sleep state.
  50197. + */
  50198. +void dwc_otg_pcd_rem_wkup_from_sleep(dwc_otg_pcd_t * pcd, int set)
  50199. +{
  50200. + glpmcfg_data_t lpmcfg;
  50201. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  50202. +
  50203. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  50204. +
  50205. + /* Check if we are in L1 state */
  50206. + if (!lpmcfg.b.prt_sleep_sts) {
  50207. + DWC_DEBUGPL(DBG_PCD, "Device is not in sleep state\n");
  50208. + return;
  50209. + }
  50210. +
  50211. + /* Check if host allows remote wakeup */
  50212. + if (!lpmcfg.b.rem_wkup_en) {
  50213. + DWC_DEBUGPL(DBG_PCD, "Host does not allow remote wakeup\n");
  50214. + return;
  50215. + }
  50216. +
  50217. + /* Check if Resume OK */
  50218. + if (!lpmcfg.b.sleep_state_resumeok) {
  50219. + DWC_DEBUGPL(DBG_PCD, "Sleep state resume is not OK\n");
  50220. + return;
  50221. + }
  50222. +
  50223. + lpmcfg.d32 = DWC_READ_REG32(&core_if->core_global_regs->glpmcfg);
  50224. + lpmcfg.b.en_utmi_sleep = 0;
  50225. + lpmcfg.b.hird_thres &= (~(1 << 4));
  50226. + DWC_WRITE_REG32(&core_if->core_global_regs->glpmcfg, lpmcfg.d32);
  50227. +
  50228. + if (set) {
  50229. + dctl_data_t dctl = {.d32 = 0 };
  50230. + dctl.b.rmtwkupsig = 1;
  50231. + /* Set RmtWkUpSig bit to start remote wakup signaling.
  50232. + * Hardware will automatically clear this bit.
  50233. + */
  50234. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl,
  50235. + 0, dctl.d32);
  50236. + DWC_DEBUGPL(DBG_PCD, "Set Remote Wakeup\n");
  50237. + }
  50238. +
  50239. +}
  50240. +#endif
  50241. +
  50242. +/**
  50243. + * Performs remote wakeup.
  50244. + */
  50245. +void dwc_otg_pcd_remote_wakeup(dwc_otg_pcd_t * pcd, int set)
  50246. +{
  50247. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  50248. + dwc_irqflags_t flags;
  50249. + if (dwc_otg_is_device_mode(core_if)) {
  50250. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  50251. +#ifdef CONFIG_USB_DWC_OTG_LPM
  50252. + if (core_if->lx_state == DWC_OTG_L1) {
  50253. + dwc_otg_pcd_rem_wkup_from_sleep(pcd, set);
  50254. + } else {
  50255. +#endif
  50256. + dwc_otg_pcd_rem_wkup_from_suspend(pcd, set);
  50257. +#ifdef CONFIG_USB_DWC_OTG_LPM
  50258. + }
  50259. +#endif
  50260. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  50261. + }
  50262. + return;
  50263. +}
  50264. +
  50265. +void dwc_otg_pcd_disconnect_us(dwc_otg_pcd_t * pcd, int no_of_usecs)
  50266. +{
  50267. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  50268. + dctl_data_t dctl = { 0 };
  50269. +
  50270. + if (dwc_otg_is_device_mode(core_if)) {
  50271. + dctl.b.sftdiscon = 1;
  50272. + DWC_PRINTF("Soft disconnect for %d useconds\n",no_of_usecs);
  50273. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, 0, dctl.d32);
  50274. + dwc_udelay(no_of_usecs);
  50275. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32,0);
  50276. +
  50277. + } else{
  50278. + DWC_PRINTF("NOT SUPPORTED IN HOST MODE\n");
  50279. + }
  50280. + return;
  50281. +
  50282. +}
  50283. +
  50284. +int dwc_otg_pcd_wakeup(dwc_otg_pcd_t * pcd)
  50285. +{
  50286. + dsts_data_t dsts;
  50287. + gotgctl_data_t gotgctl;
  50288. +
  50289. + /*
  50290. + * This function starts the Protocol if no session is in progress. If
  50291. + * a session is already in progress, but the device is suspended,
  50292. + * remote wakeup signaling is started.
  50293. + */
  50294. +
  50295. + /* Check if valid session */
  50296. + gotgctl.d32 =
  50297. + DWC_READ_REG32(&(GET_CORE_IF(pcd)->core_global_regs->gotgctl));
  50298. + if (gotgctl.b.bsesvld) {
  50299. + /* Check if suspend state */
  50300. + dsts.d32 =
  50301. + DWC_READ_REG32(&
  50302. + (GET_CORE_IF(pcd)->dev_if->
  50303. + dev_global_regs->dsts));
  50304. + if (dsts.b.suspsts) {
  50305. + dwc_otg_pcd_remote_wakeup(pcd, 1);
  50306. + }
  50307. + } else {
  50308. + dwc_otg_pcd_initiate_srp(pcd);
  50309. + }
  50310. +
  50311. + return 0;
  50312. +
  50313. +}
  50314. +
  50315. +/**
  50316. + * Start the SRP timer to detect when the SRP does not complete within
  50317. + * 6 seconds.
  50318. + *
  50319. + * @param pcd the pcd structure.
  50320. + */
  50321. +void dwc_otg_pcd_initiate_srp(dwc_otg_pcd_t * pcd)
  50322. +{
  50323. + dwc_irqflags_t flags;
  50324. + DWC_SPINLOCK_IRQSAVE(pcd->lock, &flags);
  50325. + dwc_otg_initiate_srp(GET_CORE_IF(pcd));
  50326. + DWC_SPINUNLOCK_IRQRESTORE(pcd->lock, flags);
  50327. +}
  50328. +
  50329. +int dwc_otg_pcd_get_frame_number(dwc_otg_pcd_t * pcd)
  50330. +{
  50331. + return dwc_otg_get_frame_number(GET_CORE_IF(pcd));
  50332. +}
  50333. +
  50334. +int dwc_otg_pcd_is_lpm_enabled(dwc_otg_pcd_t * pcd)
  50335. +{
  50336. + return GET_CORE_IF(pcd)->core_params->lpm_enable;
  50337. +}
  50338. +
  50339. +uint32_t get_b_hnp_enable(dwc_otg_pcd_t * pcd)
  50340. +{
  50341. + return pcd->b_hnp_enable;
  50342. +}
  50343. +
  50344. +uint32_t get_a_hnp_support(dwc_otg_pcd_t * pcd)
  50345. +{
  50346. + return pcd->a_hnp_support;
  50347. +}
  50348. +
  50349. +uint32_t get_a_alt_hnp_support(dwc_otg_pcd_t * pcd)
  50350. +{
  50351. + return pcd->a_alt_hnp_support;
  50352. +}
  50353. +
  50354. +int dwc_otg_pcd_get_rmwkup_enable(dwc_otg_pcd_t * pcd)
  50355. +{
  50356. + return pcd->remote_wakeup_enable;
  50357. +}
  50358. +
  50359. +#endif /* DWC_HOST_ONLY */
  50360. --- /dev/null
  50361. +++ b/drivers/usb/host/dwc_otg/dwc_otg_pcd.h
  50362. @@ -0,0 +1,273 @@
  50363. +/* ==========================================================================
  50364. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_pcd.h $
  50365. + * $Revision: #48 $
  50366. + * $Date: 2012/08/10 $
  50367. + * $Change: 2047372 $
  50368. + *
  50369. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  50370. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  50371. + * otherwise expressly agreed to in writing between Synopsys and you.
  50372. + *
  50373. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  50374. + * any End User Software License Agreement or Agreement for Licensed Product
  50375. + * with Synopsys or any supplement thereto. You are permitted to use and
  50376. + * redistribute this Software in source and binary forms, with or without
  50377. + * modification, provided that redistributions of source code must retain this
  50378. + * notice. You may not view, use, disclose, copy or distribute this file or
  50379. + * any information contained herein except pursuant to this license grant from
  50380. + * Synopsys. If you do not agree with this notice, including the disclaimer
  50381. + * below, then you are not authorized to use the Software.
  50382. + *
  50383. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  50384. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  50385. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  50386. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  50387. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  50388. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  50389. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  50390. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  50391. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50392. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  50393. + * DAMAGE.
  50394. + * ========================================================================== */
  50395. +#ifndef DWC_HOST_ONLY
  50396. +#if !defined(__DWC_PCD_H__)
  50397. +#define __DWC_PCD_H__
  50398. +
  50399. +#include "dwc_otg_os_dep.h"
  50400. +#include "usb.h"
  50401. +#include "dwc_otg_cil.h"
  50402. +#include "dwc_otg_pcd_if.h"
  50403. +#include "dwc_otg_driver.h"
  50404. +
  50405. +struct cfiobject;
  50406. +
  50407. +/**
  50408. + * @file
  50409. + *
  50410. + * This file contains the structures, constants, and interfaces for
  50411. + * the Perpherial Contoller Driver (PCD).
  50412. + *
  50413. + * The Peripheral Controller Driver (PCD) for Linux will implement the
  50414. + * Gadget API, so that the existing Gadget drivers can be used. For
  50415. + * the Mass Storage Function driver the File-backed USB Storage Gadget
  50416. + * (FBS) driver will be used. The FBS driver supports the
  50417. + * Control-Bulk (CB), Control-Bulk-Interrupt (CBI), and Bulk-Only
  50418. + * transports.
  50419. + *
  50420. + */
  50421. +
  50422. +/** Invalid DMA Address */
  50423. +#define DWC_DMA_ADDR_INVALID (~(dwc_dma_t)0)
  50424. +
  50425. +/** Max Transfer size for any EP */
  50426. +#define DDMA_MAX_TRANSFER_SIZE 65535
  50427. +
  50428. +/**
  50429. + * Get the pointer to the core_if from the pcd pointer.
  50430. + */
  50431. +#define GET_CORE_IF( _pcd ) (_pcd->core_if)
  50432. +
  50433. +/**
  50434. + * States of EP0.
  50435. + */
  50436. +typedef enum ep0_state {
  50437. + EP0_DISCONNECT, /* no host */
  50438. + EP0_IDLE,
  50439. + EP0_IN_DATA_PHASE,
  50440. + EP0_OUT_DATA_PHASE,
  50441. + EP0_IN_STATUS_PHASE,
  50442. + EP0_OUT_STATUS_PHASE,
  50443. + EP0_STALL,
  50444. +} ep0state_e;
  50445. +
  50446. +/** Fordward declaration.*/
  50447. +struct dwc_otg_pcd;
  50448. +
  50449. +/** DWC_otg iso request structure.
  50450. + *
  50451. + */
  50452. +typedef struct usb_iso_request dwc_otg_pcd_iso_request_t;
  50453. +
  50454. +#ifdef DWC_UTE_PER_IO
  50455. +
  50456. +/**
  50457. + * This shall be the exact analogy of the same type structure defined in the
  50458. + * usb_gadget.h. Each descriptor contains
  50459. + */
  50460. +struct dwc_iso_pkt_desc_port {
  50461. + uint32_t offset;
  50462. + uint32_t length; /* expected length */
  50463. + uint32_t actual_length;
  50464. + uint32_t status;
  50465. +};
  50466. +
  50467. +struct dwc_iso_xreq_port {
  50468. + /** transfer/submission flag */
  50469. + uint32_t tr_sub_flags;
  50470. + /** Start the request ASAP */
  50471. +#define DWC_EREQ_TF_ASAP 0x00000002
  50472. + /** Just enqueue the request w/o initiating a transfer */
  50473. +#define DWC_EREQ_TF_ENQUEUE 0x00000004
  50474. +
  50475. + /**
  50476. + * count of ISO packets attached to this request - shall
  50477. + * not exceed the pio_alloc_pkt_count
  50478. + */
  50479. + uint32_t pio_pkt_count;
  50480. + /** count of ISO packets allocated for this request */
  50481. + uint32_t pio_alloc_pkt_count;
  50482. + /** number of ISO packet errors */
  50483. + uint32_t error_count;
  50484. + /** reserved for future extension */
  50485. + uint32_t res;
  50486. + /** Will be allocated and freed in the UTE gadget and based on the CFC value */
  50487. + struct dwc_iso_pkt_desc_port *per_io_frame_descs;
  50488. +};
  50489. +#endif
  50490. +/** DWC_otg request structure.
  50491. + * This structure is a list of requests.
  50492. + */
  50493. +typedef struct dwc_otg_pcd_request {
  50494. + void *priv;
  50495. + void *buf;
  50496. + dwc_dma_t dma;
  50497. + uint32_t length;
  50498. + uint32_t actual;
  50499. + unsigned sent_zlp:1;
  50500. + /**
  50501. + * Used instead of original buffer if
  50502. + * it(physical address) is not dword-aligned.
  50503. + **/
  50504. + uint8_t *dw_align_buf;
  50505. + dwc_dma_t dw_align_buf_dma;
  50506. +
  50507. + DWC_CIRCLEQ_ENTRY(dwc_otg_pcd_request) queue_entry;
  50508. +#ifdef DWC_UTE_PER_IO
  50509. + struct dwc_iso_xreq_port ext_req;
  50510. + //void *priv_ereq_nport; /* */
  50511. +#endif
  50512. +} dwc_otg_pcd_request_t;
  50513. +
  50514. +DWC_CIRCLEQ_HEAD(req_list, dwc_otg_pcd_request);
  50515. +
  50516. +/** PCD EP structure.
  50517. + * This structure describes an EP, there is an array of EPs in the PCD
  50518. + * structure.
  50519. + */
  50520. +typedef struct dwc_otg_pcd_ep {
  50521. + /** USB EP Descriptor */
  50522. + const usb_endpoint_descriptor_t *desc;
  50523. +
  50524. + /** queue of dwc_otg_pcd_requests. */
  50525. + struct req_list queue;
  50526. + unsigned stopped:1;
  50527. + unsigned disabling:1;
  50528. + unsigned dma:1;
  50529. + unsigned queue_sof:1;
  50530. +
  50531. +#ifdef DWC_EN_ISOC
  50532. + /** ISOC req handle passed */
  50533. + void *iso_req_handle;
  50534. +#endif //_EN_ISOC_
  50535. +
  50536. + /** DWC_otg ep data. */
  50537. + dwc_ep_t dwc_ep;
  50538. +
  50539. + /** Pointer to PCD */
  50540. + struct dwc_otg_pcd *pcd;
  50541. +
  50542. + void *priv;
  50543. +} dwc_otg_pcd_ep_t;
  50544. +
  50545. +/** DWC_otg PCD Structure.
  50546. + * This structure encapsulates the data for the dwc_otg PCD.
  50547. + */
  50548. +struct dwc_otg_pcd {
  50549. + const struct dwc_otg_pcd_function_ops *fops;
  50550. + /** The DWC otg device pointer */
  50551. + struct dwc_otg_device *otg_dev;
  50552. + /** Core Interface */
  50553. + dwc_otg_core_if_t *core_if;
  50554. + /** State of EP0 */
  50555. + ep0state_e ep0state;
  50556. + /** EP0 Request is pending */
  50557. + unsigned ep0_pending:1;
  50558. + /** Indicates when SET CONFIGURATION Request is in process */
  50559. + unsigned request_config:1;
  50560. + /** The state of the Remote Wakeup Enable. */
  50561. + unsigned remote_wakeup_enable:1;
  50562. + /** The state of the B-Device HNP Enable. */
  50563. + unsigned b_hnp_enable:1;
  50564. + /** The state of A-Device HNP Support. */
  50565. + unsigned a_hnp_support:1;
  50566. + /** The state of the A-Device Alt HNP support. */
  50567. + unsigned a_alt_hnp_support:1;
  50568. + /** Count of pending Requests */
  50569. + unsigned request_pending;
  50570. +
  50571. + /** SETUP packet for EP0
  50572. + * This structure is allocated as a DMA buffer on PCD initialization
  50573. + * with enough space for up to 3 setup packets.
  50574. + */
  50575. + union {
  50576. + usb_device_request_t req;
  50577. + uint32_t d32[2];
  50578. + } *setup_pkt;
  50579. +
  50580. + dwc_dma_t setup_pkt_dma_handle;
  50581. +
  50582. + /* Additional buffer and flag for CTRL_WR premature case */
  50583. + uint8_t *backup_buf;
  50584. + unsigned data_terminated;
  50585. +
  50586. + /** 2-byte dma buffer used to return status from GET_STATUS */
  50587. + uint16_t *status_buf;
  50588. + dwc_dma_t status_buf_dma_handle;
  50589. +
  50590. + /** EP0 */
  50591. + dwc_otg_pcd_ep_t ep0;
  50592. +
  50593. + /** Array of IN EPs. */
  50594. + dwc_otg_pcd_ep_t in_ep[MAX_EPS_CHANNELS - 1];
  50595. + /** Array of OUT EPs. */
  50596. + dwc_otg_pcd_ep_t out_ep[MAX_EPS_CHANNELS - 1];
  50597. + /** number of valid EPs in the above array. */
  50598. +// unsigned num_eps : 4;
  50599. + dwc_spinlock_t *lock;
  50600. +
  50601. + /** Tasklet to defer starting of TEST mode transmissions until
  50602. + * Status Phase has been completed.
  50603. + */
  50604. + dwc_tasklet_t *test_mode_tasklet;
  50605. +
  50606. + /** Tasklet to delay starting of xfer in DMA mode */
  50607. + dwc_tasklet_t *start_xfer_tasklet;
  50608. +
  50609. + /** The test mode to enter when the tasklet is executed. */
  50610. + unsigned test_mode;
  50611. + /** The cfi_api structure that implements most of the CFI API
  50612. + * and OTG specific core configuration functionality
  50613. + */
  50614. +#ifdef DWC_UTE_CFI
  50615. + struct cfiobject *cfi;
  50616. +#endif
  50617. +
  50618. +};
  50619. +
  50620. +static inline struct device *dwc_otg_pcd_to_dev(struct dwc_otg_pcd *pcd)
  50621. +{
  50622. + return &pcd->otg_dev->os_dep.platformdev->dev;
  50623. +}
  50624. +
  50625. +//FIXME this functions should be static, and this prototypes should be removed
  50626. +extern void dwc_otg_request_nuke(dwc_otg_pcd_ep_t * ep);
  50627. +extern void dwc_otg_request_done(dwc_otg_pcd_ep_t * ep,
  50628. + dwc_otg_pcd_request_t * req, int32_t status);
  50629. +
  50630. +void dwc_otg_iso_buffer_done(dwc_otg_pcd_t * pcd, dwc_otg_pcd_ep_t * ep,
  50631. + void *req_handle);
  50632. +
  50633. +extern void do_test_mode(void *data);
  50634. +#endif
  50635. +#endif /* DWC_HOST_ONLY */
  50636. --- /dev/null
  50637. +++ b/drivers/usb/host/dwc_otg/dwc_otg_pcd_if.h
  50638. @@ -0,0 +1,361 @@
  50639. +/* ==========================================================================
  50640. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_pcd_if.h $
  50641. + * $Revision: #11 $
  50642. + * $Date: 2011/10/26 $
  50643. + * $Change: 1873028 $
  50644. + *
  50645. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  50646. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  50647. + * otherwise expressly agreed to in writing between Synopsys and you.
  50648. + *
  50649. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  50650. + * any End User Software License Agreement or Agreement for Licensed Product
  50651. + * with Synopsys or any supplement thereto. You are permitted to use and
  50652. + * redistribute this Software in source and binary forms, with or without
  50653. + * modification, provided that redistributions of source code must retain this
  50654. + * notice. You may not view, use, disclose, copy or distribute this file or
  50655. + * any information contained herein except pursuant to this license grant from
  50656. + * Synopsys. If you do not agree with this notice, including the disclaimer
  50657. + * below, then you are not authorized to use the Software.
  50658. + *
  50659. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  50660. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  50661. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  50662. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  50663. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  50664. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  50665. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  50666. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  50667. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50668. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  50669. + * DAMAGE.
  50670. + * ========================================================================== */
  50671. +#ifndef DWC_HOST_ONLY
  50672. +
  50673. +#if !defined(__DWC_PCD_IF_H__)
  50674. +#define __DWC_PCD_IF_H__
  50675. +
  50676. +//#include "dwc_os.h"
  50677. +#include "dwc_otg_core_if.h"
  50678. +#include "dwc_otg_driver.h"
  50679. +
  50680. +/** @file
  50681. + * This file defines DWC_OTG PCD Core API.
  50682. + */
  50683. +
  50684. +struct dwc_otg_pcd;
  50685. +typedef struct dwc_otg_pcd dwc_otg_pcd_t;
  50686. +
  50687. +/** Maxpacket size for EP0 */
  50688. +#define MAX_EP0_SIZE 64
  50689. +/** Maxpacket size for any EP */
  50690. +#define MAX_PACKET_SIZE 1024
  50691. +
  50692. +/** @name Function Driver Callbacks */
  50693. +/** @{ */
  50694. +
  50695. +/** This function will be called whenever a previously queued request has
  50696. + * completed. The status value will be set to -DWC_E_SHUTDOWN to indicated a
  50697. + * failed or aborted transfer, or -DWC_E_RESTART to indicate the device was reset,
  50698. + * or -DWC_E_TIMEOUT to indicate it timed out, or -DWC_E_INVALID to indicate invalid
  50699. + * parameters. */
  50700. +typedef int (*dwc_completion_cb_t) (dwc_otg_pcd_t * pcd, void *ep_handle,
  50701. + void *req_handle, int32_t status,
  50702. + uint32_t actual);
  50703. +/**
  50704. + * This function will be called whenever a previousle queued ISOC request has
  50705. + * completed. Count of ISOC packets could be read using dwc_otg_pcd_get_iso_packet_count
  50706. + * function.
  50707. + * The status of each ISOC packet could be read using dwc_otg_pcd_get_iso_packet_*
  50708. + * functions.
  50709. + */
  50710. +typedef int (*dwc_isoc_completion_cb_t) (dwc_otg_pcd_t * pcd, void *ep_handle,
  50711. + void *req_handle, int proc_buf_num);
  50712. +/** This function should handle any SETUP request that cannot be handled by the
  50713. + * PCD Core. This includes most GET_DESCRIPTORs, SET_CONFIGS, Any
  50714. + * class-specific requests, etc. The function must non-blocking.
  50715. + *
  50716. + * Returns 0 on success.
  50717. + * Returns -DWC_E_NOT_SUPPORTED if the request is not supported.
  50718. + * Returns -DWC_E_INVALID if the setup request had invalid parameters or bytes.
  50719. + * Returns -DWC_E_SHUTDOWN on any other error. */
  50720. +typedef int (*dwc_setup_cb_t) (dwc_otg_pcd_t * pcd, uint8_t * bytes);
  50721. +/** This is called whenever the device has been disconnected. The function
  50722. + * driver should take appropriate action to clean up all pending requests in the
  50723. + * PCD Core, remove all endpoints (except ep0), and initialize back to reset
  50724. + * state. */
  50725. +typedef int (*dwc_disconnect_cb_t) (dwc_otg_pcd_t * pcd);
  50726. +/** This function is called when device has been connected. */
  50727. +typedef int (*dwc_connect_cb_t) (dwc_otg_pcd_t * pcd, int speed);
  50728. +/** This function is called when device has been suspended */
  50729. +typedef int (*dwc_suspend_cb_t) (dwc_otg_pcd_t * pcd);
  50730. +/** This function is called when device has received LPM tokens, i.e.
  50731. + * device has been sent to sleep state. */
  50732. +typedef int (*dwc_sleep_cb_t) (dwc_otg_pcd_t * pcd);
  50733. +/** This function is called when device has been resumed
  50734. + * from suspend(L2) or L1 sleep state. */
  50735. +typedef int (*dwc_resume_cb_t) (dwc_otg_pcd_t * pcd);
  50736. +/** This function is called whenever hnp params has been changed.
  50737. + * User can call get_b_hnp_enable, get_a_hnp_support, get_a_alt_hnp_support functions
  50738. + * to get hnp parameters. */
  50739. +typedef int (*dwc_hnp_params_changed_cb_t) (dwc_otg_pcd_t * pcd);
  50740. +/** This function is called whenever USB RESET is detected. */
  50741. +typedef int (*dwc_reset_cb_t) (dwc_otg_pcd_t * pcd);
  50742. +
  50743. +typedef int (*cfi_setup_cb_t) (dwc_otg_pcd_t * pcd, void *ctrl_req_bytes);
  50744. +
  50745. +/**
  50746. + *
  50747. + * @param ep_handle Void pointer to the usb_ep structure
  50748. + * @param ereq_port Pointer to the extended request structure created in the
  50749. + * portable part.
  50750. + */
  50751. +typedef int (*xiso_completion_cb_t) (dwc_otg_pcd_t * pcd, void *ep_handle,
  50752. + void *req_handle, int32_t status,
  50753. + void *ereq_port);
  50754. +/** Function Driver Ops Data Structure */
  50755. +struct dwc_otg_pcd_function_ops {
  50756. + dwc_connect_cb_t connect;
  50757. + dwc_disconnect_cb_t disconnect;
  50758. + dwc_setup_cb_t setup;
  50759. + dwc_completion_cb_t complete;
  50760. + dwc_isoc_completion_cb_t isoc_complete;
  50761. + dwc_suspend_cb_t suspend;
  50762. + dwc_sleep_cb_t sleep;
  50763. + dwc_resume_cb_t resume;
  50764. + dwc_reset_cb_t reset;
  50765. + dwc_hnp_params_changed_cb_t hnp_changed;
  50766. + cfi_setup_cb_t cfi_setup;
  50767. +#ifdef DWC_UTE_PER_IO
  50768. + xiso_completion_cb_t xisoc_complete;
  50769. +#endif
  50770. +};
  50771. +/** @} */
  50772. +
  50773. +/** @name Function Driver Functions */
  50774. +/** @{ */
  50775. +
  50776. +/** Call this function to get pointer on dwc_otg_pcd_t,
  50777. + * this pointer will be used for all PCD API functions.
  50778. + *
  50779. + * @param core_if The DWC_OTG Core
  50780. + */
  50781. +extern dwc_otg_pcd_t *dwc_otg_pcd_init(dwc_otg_device_t *otg_dev);
  50782. +
  50783. +/** Frees PCD allocated by dwc_otg_pcd_init
  50784. + *
  50785. + * @param pcd The PCD
  50786. + */
  50787. +extern void dwc_otg_pcd_remove(dwc_otg_pcd_t * pcd);
  50788. +
  50789. +/** Call this to bind the function driver to the PCD Core.
  50790. + *
  50791. + * @param pcd Pointer on dwc_otg_pcd_t returned by dwc_otg_pcd_init function.
  50792. + * @param fops The Function Driver Ops data structure containing pointers to all callbacks.
  50793. + */
  50794. +extern void dwc_otg_pcd_start(dwc_otg_pcd_t * pcd,
  50795. + const struct dwc_otg_pcd_function_ops *fops);
  50796. +
  50797. +/** Enables an endpoint for use. This function enables an endpoint in
  50798. + * the PCD. The endpoint is described by the ep_desc which has the
  50799. + * same format as a USB ep descriptor. The ep_handle parameter is used to refer
  50800. + * to the endpoint from other API functions and in callbacks. Normally this
  50801. + * should be called after a SET_CONFIGURATION/SET_INTERFACE to configure the
  50802. + * core for that interface.
  50803. + *
  50804. + * Returns -DWC_E_INVALID if invalid parameters were passed.
  50805. + * Returns -DWC_E_SHUTDOWN if any other error ocurred.
  50806. + * Returns 0 on success.
  50807. + *
  50808. + * @param pcd The PCD
  50809. + * @param ep_desc Endpoint descriptor
  50810. + * @param usb_ep Handle on endpoint, that will be used to identify endpoint.
  50811. + */
  50812. +extern int dwc_otg_pcd_ep_enable(dwc_otg_pcd_t * pcd,
  50813. + const uint8_t * ep_desc, void *usb_ep);
  50814. +
  50815. +/** Disable the endpoint referenced by ep_handle.
  50816. + *
  50817. + * Returns -DWC_E_INVALID if invalid parameters were passed.
  50818. + * Returns -DWC_E_SHUTDOWN if any other error occurred.
  50819. + * Returns 0 on success. */
  50820. +extern int dwc_otg_pcd_ep_disable(dwc_otg_pcd_t * pcd, void *ep_handle);
  50821. +
  50822. +/** Queue a data transfer request on the endpoint referenced by ep_handle.
  50823. + * After the transfer is completes, the complete callback will be called with
  50824. + * the request status.
  50825. + *
  50826. + * @param pcd The PCD
  50827. + * @param ep_handle The handle of the endpoint
  50828. + * @param buf The buffer for the data
  50829. + * @param dma_buf The DMA buffer for the data
  50830. + * @param buflen The length of the data transfer
  50831. + * @param zero Specifies whether to send zero length last packet.
  50832. + * @param req_handle Set this handle to any value to use to reference this
  50833. + * request in the ep_dequeue function or from the complete callback
  50834. + * @param atomic_alloc If driver need to perform atomic allocations
  50835. + * for internal data structures.
  50836. + *
  50837. + * Returns -DWC_E_INVALID if invalid parameters were passed.
  50838. + * Returns -DWC_E_SHUTDOWN if any other error ocurred.
  50839. + * Returns 0 on success. */
  50840. +extern int dwc_otg_pcd_ep_queue(dwc_otg_pcd_t * pcd, void *ep_handle,
  50841. + uint8_t * buf, dwc_dma_t dma_buf,
  50842. + uint32_t buflen, int zero, void *req_handle,
  50843. + int atomic_alloc);
  50844. +#ifdef DWC_UTE_PER_IO
  50845. +/**
  50846. + *
  50847. + * @param ereq_nonport Pointer to the extended request part of the
  50848. + * usb_request structure defined in usb_gadget.h file.
  50849. + */
  50850. +extern int dwc_otg_pcd_xiso_ep_queue(dwc_otg_pcd_t * pcd, void *ep_handle,
  50851. + uint8_t * buf, dwc_dma_t dma_buf,
  50852. + uint32_t buflen, int zero,
  50853. + void *req_handle, int atomic_alloc,
  50854. + void *ereq_nonport);
  50855. +
  50856. +#endif
  50857. +
  50858. +/** De-queue the specified data transfer that has not yet completed.
  50859. + *
  50860. + * Returns -DWC_E_INVALID if invalid parameters were passed.
  50861. + * Returns -DWC_E_SHUTDOWN if any other error ocurred.
  50862. + * Returns 0 on success. */
  50863. +extern int dwc_otg_pcd_ep_dequeue(dwc_otg_pcd_t * pcd, void *ep_handle,
  50864. + void *req_handle);
  50865. +
  50866. +/** Halt (STALL) an endpoint or clear it.
  50867. + *
  50868. + * Returns -DWC_E_INVALID if invalid parameters were passed.
  50869. + * Returns -DWC_E_SHUTDOWN if any other error ocurred.
  50870. + * Returns -DWC_E_AGAIN if the STALL cannot be sent and must be tried again later
  50871. + * Returns 0 on success. */
  50872. +extern int dwc_otg_pcd_ep_halt(dwc_otg_pcd_t * pcd, void *ep_handle, int value);
  50873. +
  50874. +/** This function */
  50875. +extern int dwc_otg_pcd_ep_wedge(dwc_otg_pcd_t * pcd, void *ep_handle);
  50876. +
  50877. +/** This function should be called on every hardware interrupt */
  50878. +extern int32_t dwc_otg_pcd_handle_intr(dwc_otg_pcd_t * pcd);
  50879. +
  50880. +/** This function returns current frame number */
  50881. +extern int dwc_otg_pcd_get_frame_number(dwc_otg_pcd_t * pcd);
  50882. +
  50883. +/**
  50884. + * Start isochronous transfers on the endpoint referenced by ep_handle.
  50885. + * For isochronous transfers duble buffering is used.
  50886. + * After processing each of buffers comlete callback will be called with
  50887. + * status for each transaction.
  50888. + *
  50889. + * @param pcd The PCD
  50890. + * @param ep_handle The handle of the endpoint
  50891. + * @param buf0 The virtual address of first data buffer
  50892. + * @param buf1 The virtual address of second data buffer
  50893. + * @param dma0 The DMA address of first data buffer
  50894. + * @param dma1 The DMA address of second data buffer
  50895. + * @param sync_frame Data pattern frame number
  50896. + * @param dp_frame Data size for pattern frame
  50897. + * @param data_per_frame Data size for regular frame
  50898. + * @param start_frame Frame number to start transfers, if -1 then start transfers ASAP.
  50899. + * @param buf_proc_intrvl Interval of ISOC Buffer processing
  50900. + * @param req_handle Handle of ISOC request
  50901. + * @param atomic_alloc Specefies whether to perform atomic allocation for
  50902. + * internal data structures.
  50903. + *
  50904. + * Returns -DWC_E_NO_MEMORY if there is no enough memory.
  50905. + * Returns -DWC_E_INVALID if incorrect arguments are passed to the function.
  50906. + * Returns -DW_E_SHUTDOWN for any other error.
  50907. + * Returns 0 on success
  50908. + */
  50909. +extern int dwc_otg_pcd_iso_ep_start(dwc_otg_pcd_t * pcd, void *ep_handle,
  50910. + uint8_t * buf0, uint8_t * buf1,
  50911. + dwc_dma_t dma0, dwc_dma_t dma1,
  50912. + int sync_frame, int dp_frame,
  50913. + int data_per_frame, int start_frame,
  50914. + int buf_proc_intrvl, void *req_handle,
  50915. + int atomic_alloc);
  50916. +
  50917. +/** Stop ISOC transfers on endpoint referenced by ep_handle.
  50918. + *
  50919. + * @param pcd The PCD
  50920. + * @param ep_handle The handle of the endpoint
  50921. + * @param req_handle Handle of ISOC request
  50922. + *
  50923. + * Returns -DWC_E_INVALID if incorrect arguments are passed to the function
  50924. + * Returns 0 on success
  50925. + */
  50926. +int dwc_otg_pcd_iso_ep_stop(dwc_otg_pcd_t * pcd, void *ep_handle,
  50927. + void *req_handle);
  50928. +
  50929. +/** Get ISOC packet status.
  50930. + *
  50931. + * @param pcd The PCD
  50932. + * @param ep_handle The handle of the endpoint
  50933. + * @param iso_req_handle Isochronoush request handle
  50934. + * @param packet Number of packet
  50935. + * @param status Out parameter for returning status
  50936. + * @param actual Out parameter for returning actual length
  50937. + * @param offset Out parameter for returning offset
  50938. + *
  50939. + */
  50940. +extern void dwc_otg_pcd_get_iso_packet_params(dwc_otg_pcd_t * pcd,
  50941. + void *ep_handle,
  50942. + void *iso_req_handle, int packet,
  50943. + int *status, int *actual,
  50944. + int *offset);
  50945. +
  50946. +/** Get ISOC packet count.
  50947. + *
  50948. + * @param pcd The PCD
  50949. + * @param ep_handle The handle of the endpoint
  50950. + * @param iso_req_handle
  50951. + */
  50952. +extern int dwc_otg_pcd_get_iso_packet_count(dwc_otg_pcd_t * pcd,
  50953. + void *ep_handle,
  50954. + void *iso_req_handle);
  50955. +
  50956. +/** This function starts the SRP Protocol if no session is in progress. If
  50957. + * a session is already in progress, but the device is suspended,
  50958. + * remote wakeup signaling is started.
  50959. + */
  50960. +extern int dwc_otg_pcd_wakeup(dwc_otg_pcd_t * pcd);
  50961. +
  50962. +/** This function returns 1 if LPM support is enabled, and 0 otherwise. */
  50963. +extern int dwc_otg_pcd_is_lpm_enabled(dwc_otg_pcd_t * pcd);
  50964. +
  50965. +/** This function returns 1 if remote wakeup is allowed and 0, otherwise. */
  50966. +extern int dwc_otg_pcd_get_rmwkup_enable(dwc_otg_pcd_t * pcd);
  50967. +
  50968. +/** Initiate SRP */
  50969. +extern void dwc_otg_pcd_initiate_srp(dwc_otg_pcd_t * pcd);
  50970. +
  50971. +/** Starts remote wakeup signaling. */
  50972. +extern void dwc_otg_pcd_remote_wakeup(dwc_otg_pcd_t * pcd, int set);
  50973. +
  50974. +/** Starts micorsecond soft disconnect. */
  50975. +extern void dwc_otg_pcd_disconnect_us(dwc_otg_pcd_t * pcd, int no_of_usecs);
  50976. +/** This function returns whether device is dualspeed.*/
  50977. +extern uint32_t dwc_otg_pcd_is_dualspeed(dwc_otg_pcd_t * pcd);
  50978. +
  50979. +/** This function returns whether device is otg. */
  50980. +extern uint32_t dwc_otg_pcd_is_otg(dwc_otg_pcd_t * pcd);
  50981. +
  50982. +/** These functions allow to get hnp parameters */
  50983. +extern uint32_t get_b_hnp_enable(dwc_otg_pcd_t * pcd);
  50984. +extern uint32_t get_a_hnp_support(dwc_otg_pcd_t * pcd);
  50985. +extern uint32_t get_a_alt_hnp_support(dwc_otg_pcd_t * pcd);
  50986. +
  50987. +/** CFI specific Interface functions */
  50988. +/** Allocate a cfi buffer */
  50989. +extern uint8_t *cfiw_ep_alloc_buffer(dwc_otg_pcd_t * pcd, void *pep,
  50990. + dwc_dma_t * addr, size_t buflen,
  50991. + int flags);
  50992. +
  50993. +/******************************************************************************/
  50994. +
  50995. +/** @} */
  50996. +
  50997. +#endif /* __DWC_PCD_IF_H__ */
  50998. +
  50999. +#endif /* DWC_HOST_ONLY */
  51000. --- /dev/null
  51001. +++ b/drivers/usb/host/dwc_otg/dwc_otg_pcd_intr.c
  51002. @@ -0,0 +1,5148 @@
  51003. +/* ==========================================================================
  51004. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_pcd_intr.c $
  51005. + * $Revision: #116 $
  51006. + * $Date: 2012/08/10 $
  51007. + * $Change: 2047372 $
  51008. + *
  51009. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  51010. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  51011. + * otherwise expressly agreed to in writing between Synopsys and you.
  51012. + *
  51013. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  51014. + * any End User Software License Agreement or Agreement for Licensed Product
  51015. + * with Synopsys or any supplement thereto. You are permitted to use and
  51016. + * redistribute this Software in source and binary forms, with or without
  51017. + * modification, provided that redistributions of source code must retain this
  51018. + * notice. You may not view, use, disclose, copy or distribute this file or
  51019. + * any information contained herein except pursuant to this license grant from
  51020. + * Synopsys. If you do not agree with this notice, including the disclaimer
  51021. + * below, then you are not authorized to use the Software.
  51022. + *
  51023. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  51024. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  51025. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  51026. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  51027. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  51028. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  51029. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  51030. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  51031. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  51032. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  51033. + * DAMAGE.
  51034. + * ========================================================================== */
  51035. +#ifndef DWC_HOST_ONLY
  51036. +
  51037. +#include "dwc_otg_pcd.h"
  51038. +
  51039. +#ifdef DWC_UTE_CFI
  51040. +#include "dwc_otg_cfi.h"
  51041. +#endif
  51042. +
  51043. +#ifdef DWC_UTE_PER_IO
  51044. +extern void complete_xiso_ep(dwc_otg_pcd_ep_t * ep);
  51045. +#endif
  51046. +//#define PRINT_CFI_DMA_DESCS
  51047. +
  51048. +#define DEBUG_EP0
  51049. +
  51050. +/**
  51051. + * This function updates OTG.
  51052. + */
  51053. +static void dwc_otg_pcd_update_otg(dwc_otg_pcd_t * pcd, const unsigned reset)
  51054. +{
  51055. +
  51056. + if (reset) {
  51057. + pcd->b_hnp_enable = 0;
  51058. + pcd->a_hnp_support = 0;
  51059. + pcd->a_alt_hnp_support = 0;
  51060. + }
  51061. +
  51062. + if (pcd->fops->hnp_changed) {
  51063. + pcd->fops->hnp_changed(pcd);
  51064. + }
  51065. +}
  51066. +
  51067. +/** @file
  51068. + * This file contains the implementation of the PCD Interrupt handlers.
  51069. + *
  51070. + * The PCD handles the device interrupts. Many conditions can cause a
  51071. + * device interrupt. When an interrupt occurs, the device interrupt
  51072. + * service routine determines the cause of the interrupt and
  51073. + * dispatches handling to the appropriate function. These interrupt
  51074. + * handling functions are described below.
  51075. + * All interrupt registers are processed from LSB to MSB.
  51076. + */
  51077. +
  51078. +/**
  51079. + * This function prints the ep0 state for debug purposes.
  51080. + */
  51081. +static inline void print_ep0_state(dwc_otg_pcd_t * pcd)
  51082. +{
  51083. +#ifdef DEBUG
  51084. + char str[40];
  51085. +
  51086. + switch (pcd->ep0state) {
  51087. + case EP0_DISCONNECT:
  51088. + dwc_strcpy(str, "EP0_DISCONNECT");
  51089. + break;
  51090. + case EP0_IDLE:
  51091. + dwc_strcpy(str, "EP0_IDLE");
  51092. + break;
  51093. + case EP0_IN_DATA_PHASE:
  51094. + dwc_strcpy(str, "EP0_IN_DATA_PHASE");
  51095. + break;
  51096. + case EP0_OUT_DATA_PHASE:
  51097. + dwc_strcpy(str, "EP0_OUT_DATA_PHASE");
  51098. + break;
  51099. + case EP0_IN_STATUS_PHASE:
  51100. + dwc_strcpy(str, "EP0_IN_STATUS_PHASE");
  51101. + break;
  51102. + case EP0_OUT_STATUS_PHASE:
  51103. + dwc_strcpy(str, "EP0_OUT_STATUS_PHASE");
  51104. + break;
  51105. + case EP0_STALL:
  51106. + dwc_strcpy(str, "EP0_STALL");
  51107. + break;
  51108. + default:
  51109. + dwc_strcpy(str, "EP0_INVALID");
  51110. + }
  51111. +
  51112. + DWC_DEBUGPL(DBG_ANY, "%s(%d)\n", str, pcd->ep0state);
  51113. +#endif
  51114. +}
  51115. +
  51116. +/**
  51117. + * This function calculate the size of the payload in the memory
  51118. + * for out endpoints and prints size for debug purposes(used in
  51119. + * 2.93a DevOutNak feature).
  51120. + */
  51121. +static inline void print_memory_payload(dwc_otg_pcd_t * pcd, dwc_ep_t * ep)
  51122. +{
  51123. +#ifdef DEBUG
  51124. + deptsiz_data_t deptsiz_init = {.d32 = 0 };
  51125. + deptsiz_data_t deptsiz_updt = {.d32 = 0 };
  51126. + int pack_num;
  51127. + unsigned payload;
  51128. +
  51129. + deptsiz_init.d32 = pcd->core_if->start_doeptsiz_val[ep->num];
  51130. + deptsiz_updt.d32 =
  51131. + DWC_READ_REG32(&pcd->core_if->dev_if->
  51132. + out_ep_regs[ep->num]->doeptsiz);
  51133. + /* Payload will be */
  51134. + payload = deptsiz_init.b.xfersize - deptsiz_updt.b.xfersize;
  51135. + /* Packet count is decremented every time a packet
  51136. + * is written to the RxFIFO not in to the external memory
  51137. + * So, if payload == 0, then it means no packet was sent to ext memory*/
  51138. + pack_num = (!payload) ? 0 : (deptsiz_init.b.pktcnt - deptsiz_updt.b.pktcnt);
  51139. + DWC_DEBUGPL(DBG_PCDV,
  51140. + "Payload for EP%d-%s\n",
  51141. + ep->num, (ep->is_in ? "IN" : "OUT"));
  51142. + DWC_DEBUGPL(DBG_PCDV,
  51143. + "Number of transfered bytes = 0x%08x\n", payload);
  51144. + DWC_DEBUGPL(DBG_PCDV,
  51145. + "Number of transfered packets = %d\n", pack_num);
  51146. +#endif
  51147. +}
  51148. +
  51149. +
  51150. +#ifdef DWC_UTE_CFI
  51151. +static inline void print_desc(struct dwc_otg_dma_desc *ddesc,
  51152. + const uint8_t * epname, int descnum)
  51153. +{
  51154. + CFI_INFO
  51155. + ("%s DMA_DESC(%d) buf=0x%08x bytes=0x%04x; sp=0x%x; l=0x%x; sts=0x%02x; bs=0x%02x\n",
  51156. + epname, descnum, ddesc->buf, ddesc->status.b.bytes,
  51157. + ddesc->status.b.sp, ddesc->status.b.l, ddesc->status.b.sts,
  51158. + ddesc->status.b.bs);
  51159. +}
  51160. +#endif
  51161. +
  51162. +/**
  51163. + * This function returns pointer to in ep struct with number ep_num
  51164. + */
  51165. +static inline dwc_otg_pcd_ep_t *get_in_ep(dwc_otg_pcd_t * pcd, uint32_t ep_num)
  51166. +{
  51167. + int i;
  51168. + int num_in_eps = GET_CORE_IF(pcd)->dev_if->num_in_eps;
  51169. + if (ep_num == 0) {
  51170. + return &pcd->ep0;
  51171. + } else {
  51172. + for (i = 0; i < num_in_eps; ++i) {
  51173. + if (pcd->in_ep[i].dwc_ep.num == ep_num)
  51174. + return &pcd->in_ep[i];
  51175. + }
  51176. + return 0;
  51177. + }
  51178. +}
  51179. +
  51180. +/**
  51181. + * This function returns pointer to out ep struct with number ep_num
  51182. + */
  51183. +static inline dwc_otg_pcd_ep_t *get_out_ep(dwc_otg_pcd_t * pcd, uint32_t ep_num)
  51184. +{
  51185. + int i;
  51186. + int num_out_eps = GET_CORE_IF(pcd)->dev_if->num_out_eps;
  51187. + if (ep_num == 0) {
  51188. + return &pcd->ep0;
  51189. + } else {
  51190. + for (i = 0; i < num_out_eps; ++i) {
  51191. + if (pcd->out_ep[i].dwc_ep.num == ep_num)
  51192. + return &pcd->out_ep[i];
  51193. + }
  51194. + return 0;
  51195. + }
  51196. +}
  51197. +
  51198. +/**
  51199. + * This functions gets a pointer to an EP from the wIndex address
  51200. + * value of the control request.
  51201. + */
  51202. +dwc_otg_pcd_ep_t *get_ep_by_addr(dwc_otg_pcd_t * pcd, u16 wIndex)
  51203. +{
  51204. + dwc_otg_pcd_ep_t *ep;
  51205. + uint32_t ep_num = UE_GET_ADDR(wIndex);
  51206. +
  51207. + if (ep_num == 0) {
  51208. + ep = &pcd->ep0;
  51209. + } else if (UE_GET_DIR(wIndex) == UE_DIR_IN) { /* in ep */
  51210. + ep = &pcd->in_ep[ep_num - 1];
  51211. + } else {
  51212. + ep = &pcd->out_ep[ep_num - 1];
  51213. + }
  51214. +
  51215. + return ep;
  51216. +}
  51217. +
  51218. +/**
  51219. + * This function checks the EP request queue, if the queue is not
  51220. + * empty the next request is started.
  51221. + */
  51222. +void start_next_request(dwc_otg_pcd_ep_t * ep)
  51223. +{
  51224. + dwc_otg_pcd_request_t *req = 0;
  51225. + uint32_t max_transfer =
  51226. + GET_CORE_IF(ep->pcd)->core_params->max_transfer_size;
  51227. +
  51228. +#ifdef DWC_UTE_CFI
  51229. + struct dwc_otg_pcd *pcd;
  51230. + pcd = ep->pcd;
  51231. +#endif
  51232. +
  51233. + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  51234. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  51235. +
  51236. +#ifdef DWC_UTE_CFI
  51237. + if (ep->dwc_ep.buff_mode != BM_STANDARD) {
  51238. + ep->dwc_ep.cfi_req_len = req->length;
  51239. + pcd->cfi->ops.build_descriptors(pcd->cfi, pcd, ep, req);
  51240. + } else {
  51241. +#endif
  51242. + /* Setup and start the Transfer */
  51243. + if (req->dw_align_buf) {
  51244. + ep->dwc_ep.dma_addr = req->dw_align_buf_dma;
  51245. + ep->dwc_ep.start_xfer_buff = req->dw_align_buf;
  51246. + ep->dwc_ep.xfer_buff = req->dw_align_buf;
  51247. + } else {
  51248. + ep->dwc_ep.dma_addr = req->dma;
  51249. + ep->dwc_ep.start_xfer_buff = req->buf;
  51250. + ep->dwc_ep.xfer_buff = req->buf;
  51251. + }
  51252. + ep->dwc_ep.sent_zlp = 0;
  51253. + ep->dwc_ep.total_len = req->length;
  51254. + ep->dwc_ep.xfer_len = 0;
  51255. + ep->dwc_ep.xfer_count = 0;
  51256. +
  51257. + ep->dwc_ep.maxxfer = max_transfer;
  51258. + if (GET_CORE_IF(ep->pcd)->dma_desc_enable) {
  51259. + uint32_t out_max_xfer = DDMA_MAX_TRANSFER_SIZE
  51260. + - (DDMA_MAX_TRANSFER_SIZE % 4);
  51261. + if (ep->dwc_ep.is_in) {
  51262. + if (ep->dwc_ep.maxxfer >
  51263. + DDMA_MAX_TRANSFER_SIZE) {
  51264. + ep->dwc_ep.maxxfer =
  51265. + DDMA_MAX_TRANSFER_SIZE;
  51266. + }
  51267. + } else {
  51268. + if (ep->dwc_ep.maxxfer > out_max_xfer) {
  51269. + ep->dwc_ep.maxxfer =
  51270. + out_max_xfer;
  51271. + }
  51272. + }
  51273. + }
  51274. + if (ep->dwc_ep.maxxfer < ep->dwc_ep.total_len) {
  51275. + ep->dwc_ep.maxxfer -=
  51276. + (ep->dwc_ep.maxxfer % ep->dwc_ep.maxpacket);
  51277. + }
  51278. + if (req->sent_zlp) {
  51279. + if ((ep->dwc_ep.total_len %
  51280. + ep->dwc_ep.maxpacket == 0)
  51281. + && (ep->dwc_ep.total_len != 0)) {
  51282. + ep->dwc_ep.sent_zlp = 1;
  51283. + }
  51284. +
  51285. + }
  51286. +#ifdef DWC_UTE_CFI
  51287. + }
  51288. +#endif
  51289. + dwc_otg_ep_start_transfer(GET_CORE_IF(ep->pcd), &ep->dwc_ep);
  51290. + } else if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
  51291. + DWC_PRINTF("There are no more ISOC requests \n");
  51292. + ep->dwc_ep.frame_num = 0xFFFFFFFF;
  51293. + }
  51294. +}
  51295. +
  51296. +/**
  51297. + * This function handles the SOF Interrupts. At this time the SOF
  51298. + * Interrupt is disabled.
  51299. + */
  51300. +int32_t dwc_otg_pcd_handle_sof_intr(dwc_otg_pcd_t * pcd)
  51301. +{
  51302. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  51303. +
  51304. + gintsts_data_t gintsts;
  51305. +
  51306. + DWC_DEBUGPL(DBG_PCD, "SOF\n");
  51307. +
  51308. + /* Clear interrupt */
  51309. + gintsts.d32 = 0;
  51310. + gintsts.b.sofintr = 1;
  51311. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  51312. +
  51313. + return 1;
  51314. +}
  51315. +
  51316. +/**
  51317. + * This function handles the Rx Status Queue Level Interrupt, which
  51318. + * indicates that there is a least one packet in the Rx FIFO. The
  51319. + * packets are moved from the FIFO to memory, where they will be
  51320. + * processed when the Endpoint Interrupt Register indicates Transfer
  51321. + * Complete or SETUP Phase Done.
  51322. + *
  51323. + * Repeat the following until the Rx Status Queue is empty:
  51324. + * -# Read the Receive Status Pop Register (GRXSTSP) to get Packet
  51325. + * info
  51326. + * -# If Receive FIFO is empty then skip to step Clear the interrupt
  51327. + * and exit
  51328. + * -# If SETUP Packet call dwc_otg_read_setup_packet to copy the
  51329. + * SETUP data to the buffer
  51330. + * -# If OUT Data Packet call dwc_otg_read_packet to copy the data
  51331. + * to the destination buffer
  51332. + */
  51333. +int32_t dwc_otg_pcd_handle_rx_status_q_level_intr(dwc_otg_pcd_t * pcd)
  51334. +{
  51335. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  51336. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  51337. + gintmsk_data_t gintmask = {.d32 = 0 };
  51338. + device_grxsts_data_t status;
  51339. + dwc_otg_pcd_ep_t *ep;
  51340. + gintsts_data_t gintsts;
  51341. +#ifdef DEBUG
  51342. + static char *dpid_str[] = { "D0", "D2", "D1", "MDATA" };
  51343. +#endif
  51344. +
  51345. + //DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, _pcd);
  51346. + /* Disable the Rx Status Queue Level interrupt */
  51347. + gintmask.b.rxstsqlvl = 1;
  51348. + DWC_MODIFY_REG32(&global_regs->gintmsk, gintmask.d32, 0);
  51349. +
  51350. + /* Get the Status from the top of the FIFO */
  51351. + status.d32 = DWC_READ_REG32(&global_regs->grxstsp);
  51352. +
  51353. + DWC_DEBUGPL(DBG_PCD, "EP:%d BCnt:%d DPID:%s "
  51354. + "pktsts:%x Frame:%d(0x%0x)\n",
  51355. + status.b.epnum, status.b.bcnt,
  51356. + dpid_str[status.b.dpid],
  51357. + status.b.pktsts, status.b.fn, status.b.fn);
  51358. + /* Get pointer to EP structure */
  51359. + ep = get_out_ep(pcd, status.b.epnum);
  51360. +
  51361. + switch (status.b.pktsts) {
  51362. + case DWC_DSTS_GOUT_NAK:
  51363. + DWC_DEBUGPL(DBG_PCDV, "Global OUT NAK\n");
  51364. + break;
  51365. + case DWC_STS_DATA_UPDT:
  51366. + DWC_DEBUGPL(DBG_PCDV, "OUT Data Packet\n");
  51367. + if (status.b.bcnt && ep->dwc_ep.xfer_buff) {
  51368. + /** @todo NGS Check for buffer overflow? */
  51369. + dwc_otg_read_packet(core_if,
  51370. + ep->dwc_ep.xfer_buff,
  51371. + status.b.bcnt);
  51372. + ep->dwc_ep.xfer_count += status.b.bcnt;
  51373. + ep->dwc_ep.xfer_buff += status.b.bcnt;
  51374. + }
  51375. + break;
  51376. + case DWC_STS_XFER_COMP:
  51377. + DWC_DEBUGPL(DBG_PCDV, "OUT Complete\n");
  51378. + break;
  51379. + case DWC_DSTS_SETUP_COMP:
  51380. +#ifdef DEBUG_EP0
  51381. + DWC_DEBUGPL(DBG_PCDV, "Setup Complete\n");
  51382. +#endif
  51383. + break;
  51384. + case DWC_DSTS_SETUP_UPDT:
  51385. + dwc_otg_read_setup_packet(core_if, pcd->setup_pkt->d32);
  51386. +#ifdef DEBUG_EP0
  51387. + DWC_DEBUGPL(DBG_PCD,
  51388. + "SETUP PKT: %02x.%02x v%04x i%04x l%04x\n",
  51389. + pcd->setup_pkt->req.bmRequestType,
  51390. + pcd->setup_pkt->req.bRequest,
  51391. + UGETW(pcd->setup_pkt->req.wValue),
  51392. + UGETW(pcd->setup_pkt->req.wIndex),
  51393. + UGETW(pcd->setup_pkt->req.wLength));
  51394. +#endif
  51395. + ep->dwc_ep.xfer_count += status.b.bcnt;
  51396. + break;
  51397. + default:
  51398. + DWC_DEBUGPL(DBG_PCDV, "Invalid Packet Status (0x%0x)\n",
  51399. + status.b.pktsts);
  51400. + break;
  51401. + }
  51402. +
  51403. + /* Enable the Rx Status Queue Level interrupt */
  51404. + DWC_MODIFY_REG32(&global_regs->gintmsk, 0, gintmask.d32);
  51405. + /* Clear interrupt */
  51406. + gintsts.d32 = 0;
  51407. + gintsts.b.rxstsqlvl = 1;
  51408. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  51409. +
  51410. + //DWC_DEBUGPL(DBG_PCDV, "EXIT: %s\n", __func__);
  51411. + return 1;
  51412. +}
  51413. +
  51414. +/**
  51415. + * This function examines the Device IN Token Learning Queue to
  51416. + * determine the EP number of the last IN token received. This
  51417. + * implementation is for the Mass Storage device where there are only
  51418. + * 2 IN EPs (Control-IN and BULK-IN).
  51419. + *
  51420. + * The EP numbers for the first six IN Tokens are in DTKNQR1 and there
  51421. + * are 8 EP Numbers in each of the other possible DTKNQ Registers.
  51422. + *
  51423. + * @param core_if Programming view of DWC_otg controller.
  51424. + *
  51425. + */
  51426. +static inline int get_ep_of_last_in_token(dwc_otg_core_if_t * core_if)
  51427. +{
  51428. + dwc_otg_device_global_regs_t *dev_global_regs =
  51429. + core_if->dev_if->dev_global_regs;
  51430. + const uint32_t TOKEN_Q_DEPTH = core_if->hwcfg2.b.dev_token_q_depth;
  51431. + /* Number of Token Queue Registers */
  51432. + const int DTKNQ_REG_CNT = (TOKEN_Q_DEPTH + 7) / 8;
  51433. + dtknq1_data_t dtknqr1;
  51434. + uint32_t in_tkn_epnums[4];
  51435. + int ndx = 0;
  51436. + int i = 0;
  51437. + volatile uint32_t *addr = &dev_global_regs->dtknqr1;
  51438. + int epnum = 0;
  51439. +
  51440. + //DWC_DEBUGPL(DBG_PCD,"dev_token_q_depth=%d\n",TOKEN_Q_DEPTH);
  51441. +
  51442. + /* Read the DTKNQ Registers */
  51443. + for (i = 0; i < DTKNQ_REG_CNT; i++) {
  51444. + in_tkn_epnums[i] = DWC_READ_REG32(addr);
  51445. + DWC_DEBUGPL(DBG_PCDV, "DTKNQR%d=0x%08x\n", i + 1,
  51446. + in_tkn_epnums[i]);
  51447. + if (addr == &dev_global_regs->dvbusdis) {
  51448. + addr = &dev_global_regs->dtknqr3_dthrctl;
  51449. + } else {
  51450. + ++addr;
  51451. + }
  51452. +
  51453. + }
  51454. +
  51455. + /* Copy the DTKNQR1 data to the bit field. */
  51456. + dtknqr1.d32 = in_tkn_epnums[0];
  51457. + /* Get the EP numbers */
  51458. + in_tkn_epnums[0] = dtknqr1.b.epnums0_5;
  51459. + ndx = dtknqr1.b.intknwptr - 1;
  51460. +
  51461. + //DWC_DEBUGPL(DBG_PCDV,"ndx=%d\n",ndx);
  51462. + if (ndx == -1) {
  51463. + /** @todo Find a simpler way to calculate the max
  51464. + * queue position.*/
  51465. + int cnt = TOKEN_Q_DEPTH;
  51466. + if (TOKEN_Q_DEPTH <= 6) {
  51467. + cnt = TOKEN_Q_DEPTH - 1;
  51468. + } else if (TOKEN_Q_DEPTH <= 14) {
  51469. + cnt = TOKEN_Q_DEPTH - 7;
  51470. + } else if (TOKEN_Q_DEPTH <= 22) {
  51471. + cnt = TOKEN_Q_DEPTH - 15;
  51472. + } else {
  51473. + cnt = TOKEN_Q_DEPTH - 23;
  51474. + }
  51475. + epnum = (in_tkn_epnums[DTKNQ_REG_CNT - 1] >> (cnt * 4)) & 0xF;
  51476. + } else {
  51477. + if (ndx <= 5) {
  51478. + epnum = (in_tkn_epnums[0] >> (ndx * 4)) & 0xF;
  51479. + } else if (ndx <= 13) {
  51480. + ndx -= 6;
  51481. + epnum = (in_tkn_epnums[1] >> (ndx * 4)) & 0xF;
  51482. + } else if (ndx <= 21) {
  51483. + ndx -= 14;
  51484. + epnum = (in_tkn_epnums[2] >> (ndx * 4)) & 0xF;
  51485. + } else if (ndx <= 29) {
  51486. + ndx -= 22;
  51487. + epnum = (in_tkn_epnums[3] >> (ndx * 4)) & 0xF;
  51488. + }
  51489. + }
  51490. + //DWC_DEBUGPL(DBG_PCD,"epnum=%d\n",epnum);
  51491. + return epnum;
  51492. +}
  51493. +
  51494. +/**
  51495. + * This interrupt occurs when the non-periodic Tx FIFO is half-empty.
  51496. + * The active request is checked for the next packet to be loaded into
  51497. + * the non-periodic Tx FIFO.
  51498. + */
  51499. +int32_t dwc_otg_pcd_handle_np_tx_fifo_empty_intr(dwc_otg_pcd_t * pcd)
  51500. +{
  51501. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  51502. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  51503. + dwc_otg_dev_in_ep_regs_t *ep_regs;
  51504. + gnptxsts_data_t txstatus = {.d32 = 0 };
  51505. + gintsts_data_t gintsts;
  51506. +
  51507. + int epnum = 0;
  51508. + dwc_otg_pcd_ep_t *ep = 0;
  51509. + uint32_t len = 0;
  51510. + int dwords;
  51511. +
  51512. + /* Get the epnum from the IN Token Learning Queue. */
  51513. + epnum = get_ep_of_last_in_token(core_if);
  51514. + ep = get_in_ep(pcd, epnum);
  51515. +
  51516. + DWC_DEBUGPL(DBG_PCD, "NP TxFifo Empty: %d \n", epnum);
  51517. +
  51518. + ep_regs = core_if->dev_if->in_ep_regs[epnum];
  51519. +
  51520. + len = ep->dwc_ep.xfer_len - ep->dwc_ep.xfer_count;
  51521. + if (len > ep->dwc_ep.maxpacket) {
  51522. + len = ep->dwc_ep.maxpacket;
  51523. + }
  51524. + dwords = (len + 3) / 4;
  51525. +
  51526. + /* While there is space in the queue and space in the FIFO and
  51527. + * More data to tranfer, Write packets to the Tx FIFO */
  51528. + txstatus.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
  51529. + DWC_DEBUGPL(DBG_PCDV, "b4 GNPTXSTS=0x%08x\n", txstatus.d32);
  51530. +
  51531. + while (txstatus.b.nptxqspcavail > 0 &&
  51532. + txstatus.b.nptxfspcavail > dwords &&
  51533. + ep->dwc_ep.xfer_count < ep->dwc_ep.xfer_len) {
  51534. + /* Write the FIFO */
  51535. + dwc_otg_ep_write_packet(core_if, &ep->dwc_ep, 0);
  51536. + len = ep->dwc_ep.xfer_len - ep->dwc_ep.xfer_count;
  51537. +
  51538. + if (len > ep->dwc_ep.maxpacket) {
  51539. + len = ep->dwc_ep.maxpacket;
  51540. + }
  51541. +
  51542. + dwords = (len + 3) / 4;
  51543. + txstatus.d32 = DWC_READ_REG32(&global_regs->gnptxsts);
  51544. + DWC_DEBUGPL(DBG_PCDV, "GNPTXSTS=0x%08x\n", txstatus.d32);
  51545. + }
  51546. +
  51547. + DWC_DEBUGPL(DBG_PCDV, "GNPTXSTS=0x%08x\n",
  51548. + DWC_READ_REG32(&global_regs->gnptxsts));
  51549. +
  51550. + /* Clear interrupt */
  51551. + gintsts.d32 = 0;
  51552. + gintsts.b.nptxfempty = 1;
  51553. + DWC_WRITE_REG32(&global_regs->gintsts, gintsts.d32);
  51554. +
  51555. + return 1;
  51556. +}
  51557. +
  51558. +/**
  51559. + * This function is called when dedicated Tx FIFO Empty interrupt occurs.
  51560. + * The active request is checked for the next packet to be loaded into
  51561. + * apropriate Tx FIFO.
  51562. + */
  51563. +static int32_t write_empty_tx_fifo(dwc_otg_pcd_t * pcd, uint32_t epnum)
  51564. +{
  51565. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  51566. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  51567. + dwc_otg_dev_in_ep_regs_t *ep_regs;
  51568. + dtxfsts_data_t txstatus = {.d32 = 0 };
  51569. + dwc_otg_pcd_ep_t *ep = 0;
  51570. + uint32_t len = 0;
  51571. + int dwords;
  51572. +
  51573. + ep = get_in_ep(pcd, epnum);
  51574. +
  51575. + DWC_DEBUGPL(DBG_PCD, "Dedicated TxFifo Empty: %d \n", epnum);
  51576. +
  51577. + ep_regs = core_if->dev_if->in_ep_regs[epnum];
  51578. +
  51579. + len = ep->dwc_ep.xfer_len - ep->dwc_ep.xfer_count;
  51580. +
  51581. + if (len > ep->dwc_ep.maxpacket) {
  51582. + len = ep->dwc_ep.maxpacket;
  51583. + }
  51584. +
  51585. + dwords = (len + 3) / 4;
  51586. +
  51587. + /* While there is space in the queue and space in the FIFO and
  51588. + * More data to tranfer, Write packets to the Tx FIFO */
  51589. + txstatus.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts);
  51590. + DWC_DEBUGPL(DBG_PCDV, "b4 dtxfsts[%d]=0x%08x\n", epnum, txstatus.d32);
  51591. +
  51592. + while (txstatus.b.txfspcavail > dwords &&
  51593. + ep->dwc_ep.xfer_count < ep->dwc_ep.xfer_len &&
  51594. + ep->dwc_ep.xfer_len != 0) {
  51595. + /* Write the FIFO */
  51596. + dwc_otg_ep_write_packet(core_if, &ep->dwc_ep, 0);
  51597. +
  51598. + len = ep->dwc_ep.xfer_len - ep->dwc_ep.xfer_count;
  51599. + if (len > ep->dwc_ep.maxpacket) {
  51600. + len = ep->dwc_ep.maxpacket;
  51601. + }
  51602. +
  51603. + dwords = (len + 3) / 4;
  51604. + txstatus.d32 =
  51605. + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts);
  51606. + DWC_DEBUGPL(DBG_PCDV, "dtxfsts[%d]=0x%08x\n", epnum,
  51607. + txstatus.d32);
  51608. + }
  51609. +
  51610. + DWC_DEBUGPL(DBG_PCDV, "b4 dtxfsts[%d]=0x%08x\n", epnum,
  51611. + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dtxfsts));
  51612. +
  51613. + return 1;
  51614. +}
  51615. +
  51616. +/**
  51617. + * This function is called when the Device is disconnected. It stops
  51618. + * any active requests and informs the Gadget driver of the
  51619. + * disconnect.
  51620. + */
  51621. +void dwc_otg_pcd_stop(dwc_otg_pcd_t * pcd)
  51622. +{
  51623. + int i, num_in_eps, num_out_eps;
  51624. + dwc_otg_pcd_ep_t *ep;
  51625. +
  51626. + gintmsk_data_t intr_mask = {.d32 = 0 };
  51627. +
  51628. + DWC_SPINLOCK(pcd->lock);
  51629. +
  51630. + num_in_eps = GET_CORE_IF(pcd)->dev_if->num_in_eps;
  51631. + num_out_eps = GET_CORE_IF(pcd)->dev_if->num_out_eps;
  51632. +
  51633. + DWC_DEBUGPL(DBG_PCDV, "%s() \n", __func__);
  51634. + /* don't disconnect drivers more than once */
  51635. + if (pcd->ep0state == EP0_DISCONNECT) {
  51636. + DWC_DEBUGPL(DBG_ANY, "%s() Already Disconnected\n", __func__);
  51637. + DWC_SPINUNLOCK(pcd->lock);
  51638. + return;
  51639. + }
  51640. + pcd->ep0state = EP0_DISCONNECT;
  51641. +
  51642. + /* Reset the OTG state. */
  51643. + dwc_otg_pcd_update_otg(pcd, 1);
  51644. +
  51645. + /* Disable the NP Tx Fifo Empty Interrupt. */
  51646. + intr_mask.b.nptxfempty = 1;
  51647. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
  51648. + intr_mask.d32, 0);
  51649. +
  51650. + /* Flush the FIFOs */
  51651. + /**@todo NGS Flush Periodic FIFOs */
  51652. + dwc_otg_flush_tx_fifo(GET_CORE_IF(pcd), 0x10);
  51653. + dwc_otg_flush_rx_fifo(GET_CORE_IF(pcd));
  51654. +
  51655. + /* prevent new request submissions, kill any outstanding requests */
  51656. + ep = &pcd->ep0;
  51657. + dwc_otg_request_nuke(ep);
  51658. + /* prevent new request submissions, kill any outstanding requests */
  51659. + for (i = 0; i < num_in_eps; i++) {
  51660. + dwc_otg_pcd_ep_t *ep = &pcd->in_ep[i];
  51661. + dwc_otg_request_nuke(ep);
  51662. + }
  51663. + /* prevent new request submissions, kill any outstanding requests */
  51664. + for (i = 0; i < num_out_eps; i++) {
  51665. + dwc_otg_pcd_ep_t *ep = &pcd->out_ep[i];
  51666. + dwc_otg_request_nuke(ep);
  51667. + }
  51668. +
  51669. + /* report disconnect; the driver is already quiesced */
  51670. + if (pcd->fops->disconnect) {
  51671. + DWC_SPINUNLOCK(pcd->lock);
  51672. + pcd->fops->disconnect(pcd);
  51673. + DWC_SPINLOCK(pcd->lock);
  51674. + }
  51675. + DWC_SPINUNLOCK(pcd->lock);
  51676. +}
  51677. +
  51678. +/**
  51679. + * This interrupt indicates that ...
  51680. + */
  51681. +int32_t dwc_otg_pcd_handle_i2c_intr(dwc_otg_pcd_t * pcd)
  51682. +{
  51683. + gintmsk_data_t intr_mask = {.d32 = 0 };
  51684. + gintsts_data_t gintsts;
  51685. +
  51686. + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n", "i2cintr");
  51687. + intr_mask.b.i2cintr = 1;
  51688. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
  51689. + intr_mask.d32, 0);
  51690. +
  51691. + /* Clear interrupt */
  51692. + gintsts.d32 = 0;
  51693. + gintsts.b.i2cintr = 1;
  51694. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  51695. + gintsts.d32);
  51696. + return 1;
  51697. +}
  51698. +
  51699. +/**
  51700. + * This interrupt indicates that ...
  51701. + */
  51702. +int32_t dwc_otg_pcd_handle_early_suspend_intr(dwc_otg_pcd_t * pcd)
  51703. +{
  51704. + gintsts_data_t gintsts;
  51705. +#if defined(VERBOSE)
  51706. + DWC_PRINTF("Early Suspend Detected\n");
  51707. +#endif
  51708. +
  51709. + /* Clear interrupt */
  51710. + gintsts.d32 = 0;
  51711. + gintsts.b.erlysuspend = 1;
  51712. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  51713. + gintsts.d32);
  51714. + return 1;
  51715. +}
  51716. +
  51717. +/**
  51718. + * This function configures EPO to receive SETUP packets.
  51719. + *
  51720. + * @todo NGS: Update the comments from the HW FS.
  51721. + *
  51722. + * -# Program the following fields in the endpoint specific registers
  51723. + * for Control OUT EP 0, in order to receive a setup packet
  51724. + * - DOEPTSIZ0.Packet Count = 3 (To receive up to 3 back to back
  51725. + * setup packets)
  51726. + * - DOEPTSIZE0.Transfer Size = 24 Bytes (To receive up to 3 back
  51727. + * to back setup packets)
  51728. + * - In DMA mode, DOEPDMA0 Register with a memory address to
  51729. + * store any setup packets received
  51730. + *
  51731. + * @param core_if Programming view of DWC_otg controller.
  51732. + * @param pcd Programming view of the PCD.
  51733. + */
  51734. +static inline void ep0_out_start(dwc_otg_core_if_t * core_if,
  51735. + dwc_otg_pcd_t * pcd)
  51736. +{
  51737. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  51738. + deptsiz0_data_t doeptsize0 = {.d32 = 0 };
  51739. + dwc_otg_dev_dma_desc_t *dma_desc;
  51740. + depctl_data_t doepctl = {.d32 = 0 };
  51741. +
  51742. +#ifdef VERBOSE
  51743. + DWC_DEBUGPL(DBG_PCDV, "%s() doepctl0=%0x\n", __func__,
  51744. + DWC_READ_REG32(&dev_if->out_ep_regs[0]->doepctl));
  51745. +#endif
  51746. + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
  51747. + doepctl.d32 = DWC_READ_REG32(&dev_if->out_ep_regs[0]->doepctl);
  51748. + if (doepctl.b.epena) {
  51749. + return;
  51750. + }
  51751. + }
  51752. +
  51753. + doeptsize0.b.supcnt = 3;
  51754. + doeptsize0.b.pktcnt = 1;
  51755. + doeptsize0.b.xfersize = 8 * 3;
  51756. +
  51757. + if (core_if->dma_enable) {
  51758. + if (!core_if->dma_desc_enable) {
  51759. + /** put here as for Hermes mode deptisz register should not be written */
  51760. + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doeptsiz,
  51761. + doeptsize0.d32);
  51762. +
  51763. + /** @todo dma needs to handle multiple setup packets (up to 3) */
  51764. + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doepdma,
  51765. + pcd->setup_pkt_dma_handle);
  51766. + } else {
  51767. + dev_if->setup_desc_index =
  51768. + (dev_if->setup_desc_index + 1) & 1;
  51769. + dma_desc =
  51770. + dev_if->setup_desc_addr[dev_if->setup_desc_index];
  51771. +
  51772. + /** DMA Descriptor Setup */
  51773. + dma_desc->status.b.bs = BS_HOST_BUSY;
  51774. + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
  51775. + dma_desc->status.b.sr = 0;
  51776. + dma_desc->status.b.mtrf = 0;
  51777. + }
  51778. + dma_desc->status.b.l = 1;
  51779. + dma_desc->status.b.ioc = 1;
  51780. + dma_desc->status.b.bytes = pcd->ep0.dwc_ep.maxpacket;
  51781. + dma_desc->buf = pcd->setup_pkt_dma_handle;
  51782. + dma_desc->status.b.sts = 0;
  51783. + dma_desc->status.b.bs = BS_HOST_READY;
  51784. +
  51785. + /** DOEPDMA0 Register write */
  51786. + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doepdma,
  51787. + dev_if->dma_setup_desc_addr
  51788. + [dev_if->setup_desc_index]);
  51789. + }
  51790. +
  51791. + } else {
  51792. + /** put here as for Hermes mode deptisz register should not be written */
  51793. + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doeptsiz,
  51794. + doeptsize0.d32);
  51795. + }
  51796. +
  51797. + /** DOEPCTL0 Register write cnak will be set after setup interrupt */
  51798. + doepctl.d32 = 0;
  51799. + doepctl.b.epena = 1;
  51800. + if (core_if->snpsid <= OTG_CORE_REV_2_94a) {
  51801. + doepctl.b.cnak = 1;
  51802. + DWC_WRITE_REG32(&dev_if->out_ep_regs[0]->doepctl, doepctl.d32);
  51803. + } else {
  51804. + DWC_MODIFY_REG32(&dev_if->out_ep_regs[0]->doepctl, 0, doepctl.d32);
  51805. + }
  51806. +
  51807. +#ifdef VERBOSE
  51808. + DWC_DEBUGPL(DBG_PCDV, "doepctl0=%0x\n",
  51809. + DWC_READ_REG32(&dev_if->out_ep_regs[0]->doepctl));
  51810. + DWC_DEBUGPL(DBG_PCDV, "diepctl0=%0x\n",
  51811. + DWC_READ_REG32(&dev_if->in_ep_regs[0]->diepctl));
  51812. +#endif
  51813. +}
  51814. +
  51815. +/**
  51816. + * This interrupt occurs when a USB Reset is detected. When the USB
  51817. + * Reset Interrupt occurs the device state is set to DEFAULT and the
  51818. + * EP0 state is set to IDLE.
  51819. + * -# Set the NAK bit for all OUT endpoints (DOEPCTLn.SNAK = 1)
  51820. + * -# Unmask the following interrupt bits
  51821. + * - DAINTMSK.INEP0 = 1 (Control 0 IN endpoint)
  51822. + * - DAINTMSK.OUTEP0 = 1 (Control 0 OUT endpoint)
  51823. + * - DOEPMSK.SETUP = 1
  51824. + * - DOEPMSK.XferCompl = 1
  51825. + * - DIEPMSK.XferCompl = 1
  51826. + * - DIEPMSK.TimeOut = 1
  51827. + * -# Program the following fields in the endpoint specific registers
  51828. + * for Control OUT EP 0, in order to receive a setup packet
  51829. + * - DOEPTSIZ0.Packet Count = 3 (To receive up to 3 back to back
  51830. + * setup packets)
  51831. + * - DOEPTSIZE0.Transfer Size = 24 Bytes (To receive up to 3 back
  51832. + * to back setup packets)
  51833. + * - In DMA mode, DOEPDMA0 Register with a memory address to
  51834. + * store any setup packets received
  51835. + * At this point, all the required initialization, except for enabling
  51836. + * the control 0 OUT endpoint is done, for receiving SETUP packets.
  51837. + */
  51838. +int32_t dwc_otg_pcd_handle_usb_reset_intr(dwc_otg_pcd_t * pcd)
  51839. +{
  51840. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  51841. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  51842. + depctl_data_t doepctl = {.d32 = 0 };
  51843. + depctl_data_t diepctl = {.d32 = 0 };
  51844. + daint_data_t daintmsk = {.d32 = 0 };
  51845. + doepmsk_data_t doepmsk = {.d32 = 0 };
  51846. + diepmsk_data_t diepmsk = {.d32 = 0 };
  51847. + dcfg_data_t dcfg = {.d32 = 0 };
  51848. + grstctl_t resetctl = {.d32 = 0 };
  51849. + dctl_data_t dctl = {.d32 = 0 };
  51850. + int i = 0;
  51851. + gintsts_data_t gintsts;
  51852. + pcgcctl_data_t power = {.d32 = 0 };
  51853. +
  51854. + power.d32 = DWC_READ_REG32(core_if->pcgcctl);
  51855. + if (power.b.stoppclk) {
  51856. + power.d32 = 0;
  51857. + power.b.stoppclk = 1;
  51858. + DWC_MODIFY_REG32(core_if->pcgcctl, power.d32, 0);
  51859. +
  51860. + power.b.pwrclmp = 1;
  51861. + DWC_MODIFY_REG32(core_if->pcgcctl, power.d32, 0);
  51862. +
  51863. + power.b.rstpdwnmodule = 1;
  51864. + DWC_MODIFY_REG32(core_if->pcgcctl, power.d32, 0);
  51865. + }
  51866. +
  51867. + core_if->lx_state = DWC_OTG_L0;
  51868. +
  51869. + DWC_PRINTF("USB RESET\n");
  51870. +#ifdef DWC_EN_ISOC
  51871. + for (i = 1; i < 16; ++i) {
  51872. + dwc_otg_pcd_ep_t *ep;
  51873. + dwc_ep_t *dwc_ep;
  51874. + ep = get_in_ep(pcd, i);
  51875. + if (ep != 0) {
  51876. + dwc_ep = &ep->dwc_ep;
  51877. + dwc_ep->next_frame = 0xffffffff;
  51878. + }
  51879. + }
  51880. +#endif /* DWC_EN_ISOC */
  51881. +
  51882. + /* reset the HNP settings */
  51883. + dwc_otg_pcd_update_otg(pcd, 1);
  51884. +
  51885. + /* Clear the Remote Wakeup Signalling */
  51886. + dctl.b.rmtwkupsig = 1;
  51887. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32, 0);
  51888. +
  51889. + /* Set NAK for all OUT EPs */
  51890. + doepctl.b.snak = 1;
  51891. + for (i = 0; i <= dev_if->num_out_eps; i++) {
  51892. + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doepctl, doepctl.d32);
  51893. + }
  51894. +
  51895. + /* Flush the NP Tx FIFO */
  51896. + dwc_otg_flush_tx_fifo(core_if, 0x10);
  51897. + /* Flush the Learning Queue */
  51898. + resetctl.b.intknqflsh = 1;
  51899. + DWC_WRITE_REG32(&core_if->core_global_regs->grstctl, resetctl.d32);
  51900. +
  51901. + if (!core_if->core_params->en_multiple_tx_fifo && core_if->dma_enable) {
  51902. + core_if->start_predict = 0;
  51903. + for (i = 0; i<= core_if->dev_if->num_in_eps; ++i) {
  51904. + core_if->nextep_seq[i] = 0xff; // 0xff - EP not active
  51905. + }
  51906. + core_if->nextep_seq[0] = 0;
  51907. + core_if->first_in_nextep_seq = 0;
  51908. + diepctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[0]->diepctl);
  51909. + diepctl.b.nextep = 0;
  51910. + DWC_WRITE_REG32(&dev_if->in_ep_regs[0]->diepctl, diepctl.d32);
  51911. +
  51912. + /* Update IN Endpoint Mismatch Count by active IN NP EP count + 1 */
  51913. + dcfg.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dcfg);
  51914. + dcfg.b.epmscnt = 2;
  51915. + DWC_WRITE_REG32(&dev_if->dev_global_regs->dcfg, dcfg.d32);
  51916. +
  51917. + DWC_DEBUGPL(DBG_PCDV,
  51918. + "%s first_in_nextep_seq= %2d; nextep_seq[]:\n",
  51919. + __func__, core_if->first_in_nextep_seq);
  51920. + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
  51921. + DWC_DEBUGPL(DBG_PCDV, "%2d\n", core_if->nextep_seq[i]);
  51922. + }
  51923. + }
  51924. +
  51925. + if (core_if->multiproc_int_enable) {
  51926. + daintmsk.b.inep0 = 1;
  51927. + daintmsk.b.outep0 = 1;
  51928. + DWC_WRITE_REG32(&dev_if->dev_global_regs->deachintmsk,
  51929. + daintmsk.d32);
  51930. +
  51931. + doepmsk.b.setup = 1;
  51932. + doepmsk.b.xfercompl = 1;
  51933. + doepmsk.b.ahberr = 1;
  51934. + doepmsk.b.epdisabled = 1;
  51935. +
  51936. + if ((core_if->dma_desc_enable) ||
  51937. + (core_if->dma_enable
  51938. + && core_if->snpsid >= OTG_CORE_REV_3_00a)) {
  51939. + doepmsk.b.stsphsercvd = 1;
  51940. + }
  51941. + if (core_if->dma_desc_enable)
  51942. + doepmsk.b.bna = 1;
  51943. +/*
  51944. + doepmsk.b.babble = 1;
  51945. + doepmsk.b.nyet = 1;
  51946. +
  51947. + if (core_if->dma_enable) {
  51948. + doepmsk.b.nak = 1;
  51949. + }
  51950. +*/
  51951. + DWC_WRITE_REG32(&dev_if->dev_global_regs->doepeachintmsk[0],
  51952. + doepmsk.d32);
  51953. +
  51954. + diepmsk.b.xfercompl = 1;
  51955. + diepmsk.b.timeout = 1;
  51956. + diepmsk.b.epdisabled = 1;
  51957. + diepmsk.b.ahberr = 1;
  51958. + diepmsk.b.intknepmis = 1;
  51959. + if (!core_if->en_multiple_tx_fifo && core_if->dma_enable)
  51960. + diepmsk.b.intknepmis = 0;
  51961. +
  51962. +/* if (core_if->dma_desc_enable) {
  51963. + diepmsk.b.bna = 1;
  51964. + }
  51965. +*/
  51966. +/*
  51967. + if (core_if->dma_enable) {
  51968. + diepmsk.b.nak = 1;
  51969. + }
  51970. +*/
  51971. + DWC_WRITE_REG32(&dev_if->dev_global_regs->diepeachintmsk[0],
  51972. + diepmsk.d32);
  51973. + } else {
  51974. + daintmsk.b.inep0 = 1;
  51975. + daintmsk.b.outep0 = 1;
  51976. + DWC_WRITE_REG32(&dev_if->dev_global_regs->daintmsk,
  51977. + daintmsk.d32);
  51978. +
  51979. + doepmsk.b.setup = 1;
  51980. + doepmsk.b.xfercompl = 1;
  51981. + doepmsk.b.ahberr = 1;
  51982. + doepmsk.b.epdisabled = 1;
  51983. +
  51984. + if ((core_if->dma_desc_enable) ||
  51985. + (core_if->dma_enable
  51986. + && core_if->snpsid >= OTG_CORE_REV_3_00a)) {
  51987. + doepmsk.b.stsphsercvd = 1;
  51988. + }
  51989. + if (core_if->dma_desc_enable)
  51990. + doepmsk.b.bna = 1;
  51991. + DWC_WRITE_REG32(&dev_if->dev_global_regs->doepmsk, doepmsk.d32);
  51992. +
  51993. + diepmsk.b.xfercompl = 1;
  51994. + diepmsk.b.timeout = 1;
  51995. + diepmsk.b.epdisabled = 1;
  51996. + diepmsk.b.ahberr = 1;
  51997. + if (!core_if->en_multiple_tx_fifo && core_if->dma_enable)
  51998. + diepmsk.b.intknepmis = 0;
  51999. +/*
  52000. + if (core_if->dma_desc_enable) {
  52001. + diepmsk.b.bna = 1;
  52002. + }
  52003. +*/
  52004. +
  52005. + DWC_WRITE_REG32(&dev_if->dev_global_regs->diepmsk, diepmsk.d32);
  52006. + }
  52007. +
  52008. + /* Reset Device Address */
  52009. + dcfg.d32 = DWC_READ_REG32(&dev_if->dev_global_regs->dcfg);
  52010. + dcfg.b.devaddr = 0;
  52011. + DWC_WRITE_REG32(&dev_if->dev_global_regs->dcfg, dcfg.d32);
  52012. +
  52013. + /* setup EP0 to receive SETUP packets */
  52014. + if (core_if->snpsid <= OTG_CORE_REV_2_94a)
  52015. + ep0_out_start(core_if, pcd);
  52016. +
  52017. + /* Clear interrupt */
  52018. + gintsts.d32 = 0;
  52019. + gintsts.b.usbreset = 1;
  52020. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  52021. +
  52022. + return 1;
  52023. +}
  52024. +
  52025. +/**
  52026. + * Get the device speed from the device status register and convert it
  52027. + * to USB speed constant.
  52028. + *
  52029. + * @param core_if Programming view of DWC_otg controller.
  52030. + */
  52031. +static int get_device_speed(dwc_otg_core_if_t * core_if)
  52032. +{
  52033. + dsts_data_t dsts;
  52034. + int speed = 0;
  52035. + dsts.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dsts);
  52036. +
  52037. + switch (dsts.b.enumspd) {
  52038. + case DWC_DSTS_ENUMSPD_HS_PHY_30MHZ_OR_60MHZ:
  52039. + speed = USB_SPEED_HIGH;
  52040. + break;
  52041. + case DWC_DSTS_ENUMSPD_FS_PHY_30MHZ_OR_60MHZ:
  52042. + case DWC_DSTS_ENUMSPD_FS_PHY_48MHZ:
  52043. + speed = USB_SPEED_FULL;
  52044. + break;
  52045. +
  52046. + case DWC_DSTS_ENUMSPD_LS_PHY_6MHZ:
  52047. + speed = USB_SPEED_LOW;
  52048. + break;
  52049. + }
  52050. +
  52051. + return speed;
  52052. +}
  52053. +
  52054. +/**
  52055. + * Read the device status register and set the device speed in the
  52056. + * data structure.
  52057. + * Set up EP0 to receive SETUP packets by calling dwc_ep0_activate.
  52058. + */
  52059. +int32_t dwc_otg_pcd_handle_enum_done_intr(dwc_otg_pcd_t * pcd)
  52060. +{
  52061. + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
  52062. + gintsts_data_t gintsts;
  52063. + gusbcfg_data_t gusbcfg;
  52064. + dwc_otg_core_global_regs_t *global_regs =
  52065. + GET_CORE_IF(pcd)->core_global_regs;
  52066. + uint8_t utmi16b, utmi8b;
  52067. + int speed;
  52068. + DWC_DEBUGPL(DBG_PCD, "SPEED ENUM\n");
  52069. +
  52070. + if (GET_CORE_IF(pcd)->snpsid >= OTG_CORE_REV_2_60a) {
  52071. + utmi16b = 6; //vahrama old value was 6;
  52072. + utmi8b = 9;
  52073. + } else {
  52074. + utmi16b = 4;
  52075. + utmi8b = 8;
  52076. + }
  52077. + dwc_otg_ep0_activate(GET_CORE_IF(pcd), &ep0->dwc_ep);
  52078. + if (GET_CORE_IF(pcd)->snpsid >= OTG_CORE_REV_3_00a) {
  52079. + ep0_out_start(GET_CORE_IF(pcd), pcd);
  52080. + }
  52081. +
  52082. +#ifdef DEBUG_EP0
  52083. + print_ep0_state(pcd);
  52084. +#endif
  52085. +
  52086. + if (pcd->ep0state == EP0_DISCONNECT) {
  52087. + pcd->ep0state = EP0_IDLE;
  52088. + } else if (pcd->ep0state == EP0_STALL) {
  52089. + pcd->ep0state = EP0_IDLE;
  52090. + }
  52091. +
  52092. + pcd->ep0state = EP0_IDLE;
  52093. +
  52094. + ep0->stopped = 0;
  52095. +
  52096. + speed = get_device_speed(GET_CORE_IF(pcd));
  52097. + pcd->fops->connect(pcd, speed);
  52098. +
  52099. + /* Set USB turnaround time based on device speed and PHY interface. */
  52100. + gusbcfg.d32 = DWC_READ_REG32(&global_regs->gusbcfg);
  52101. + if (speed == USB_SPEED_HIGH) {
  52102. + if (GET_CORE_IF(pcd)->hwcfg2.b.hs_phy_type ==
  52103. + DWC_HWCFG2_HS_PHY_TYPE_ULPI) {
  52104. + /* ULPI interface */
  52105. + gusbcfg.b.usbtrdtim = 9;
  52106. + }
  52107. + if (GET_CORE_IF(pcd)->hwcfg2.b.hs_phy_type ==
  52108. + DWC_HWCFG2_HS_PHY_TYPE_UTMI) {
  52109. + /* UTMI+ interface */
  52110. + if (GET_CORE_IF(pcd)->hwcfg4.b.utmi_phy_data_width == 0) {
  52111. + gusbcfg.b.usbtrdtim = utmi8b;
  52112. + } else if (GET_CORE_IF(pcd)->hwcfg4.
  52113. + b.utmi_phy_data_width == 1) {
  52114. + gusbcfg.b.usbtrdtim = utmi16b;
  52115. + } else if (GET_CORE_IF(pcd)->
  52116. + core_params->phy_utmi_width == 8) {
  52117. + gusbcfg.b.usbtrdtim = utmi8b;
  52118. + } else {
  52119. + gusbcfg.b.usbtrdtim = utmi16b;
  52120. + }
  52121. + }
  52122. + if (GET_CORE_IF(pcd)->hwcfg2.b.hs_phy_type ==
  52123. + DWC_HWCFG2_HS_PHY_TYPE_UTMI_ULPI) {
  52124. + /* UTMI+ OR ULPI interface */
  52125. + if (gusbcfg.b.ulpi_utmi_sel == 1) {
  52126. + /* ULPI interface */
  52127. + gusbcfg.b.usbtrdtim = 9;
  52128. + } else {
  52129. + /* UTMI+ interface */
  52130. + if (GET_CORE_IF(pcd)->
  52131. + core_params->phy_utmi_width == 16) {
  52132. + gusbcfg.b.usbtrdtim = utmi16b;
  52133. + } else {
  52134. + gusbcfg.b.usbtrdtim = utmi8b;
  52135. + }
  52136. + }
  52137. + }
  52138. + } else {
  52139. + /* Full or low speed */
  52140. + gusbcfg.b.usbtrdtim = 9;
  52141. + }
  52142. + DWC_WRITE_REG32(&global_regs->gusbcfg, gusbcfg.d32);
  52143. +
  52144. + /* Clear interrupt */
  52145. + gintsts.d32 = 0;
  52146. + gintsts.b.enumdone = 1;
  52147. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  52148. + gintsts.d32);
  52149. + return 1;
  52150. +}
  52151. +
  52152. +/**
  52153. + * This interrupt indicates that the ISO OUT Packet was dropped due to
  52154. + * Rx FIFO full or Rx Status Queue Full. If this interrupt occurs
  52155. + * read all the data from the Rx FIFO.
  52156. + */
  52157. +int32_t dwc_otg_pcd_handle_isoc_out_packet_dropped_intr(dwc_otg_pcd_t * pcd)
  52158. +{
  52159. + gintmsk_data_t intr_mask = {.d32 = 0 };
  52160. + gintsts_data_t gintsts;
  52161. +
  52162. + DWC_WARN("INTERRUPT Handler not implemented for %s\n",
  52163. + "ISOC Out Dropped");
  52164. +
  52165. + intr_mask.b.isooutdrop = 1;
  52166. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
  52167. + intr_mask.d32, 0);
  52168. +
  52169. + /* Clear interrupt */
  52170. + gintsts.d32 = 0;
  52171. + gintsts.b.isooutdrop = 1;
  52172. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  52173. + gintsts.d32);
  52174. +
  52175. + return 1;
  52176. +}
  52177. +
  52178. +/**
  52179. + * This interrupt indicates the end of the portion of the micro-frame
  52180. + * for periodic transactions. If there is a periodic transaction for
  52181. + * the next frame, load the packets into the EP periodic Tx FIFO.
  52182. + */
  52183. +int32_t dwc_otg_pcd_handle_end_periodic_frame_intr(dwc_otg_pcd_t * pcd)
  52184. +{
  52185. + gintmsk_data_t intr_mask = {.d32 = 0 };
  52186. + gintsts_data_t gintsts;
  52187. + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n", "EOP");
  52188. +
  52189. + intr_mask.b.eopframe = 1;
  52190. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
  52191. + intr_mask.d32, 0);
  52192. +
  52193. + /* Clear interrupt */
  52194. + gintsts.d32 = 0;
  52195. + gintsts.b.eopframe = 1;
  52196. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  52197. + gintsts.d32);
  52198. +
  52199. + return 1;
  52200. +}
  52201. +
  52202. +/**
  52203. + * This interrupt indicates that EP of the packet on the top of the
  52204. + * non-periodic Tx FIFO does not match EP of the IN Token received.
  52205. + *
  52206. + * The "Device IN Token Queue" Registers are read to determine the
  52207. + * order the IN Tokens have been received. The non-periodic Tx FIFO
  52208. + * is flushed, so it can be reloaded in the order seen in the IN Token
  52209. + * Queue.
  52210. + */
  52211. +int32_t dwc_otg_pcd_handle_ep_mismatch_intr(dwc_otg_pcd_t * pcd)
  52212. +{
  52213. + gintsts_data_t gintsts;
  52214. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  52215. + dctl_data_t dctl;
  52216. + gintmsk_data_t intr_mask = {.d32 = 0 };
  52217. +
  52218. + if (!core_if->en_multiple_tx_fifo && core_if->dma_enable) {
  52219. + core_if->start_predict = 1;
  52220. +
  52221. + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, core_if);
  52222. +
  52223. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  52224. + if (!gintsts.b.ginnakeff) {
  52225. + /* Disable EP Mismatch interrupt */
  52226. + intr_mask.d32 = 0;
  52227. + intr_mask.b.epmismatch = 1;
  52228. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, intr_mask.d32, 0);
  52229. + /* Enable the Global IN NAK Effective Interrupt */
  52230. + intr_mask.d32 = 0;
  52231. + intr_mask.b.ginnakeff = 1;
  52232. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, 0, intr_mask.d32);
  52233. + /* Set the global non-periodic IN NAK handshake */
  52234. + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dctl);
  52235. + dctl.b.sgnpinnak = 1;
  52236. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32);
  52237. + } else {
  52238. + DWC_PRINTF("gintsts.b.ginnakeff = 1! dctl.b.sgnpinnak not set\n");
  52239. + }
  52240. + /* Disabling of all EP's will be done in dwc_otg_pcd_handle_in_nak_effective()
  52241. + * handler after Global IN NAK Effective interrupt will be asserted */
  52242. + }
  52243. + /* Clear interrupt */
  52244. + gintsts.d32 = 0;
  52245. + gintsts.b.epmismatch = 1;
  52246. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  52247. +
  52248. + return 1;
  52249. +}
  52250. +
  52251. +/**
  52252. + * This interrupt is valid only in DMA mode. This interrupt indicates that the
  52253. + * core has stopped fetching data for IN endpoints due to the unavailability of
  52254. + * TxFIFO space or Request Queue space. This interrupt is used by the
  52255. + * application for an endpoint mismatch algorithm.
  52256. + *
  52257. + * @param pcd The PCD
  52258. + */
  52259. +int32_t dwc_otg_pcd_handle_ep_fetsusp_intr(dwc_otg_pcd_t * pcd)
  52260. +{
  52261. + gintsts_data_t gintsts;
  52262. + gintmsk_data_t gintmsk_data;
  52263. + dctl_data_t dctl;
  52264. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  52265. + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, core_if);
  52266. +
  52267. + /* Clear the global non-periodic IN NAK handshake */
  52268. + dctl.d32 = 0;
  52269. + dctl.b.cgnpinnak = 1;
  52270. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32, dctl.d32);
  52271. +
  52272. + /* Mask GINTSTS.FETSUSP interrupt */
  52273. + gintmsk_data.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
  52274. + gintmsk_data.b.fetsusp = 0;
  52275. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, gintmsk_data.d32);
  52276. +
  52277. + /* Clear interrupt */
  52278. + gintsts.d32 = 0;
  52279. + gintsts.b.fetsusp = 1;
  52280. + DWC_WRITE_REG32(&core_if->core_global_regs->gintsts, gintsts.d32);
  52281. +
  52282. + return 1;
  52283. +}
  52284. +/**
  52285. + * This funcion stalls EP0.
  52286. + */
  52287. +static inline void ep0_do_stall(dwc_otg_pcd_t * pcd, const int err_val)
  52288. +{
  52289. + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
  52290. + usb_device_request_t *ctrl = &pcd->setup_pkt->req;
  52291. + DWC_WARN("req %02x.%02x protocol STALL; err %d\n",
  52292. + ctrl->bmRequestType, ctrl->bRequest, err_val);
  52293. +
  52294. + ep0->dwc_ep.is_in = 1;
  52295. + dwc_otg_ep_set_stall(GET_CORE_IF(pcd), &ep0->dwc_ep);
  52296. + pcd->ep0.stopped = 1;
  52297. + pcd->ep0state = EP0_IDLE;
  52298. + ep0_out_start(GET_CORE_IF(pcd), pcd);
  52299. +}
  52300. +
  52301. +/**
  52302. + * This functions delegates the setup command to the gadget driver.
  52303. + */
  52304. +static inline void do_gadget_setup(dwc_otg_pcd_t * pcd,
  52305. + usb_device_request_t * ctrl)
  52306. +{
  52307. + int ret = 0;
  52308. + DWC_SPINUNLOCK(pcd->lock);
  52309. + ret = pcd->fops->setup(pcd, (uint8_t *) ctrl);
  52310. + DWC_SPINLOCK(pcd->lock);
  52311. + if (ret < 0) {
  52312. + ep0_do_stall(pcd, ret);
  52313. + }
  52314. +
  52315. + /** @todo This is a g_file_storage gadget driver specific
  52316. + * workaround: a DELAYED_STATUS result from the fsg_setup
  52317. + * routine will result in the gadget queueing a EP0 IN status
  52318. + * phase for a two-stage control transfer. Exactly the same as
  52319. + * a SET_CONFIGURATION/SET_INTERFACE except that this is a class
  52320. + * specific request. Need a generic way to know when the gadget
  52321. + * driver will queue the status phase. Can we assume when we
  52322. + * call the gadget driver setup() function that it will always
  52323. + * queue and require the following flag? Need to look into
  52324. + * this.
  52325. + */
  52326. +
  52327. + if (ret == 256 + 999) {
  52328. + pcd->request_config = 1;
  52329. + }
  52330. +}
  52331. +
  52332. +#ifdef DWC_UTE_CFI
  52333. +/**
  52334. + * This functions delegates the CFI setup commands to the gadget driver.
  52335. + * This function will return a negative value to indicate a failure.
  52336. + */
  52337. +static inline int cfi_gadget_setup(dwc_otg_pcd_t * pcd,
  52338. + struct cfi_usb_ctrlrequest *ctrl_req)
  52339. +{
  52340. + int ret = 0;
  52341. +
  52342. + if (pcd->fops && pcd->fops->cfi_setup) {
  52343. + DWC_SPINUNLOCK(pcd->lock);
  52344. + ret = pcd->fops->cfi_setup(pcd, ctrl_req);
  52345. + DWC_SPINLOCK(pcd->lock);
  52346. + if (ret < 0) {
  52347. + ep0_do_stall(pcd, ret);
  52348. + return ret;
  52349. + }
  52350. + }
  52351. +
  52352. + return ret;
  52353. +}
  52354. +#endif
  52355. +
  52356. +/**
  52357. + * This function starts the Zero-Length Packet for the IN status phase
  52358. + * of a 2 stage control transfer.
  52359. + */
  52360. +static inline void do_setup_in_status_phase(dwc_otg_pcd_t * pcd)
  52361. +{
  52362. + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
  52363. + if (pcd->ep0state == EP0_STALL) {
  52364. + return;
  52365. + }
  52366. +
  52367. + pcd->ep0state = EP0_IN_STATUS_PHASE;
  52368. +
  52369. + /* Prepare for more SETUP Packets */
  52370. + DWC_DEBUGPL(DBG_PCD, "EP0 IN ZLP\n");
  52371. + if ((GET_CORE_IF(pcd)->snpsid >= OTG_CORE_REV_3_00a)
  52372. + && (pcd->core_if->dma_desc_enable)
  52373. + && (ep0->dwc_ep.xfer_count < ep0->dwc_ep.total_len)) {
  52374. + DWC_DEBUGPL(DBG_PCDV,
  52375. + "Data terminated wait next packet in out_desc_addr\n");
  52376. + pcd->backup_buf = phys_to_virt(ep0->dwc_ep.dma_addr);
  52377. + pcd->data_terminated = 1;
  52378. + }
  52379. + ep0->dwc_ep.xfer_len = 0;
  52380. + ep0->dwc_ep.xfer_count = 0;
  52381. + ep0->dwc_ep.is_in = 1;
  52382. + ep0->dwc_ep.dma_addr = pcd->setup_pkt_dma_handle;
  52383. + dwc_otg_ep0_start_transfer(GET_CORE_IF(pcd), &ep0->dwc_ep);
  52384. +
  52385. + /* Prepare for more SETUP Packets */
  52386. + //ep0_out_start(GET_CORE_IF(pcd), pcd);
  52387. +}
  52388. +
  52389. +/**
  52390. + * This function starts the Zero-Length Packet for the OUT status phase
  52391. + * of a 2 stage control transfer.
  52392. + */
  52393. +static inline void do_setup_out_status_phase(dwc_otg_pcd_t * pcd)
  52394. +{
  52395. + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
  52396. + if (pcd->ep0state == EP0_STALL) {
  52397. + DWC_DEBUGPL(DBG_PCD, "EP0 STALLED\n");
  52398. + return;
  52399. + }
  52400. + pcd->ep0state = EP0_OUT_STATUS_PHASE;
  52401. +
  52402. + DWC_DEBUGPL(DBG_PCD, "EP0 OUT ZLP\n");
  52403. + ep0->dwc_ep.xfer_len = 0;
  52404. + ep0->dwc_ep.xfer_count = 0;
  52405. + ep0->dwc_ep.is_in = 0;
  52406. + ep0->dwc_ep.dma_addr = pcd->setup_pkt_dma_handle;
  52407. + dwc_otg_ep0_start_transfer(GET_CORE_IF(pcd), &ep0->dwc_ep);
  52408. +
  52409. + /* Prepare for more SETUP Packets */
  52410. + if (GET_CORE_IF(pcd)->dma_enable == 0) {
  52411. + ep0_out_start(GET_CORE_IF(pcd), pcd);
  52412. + }
  52413. +}
  52414. +
  52415. +/**
  52416. + * Clear the EP halt (STALL) and if pending requests start the
  52417. + * transfer.
  52418. + */
  52419. +static inline void pcd_clear_halt(dwc_otg_pcd_t * pcd, dwc_otg_pcd_ep_t * ep)
  52420. +{
  52421. + if (ep->dwc_ep.stall_clear_flag == 0)
  52422. + dwc_otg_ep_clear_stall(GET_CORE_IF(pcd), &ep->dwc_ep);
  52423. +
  52424. + /* Reactive the EP */
  52425. + dwc_otg_ep_activate(GET_CORE_IF(pcd), &ep->dwc_ep);
  52426. + if (ep->stopped) {
  52427. + ep->stopped = 0;
  52428. + /* If there is a request in the EP queue start it */
  52429. +
  52430. + /** @todo FIXME: this causes an EP mismatch in DMA mode.
  52431. + * epmismatch not yet implemented. */
  52432. +
  52433. + /*
  52434. + * Above fixme is solved by implmenting a tasklet to call the
  52435. + * start_next_request(), outside of interrupt context at some
  52436. + * time after the current time, after a clear-halt setup packet.
  52437. + * Still need to implement ep mismatch in the future if a gadget
  52438. + * ever uses more than one endpoint at once
  52439. + */
  52440. + ep->queue_sof = 1;
  52441. + DWC_TASK_SCHEDULE(pcd->start_xfer_tasklet);
  52442. + }
  52443. + /* Start Control Status Phase */
  52444. + do_setup_in_status_phase(pcd);
  52445. +}
  52446. +
  52447. +/**
  52448. + * This function is called when the SET_FEATURE TEST_MODE Setup packet
  52449. + * is sent from the host. The Device Control register is written with
  52450. + * the Test Mode bits set to the specified Test Mode. This is done as
  52451. + * a tasklet so that the "Status" phase of the control transfer
  52452. + * completes before transmitting the TEST packets.
  52453. + *
  52454. + * @todo This has not been tested since the tasklet struct was put
  52455. + * into the PCD struct!
  52456. + *
  52457. + */
  52458. +void do_test_mode(void *data)
  52459. +{
  52460. + dctl_data_t dctl;
  52461. + dwc_otg_pcd_t *pcd = (dwc_otg_pcd_t *) data;
  52462. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  52463. + int test_mode = pcd->test_mode;
  52464. +
  52465. +// DWC_WARN("%s() has not been tested since being rewritten!\n", __func__);
  52466. +
  52467. + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dctl);
  52468. + switch (test_mode) {
  52469. + case 1: // TEST_J
  52470. + dctl.b.tstctl = 1;
  52471. + break;
  52472. +
  52473. + case 2: // TEST_K
  52474. + dctl.b.tstctl = 2;
  52475. + break;
  52476. +
  52477. + case 3: // TEST_SE0_NAK
  52478. + dctl.b.tstctl = 3;
  52479. + break;
  52480. +
  52481. + case 4: // TEST_PACKET
  52482. + dctl.b.tstctl = 4;
  52483. + break;
  52484. +
  52485. + case 5: // TEST_FORCE_ENABLE
  52486. + dctl.b.tstctl = 5;
  52487. + break;
  52488. + }
  52489. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32);
  52490. +}
  52491. +
  52492. +/**
  52493. + * This function process the GET_STATUS Setup Commands.
  52494. + */
  52495. +static inline void do_get_status(dwc_otg_pcd_t * pcd)
  52496. +{
  52497. + usb_device_request_t ctrl = pcd->setup_pkt->req;
  52498. + dwc_otg_pcd_ep_t *ep;
  52499. + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
  52500. + uint16_t *status = pcd->status_buf;
  52501. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  52502. +
  52503. +#ifdef DEBUG_EP0
  52504. + DWC_DEBUGPL(DBG_PCD,
  52505. + "GET_STATUS %02x.%02x v%04x i%04x l%04x\n",
  52506. + ctrl.bmRequestType, ctrl.bRequest,
  52507. + UGETW(ctrl.wValue), UGETW(ctrl.wIndex),
  52508. + UGETW(ctrl.wLength));
  52509. +#endif
  52510. +
  52511. + switch (UT_GET_RECIPIENT(ctrl.bmRequestType)) {
  52512. + case UT_DEVICE:
  52513. + if(UGETW(ctrl.wIndex) == 0xF000) { /* OTG Status selector */
  52514. + DWC_PRINTF("wIndex - %d\n", UGETW(ctrl.wIndex));
  52515. + DWC_PRINTF("OTG VERSION - %d\n", core_if->otg_ver);
  52516. + DWC_PRINTF("OTG CAP - %d, %d\n",
  52517. + core_if->core_params->otg_cap,
  52518. + DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE);
  52519. + if (core_if->otg_ver == 1
  52520. + && core_if->core_params->otg_cap ==
  52521. + DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE) {
  52522. + uint8_t *otgsts = (uint8_t*)pcd->status_buf;
  52523. + *otgsts = (core_if->otg_sts & 0x1);
  52524. + pcd->ep0_pending = 1;
  52525. + ep0->dwc_ep.start_xfer_buff =
  52526. + (uint8_t *) otgsts;
  52527. + ep0->dwc_ep.xfer_buff = (uint8_t *) otgsts;
  52528. + ep0->dwc_ep.dma_addr =
  52529. + pcd->status_buf_dma_handle;
  52530. + ep0->dwc_ep.xfer_len = 1;
  52531. + ep0->dwc_ep.xfer_count = 0;
  52532. + ep0->dwc_ep.total_len = ep0->dwc_ep.xfer_len;
  52533. + dwc_otg_ep0_start_transfer(GET_CORE_IF(pcd),
  52534. + &ep0->dwc_ep);
  52535. + return;
  52536. + } else {
  52537. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  52538. + return;
  52539. + }
  52540. + break;
  52541. + } else {
  52542. + *status = 0x1; /* Self powered */
  52543. + *status |= pcd->remote_wakeup_enable << 1;
  52544. + break;
  52545. + }
  52546. + case UT_INTERFACE:
  52547. + *status = 0;
  52548. + break;
  52549. +
  52550. + case UT_ENDPOINT:
  52551. + ep = get_ep_by_addr(pcd, UGETW(ctrl.wIndex));
  52552. + if (ep == 0 || UGETW(ctrl.wLength) > 2) {
  52553. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  52554. + return;
  52555. + }
  52556. + /** @todo check for EP stall */
  52557. + *status = ep->stopped;
  52558. + break;
  52559. + }
  52560. + pcd->ep0_pending = 1;
  52561. + ep0->dwc_ep.start_xfer_buff = (uint8_t *) status;
  52562. + ep0->dwc_ep.xfer_buff = (uint8_t *) status;
  52563. + ep0->dwc_ep.dma_addr = pcd->status_buf_dma_handle;
  52564. + ep0->dwc_ep.xfer_len = 2;
  52565. + ep0->dwc_ep.xfer_count = 0;
  52566. + ep0->dwc_ep.total_len = ep0->dwc_ep.xfer_len;
  52567. + dwc_otg_ep0_start_transfer(GET_CORE_IF(pcd), &ep0->dwc_ep);
  52568. +}
  52569. +
  52570. +/**
  52571. + * This function process the SET_FEATURE Setup Commands.
  52572. + */
  52573. +static inline void do_set_feature(dwc_otg_pcd_t * pcd)
  52574. +{
  52575. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  52576. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  52577. + usb_device_request_t ctrl = pcd->setup_pkt->req;
  52578. + dwc_otg_pcd_ep_t *ep = 0;
  52579. + int32_t otg_cap_param = core_if->core_params->otg_cap;
  52580. + gotgctl_data_t gotgctl = {.d32 = 0 };
  52581. +
  52582. + DWC_DEBUGPL(DBG_PCD, "SET_FEATURE:%02x.%02x v%04x i%04x l%04x\n",
  52583. + ctrl.bmRequestType, ctrl.bRequest,
  52584. + UGETW(ctrl.wValue), UGETW(ctrl.wIndex),
  52585. + UGETW(ctrl.wLength));
  52586. + DWC_DEBUGPL(DBG_PCD, "otg_cap=%d\n", otg_cap_param);
  52587. +
  52588. + switch (UT_GET_RECIPIENT(ctrl.bmRequestType)) {
  52589. + case UT_DEVICE:
  52590. + switch (UGETW(ctrl.wValue)) {
  52591. + case UF_DEVICE_REMOTE_WAKEUP:
  52592. + pcd->remote_wakeup_enable = 1;
  52593. + break;
  52594. +
  52595. + case UF_TEST_MODE:
  52596. + /* Setup the Test Mode tasklet to do the Test
  52597. + * Packet generation after the SETUP Status
  52598. + * phase has completed. */
  52599. +
  52600. + /** @todo This has not been tested since the
  52601. + * tasklet struct was put into the PCD
  52602. + * struct! */
  52603. + pcd->test_mode = UGETW(ctrl.wIndex) >> 8;
  52604. + DWC_TASK_SCHEDULE(pcd->test_mode_tasklet);
  52605. + break;
  52606. +
  52607. + case UF_DEVICE_B_HNP_ENABLE:
  52608. + DWC_DEBUGPL(DBG_PCDV,
  52609. + "SET_FEATURE: USB_DEVICE_B_HNP_ENABLE\n");
  52610. +
  52611. + /* dev may initiate HNP */
  52612. + if (otg_cap_param == DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE) {
  52613. + pcd->b_hnp_enable = 1;
  52614. + dwc_otg_pcd_update_otg(pcd, 0);
  52615. + DWC_DEBUGPL(DBG_PCD, "Request B HNP\n");
  52616. + /**@todo Is the gotgctl.devhnpen cleared
  52617. + * by a USB Reset? */
  52618. + gotgctl.b.devhnpen = 1;
  52619. + gotgctl.b.hnpreq = 1;
  52620. + DWC_WRITE_REG32(&global_regs->gotgctl,
  52621. + gotgctl.d32);
  52622. + } else {
  52623. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  52624. + return;
  52625. + }
  52626. + break;
  52627. +
  52628. + case UF_DEVICE_A_HNP_SUPPORT:
  52629. + /* RH port supports HNP */
  52630. + DWC_DEBUGPL(DBG_PCDV,
  52631. + "SET_FEATURE: USB_DEVICE_A_HNP_SUPPORT\n");
  52632. + if (otg_cap_param == DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE) {
  52633. + pcd->a_hnp_support = 1;
  52634. + dwc_otg_pcd_update_otg(pcd, 0);
  52635. + } else {
  52636. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  52637. + return;
  52638. + }
  52639. + break;
  52640. +
  52641. + case UF_DEVICE_A_ALT_HNP_SUPPORT:
  52642. + /* other RH port does */
  52643. + DWC_DEBUGPL(DBG_PCDV,
  52644. + "SET_FEATURE: USB_DEVICE_A_ALT_HNP_SUPPORT\n");
  52645. + if (otg_cap_param == DWC_OTG_CAP_PARAM_HNP_SRP_CAPABLE) {
  52646. + pcd->a_alt_hnp_support = 1;
  52647. + dwc_otg_pcd_update_otg(pcd, 0);
  52648. + } else {
  52649. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  52650. + return;
  52651. + }
  52652. + break;
  52653. +
  52654. + default:
  52655. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  52656. + return;
  52657. +
  52658. + }
  52659. + do_setup_in_status_phase(pcd);
  52660. + break;
  52661. +
  52662. + case UT_INTERFACE:
  52663. + do_gadget_setup(pcd, &ctrl);
  52664. + break;
  52665. +
  52666. + case UT_ENDPOINT:
  52667. + if (UGETW(ctrl.wValue) == UF_ENDPOINT_HALT) {
  52668. + ep = get_ep_by_addr(pcd, UGETW(ctrl.wIndex));
  52669. + if (ep == 0) {
  52670. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  52671. + return;
  52672. + }
  52673. + ep->stopped = 1;
  52674. + dwc_otg_ep_set_stall(core_if, &ep->dwc_ep);
  52675. + }
  52676. + do_setup_in_status_phase(pcd);
  52677. + break;
  52678. + }
  52679. +}
  52680. +
  52681. +/**
  52682. + * This function process the CLEAR_FEATURE Setup Commands.
  52683. + */
  52684. +static inline void do_clear_feature(dwc_otg_pcd_t * pcd)
  52685. +{
  52686. + usb_device_request_t ctrl = pcd->setup_pkt->req;
  52687. + dwc_otg_pcd_ep_t *ep = 0;
  52688. +
  52689. + DWC_DEBUGPL(DBG_PCD,
  52690. + "CLEAR_FEATURE:%02x.%02x v%04x i%04x l%04x\n",
  52691. + ctrl.bmRequestType, ctrl.bRequest,
  52692. + UGETW(ctrl.wValue), UGETW(ctrl.wIndex),
  52693. + UGETW(ctrl.wLength));
  52694. +
  52695. + switch (UT_GET_RECIPIENT(ctrl.bmRequestType)) {
  52696. + case UT_DEVICE:
  52697. + switch (UGETW(ctrl.wValue)) {
  52698. + case UF_DEVICE_REMOTE_WAKEUP:
  52699. + pcd->remote_wakeup_enable = 0;
  52700. + break;
  52701. +
  52702. + case UF_TEST_MODE:
  52703. + /** @todo Add CLEAR_FEATURE for TEST modes. */
  52704. + break;
  52705. +
  52706. + default:
  52707. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  52708. + return;
  52709. + }
  52710. + do_setup_in_status_phase(pcd);
  52711. + break;
  52712. +
  52713. + case UT_ENDPOINT:
  52714. + ep = get_ep_by_addr(pcd, UGETW(ctrl.wIndex));
  52715. + if (ep == 0) {
  52716. + ep0_do_stall(pcd, -DWC_E_NOT_SUPPORTED);
  52717. + return;
  52718. + }
  52719. +
  52720. + pcd_clear_halt(pcd, ep);
  52721. +
  52722. + break;
  52723. + }
  52724. +}
  52725. +
  52726. +/**
  52727. + * This function process the SET_ADDRESS Setup Commands.
  52728. + */
  52729. +static inline void do_set_address(dwc_otg_pcd_t * pcd)
  52730. +{
  52731. + dwc_otg_dev_if_t *dev_if = GET_CORE_IF(pcd)->dev_if;
  52732. + usb_device_request_t ctrl = pcd->setup_pkt->req;
  52733. +
  52734. + if (ctrl.bmRequestType == UT_DEVICE) {
  52735. + dcfg_data_t dcfg = {.d32 = 0 };
  52736. +
  52737. +#ifdef DEBUG_EP0
  52738. +// DWC_DEBUGPL(DBG_PCDV, "SET_ADDRESS:%d\n", ctrl.wValue);
  52739. +#endif
  52740. + dcfg.b.devaddr = UGETW(ctrl.wValue);
  52741. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dcfg, 0, dcfg.d32);
  52742. + do_setup_in_status_phase(pcd);
  52743. + }
  52744. +}
  52745. +
  52746. +/**
  52747. + * This function processes SETUP commands. In Linux, the USB Command
  52748. + * processing is done in two places - the first being the PCD and the
  52749. + * second in the Gadget Driver (for example, the File-Backed Storage
  52750. + * Gadget Driver).
  52751. + *
  52752. + * <table>
  52753. + * <tr><td>Command </td><td>Driver </td><td>Description</td></tr>
  52754. + *
  52755. + * <tr><td>GET_STATUS </td><td>PCD </td><td>Command is processed as
  52756. + * defined in chapter 9 of the USB 2.0 Specification chapter 9
  52757. + * </td></tr>
  52758. + *
  52759. + * <tr><td>CLEAR_FEATURE </td><td>PCD </td><td>The Device and Endpoint
  52760. + * requests are the ENDPOINT_HALT feature is procesed, all others the
  52761. + * interface requests are ignored.</td></tr>
  52762. + *
  52763. + * <tr><td>SET_FEATURE </td><td>PCD </td><td>The Device and Endpoint
  52764. + * requests are processed by the PCD. Interface requests are passed
  52765. + * to the Gadget Driver.</td></tr>
  52766. + *
  52767. + * <tr><td>SET_ADDRESS </td><td>PCD </td><td>Program the DCFG reg,
  52768. + * with device address received </td></tr>
  52769. + *
  52770. + * <tr><td>GET_DESCRIPTOR </td><td>Gadget Driver </td><td>Return the
  52771. + * requested descriptor</td></tr>
  52772. + *
  52773. + * <tr><td>SET_DESCRIPTOR </td><td>Gadget Driver </td><td>Optional -
  52774. + * not implemented by any of the existing Gadget Drivers.</td></tr>
  52775. + *
  52776. + * <tr><td>SET_CONFIGURATION </td><td>Gadget Driver </td><td>Disable
  52777. + * all EPs and enable EPs for new configuration.</td></tr>
  52778. + *
  52779. + * <tr><td>GET_CONFIGURATION </td><td>Gadget Driver </td><td>Return
  52780. + * the current configuration</td></tr>
  52781. + *
  52782. + * <tr><td>SET_INTERFACE </td><td>Gadget Driver </td><td>Disable all
  52783. + * EPs and enable EPs for new configuration.</td></tr>
  52784. + *
  52785. + * <tr><td>GET_INTERFACE </td><td>Gadget Driver </td><td>Return the
  52786. + * current interface.</td></tr>
  52787. + *
  52788. + * <tr><td>SYNC_FRAME </td><td>PCD </td><td>Display debug
  52789. + * message.</td></tr>
  52790. + * </table>
  52791. + *
  52792. + * When the SETUP Phase Done interrupt occurs, the PCD SETUP commands are
  52793. + * processed by pcd_setup. Calling the Function Driver's setup function from
  52794. + * pcd_setup processes the gadget SETUP commands.
  52795. + */
  52796. +static inline void pcd_setup(dwc_otg_pcd_t * pcd)
  52797. +{
  52798. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  52799. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  52800. + usb_device_request_t ctrl = pcd->setup_pkt->req;
  52801. + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
  52802. +
  52803. + deptsiz0_data_t doeptsize0 = {.d32 = 0 };
  52804. +
  52805. +#ifdef DWC_UTE_CFI
  52806. + int retval = 0;
  52807. + struct cfi_usb_ctrlrequest cfi_req;
  52808. +#endif
  52809. +
  52810. + doeptsize0.d32 = DWC_READ_REG32(&dev_if->out_ep_regs[0]->doeptsiz);
  52811. +
  52812. + /** In BDMA more then 1 setup packet is not supported till 3.00a */
  52813. + if (core_if->dma_enable && core_if->dma_desc_enable == 0
  52814. + && (doeptsize0.b.supcnt < 2)
  52815. + && (core_if->snpsid < OTG_CORE_REV_2_94a)) {
  52816. + DWC_ERROR
  52817. + ("\n\n----------- CANNOT handle > 1 setup packet in DMA mode\n\n");
  52818. + }
  52819. + if ((core_if->snpsid >= OTG_CORE_REV_3_00a)
  52820. + && (core_if->dma_enable == 1) && (core_if->dma_desc_enable == 0)) {
  52821. + ctrl =
  52822. + (pcd->setup_pkt +
  52823. + (3 - doeptsize0.b.supcnt - 1 +
  52824. + ep0->dwc_ep.stp_rollover))->req;
  52825. + }
  52826. +#ifdef DEBUG_EP0
  52827. + DWC_DEBUGPL(DBG_PCD, "SETUP %02x.%02x v%04x i%04x l%04x\n",
  52828. + ctrl.bmRequestType, ctrl.bRequest,
  52829. + UGETW(ctrl.wValue), UGETW(ctrl.wIndex),
  52830. + UGETW(ctrl.wLength));
  52831. +#endif
  52832. +
  52833. + /* Clean up the request queue */
  52834. + dwc_otg_request_nuke(ep0);
  52835. + ep0->stopped = 0;
  52836. +
  52837. + if (ctrl.bmRequestType & UE_DIR_IN) {
  52838. + ep0->dwc_ep.is_in = 1;
  52839. + pcd->ep0state = EP0_IN_DATA_PHASE;
  52840. + } else {
  52841. + ep0->dwc_ep.is_in = 0;
  52842. + pcd->ep0state = EP0_OUT_DATA_PHASE;
  52843. + }
  52844. +
  52845. + if (UGETW(ctrl.wLength) == 0) {
  52846. + ep0->dwc_ep.is_in = 1;
  52847. + pcd->ep0state = EP0_IN_STATUS_PHASE;
  52848. + }
  52849. +
  52850. + if (UT_GET_TYPE(ctrl.bmRequestType) != UT_STANDARD) {
  52851. +
  52852. +#ifdef DWC_UTE_CFI
  52853. + DWC_MEMCPY(&cfi_req, &ctrl, sizeof(usb_device_request_t));
  52854. +
  52855. + //printk(KERN_ALERT "CFI: req_type=0x%02x; req=0x%02x\n",
  52856. + ctrl.bRequestType, ctrl.bRequest);
  52857. + if (UT_GET_TYPE(cfi_req.bRequestType) == UT_VENDOR) {
  52858. + if (cfi_req.bRequest > 0xB0 && cfi_req.bRequest < 0xBF) {
  52859. + retval = cfi_setup(pcd, &cfi_req);
  52860. + if (retval < 0) {
  52861. + ep0_do_stall(pcd, retval);
  52862. + pcd->ep0_pending = 0;
  52863. + return;
  52864. + }
  52865. +
  52866. + /* if need gadget setup then call it and check the retval */
  52867. + if (pcd->cfi->need_gadget_att) {
  52868. + retval =
  52869. + cfi_gadget_setup(pcd,
  52870. + &pcd->
  52871. + cfi->ctrl_req);
  52872. + if (retval < 0) {
  52873. + pcd->ep0_pending = 0;
  52874. + return;
  52875. + }
  52876. + }
  52877. +
  52878. + if (pcd->cfi->need_status_in_complete) {
  52879. + do_setup_in_status_phase(pcd);
  52880. + }
  52881. + return;
  52882. + }
  52883. + }
  52884. +#endif
  52885. +
  52886. + /* handle non-standard (class/vendor) requests in the gadget driver */
  52887. + do_gadget_setup(pcd, &ctrl);
  52888. + return;
  52889. + }
  52890. +
  52891. + /** @todo NGS: Handle bad setup packet? */
  52892. +
  52893. +///////////////////////////////////////////
  52894. +//// --- Standard Request handling --- ////
  52895. +
  52896. + switch (ctrl.bRequest) {
  52897. + case UR_GET_STATUS:
  52898. + do_get_status(pcd);
  52899. + break;
  52900. +
  52901. + case UR_CLEAR_FEATURE:
  52902. + do_clear_feature(pcd);
  52903. + break;
  52904. +
  52905. + case UR_SET_FEATURE:
  52906. + do_set_feature(pcd);
  52907. + break;
  52908. +
  52909. + case UR_SET_ADDRESS:
  52910. + do_set_address(pcd);
  52911. + break;
  52912. +
  52913. + case UR_SET_INTERFACE:
  52914. + case UR_SET_CONFIG:
  52915. +// _pcd->request_config = 1; /* Configuration changed */
  52916. + do_gadget_setup(pcd, &ctrl);
  52917. + break;
  52918. +
  52919. + case UR_SYNCH_FRAME:
  52920. + do_gadget_setup(pcd, &ctrl);
  52921. + break;
  52922. +
  52923. + default:
  52924. + /* Call the Gadget Driver's setup functions */
  52925. + do_gadget_setup(pcd, &ctrl);
  52926. + break;
  52927. + }
  52928. +}
  52929. +
  52930. +/**
  52931. + * This function completes the ep0 control transfer.
  52932. + */
  52933. +static int32_t ep0_complete_request(dwc_otg_pcd_ep_t * ep)
  52934. +{
  52935. + dwc_otg_core_if_t *core_if = GET_CORE_IF(ep->pcd);
  52936. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  52937. + dwc_otg_dev_in_ep_regs_t *in_ep_regs =
  52938. + dev_if->in_ep_regs[ep->dwc_ep.num];
  52939. +#ifdef DEBUG_EP0
  52940. + dwc_otg_dev_out_ep_regs_t *out_ep_regs =
  52941. + dev_if->out_ep_regs[ep->dwc_ep.num];
  52942. +#endif
  52943. + deptsiz0_data_t deptsiz;
  52944. + dev_dma_desc_sts_t desc_sts;
  52945. + dwc_otg_pcd_request_t *req;
  52946. + int is_last = 0;
  52947. + dwc_otg_pcd_t *pcd = ep->pcd;
  52948. +
  52949. +#ifdef DWC_UTE_CFI
  52950. + struct cfi_usb_ctrlrequest *ctrlreq;
  52951. + int retval = -DWC_E_NOT_SUPPORTED;
  52952. +#endif
  52953. +
  52954. + desc_sts.b.bytes = 0;
  52955. +
  52956. + if (pcd->ep0_pending && DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  52957. + if (ep->dwc_ep.is_in) {
  52958. +#ifdef DEBUG_EP0
  52959. + DWC_DEBUGPL(DBG_PCDV, "Do setup OUT status phase\n");
  52960. +#endif
  52961. + do_setup_out_status_phase(pcd);
  52962. + } else {
  52963. +#ifdef DEBUG_EP0
  52964. + DWC_DEBUGPL(DBG_PCDV, "Do setup IN status phase\n");
  52965. +#endif
  52966. +
  52967. +#ifdef DWC_UTE_CFI
  52968. + ctrlreq = &pcd->cfi->ctrl_req;
  52969. +
  52970. + if (UT_GET_TYPE(ctrlreq->bRequestType) == UT_VENDOR) {
  52971. + if (ctrlreq->bRequest > 0xB0
  52972. + && ctrlreq->bRequest < 0xBF) {
  52973. +
  52974. + /* Return if the PCD failed to handle the request */
  52975. + if ((retval =
  52976. + pcd->cfi->ops.
  52977. + ctrl_write_complete(pcd->cfi,
  52978. + pcd)) < 0) {
  52979. + CFI_INFO
  52980. + ("ERROR setting a new value in the PCD(%d)\n",
  52981. + retval);
  52982. + ep0_do_stall(pcd, retval);
  52983. + pcd->ep0_pending = 0;
  52984. + return 0;
  52985. + }
  52986. +
  52987. + /* If the gadget needs to be notified on the request */
  52988. + if (pcd->cfi->need_gadget_att == 1) {
  52989. + //retval = do_gadget_setup(pcd, &pcd->cfi->ctrl_req);
  52990. + retval =
  52991. + cfi_gadget_setup(pcd,
  52992. + &pcd->cfi->
  52993. + ctrl_req);
  52994. +
  52995. + /* Return from the function if the gadget failed to process
  52996. + * the request properly - this should never happen !!!
  52997. + */
  52998. + if (retval < 0) {
  52999. + CFI_INFO
  53000. + ("ERROR setting a new value in the gadget(%d)\n",
  53001. + retval);
  53002. + pcd->ep0_pending = 0;
  53003. + return 0;
  53004. + }
  53005. + }
  53006. +
  53007. + CFI_INFO("%s: RETVAL=%d\n", __func__,
  53008. + retval);
  53009. + /* If we hit here then the PCD and the gadget has properly
  53010. + * handled the request - so send the ZLP IN to the host.
  53011. + */
  53012. + /* @todo: MAS - decide whether we need to start the setup
  53013. + * stage based on the need_setup value of the cfi object
  53014. + */
  53015. + do_setup_in_status_phase(pcd);
  53016. + pcd->ep0_pending = 0;
  53017. + return 1;
  53018. + }
  53019. + }
  53020. +#endif
  53021. +
  53022. + do_setup_in_status_phase(pcd);
  53023. + }
  53024. + pcd->ep0_pending = 0;
  53025. + return 1;
  53026. + }
  53027. +
  53028. + if (DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  53029. + return 0;
  53030. + }
  53031. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  53032. +
  53033. + if (pcd->ep0state == EP0_OUT_STATUS_PHASE
  53034. + || pcd->ep0state == EP0_IN_STATUS_PHASE) {
  53035. + is_last = 1;
  53036. + } else if (ep->dwc_ep.is_in) {
  53037. + deptsiz.d32 = DWC_READ_REG32(&in_ep_regs->dieptsiz);
  53038. + if (core_if->dma_desc_enable != 0)
  53039. + desc_sts = dev_if->in_desc_addr->status;
  53040. +#ifdef DEBUG_EP0
  53041. + DWC_DEBUGPL(DBG_PCDV, "%d len=%d xfersize=%d pktcnt=%d\n",
  53042. + ep->dwc_ep.num, ep->dwc_ep.xfer_len,
  53043. + deptsiz.b.xfersize, deptsiz.b.pktcnt);
  53044. +#endif
  53045. +
  53046. + if (((core_if->dma_desc_enable == 0)
  53047. + && (deptsiz.b.xfersize == 0))
  53048. + || ((core_if->dma_desc_enable != 0)
  53049. + && (desc_sts.b.bytes == 0))) {
  53050. + req->actual = ep->dwc_ep.xfer_count;
  53051. + /* Is a Zero Len Packet needed? */
  53052. + if (req->sent_zlp) {
  53053. +#ifdef DEBUG_EP0
  53054. + DWC_DEBUGPL(DBG_PCD, "Setup Rx ZLP\n");
  53055. +#endif
  53056. + req->sent_zlp = 0;
  53057. + }
  53058. + do_setup_out_status_phase(pcd);
  53059. + }
  53060. + } else {
  53061. + /* ep0-OUT */
  53062. +#ifdef DEBUG_EP0
  53063. + deptsiz.d32 = DWC_READ_REG32(&out_ep_regs->doeptsiz);
  53064. + DWC_DEBUGPL(DBG_PCDV, "%d len=%d xsize=%d pktcnt=%d\n",
  53065. + ep->dwc_ep.num, ep->dwc_ep.xfer_len,
  53066. + deptsiz.b.xfersize, deptsiz.b.pktcnt);
  53067. +#endif
  53068. + req->actual = ep->dwc_ep.xfer_count;
  53069. +
  53070. + /* Is a Zero Len Packet needed? */
  53071. + if (req->sent_zlp) {
  53072. +#ifdef DEBUG_EP0
  53073. + DWC_DEBUGPL(DBG_PCDV, "Setup Tx ZLP\n");
  53074. +#endif
  53075. + req->sent_zlp = 0;
  53076. + }
  53077. + /* For older cores do setup in status phase in Slave/BDMA modes,
  53078. + * starting from 3.00 do that only in slave, and for DMA modes
  53079. + * just re-enable ep 0 OUT here*/
  53080. + if (core_if->dma_enable == 0
  53081. + || (core_if->dma_desc_enable == 0
  53082. + && core_if->snpsid <= OTG_CORE_REV_2_94a)) {
  53083. + do_setup_in_status_phase(pcd);
  53084. + } else if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
  53085. + DWC_DEBUGPL(DBG_PCDV,
  53086. + "Enable out ep before in status phase\n");
  53087. + ep0_out_start(core_if, pcd);
  53088. + }
  53089. + }
  53090. +
  53091. + /* Complete the request */
  53092. + if (is_last) {
  53093. + dwc_otg_request_done(ep, req, 0);
  53094. + ep->dwc_ep.start_xfer_buff = 0;
  53095. + ep->dwc_ep.xfer_buff = 0;
  53096. + ep->dwc_ep.xfer_len = 0;
  53097. + return 1;
  53098. + }
  53099. + return 0;
  53100. +}
  53101. +
  53102. +#ifdef DWC_UTE_CFI
  53103. +/**
  53104. + * This function calculates traverses all the CFI DMA descriptors and
  53105. + * and accumulates the bytes that are left to be transfered.
  53106. + *
  53107. + * @return The total bytes left to transfered, or a negative value as failure
  53108. + */
  53109. +static inline int cfi_calc_desc_residue(dwc_otg_pcd_ep_t * ep)
  53110. +{
  53111. + int32_t ret = 0;
  53112. + int i;
  53113. + struct dwc_otg_dma_desc *ddesc = NULL;
  53114. + struct cfi_ep *cfiep;
  53115. +
  53116. + /* See if the pcd_ep has its respective cfi_ep mapped */
  53117. + cfiep = get_cfi_ep_by_pcd_ep(ep->pcd->cfi, ep);
  53118. + if (!cfiep) {
  53119. + CFI_INFO("%s: Failed to find ep\n", __func__);
  53120. + return -1;
  53121. + }
  53122. +
  53123. + ddesc = ep->dwc_ep.descs;
  53124. +
  53125. + for (i = 0; (i < cfiep->desc_count) && (i < MAX_DMA_DESCS_PER_EP); i++) {
  53126. +
  53127. +#if defined(PRINT_CFI_DMA_DESCS)
  53128. + print_desc(ddesc, ep->ep.name, i);
  53129. +#endif
  53130. + ret += ddesc->status.b.bytes;
  53131. + ddesc++;
  53132. + }
  53133. +
  53134. + if (ret)
  53135. + CFI_INFO("!!!!!!!!!! WARNING (%s) - residue=%d\n", __func__,
  53136. + ret);
  53137. +
  53138. + return ret;
  53139. +}
  53140. +#endif
  53141. +
  53142. +/**
  53143. + * This function completes the request for the EP. If there are
  53144. + * additional requests for the EP in the queue they will be started.
  53145. + */
  53146. +static void complete_ep(dwc_otg_pcd_ep_t * ep)
  53147. +{
  53148. + dwc_otg_core_if_t *core_if = GET_CORE_IF(ep->pcd);
  53149. + struct device *dev = dwc_otg_pcd_to_dev(ep->pcd);
  53150. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  53151. + dwc_otg_dev_in_ep_regs_t *in_ep_regs =
  53152. + dev_if->in_ep_regs[ep->dwc_ep.num];
  53153. + deptsiz_data_t deptsiz;
  53154. + dev_dma_desc_sts_t desc_sts;
  53155. + dwc_otg_pcd_request_t *req = 0;
  53156. + dwc_otg_dev_dma_desc_t *dma_desc;
  53157. + uint32_t byte_count = 0;
  53158. + int is_last = 0;
  53159. + int i;
  53160. +
  53161. + DWC_DEBUGPL(DBG_PCDV, "%s() %d-%s\n", __func__, ep->dwc_ep.num,
  53162. + (ep->dwc_ep.is_in ? "IN" : "OUT"));
  53163. +
  53164. + /* Get any pending requests */
  53165. + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  53166. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  53167. + if (!req) {
  53168. + DWC_PRINTF("complete_ep 0x%p, req = NULL!\n", ep);
  53169. + return;
  53170. + }
  53171. + } else {
  53172. + DWC_PRINTF("complete_ep 0x%p, ep->queue empty!\n", ep);
  53173. + return;
  53174. + }
  53175. +
  53176. + DWC_DEBUGPL(DBG_PCD, "Requests %d\n", ep->pcd->request_pending);
  53177. +
  53178. + if (ep->dwc_ep.is_in) {
  53179. + deptsiz.d32 = DWC_READ_REG32(&in_ep_regs->dieptsiz);
  53180. +
  53181. + if (core_if->dma_enable) {
  53182. + if (core_if->dma_desc_enable == 0) {
  53183. + if (deptsiz.b.xfersize == 0
  53184. + && deptsiz.b.pktcnt == 0) {
  53185. + byte_count =
  53186. + ep->dwc_ep.xfer_len -
  53187. + ep->dwc_ep.xfer_count;
  53188. +
  53189. + ep->dwc_ep.xfer_buff += byte_count;
  53190. + ep->dwc_ep.dma_addr += byte_count;
  53191. + ep->dwc_ep.xfer_count += byte_count;
  53192. +
  53193. + DWC_DEBUGPL(DBG_PCDV,
  53194. + "%d-%s len=%d xfersize=%d pktcnt=%d\n",
  53195. + ep->dwc_ep.num,
  53196. + (ep->dwc_ep.
  53197. + is_in ? "IN" : "OUT"),
  53198. + ep->dwc_ep.xfer_len,
  53199. + deptsiz.b.xfersize,
  53200. + deptsiz.b.pktcnt);
  53201. +
  53202. + if (ep->dwc_ep.xfer_len <
  53203. + ep->dwc_ep.total_len) {
  53204. + dwc_otg_ep_start_transfer
  53205. + (core_if, &ep->dwc_ep);
  53206. + } else if (ep->dwc_ep.sent_zlp) {
  53207. + /*
  53208. + * This fragment of code should initiate 0
  53209. + * length transfer in case if it is queued
  53210. + * a transfer with size divisible to EPs max
  53211. + * packet size and with usb_request zero field
  53212. + * is set, which means that after data is transfered,
  53213. + * it is also should be transfered
  53214. + * a 0 length packet at the end. For Slave and
  53215. + * Buffer DMA modes in this case SW has
  53216. + * to initiate 2 transfers one with transfer size,
  53217. + * and the second with 0 size. For Descriptor
  53218. + * DMA mode SW is able to initiate a transfer,
  53219. + * which will handle all the packets including
  53220. + * the last 0 length.
  53221. + */
  53222. + ep->dwc_ep.sent_zlp = 0;
  53223. + dwc_otg_ep_start_zl_transfer
  53224. + (core_if, &ep->dwc_ep);
  53225. + } else {
  53226. + is_last = 1;
  53227. + }
  53228. + } else {
  53229. + if (ep->dwc_ep.type ==
  53230. + DWC_OTG_EP_TYPE_ISOC) {
  53231. + req->actual = 0;
  53232. + dwc_otg_request_done(ep, req, 0);
  53233. +
  53234. + ep->dwc_ep.start_xfer_buff = 0;
  53235. + ep->dwc_ep.xfer_buff = 0;
  53236. + ep->dwc_ep.xfer_len = 0;
  53237. +
  53238. + /* If there is a request in the queue start it. */
  53239. + start_next_request(ep);
  53240. + } else
  53241. + DWC_WARN
  53242. + ("Incomplete transfer (%d - %s [siz=%d pkt=%d])\n",
  53243. + ep->dwc_ep.num,
  53244. + (ep->dwc_ep.is_in ? "IN" : "OUT"),
  53245. + deptsiz.b.xfersize,
  53246. + deptsiz.b.pktcnt);
  53247. + }
  53248. + } else {
  53249. + dma_desc = ep->dwc_ep.desc_addr;
  53250. + byte_count = 0;
  53251. + ep->dwc_ep.sent_zlp = 0;
  53252. +
  53253. +#ifdef DWC_UTE_CFI
  53254. + CFI_INFO("%s: BUFFER_MODE=%d\n", __func__,
  53255. + ep->dwc_ep.buff_mode);
  53256. + if (ep->dwc_ep.buff_mode != BM_STANDARD) {
  53257. + int residue;
  53258. +
  53259. + residue = cfi_calc_desc_residue(ep);
  53260. + if (residue < 0)
  53261. + return;
  53262. +
  53263. + byte_count = residue;
  53264. + } else {
  53265. +#endif
  53266. + for (i = 0; i < ep->dwc_ep.desc_cnt;
  53267. + ++i) {
  53268. + desc_sts = dma_desc->status;
  53269. + byte_count += desc_sts.b.bytes;
  53270. + dma_desc++;
  53271. + }
  53272. +#ifdef DWC_UTE_CFI
  53273. + }
  53274. +#endif
  53275. + if (byte_count == 0) {
  53276. + ep->dwc_ep.xfer_count =
  53277. + ep->dwc_ep.total_len;
  53278. + is_last = 1;
  53279. + } else {
  53280. + DWC_WARN("Incomplete transfer\n");
  53281. + }
  53282. + }
  53283. + } else {
  53284. + if (deptsiz.b.xfersize == 0 && deptsiz.b.pktcnt == 0) {
  53285. + DWC_DEBUGPL(DBG_PCDV,
  53286. + "%d-%s len=%d xfersize=%d pktcnt=%d\n",
  53287. + ep->dwc_ep.num,
  53288. + ep->dwc_ep.is_in ? "IN" : "OUT",
  53289. + ep->dwc_ep.xfer_len,
  53290. + deptsiz.b.xfersize,
  53291. + deptsiz.b.pktcnt);
  53292. +
  53293. + /* Check if the whole transfer was completed,
  53294. + * if no, setup transfer for next portion of data
  53295. + */
  53296. + if (ep->dwc_ep.xfer_len < ep->dwc_ep.total_len) {
  53297. + dwc_otg_ep_start_transfer(core_if,
  53298. + &ep->dwc_ep);
  53299. + } else if (ep->dwc_ep.sent_zlp) {
  53300. + /*
  53301. + * This fragment of code should initiate 0
  53302. + * length trasfer in case if it is queued
  53303. + * a trasfer with size divisible to EPs max
  53304. + * packet size and with usb_request zero field
  53305. + * is set, which means that after data is transfered,
  53306. + * it is also should be transfered
  53307. + * a 0 length packet at the end. For Slave and
  53308. + * Buffer DMA modes in this case SW has
  53309. + * to initiate 2 transfers one with transfer size,
  53310. + * and the second with 0 size. For Desriptor
  53311. + * DMA mode SW is able to initiate a transfer,
  53312. + * which will handle all the packets including
  53313. + * the last 0 legth.
  53314. + */
  53315. + ep->dwc_ep.sent_zlp = 0;
  53316. + dwc_otg_ep_start_zl_transfer(core_if,
  53317. + &ep->dwc_ep);
  53318. + } else {
  53319. + is_last = 1;
  53320. + }
  53321. + } else {
  53322. + DWC_WARN
  53323. + ("Incomplete transfer (%d-%s [siz=%d pkt=%d])\n",
  53324. + ep->dwc_ep.num,
  53325. + (ep->dwc_ep.is_in ? "IN" : "OUT"),
  53326. + deptsiz.b.xfersize, deptsiz.b.pktcnt);
  53327. + }
  53328. + }
  53329. + } else {
  53330. + dwc_otg_dev_out_ep_regs_t *out_ep_regs =
  53331. + dev_if->out_ep_regs[ep->dwc_ep.num];
  53332. + desc_sts.d32 = 0;
  53333. + if (core_if->dma_enable) {
  53334. + if (core_if->dma_desc_enable) {
  53335. + dma_desc = ep->dwc_ep.desc_addr;
  53336. + byte_count = 0;
  53337. + ep->dwc_ep.sent_zlp = 0;
  53338. +
  53339. +#ifdef DWC_UTE_CFI
  53340. + CFI_INFO("%s: BUFFER_MODE=%d\n", __func__,
  53341. + ep->dwc_ep.buff_mode);
  53342. + if (ep->dwc_ep.buff_mode != BM_STANDARD) {
  53343. + int residue;
  53344. + residue = cfi_calc_desc_residue(ep);
  53345. + if (residue < 0)
  53346. + return;
  53347. + byte_count = residue;
  53348. + } else {
  53349. +#endif
  53350. +
  53351. + for (i = 0; i < ep->dwc_ep.desc_cnt;
  53352. + ++i) {
  53353. + desc_sts = dma_desc->status;
  53354. + byte_count += desc_sts.b.bytes;
  53355. + dma_desc++;
  53356. + }
  53357. +
  53358. +#ifdef DWC_UTE_CFI
  53359. + }
  53360. +#endif
  53361. + /* Checking for interrupt Out transfers with not
  53362. + * dword aligned mps sizes
  53363. + */
  53364. + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_INTR &&
  53365. + (ep->dwc_ep.maxpacket%4)) {
  53366. + ep->dwc_ep.xfer_count =
  53367. + ep->dwc_ep.total_len - byte_count;
  53368. + if ((ep->dwc_ep.xfer_len %
  53369. + ep->dwc_ep.maxpacket)
  53370. + && (ep->dwc_ep.xfer_len /
  53371. + ep->dwc_ep.maxpacket <
  53372. + MAX_DMA_DESC_CNT))
  53373. + ep->dwc_ep.xfer_len -=
  53374. + (ep->dwc_ep.desc_cnt -
  53375. + 1) * ep->dwc_ep.maxpacket +
  53376. + ep->dwc_ep.xfer_len %
  53377. + ep->dwc_ep.maxpacket;
  53378. + else
  53379. + ep->dwc_ep.xfer_len -=
  53380. + ep->dwc_ep.desc_cnt *
  53381. + ep->dwc_ep.maxpacket;
  53382. + if (ep->dwc_ep.xfer_len > 0) {
  53383. + dwc_otg_ep_start_transfer
  53384. + (core_if, &ep->dwc_ep);
  53385. + } else {
  53386. + is_last = 1;
  53387. + }
  53388. + } else {
  53389. + ep->dwc_ep.xfer_count =
  53390. + ep->dwc_ep.total_len - byte_count +
  53391. + ((4 -
  53392. + (ep->dwc_ep.
  53393. + total_len & 0x3)) & 0x3);
  53394. + is_last = 1;
  53395. + }
  53396. + } else {
  53397. + deptsiz.d32 = 0;
  53398. + deptsiz.d32 =
  53399. + DWC_READ_REG32(&out_ep_regs->doeptsiz);
  53400. +
  53401. + byte_count = (ep->dwc_ep.xfer_len -
  53402. + ep->dwc_ep.xfer_count -
  53403. + deptsiz.b.xfersize);
  53404. + ep->dwc_ep.xfer_buff += byte_count;
  53405. + ep->dwc_ep.dma_addr += byte_count;
  53406. + ep->dwc_ep.xfer_count += byte_count;
  53407. +
  53408. + /* Check if the whole transfer was completed,
  53409. + * if no, setup transfer for next portion of data
  53410. + */
  53411. + if (ep->dwc_ep.xfer_len < ep->dwc_ep.total_len) {
  53412. + dwc_otg_ep_start_transfer(core_if,
  53413. + &ep->dwc_ep);
  53414. + } else if (ep->dwc_ep.sent_zlp) {
  53415. + /*
  53416. + * This fragment of code should initiate 0
  53417. + * length trasfer in case if it is queued
  53418. + * a trasfer with size divisible to EPs max
  53419. + * packet size and with usb_request zero field
  53420. + * is set, which means that after data is transfered,
  53421. + * it is also should be transfered
  53422. + * a 0 length packet at the end. For Slave and
  53423. + * Buffer DMA modes in this case SW has
  53424. + * to initiate 2 transfers one with transfer size,
  53425. + * and the second with 0 size. For Desriptor
  53426. + * DMA mode SW is able to initiate a transfer,
  53427. + * which will handle all the packets including
  53428. + * the last 0 legth.
  53429. + */
  53430. + ep->dwc_ep.sent_zlp = 0;
  53431. + dwc_otg_ep_start_zl_transfer(core_if,
  53432. + &ep->dwc_ep);
  53433. + } else {
  53434. + is_last = 1;
  53435. + }
  53436. + }
  53437. + } else {
  53438. + /* Check if the whole transfer was completed,
  53439. + * if no, setup transfer for next portion of data
  53440. + */
  53441. + if (ep->dwc_ep.xfer_len < ep->dwc_ep.total_len) {
  53442. + dwc_otg_ep_start_transfer(core_if, &ep->dwc_ep);
  53443. + } else if (ep->dwc_ep.sent_zlp) {
  53444. + /*
  53445. + * This fragment of code should initiate 0
  53446. + * length transfer in case if it is queued
  53447. + * a transfer with size divisible to EPs max
  53448. + * packet size and with usb_request zero field
  53449. + * is set, which means that after data is transfered,
  53450. + * it is also should be transfered
  53451. + * a 0 length packet at the end. For Slave and
  53452. + * Buffer DMA modes in this case SW has
  53453. + * to initiate 2 transfers one with transfer size,
  53454. + * and the second with 0 size. For Descriptor
  53455. + * DMA mode SW is able to initiate a transfer,
  53456. + * which will handle all the packets including
  53457. + * the last 0 length.
  53458. + */
  53459. + ep->dwc_ep.sent_zlp = 0;
  53460. + dwc_otg_ep_start_zl_transfer(core_if,
  53461. + &ep->dwc_ep);
  53462. + } else {
  53463. + is_last = 1;
  53464. + }
  53465. + }
  53466. +
  53467. + DWC_DEBUGPL(DBG_PCDV,
  53468. + "addr %p, %d-%s len=%d cnt=%d xsize=%d pktcnt=%d\n",
  53469. + &out_ep_regs->doeptsiz, ep->dwc_ep.num,
  53470. + ep->dwc_ep.is_in ? "IN" : "OUT",
  53471. + ep->dwc_ep.xfer_len, ep->dwc_ep.xfer_count,
  53472. + deptsiz.b.xfersize, deptsiz.b.pktcnt);
  53473. + }
  53474. +
  53475. + /* Complete the request */
  53476. + if (is_last) {
  53477. +#ifdef DWC_UTE_CFI
  53478. + if (ep->dwc_ep.buff_mode != BM_STANDARD) {
  53479. + req->actual = ep->dwc_ep.cfi_req_len - byte_count;
  53480. + } else {
  53481. +#endif
  53482. + req->actual = ep->dwc_ep.xfer_count;
  53483. +#ifdef DWC_UTE_CFI
  53484. + }
  53485. +#endif
  53486. + if (req->dw_align_buf) {
  53487. + if (!ep->dwc_ep.is_in) {
  53488. + dwc_memcpy(req->buf, req->dw_align_buf, req->length);
  53489. + }
  53490. + DWC_DMA_FREE(dev, req->length, req->dw_align_buf,
  53491. + req->dw_align_buf_dma);
  53492. + }
  53493. +
  53494. + dwc_otg_request_done(ep, req, 0);
  53495. +
  53496. + ep->dwc_ep.start_xfer_buff = 0;
  53497. + ep->dwc_ep.xfer_buff = 0;
  53498. + ep->dwc_ep.xfer_len = 0;
  53499. +
  53500. + /* If there is a request in the queue start it. */
  53501. + start_next_request(ep);
  53502. + }
  53503. +}
  53504. +
  53505. +#ifdef DWC_EN_ISOC
  53506. +
  53507. +/**
  53508. + * This function BNA interrupt for Isochronous EPs
  53509. + *
  53510. + */
  53511. +static void dwc_otg_pcd_handle_iso_bna(dwc_otg_pcd_ep_t * ep)
  53512. +{
  53513. + dwc_ep_t *dwc_ep = &ep->dwc_ep;
  53514. + volatile uint32_t *addr;
  53515. + depctl_data_t depctl = {.d32 = 0 };
  53516. + dwc_otg_pcd_t *pcd = ep->pcd;
  53517. + dwc_otg_dev_dma_desc_t *dma_desc;
  53518. + int i;
  53519. +
  53520. + dma_desc =
  53521. + dwc_ep->iso_desc_addr + dwc_ep->desc_cnt * (dwc_ep->proc_buf_num);
  53522. +
  53523. + if (dwc_ep->is_in) {
  53524. + dev_dma_desc_sts_t sts = {.d32 = 0 };
  53525. + for (i = 0; i < dwc_ep->desc_cnt; ++i, ++dma_desc) {
  53526. + sts.d32 = dma_desc->status.d32;
  53527. + sts.b_iso_in.bs = BS_HOST_READY;
  53528. + dma_desc->status.d32 = sts.d32;
  53529. + }
  53530. + } else {
  53531. + dev_dma_desc_sts_t sts = {.d32 = 0 };
  53532. + for (i = 0; i < dwc_ep->desc_cnt; ++i, ++dma_desc) {
  53533. + sts.d32 = dma_desc->status.d32;
  53534. + sts.b_iso_out.bs = BS_HOST_READY;
  53535. + dma_desc->status.d32 = sts.d32;
  53536. + }
  53537. + }
  53538. +
  53539. + if (dwc_ep->is_in == 0) {
  53540. + addr =
  53541. + &GET_CORE_IF(pcd)->dev_if->out_ep_regs[dwc_ep->
  53542. + num]->doepctl;
  53543. + } else {
  53544. + addr =
  53545. + &GET_CORE_IF(pcd)->dev_if->in_ep_regs[dwc_ep->num]->diepctl;
  53546. + }
  53547. + depctl.b.epena = 1;
  53548. + DWC_MODIFY_REG32(addr, depctl.d32, depctl.d32);
  53549. +}
  53550. +
  53551. +/**
  53552. + * This function sets latest iso packet information(non-PTI mode)
  53553. + *
  53554. + * @param core_if Programming view of DWC_otg controller.
  53555. + * @param ep The EP to start the transfer on.
  53556. + *
  53557. + */
  53558. +void set_current_pkt_info(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  53559. +{
  53560. + deptsiz_data_t deptsiz = {.d32 = 0 };
  53561. + dma_addr_t dma_addr;
  53562. + uint32_t offset;
  53563. +
  53564. + if (ep->proc_buf_num)
  53565. + dma_addr = ep->dma_addr1;
  53566. + else
  53567. + dma_addr = ep->dma_addr0;
  53568. +
  53569. + if (ep->is_in) {
  53570. + deptsiz.d32 =
  53571. + DWC_READ_REG32(&core_if->dev_if->
  53572. + in_ep_regs[ep->num]->dieptsiz);
  53573. + offset = ep->data_per_frame;
  53574. + } else {
  53575. + deptsiz.d32 =
  53576. + DWC_READ_REG32(&core_if->dev_if->
  53577. + out_ep_regs[ep->num]->doeptsiz);
  53578. + offset =
  53579. + ep->data_per_frame +
  53580. + (0x4 & (0x4 - (ep->data_per_frame & 0x3)));
  53581. + }
  53582. +
  53583. + if (!deptsiz.b.xfersize) {
  53584. + ep->pkt_info[ep->cur_pkt].length = ep->data_per_frame;
  53585. + ep->pkt_info[ep->cur_pkt].offset =
  53586. + ep->cur_pkt_dma_addr - dma_addr;
  53587. + ep->pkt_info[ep->cur_pkt].status = 0;
  53588. + } else {
  53589. + ep->pkt_info[ep->cur_pkt].length = ep->data_per_frame;
  53590. + ep->pkt_info[ep->cur_pkt].offset =
  53591. + ep->cur_pkt_dma_addr - dma_addr;
  53592. + ep->pkt_info[ep->cur_pkt].status = -DWC_E_NO_DATA;
  53593. + }
  53594. + ep->cur_pkt_addr += offset;
  53595. + ep->cur_pkt_dma_addr += offset;
  53596. + ep->cur_pkt++;
  53597. +}
  53598. +
  53599. +/**
  53600. + * This function sets latest iso packet information(DDMA mode)
  53601. + *
  53602. + * @param core_if Programming view of DWC_otg controller.
  53603. + * @param dwc_ep The EP to start the transfer on.
  53604. + *
  53605. + */
  53606. +static void set_ddma_iso_pkts_info(dwc_otg_core_if_t * core_if,
  53607. + dwc_ep_t * dwc_ep)
  53608. +{
  53609. + dwc_otg_dev_dma_desc_t *dma_desc;
  53610. + dev_dma_desc_sts_t sts = {.d32 = 0 };
  53611. + iso_pkt_info_t *iso_packet;
  53612. + uint32_t data_per_desc;
  53613. + uint32_t offset;
  53614. + int i, j;
  53615. +
  53616. + iso_packet = dwc_ep->pkt_info;
  53617. +
  53618. + /** Reinit closed DMA Descriptors*/
  53619. + /** ISO OUT EP */
  53620. + if (dwc_ep->is_in == 0) {
  53621. + dma_desc =
  53622. + dwc_ep->iso_desc_addr +
  53623. + dwc_ep->desc_cnt * dwc_ep->proc_buf_num;
  53624. + offset = 0;
  53625. +
  53626. + for (i = 0; i < dwc_ep->desc_cnt - dwc_ep->pkt_per_frm;
  53627. + i += dwc_ep->pkt_per_frm) {
  53628. + for (j = 0; j < dwc_ep->pkt_per_frm; ++j) {
  53629. + data_per_desc =
  53630. + ((j + 1) * dwc_ep->maxpacket >
  53631. + dwc_ep->
  53632. + data_per_frame) ? dwc_ep->data_per_frame -
  53633. + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
  53634. + data_per_desc +=
  53635. + (data_per_desc % 4) ? (4 -
  53636. + data_per_desc %
  53637. + 4) : 0;
  53638. +
  53639. + sts.d32 = dma_desc->status.d32;
  53640. +
  53641. + /* Write status in iso_packet_decsriptor */
  53642. + iso_packet->status =
  53643. + sts.b_iso_out.rxsts +
  53644. + (sts.b_iso_out.bs ^ BS_DMA_DONE);
  53645. + if (iso_packet->status) {
  53646. + iso_packet->status = -DWC_E_NO_DATA;
  53647. + }
  53648. +
  53649. + /* Received data length */
  53650. + if (!sts.b_iso_out.rxbytes) {
  53651. + iso_packet->length =
  53652. + data_per_desc -
  53653. + sts.b_iso_out.rxbytes;
  53654. + } else {
  53655. + iso_packet->length =
  53656. + data_per_desc -
  53657. + sts.b_iso_out.rxbytes + (4 -
  53658. + dwc_ep->data_per_frame
  53659. + % 4);
  53660. + }
  53661. +
  53662. + iso_packet->offset = offset;
  53663. +
  53664. + offset += data_per_desc;
  53665. + dma_desc++;
  53666. + iso_packet++;
  53667. + }
  53668. + }
  53669. +
  53670. + for (j = 0; j < dwc_ep->pkt_per_frm - 1; ++j) {
  53671. + data_per_desc =
  53672. + ((j + 1) * dwc_ep->maxpacket >
  53673. + dwc_ep->data_per_frame) ? dwc_ep->data_per_frame -
  53674. + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
  53675. + data_per_desc +=
  53676. + (data_per_desc % 4) ? (4 - data_per_desc % 4) : 0;
  53677. +
  53678. + sts.d32 = dma_desc->status.d32;
  53679. +
  53680. + /* Write status in iso_packet_decsriptor */
  53681. + iso_packet->status =
  53682. + sts.b_iso_out.rxsts +
  53683. + (sts.b_iso_out.bs ^ BS_DMA_DONE);
  53684. + if (iso_packet->status) {
  53685. + iso_packet->status = -DWC_E_NO_DATA;
  53686. + }
  53687. +
  53688. + /* Received data length */
  53689. + iso_packet->length =
  53690. + dwc_ep->data_per_frame - sts.b_iso_out.rxbytes;
  53691. +
  53692. + iso_packet->offset = offset;
  53693. +
  53694. + offset += data_per_desc;
  53695. + iso_packet++;
  53696. + dma_desc++;
  53697. + }
  53698. +
  53699. + sts.d32 = dma_desc->status.d32;
  53700. +
  53701. + /* Write status in iso_packet_decsriptor */
  53702. + iso_packet->status =
  53703. + sts.b_iso_out.rxsts + (sts.b_iso_out.bs ^ BS_DMA_DONE);
  53704. + if (iso_packet->status) {
  53705. + iso_packet->status = -DWC_E_NO_DATA;
  53706. + }
  53707. + /* Received data length */
  53708. + if (!sts.b_iso_out.rxbytes) {
  53709. + iso_packet->length =
  53710. + dwc_ep->data_per_frame - sts.b_iso_out.rxbytes;
  53711. + } else {
  53712. + iso_packet->length =
  53713. + dwc_ep->data_per_frame - sts.b_iso_out.rxbytes +
  53714. + (4 - dwc_ep->data_per_frame % 4);
  53715. + }
  53716. +
  53717. + iso_packet->offset = offset;
  53718. + } else {
  53719. +/** ISO IN EP */
  53720. +
  53721. + dma_desc =
  53722. + dwc_ep->iso_desc_addr +
  53723. + dwc_ep->desc_cnt * dwc_ep->proc_buf_num;
  53724. +
  53725. + for (i = 0; i < dwc_ep->desc_cnt - 1; i++) {
  53726. + sts.d32 = dma_desc->status.d32;
  53727. +
  53728. + /* Write status in iso packet descriptor */
  53729. + iso_packet->status =
  53730. + sts.b_iso_in.txsts +
  53731. + (sts.b_iso_in.bs ^ BS_DMA_DONE);
  53732. + if (iso_packet->status != 0) {
  53733. + iso_packet->status = -DWC_E_NO_DATA;
  53734. +
  53735. + }
  53736. + /* Bytes has been transfered */
  53737. + iso_packet->length =
  53738. + dwc_ep->data_per_frame - sts.b_iso_in.txbytes;
  53739. +
  53740. + dma_desc++;
  53741. + iso_packet++;
  53742. + }
  53743. +
  53744. + sts.d32 = dma_desc->status.d32;
  53745. + while (sts.b_iso_in.bs == BS_DMA_BUSY) {
  53746. + sts.d32 = dma_desc->status.d32;
  53747. + }
  53748. +
  53749. + /* Write status in iso packet descriptor ??? do be done with ERROR codes */
  53750. + iso_packet->status =
  53751. + sts.b_iso_in.txsts + (sts.b_iso_in.bs ^ BS_DMA_DONE);
  53752. + if (iso_packet->status != 0) {
  53753. + iso_packet->status = -DWC_E_NO_DATA;
  53754. + }
  53755. +
  53756. + /* Bytes has been transfered */
  53757. + iso_packet->length =
  53758. + dwc_ep->data_per_frame - sts.b_iso_in.txbytes;
  53759. + }
  53760. +}
  53761. +
  53762. +/**
  53763. + * This function reinitialize DMA Descriptors for Isochronous transfer
  53764. + *
  53765. + * @param core_if Programming view of DWC_otg controller.
  53766. + * @param dwc_ep The EP to start the transfer on.
  53767. + *
  53768. + */
  53769. +static void reinit_ddma_iso_xfer(dwc_otg_core_if_t * core_if, dwc_ep_t * dwc_ep)
  53770. +{
  53771. + int i, j;
  53772. + dwc_otg_dev_dma_desc_t *dma_desc;
  53773. + dma_addr_t dma_ad;
  53774. + volatile uint32_t *addr;
  53775. + dev_dma_desc_sts_t sts = {.d32 = 0 };
  53776. + uint32_t data_per_desc;
  53777. +
  53778. + if (dwc_ep->is_in == 0) {
  53779. + addr = &core_if->dev_if->out_ep_regs[dwc_ep->num]->doepctl;
  53780. + } else {
  53781. + addr = &core_if->dev_if->in_ep_regs[dwc_ep->num]->diepctl;
  53782. + }
  53783. +
  53784. + if (dwc_ep->proc_buf_num == 0) {
  53785. + /** Buffer 0 descriptors setup */
  53786. + dma_ad = dwc_ep->dma_addr0;
  53787. + } else {
  53788. + /** Buffer 1 descriptors setup */
  53789. + dma_ad = dwc_ep->dma_addr1;
  53790. + }
  53791. +
  53792. + /** Reinit closed DMA Descriptors*/
  53793. + /** ISO OUT EP */
  53794. + if (dwc_ep->is_in == 0) {
  53795. + dma_desc =
  53796. + dwc_ep->iso_desc_addr +
  53797. + dwc_ep->desc_cnt * dwc_ep->proc_buf_num;
  53798. +
  53799. + sts.b_iso_out.bs = BS_HOST_READY;
  53800. + sts.b_iso_out.rxsts = 0;
  53801. + sts.b_iso_out.l = 0;
  53802. + sts.b_iso_out.sp = 0;
  53803. + sts.b_iso_out.ioc = 0;
  53804. + sts.b_iso_out.pid = 0;
  53805. + sts.b_iso_out.framenum = 0;
  53806. +
  53807. + for (i = 0; i < dwc_ep->desc_cnt - dwc_ep->pkt_per_frm;
  53808. + i += dwc_ep->pkt_per_frm) {
  53809. + for (j = 0; j < dwc_ep->pkt_per_frm; ++j) {
  53810. + data_per_desc =
  53811. + ((j + 1) * dwc_ep->maxpacket >
  53812. + dwc_ep->
  53813. + data_per_frame) ? dwc_ep->data_per_frame -
  53814. + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
  53815. + data_per_desc +=
  53816. + (data_per_desc % 4) ? (4 -
  53817. + data_per_desc %
  53818. + 4) : 0;
  53819. + sts.b_iso_out.rxbytes = data_per_desc;
  53820. + dma_desc->buf = dma_ad;
  53821. + dma_desc->status.d32 = sts.d32;
  53822. +
  53823. + dma_ad += data_per_desc;
  53824. + dma_desc++;
  53825. + }
  53826. + }
  53827. +
  53828. + for (j = 0; j < dwc_ep->pkt_per_frm - 1; ++j) {
  53829. +
  53830. + data_per_desc =
  53831. + ((j + 1) * dwc_ep->maxpacket >
  53832. + dwc_ep->data_per_frame) ? dwc_ep->data_per_frame -
  53833. + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
  53834. + data_per_desc +=
  53835. + (data_per_desc % 4) ? (4 - data_per_desc % 4) : 0;
  53836. + sts.b_iso_out.rxbytes = data_per_desc;
  53837. +
  53838. + dma_desc->buf = dma_ad;
  53839. + dma_desc->status.d32 = sts.d32;
  53840. +
  53841. + dma_desc++;
  53842. + dma_ad += data_per_desc;
  53843. + }
  53844. +
  53845. + sts.b_iso_out.ioc = 1;
  53846. + sts.b_iso_out.l = dwc_ep->proc_buf_num;
  53847. +
  53848. + data_per_desc =
  53849. + ((j + 1) * dwc_ep->maxpacket >
  53850. + dwc_ep->data_per_frame) ? dwc_ep->data_per_frame -
  53851. + j * dwc_ep->maxpacket : dwc_ep->maxpacket;
  53852. + data_per_desc +=
  53853. + (data_per_desc % 4) ? (4 - data_per_desc % 4) : 0;
  53854. + sts.b_iso_out.rxbytes = data_per_desc;
  53855. +
  53856. + dma_desc->buf = dma_ad;
  53857. + dma_desc->status.d32 = sts.d32;
  53858. + } else {
  53859. +/** ISO IN EP */
  53860. +
  53861. + dma_desc =
  53862. + dwc_ep->iso_desc_addr +
  53863. + dwc_ep->desc_cnt * dwc_ep->proc_buf_num;
  53864. +
  53865. + sts.b_iso_in.bs = BS_HOST_READY;
  53866. + sts.b_iso_in.txsts = 0;
  53867. + sts.b_iso_in.sp = 0;
  53868. + sts.b_iso_in.ioc = 0;
  53869. + sts.b_iso_in.pid = dwc_ep->pkt_per_frm;
  53870. + sts.b_iso_in.framenum = dwc_ep->next_frame;
  53871. + sts.b_iso_in.txbytes = dwc_ep->data_per_frame;
  53872. + sts.b_iso_in.l = 0;
  53873. +
  53874. + for (i = 0; i < dwc_ep->desc_cnt - 1; i++) {
  53875. + dma_desc->buf = dma_ad;
  53876. + dma_desc->status.d32 = sts.d32;
  53877. +
  53878. + sts.b_iso_in.framenum += dwc_ep->bInterval;
  53879. + dma_ad += dwc_ep->data_per_frame;
  53880. + dma_desc++;
  53881. + }
  53882. +
  53883. + sts.b_iso_in.ioc = 1;
  53884. + sts.b_iso_in.l = dwc_ep->proc_buf_num;
  53885. +
  53886. + dma_desc->buf = dma_ad;
  53887. + dma_desc->status.d32 = sts.d32;
  53888. +
  53889. + dwc_ep->next_frame =
  53890. + sts.b_iso_in.framenum + dwc_ep->bInterval * 1;
  53891. + }
  53892. + dwc_ep->proc_buf_num = (dwc_ep->proc_buf_num ^ 1) & 0x1;
  53893. +}
  53894. +
  53895. +/**
  53896. + * This function is to handle Iso EP transfer complete interrupt
  53897. + * in case Iso out packet was dropped
  53898. + *
  53899. + * @param core_if Programming view of DWC_otg controller.
  53900. + * @param dwc_ep The EP for wihich transfer complete was asserted
  53901. + *
  53902. + */
  53903. +static uint32_t handle_iso_out_pkt_dropped(dwc_otg_core_if_t * core_if,
  53904. + dwc_ep_t * dwc_ep)
  53905. +{
  53906. + uint32_t dma_addr;
  53907. + uint32_t drp_pkt;
  53908. + uint32_t drp_pkt_cnt;
  53909. + deptsiz_data_t deptsiz = {.d32 = 0 };
  53910. + depctl_data_t depctl = {.d32 = 0 };
  53911. + int i;
  53912. +
  53913. + deptsiz.d32 =
  53914. + DWC_READ_REG32(&core_if->dev_if->
  53915. + out_ep_regs[dwc_ep->num]->doeptsiz);
  53916. +
  53917. + drp_pkt = dwc_ep->pkt_cnt - deptsiz.b.pktcnt;
  53918. + drp_pkt_cnt = dwc_ep->pkt_per_frm - (drp_pkt % dwc_ep->pkt_per_frm);
  53919. +
  53920. + /* Setting dropped packets status */
  53921. + for (i = 0; i < drp_pkt_cnt; ++i) {
  53922. + dwc_ep->pkt_info[drp_pkt].status = -DWC_E_NO_DATA;
  53923. + drp_pkt++;
  53924. + deptsiz.b.pktcnt--;
  53925. + }
  53926. +
  53927. + if (deptsiz.b.pktcnt > 0) {
  53928. + deptsiz.b.xfersize =
  53929. + dwc_ep->xfer_len - (dwc_ep->pkt_cnt -
  53930. + deptsiz.b.pktcnt) * dwc_ep->maxpacket;
  53931. + } else {
  53932. + deptsiz.b.xfersize = 0;
  53933. + deptsiz.b.pktcnt = 0;
  53934. + }
  53935. +
  53936. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[dwc_ep->num]->doeptsiz,
  53937. + deptsiz.d32);
  53938. +
  53939. + if (deptsiz.b.pktcnt > 0) {
  53940. + if (dwc_ep->proc_buf_num) {
  53941. + dma_addr =
  53942. + dwc_ep->dma_addr1 + dwc_ep->xfer_len -
  53943. + deptsiz.b.xfersize;
  53944. + } else {
  53945. + dma_addr =
  53946. + dwc_ep->dma_addr0 + dwc_ep->xfer_len -
  53947. + deptsiz.b.xfersize;;
  53948. + }
  53949. +
  53950. + DWC_WRITE_REG32(&core_if->dev_if->
  53951. + out_ep_regs[dwc_ep->num]->doepdma, dma_addr);
  53952. +
  53953. + /** Re-enable endpoint, clear nak */
  53954. + depctl.d32 = 0;
  53955. + depctl.b.epena = 1;
  53956. + depctl.b.cnak = 1;
  53957. +
  53958. + DWC_MODIFY_REG32(&core_if->dev_if->
  53959. + out_ep_regs[dwc_ep->num]->doepctl, depctl.d32,
  53960. + depctl.d32);
  53961. + return 0;
  53962. + } else {
  53963. + return 1;
  53964. + }
  53965. +}
  53966. +
  53967. +/**
  53968. + * This function sets iso packets information(PTI mode)
  53969. + *
  53970. + * @param core_if Programming view of DWC_otg controller.
  53971. + * @param ep The EP to start the transfer on.
  53972. + *
  53973. + */
  53974. +static uint32_t set_iso_pkts_info(dwc_otg_core_if_t * core_if, dwc_ep_t * ep)
  53975. +{
  53976. + int i, j;
  53977. + dma_addr_t dma_ad;
  53978. + iso_pkt_info_t *packet_info = ep->pkt_info;
  53979. + uint32_t offset;
  53980. + uint32_t frame_data;
  53981. + deptsiz_data_t deptsiz;
  53982. +
  53983. + if (ep->proc_buf_num == 0) {
  53984. + /** Buffer 0 descriptors setup */
  53985. + dma_ad = ep->dma_addr0;
  53986. + } else {
  53987. + /** Buffer 1 descriptors setup */
  53988. + dma_ad = ep->dma_addr1;
  53989. + }
  53990. +
  53991. + if (ep->is_in) {
  53992. + deptsiz.d32 =
  53993. + DWC_READ_REG32(&core_if->dev_if->in_ep_regs[ep->num]->
  53994. + dieptsiz);
  53995. + } else {
  53996. + deptsiz.d32 =
  53997. + DWC_READ_REG32(&core_if->dev_if->out_ep_regs[ep->num]->
  53998. + doeptsiz);
  53999. + }
  54000. +
  54001. + if (!deptsiz.b.xfersize) {
  54002. + offset = 0;
  54003. + for (i = 0; i < ep->pkt_cnt; i += ep->pkt_per_frm) {
  54004. + frame_data = ep->data_per_frame;
  54005. + for (j = 0; j < ep->pkt_per_frm; ++j) {
  54006. +
  54007. + /* Packet status - is not set as initially
  54008. + * it is set to 0 and if packet was sent
  54009. + successfully, status field will remain 0*/
  54010. +
  54011. + /* Bytes has been transfered */
  54012. + packet_info->length =
  54013. + (ep->maxpacket <
  54014. + frame_data) ? ep->maxpacket : frame_data;
  54015. +
  54016. + /* Received packet offset */
  54017. + packet_info->offset = offset;
  54018. + offset += packet_info->length;
  54019. + frame_data -= packet_info->length;
  54020. +
  54021. + packet_info++;
  54022. + }
  54023. + }
  54024. + return 1;
  54025. + } else {
  54026. + /* This is a workaround for in case of Transfer Complete with
  54027. + * PktDrpSts interrupts merging - in this case Transfer complete
  54028. + * interrupt for Isoc Out Endpoint is asserted without PktDrpSts
  54029. + * set and with DOEPTSIZ register non zero. Investigations showed,
  54030. + * that this happens when Out packet is dropped, but because of
  54031. + * interrupts merging during first interrupt handling PktDrpSts
  54032. + * bit is cleared and for next merged interrupts it is not reset.
  54033. + * In this case SW hadles the interrupt as if PktDrpSts bit is set.
  54034. + */
  54035. + if (ep->is_in) {
  54036. + return 1;
  54037. + } else {
  54038. + return handle_iso_out_pkt_dropped(core_if, ep);
  54039. + }
  54040. + }
  54041. +}
  54042. +
  54043. +/**
  54044. + * This function is to handle Iso EP transfer complete interrupt
  54045. + *
  54046. + * @param pcd The PCD
  54047. + * @param ep The EP for which transfer complete was asserted
  54048. + *
  54049. + */
  54050. +static void complete_iso_ep(dwc_otg_pcd_t * pcd, dwc_otg_pcd_ep_t * ep)
  54051. +{
  54052. + dwc_otg_core_if_t *core_if = GET_CORE_IF(ep->pcd);
  54053. + dwc_ep_t *dwc_ep = &ep->dwc_ep;
  54054. + uint8_t is_last = 0;
  54055. +
  54056. + if (ep->dwc_ep.next_frame == 0xffffffff) {
  54057. + DWC_WARN("Next frame is not set!\n");
  54058. + return;
  54059. + }
  54060. +
  54061. + if (core_if->dma_enable) {
  54062. + if (core_if->dma_desc_enable) {
  54063. + set_ddma_iso_pkts_info(core_if, dwc_ep);
  54064. + reinit_ddma_iso_xfer(core_if, dwc_ep);
  54065. + is_last = 1;
  54066. + } else {
  54067. + if (core_if->pti_enh_enable) {
  54068. + if (set_iso_pkts_info(core_if, dwc_ep)) {
  54069. + dwc_ep->proc_buf_num =
  54070. + (dwc_ep->proc_buf_num ^ 1) & 0x1;
  54071. + dwc_otg_iso_ep_start_buf_transfer
  54072. + (core_if, dwc_ep);
  54073. + is_last = 1;
  54074. + }
  54075. + } else {
  54076. + set_current_pkt_info(core_if, dwc_ep);
  54077. + if (dwc_ep->cur_pkt >= dwc_ep->pkt_cnt) {
  54078. + is_last = 1;
  54079. + dwc_ep->cur_pkt = 0;
  54080. + dwc_ep->proc_buf_num =
  54081. + (dwc_ep->proc_buf_num ^ 1) & 0x1;
  54082. + if (dwc_ep->proc_buf_num) {
  54083. + dwc_ep->cur_pkt_addr =
  54084. + dwc_ep->xfer_buff1;
  54085. + dwc_ep->cur_pkt_dma_addr =
  54086. + dwc_ep->dma_addr1;
  54087. + } else {
  54088. + dwc_ep->cur_pkt_addr =
  54089. + dwc_ep->xfer_buff0;
  54090. + dwc_ep->cur_pkt_dma_addr =
  54091. + dwc_ep->dma_addr0;
  54092. + }
  54093. +
  54094. + }
  54095. + dwc_otg_iso_ep_start_frm_transfer(core_if,
  54096. + dwc_ep);
  54097. + }
  54098. + }
  54099. + } else {
  54100. + set_current_pkt_info(core_if, dwc_ep);
  54101. + if (dwc_ep->cur_pkt >= dwc_ep->pkt_cnt) {
  54102. + is_last = 1;
  54103. + dwc_ep->cur_pkt = 0;
  54104. + dwc_ep->proc_buf_num = (dwc_ep->proc_buf_num ^ 1) & 0x1;
  54105. + if (dwc_ep->proc_buf_num) {
  54106. + dwc_ep->cur_pkt_addr = dwc_ep->xfer_buff1;
  54107. + dwc_ep->cur_pkt_dma_addr = dwc_ep->dma_addr1;
  54108. + } else {
  54109. + dwc_ep->cur_pkt_addr = dwc_ep->xfer_buff0;
  54110. + dwc_ep->cur_pkt_dma_addr = dwc_ep->dma_addr0;
  54111. + }
  54112. +
  54113. + }
  54114. + dwc_otg_iso_ep_start_frm_transfer(core_if, dwc_ep);
  54115. + }
  54116. + if (is_last)
  54117. + dwc_otg_iso_buffer_done(pcd, ep, ep->iso_req_handle);
  54118. +}
  54119. +#endif /* DWC_EN_ISOC */
  54120. +
  54121. +/**
  54122. + * This function handle BNA interrupt for Non Isochronous EPs
  54123. + *
  54124. + */
  54125. +static void dwc_otg_pcd_handle_noniso_bna(dwc_otg_pcd_ep_t * ep)
  54126. +{
  54127. + dwc_ep_t *dwc_ep = &ep->dwc_ep;
  54128. + volatile uint32_t *addr;
  54129. + depctl_data_t depctl = {.d32 = 0 };
  54130. + dwc_otg_pcd_t *pcd = ep->pcd;
  54131. + dwc_otg_dev_dma_desc_t *dma_desc;
  54132. + dev_dma_desc_sts_t sts = {.d32 = 0 };
  54133. + dwc_otg_core_if_t *core_if = ep->pcd->core_if;
  54134. + int i, start;
  54135. +
  54136. + if (!dwc_ep->desc_cnt)
  54137. + DWC_WARN("Ep%d %s Descriptor count = %d \n", dwc_ep->num,
  54138. + (dwc_ep->is_in ? "IN" : "OUT"), dwc_ep->desc_cnt);
  54139. +
  54140. + if (core_if->core_params->cont_on_bna && !dwc_ep->is_in
  54141. + && dwc_ep->type != DWC_OTG_EP_TYPE_CONTROL) {
  54142. + uint32_t doepdma;
  54143. + dwc_otg_dev_out_ep_regs_t *out_regs =
  54144. + core_if->dev_if->out_ep_regs[dwc_ep->num];
  54145. + doepdma = DWC_READ_REG32(&(out_regs->doepdma));
  54146. + start = (doepdma - dwc_ep->dma_desc_addr)/sizeof(dwc_otg_dev_dma_desc_t);
  54147. + dma_desc = &(dwc_ep->desc_addr[start]);
  54148. + } else {
  54149. + start = 0;
  54150. + dma_desc = dwc_ep->desc_addr;
  54151. + }
  54152. +
  54153. +
  54154. + for (i = start; i < dwc_ep->desc_cnt; ++i, ++dma_desc) {
  54155. + sts.d32 = dma_desc->status.d32;
  54156. + sts.b.bs = BS_HOST_READY;
  54157. + dma_desc->status.d32 = sts.d32;
  54158. + }
  54159. +
  54160. + if (dwc_ep->is_in == 0) {
  54161. + addr =
  54162. + &GET_CORE_IF(pcd)->dev_if->out_ep_regs[dwc_ep->num]->
  54163. + doepctl;
  54164. + } else {
  54165. + addr =
  54166. + &GET_CORE_IF(pcd)->dev_if->in_ep_regs[dwc_ep->num]->diepctl;
  54167. + }
  54168. + depctl.b.epena = 1;
  54169. + depctl.b.cnak = 1;
  54170. + DWC_MODIFY_REG32(addr, 0, depctl.d32);
  54171. +}
  54172. +
  54173. +/**
  54174. + * This function handles EP0 Control transfers.
  54175. + *
  54176. + * The state of the control transfers are tracked in
  54177. + * <code>ep0state</code>.
  54178. + */
  54179. +static void handle_ep0(dwc_otg_pcd_t * pcd)
  54180. +{
  54181. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  54182. + dwc_otg_pcd_ep_t *ep0 = &pcd->ep0;
  54183. + dev_dma_desc_sts_t desc_sts;
  54184. + deptsiz0_data_t deptsiz;
  54185. + uint32_t byte_count;
  54186. +
  54187. +#ifdef DEBUG_EP0
  54188. + DWC_DEBUGPL(DBG_PCDV, "%s()\n", __func__);
  54189. + print_ep0_state(pcd);
  54190. +#endif
  54191. +
  54192. +// DWC_PRINTF("HANDLE EP0\n");
  54193. +
  54194. + switch (pcd->ep0state) {
  54195. + case EP0_DISCONNECT:
  54196. + break;
  54197. +
  54198. + case EP0_IDLE:
  54199. + pcd->request_config = 0;
  54200. +
  54201. + pcd_setup(pcd);
  54202. + break;
  54203. +
  54204. + case EP0_IN_DATA_PHASE:
  54205. +#ifdef DEBUG_EP0
  54206. + DWC_DEBUGPL(DBG_PCD, "DATA_IN EP%d-%s: type=%d, mps=%d\n",
  54207. + ep0->dwc_ep.num, (ep0->dwc_ep.is_in ? "IN" : "OUT"),
  54208. + ep0->dwc_ep.type, ep0->dwc_ep.maxpacket);
  54209. +#endif
  54210. +
  54211. + if (core_if->dma_enable != 0) {
  54212. + /*
  54213. + * For EP0 we can only program 1 packet at a time so we
  54214. + * need to do the make calculations after each complete.
  54215. + * Call write_packet to make the calculations, as in
  54216. + * slave mode, and use those values to determine if we
  54217. + * can complete.
  54218. + */
  54219. + if (core_if->dma_desc_enable == 0) {
  54220. + deptsiz.d32 =
  54221. + DWC_READ_REG32(&core_if->
  54222. + dev_if->in_ep_regs[0]->
  54223. + dieptsiz);
  54224. + byte_count =
  54225. + ep0->dwc_ep.xfer_len - deptsiz.b.xfersize;
  54226. + } else {
  54227. + desc_sts =
  54228. + core_if->dev_if->in_desc_addr->status;
  54229. + byte_count =
  54230. + ep0->dwc_ep.xfer_len - desc_sts.b.bytes;
  54231. + }
  54232. + ep0->dwc_ep.xfer_count += byte_count;
  54233. + ep0->dwc_ep.xfer_buff += byte_count;
  54234. + ep0->dwc_ep.dma_addr += byte_count;
  54235. + }
  54236. + if (ep0->dwc_ep.xfer_count < ep0->dwc_ep.total_len) {
  54237. + dwc_otg_ep0_continue_transfer(GET_CORE_IF(pcd),
  54238. + &ep0->dwc_ep);
  54239. + DWC_DEBUGPL(DBG_PCD, "CONTINUE TRANSFER\n");
  54240. + } else if (ep0->dwc_ep.sent_zlp) {
  54241. + dwc_otg_ep0_continue_transfer(GET_CORE_IF(pcd),
  54242. + &ep0->dwc_ep);
  54243. + ep0->dwc_ep.sent_zlp = 0;
  54244. + DWC_DEBUGPL(DBG_PCD, "CONTINUE TRANSFER sent zlp\n");
  54245. + } else {
  54246. + ep0_complete_request(ep0);
  54247. + DWC_DEBUGPL(DBG_PCD, "COMPLETE TRANSFER\n");
  54248. + }
  54249. + break;
  54250. + case EP0_OUT_DATA_PHASE:
  54251. +#ifdef DEBUG_EP0
  54252. + DWC_DEBUGPL(DBG_PCD, "DATA_OUT EP%d-%s: type=%d, mps=%d\n",
  54253. + ep0->dwc_ep.num, (ep0->dwc_ep.is_in ? "IN" : "OUT"),
  54254. + ep0->dwc_ep.type, ep0->dwc_ep.maxpacket);
  54255. +#endif
  54256. + if (core_if->dma_enable != 0) {
  54257. + if (core_if->dma_desc_enable == 0) {
  54258. + deptsiz.d32 =
  54259. + DWC_READ_REG32(&core_if->
  54260. + dev_if->out_ep_regs[0]->
  54261. + doeptsiz);
  54262. + byte_count =
  54263. + ep0->dwc_ep.maxpacket - deptsiz.b.xfersize;
  54264. + } else {
  54265. + desc_sts =
  54266. + core_if->dev_if->out_desc_addr->status;
  54267. + byte_count =
  54268. + ep0->dwc_ep.maxpacket - desc_sts.b.bytes;
  54269. + }
  54270. + ep0->dwc_ep.xfer_count += byte_count;
  54271. + ep0->dwc_ep.xfer_buff += byte_count;
  54272. + ep0->dwc_ep.dma_addr += byte_count;
  54273. + }
  54274. + if (ep0->dwc_ep.xfer_count < ep0->dwc_ep.total_len) {
  54275. + dwc_otg_ep0_continue_transfer(GET_CORE_IF(pcd),
  54276. + &ep0->dwc_ep);
  54277. + DWC_DEBUGPL(DBG_PCD, "CONTINUE TRANSFER\n");
  54278. + } else if (ep0->dwc_ep.sent_zlp) {
  54279. + dwc_otg_ep0_continue_transfer(GET_CORE_IF(pcd),
  54280. + &ep0->dwc_ep);
  54281. + ep0->dwc_ep.sent_zlp = 0;
  54282. + DWC_DEBUGPL(DBG_PCD, "CONTINUE TRANSFER sent zlp\n");
  54283. + } else {
  54284. + ep0_complete_request(ep0);
  54285. + DWC_DEBUGPL(DBG_PCD, "COMPLETE TRANSFER\n");
  54286. + }
  54287. + break;
  54288. +
  54289. + case EP0_IN_STATUS_PHASE:
  54290. + case EP0_OUT_STATUS_PHASE:
  54291. + DWC_DEBUGPL(DBG_PCD, "CASE: EP0_STATUS\n");
  54292. + ep0_complete_request(ep0);
  54293. + pcd->ep0state = EP0_IDLE;
  54294. + ep0->stopped = 1;
  54295. + ep0->dwc_ep.is_in = 0; /* OUT for next SETUP */
  54296. +
  54297. + /* Prepare for more SETUP Packets */
  54298. + if (core_if->dma_enable) {
  54299. + ep0_out_start(core_if, pcd);
  54300. + }
  54301. + break;
  54302. +
  54303. + case EP0_STALL:
  54304. + DWC_ERROR("EP0 STALLed, should not get here pcd_setup()\n");
  54305. + break;
  54306. + }
  54307. +#ifdef DEBUG_EP0
  54308. + print_ep0_state(pcd);
  54309. +#endif
  54310. +}
  54311. +
  54312. +/**
  54313. + * Restart transfer
  54314. + */
  54315. +static void restart_transfer(dwc_otg_pcd_t * pcd, const uint32_t epnum)
  54316. +{
  54317. + dwc_otg_core_if_t *core_if;
  54318. + dwc_otg_dev_if_t *dev_if;
  54319. + deptsiz_data_t dieptsiz = {.d32 = 0 };
  54320. + dwc_otg_pcd_ep_t *ep;
  54321. +
  54322. + ep = get_in_ep(pcd, epnum);
  54323. +
  54324. +#ifdef DWC_EN_ISOC
  54325. + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
  54326. + return;
  54327. + }
  54328. +#endif /* DWC_EN_ISOC */
  54329. +
  54330. + core_if = GET_CORE_IF(pcd);
  54331. + dev_if = core_if->dev_if;
  54332. +
  54333. + dieptsiz.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dieptsiz);
  54334. +
  54335. + DWC_DEBUGPL(DBG_PCD, "xfer_buff=%p xfer_count=%0x xfer_len=%0x"
  54336. + " stopped=%d\n", ep->dwc_ep.xfer_buff,
  54337. + ep->dwc_ep.xfer_count, ep->dwc_ep.xfer_len, ep->stopped);
  54338. + /*
  54339. + * If xfersize is 0 and pktcnt in not 0, resend the last packet.
  54340. + */
  54341. + if (dieptsiz.b.pktcnt && dieptsiz.b.xfersize == 0 &&
  54342. + ep->dwc_ep.start_xfer_buff != 0) {
  54343. + if (ep->dwc_ep.total_len <= ep->dwc_ep.maxpacket) {
  54344. + ep->dwc_ep.xfer_count = 0;
  54345. + ep->dwc_ep.xfer_buff = ep->dwc_ep.start_xfer_buff;
  54346. + ep->dwc_ep.xfer_len = ep->dwc_ep.xfer_count;
  54347. + } else {
  54348. + ep->dwc_ep.xfer_count -= ep->dwc_ep.maxpacket;
  54349. + /* convert packet size to dwords. */
  54350. + ep->dwc_ep.xfer_buff -= ep->dwc_ep.maxpacket;
  54351. + ep->dwc_ep.xfer_len = ep->dwc_ep.xfer_count;
  54352. + }
  54353. + ep->stopped = 0;
  54354. + DWC_DEBUGPL(DBG_PCD, "xfer_buff=%p xfer_count=%0x "
  54355. + "xfer_len=%0x stopped=%d\n",
  54356. + ep->dwc_ep.xfer_buff,
  54357. + ep->dwc_ep.xfer_count, ep->dwc_ep.xfer_len,
  54358. + ep->stopped);
  54359. + if (epnum == 0) {
  54360. + dwc_otg_ep0_start_transfer(core_if, &ep->dwc_ep);
  54361. + } else {
  54362. + dwc_otg_ep_start_transfer(core_if, &ep->dwc_ep);
  54363. + }
  54364. + }
  54365. +}
  54366. +
  54367. +/*
  54368. + * This function create new nextep sequnce based on Learn Queue.
  54369. + *
  54370. + * @param core_if Programming view of DWC_otg controller
  54371. + */
  54372. +void predict_nextep_seq( dwc_otg_core_if_t * core_if)
  54373. +{
  54374. + dwc_otg_device_global_regs_t *dev_global_regs =
  54375. + core_if->dev_if->dev_global_regs;
  54376. + const uint32_t TOKEN_Q_DEPTH = core_if->hwcfg2.b.dev_token_q_depth;
  54377. + /* Number of Token Queue Registers */
  54378. + const int DTKNQ_REG_CNT = (TOKEN_Q_DEPTH + 7) / 8;
  54379. + dtknq1_data_t dtknqr1;
  54380. + uint32_t in_tkn_epnums[4];
  54381. + uint8_t seqnum[MAX_EPS_CHANNELS];
  54382. + uint8_t intkn_seq[TOKEN_Q_DEPTH];
  54383. + grstctl_t resetctl = {.d32 = 0 };
  54384. + uint8_t temp;
  54385. + int ndx = 0;
  54386. + int start = 0;
  54387. + int end = 0;
  54388. + int sort_done = 0;
  54389. + int i = 0;
  54390. + volatile uint32_t *addr = &dev_global_regs->dtknqr1;
  54391. +
  54392. +
  54393. + DWC_DEBUGPL(DBG_PCD,"dev_token_q_depth=%d\n",TOKEN_Q_DEPTH);
  54394. +
  54395. + /* Read the DTKNQ Registers */
  54396. + for (i = 0; i < DTKNQ_REG_CNT; i++) {
  54397. + in_tkn_epnums[i] = DWC_READ_REG32(addr);
  54398. + DWC_DEBUGPL(DBG_PCDV, "DTKNQR%d=0x%08x\n", i + 1,
  54399. + in_tkn_epnums[i]);
  54400. + if (addr == &dev_global_regs->dvbusdis) {
  54401. + addr = &dev_global_regs->dtknqr3_dthrctl;
  54402. + } else {
  54403. + ++addr;
  54404. + }
  54405. +
  54406. + }
  54407. +
  54408. + /* Copy the DTKNQR1 data to the bit field. */
  54409. + dtknqr1.d32 = in_tkn_epnums[0];
  54410. + if (dtknqr1.b.wrap_bit) {
  54411. + ndx = dtknqr1.b.intknwptr;
  54412. + end = ndx -1;
  54413. + if (end < 0)
  54414. + end = TOKEN_Q_DEPTH -1;
  54415. + } else {
  54416. + ndx = 0;
  54417. + end = dtknqr1.b.intknwptr -1;
  54418. + if (end < 0)
  54419. + end = 0;
  54420. + }
  54421. + start = ndx;
  54422. +
  54423. + /* Fill seqnum[] by initial values: EP number + 31 */
  54424. + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
  54425. + seqnum[i] = i +31;
  54426. + }
  54427. +
  54428. + /* Fill intkn_seq[] from in_tkn_epnums[0] */
  54429. + for (i=0; i < 6; i++)
  54430. + intkn_seq[i] = (in_tkn_epnums[0] >> ((7-i) * 4)) & 0xf;
  54431. +
  54432. + if (TOKEN_Q_DEPTH > 6) {
  54433. + /* Fill intkn_seq[] from in_tkn_epnums[1] */
  54434. + for (i=6; i < 14; i++)
  54435. + intkn_seq[i] =
  54436. + (in_tkn_epnums[1] >> ((7 - (i - 6)) * 4)) & 0xf;
  54437. + }
  54438. +
  54439. + if (TOKEN_Q_DEPTH > 14) {
  54440. + /* Fill intkn_seq[] from in_tkn_epnums[1] */
  54441. + for (i=14; i < 22; i++)
  54442. + intkn_seq[i] =
  54443. + (in_tkn_epnums[2] >> ((7 - (i - 14)) * 4)) & 0xf;
  54444. + }
  54445. +
  54446. + if (TOKEN_Q_DEPTH > 22) {
  54447. + /* Fill intkn_seq[] from in_tkn_epnums[1] */
  54448. + for (i=22; i < 30; i++)
  54449. + intkn_seq[i] =
  54450. + (in_tkn_epnums[3] >> ((7 - (i - 22)) * 4)) & 0xf;
  54451. + }
  54452. +
  54453. + DWC_DEBUGPL(DBG_PCDV, "%s start=%d end=%d intkn_seq[]:\n", __func__,
  54454. + start, end);
  54455. + for (i=0; i<TOKEN_Q_DEPTH; i++)
  54456. + DWC_DEBUGPL(DBG_PCDV,"%d\n", intkn_seq[i]);
  54457. +
  54458. + /* Update seqnum based on intkn_seq[] */
  54459. + i = 0;
  54460. + do {
  54461. + seqnum[intkn_seq[ndx]] = i;
  54462. + ndx++;
  54463. + i++;
  54464. + if (ndx == TOKEN_Q_DEPTH)
  54465. + ndx = 0;
  54466. + } while ( i < TOKEN_Q_DEPTH );
  54467. +
  54468. + /* Mark non active EP's in seqnum[] by 0xff */
  54469. + for (i=0; i<=core_if->dev_if->num_in_eps; i++) {
  54470. + if (core_if->nextep_seq[i] == 0xff )
  54471. + seqnum[i] = 0xff;
  54472. + }
  54473. +
  54474. + /* Sort seqnum[] */
  54475. + sort_done = 0;
  54476. + while (!sort_done) {
  54477. + sort_done = 1;
  54478. + for (i=0; i<core_if->dev_if->num_in_eps; i++) {
  54479. + if (seqnum[i] > seqnum[i+1]) {
  54480. + temp = seqnum[i];
  54481. + seqnum[i] = seqnum[i+1];
  54482. + seqnum[i+1] = temp;
  54483. + sort_done = 0;
  54484. + }
  54485. + }
  54486. + }
  54487. +
  54488. + ndx = start + seqnum[0];
  54489. + if (ndx >= TOKEN_Q_DEPTH)
  54490. + ndx = ndx % TOKEN_Q_DEPTH;
  54491. + core_if->first_in_nextep_seq = intkn_seq[ndx];
  54492. +
  54493. + /* Update seqnum[] by EP numbers */
  54494. + for (i=0; i<=core_if->dev_if->num_in_eps; i++) {
  54495. + ndx = start + i;
  54496. + if (seqnum[i] < 31) {
  54497. + ndx = start + seqnum[i];
  54498. + if (ndx >= TOKEN_Q_DEPTH)
  54499. + ndx = ndx % TOKEN_Q_DEPTH;
  54500. + seqnum[i] = intkn_seq[ndx];
  54501. + } else {
  54502. + if (seqnum[i] < 0xff) {
  54503. + seqnum[i] = seqnum[i] - 31;
  54504. + } else {
  54505. + break;
  54506. + }
  54507. + }
  54508. + }
  54509. +
  54510. + /* Update nextep_seq[] based on seqnum[] */
  54511. + for (i=0; i<core_if->dev_if->num_in_eps; i++) {
  54512. + if (seqnum[i] != 0xff) {
  54513. + if (seqnum[i+1] != 0xff) {
  54514. + core_if->nextep_seq[seqnum[i]] = seqnum[i+1];
  54515. + } else {
  54516. + core_if->nextep_seq[seqnum[i]] = core_if->first_in_nextep_seq;
  54517. + break;
  54518. + }
  54519. + } else {
  54520. + break;
  54521. + }
  54522. + }
  54523. +
  54524. + DWC_DEBUGPL(DBG_PCDV, "%s first_in_nextep_seq= %2d; nextep_seq[]:\n",
  54525. + __func__, core_if->first_in_nextep_seq);
  54526. + for (i=0; i <= core_if->dev_if->num_in_eps; i++) {
  54527. + DWC_DEBUGPL(DBG_PCDV,"%2d\n", core_if->nextep_seq[i]);
  54528. + }
  54529. +
  54530. + /* Flush the Learning Queue */
  54531. + resetctl.d32 = DWC_READ_REG32(&core_if->core_global_regs->grstctl);
  54532. + resetctl.b.intknqflsh = 1;
  54533. + DWC_WRITE_REG32(&core_if->core_global_regs->grstctl, resetctl.d32);
  54534. +
  54535. +
  54536. +}
  54537. +
  54538. +/**
  54539. + * handle the IN EP disable interrupt.
  54540. + */
  54541. +static inline void handle_in_ep_disable_intr(dwc_otg_pcd_t * pcd,
  54542. + const uint32_t epnum)
  54543. +{
  54544. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  54545. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  54546. + deptsiz_data_t dieptsiz = {.d32 = 0 };
  54547. + dctl_data_t dctl = {.d32 = 0 };
  54548. + dwc_otg_pcd_ep_t *ep;
  54549. + dwc_ep_t *dwc_ep;
  54550. + gintmsk_data_t gintmsk_data;
  54551. + depctl_data_t depctl;
  54552. + uint32_t diepdma;
  54553. + uint32_t remain_to_transfer = 0;
  54554. + uint8_t i;
  54555. + uint32_t xfer_size;
  54556. +
  54557. + ep = get_in_ep(pcd, epnum);
  54558. + dwc_ep = &ep->dwc_ep;
  54559. +
  54560. + if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
  54561. + dwc_otg_flush_tx_fifo(core_if, dwc_ep->tx_fifo_num);
  54562. + complete_ep(ep);
  54563. + return;
  54564. + }
  54565. +
  54566. + DWC_DEBUGPL(DBG_PCD, "diepctl%d=%0x\n", epnum,
  54567. + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->diepctl));
  54568. + dieptsiz.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->dieptsiz);
  54569. + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->diepctl);
  54570. +
  54571. + DWC_DEBUGPL(DBG_ANY, "pktcnt=%d size=%d\n",
  54572. + dieptsiz.b.pktcnt, dieptsiz.b.xfersize);
  54573. +
  54574. + if ((core_if->start_predict == 0) || (depctl.b.eptype & 1)) {
  54575. + if (ep->stopped) {
  54576. + if (core_if->en_multiple_tx_fifo)
  54577. + /* Flush the Tx FIFO */
  54578. + dwc_otg_flush_tx_fifo(core_if, dwc_ep->tx_fifo_num);
  54579. + /* Clear the Global IN NP NAK */
  54580. + dctl.d32 = 0;
  54581. + dctl.b.cgnpinnak = 1;
  54582. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, dctl.d32, dctl.d32);
  54583. + /* Restart the transaction */
  54584. + if (dieptsiz.b.pktcnt != 0 || dieptsiz.b.xfersize != 0) {
  54585. + restart_transfer(pcd, epnum);
  54586. + }
  54587. + } else {
  54588. + /* Restart the transaction */
  54589. + if (dieptsiz.b.pktcnt != 0 || dieptsiz.b.xfersize != 0) {
  54590. + restart_transfer(pcd, epnum);
  54591. + }
  54592. + DWC_DEBUGPL(DBG_ANY, "STOPPED!!!\n");
  54593. + }
  54594. + return;
  54595. + }
  54596. +
  54597. + if (core_if->start_predict > 2) { // NP IN EP
  54598. + core_if->start_predict--;
  54599. + return;
  54600. + }
  54601. +
  54602. + core_if->start_predict--;
  54603. +
  54604. + if (core_if->start_predict == 1) { // All NP IN Ep's disabled now
  54605. +
  54606. + predict_nextep_seq(core_if);
  54607. +
  54608. + /* Update all active IN EP's NextEP field based of nextep_seq[] */
  54609. + for ( i = 0; i <= core_if->dev_if->num_in_eps; i++) {
  54610. + depctl.d32 =
  54611. + DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  54612. + if (core_if->nextep_seq[i] != 0xff) { // Active NP IN EP
  54613. + depctl.b.nextep = core_if->nextep_seq[i];
  54614. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepctl, depctl.d32);
  54615. + }
  54616. + }
  54617. + /* Flush Shared NP TxFIFO */
  54618. + dwc_otg_flush_tx_fifo(core_if, 0);
  54619. + /* Rewind buffers */
  54620. + if (!core_if->dma_desc_enable) {
  54621. + i = core_if->first_in_nextep_seq;
  54622. + do {
  54623. + ep = get_in_ep(pcd, i);
  54624. + dieptsiz.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->dieptsiz);
  54625. + xfer_size = ep->dwc_ep.total_len - ep->dwc_ep.xfer_count;
  54626. + if (xfer_size > ep->dwc_ep.maxxfer)
  54627. + xfer_size = ep->dwc_ep.maxxfer;
  54628. + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  54629. + if (dieptsiz.b.pktcnt != 0) {
  54630. + if (xfer_size == 0) {
  54631. + remain_to_transfer = 0;
  54632. + } else {
  54633. + if ((xfer_size % ep->dwc_ep.maxpacket) == 0) {
  54634. + remain_to_transfer =
  54635. + dieptsiz.b.pktcnt * ep->dwc_ep.maxpacket;
  54636. + } else {
  54637. + remain_to_transfer = ((dieptsiz.b.pktcnt -1) * ep->dwc_ep.maxpacket)
  54638. + + (xfer_size % ep->dwc_ep.maxpacket);
  54639. + }
  54640. + }
  54641. + diepdma = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepdma);
  54642. + dieptsiz.b.xfersize = remain_to_transfer;
  54643. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->dieptsiz, dieptsiz.d32);
  54644. + diepdma = ep->dwc_ep.dma_addr + (xfer_size - remain_to_transfer);
  54645. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepdma, diepdma);
  54646. + }
  54647. + i = core_if->nextep_seq[i];
  54648. + } while (i != core_if->first_in_nextep_seq);
  54649. + } else { // dma_desc_enable
  54650. + DWC_PRINTF("%s Learning Queue not supported in DDMA\n", __func__);
  54651. + }
  54652. +
  54653. + /* Restart transfers in predicted sequences */
  54654. + i = core_if->first_in_nextep_seq;
  54655. + do {
  54656. + dieptsiz.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->dieptsiz);
  54657. + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  54658. + if (dieptsiz.b.pktcnt != 0) {
  54659. + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  54660. + depctl.b.epena = 1;
  54661. + depctl.b.cnak = 1;
  54662. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepctl, depctl.d32);
  54663. + }
  54664. + i = core_if->nextep_seq[i];
  54665. + } while (i != core_if->first_in_nextep_seq);
  54666. +
  54667. + /* Clear the global non-periodic IN NAK handshake */
  54668. + dctl.d32 = 0;
  54669. + dctl.b.cgnpinnak = 1;
  54670. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, dctl.d32, dctl.d32);
  54671. +
  54672. + /* Unmask EP Mismatch interrupt */
  54673. + gintmsk_data.d32 = 0;
  54674. + gintmsk_data.b.epmismatch = 1;
  54675. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, 0, gintmsk_data.d32);
  54676. +
  54677. + core_if->start_predict = 0;
  54678. +
  54679. + }
  54680. +}
  54681. +
  54682. +/**
  54683. + * Handler for the IN EP timeout handshake interrupt.
  54684. + */
  54685. +static inline void handle_in_ep_timeout_intr(dwc_otg_pcd_t * pcd,
  54686. + const uint32_t epnum)
  54687. +{
  54688. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  54689. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  54690. +
  54691. +#ifdef DEBUG
  54692. + deptsiz_data_t dieptsiz = {.d32 = 0 };
  54693. + uint32_t num = 0;
  54694. +#endif
  54695. + dctl_data_t dctl = {.d32 = 0 };
  54696. + dwc_otg_pcd_ep_t *ep;
  54697. +
  54698. + gintmsk_data_t intr_mask = {.d32 = 0 };
  54699. +
  54700. + ep = get_in_ep(pcd, epnum);
  54701. +
  54702. + /* Disable the NP Tx Fifo Empty Interrrupt */
  54703. + if (!core_if->dma_enable) {
  54704. + intr_mask.b.nptxfempty = 1;
  54705. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk,
  54706. + intr_mask.d32, 0);
  54707. + }
  54708. + /** @todo NGS Check EP type.
  54709. + * Implement for Periodic EPs */
  54710. + /*
  54711. + * Non-periodic EP
  54712. + */
  54713. + /* Enable the Global IN NAK Effective Interrupt */
  54714. + intr_mask.b.ginnakeff = 1;
  54715. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk, 0, intr_mask.d32);
  54716. +
  54717. + /* Set Global IN NAK */
  54718. + dctl.b.sgnpinnak = 1;
  54719. + DWC_MODIFY_REG32(&dev_if->dev_global_regs->dctl, dctl.d32, dctl.d32);
  54720. +
  54721. + ep->stopped = 1;
  54722. +
  54723. +#ifdef DEBUG
  54724. + dieptsiz.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[num]->dieptsiz);
  54725. + DWC_DEBUGPL(DBG_ANY, "pktcnt=%d size=%d\n",
  54726. + dieptsiz.b.pktcnt, dieptsiz.b.xfersize);
  54727. +#endif
  54728. +
  54729. +#ifdef DISABLE_PERIODIC_EP
  54730. + /*
  54731. + * Set the NAK bit for this EP to
  54732. + * start the disable process.
  54733. + */
  54734. + diepctl.d32 = 0;
  54735. + diepctl.b.snak = 1;
  54736. + DWC_MODIFY_REG32(&dev_if->in_ep_regs[num]->diepctl, diepctl.d32,
  54737. + diepctl.d32);
  54738. + ep->disabling = 1;
  54739. + ep->stopped = 1;
  54740. +#endif
  54741. +}
  54742. +
  54743. +/**
  54744. + * Handler for the IN EP NAK interrupt.
  54745. + */
  54746. +static inline int32_t handle_in_ep_nak_intr(dwc_otg_pcd_t * pcd,
  54747. + const uint32_t epnum)
  54748. +{
  54749. + /** @todo implement ISR */
  54750. + dwc_otg_core_if_t *core_if;
  54751. + diepmsk_data_t intr_mask = {.d32 = 0 };
  54752. +
  54753. + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n", "IN EP NAK");
  54754. + core_if = GET_CORE_IF(pcd);
  54755. + intr_mask.b.nak = 1;
  54756. +
  54757. + if (core_if->multiproc_int_enable) {
  54758. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  54759. + diepeachintmsk[epnum], intr_mask.d32, 0);
  54760. + } else {
  54761. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->diepmsk,
  54762. + intr_mask.d32, 0);
  54763. + }
  54764. +
  54765. + return 1;
  54766. +}
  54767. +
  54768. +/**
  54769. + * Handler for the OUT EP Babble interrupt.
  54770. + */
  54771. +static inline int32_t handle_out_ep_babble_intr(dwc_otg_pcd_t * pcd,
  54772. + const uint32_t epnum)
  54773. +{
  54774. + /** @todo implement ISR */
  54775. + dwc_otg_core_if_t *core_if;
  54776. + doepmsk_data_t intr_mask = {.d32 = 0 };
  54777. +
  54778. + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n",
  54779. + "OUT EP Babble");
  54780. + core_if = GET_CORE_IF(pcd);
  54781. + intr_mask.b.babble = 1;
  54782. +
  54783. + if (core_if->multiproc_int_enable) {
  54784. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  54785. + doepeachintmsk[epnum], intr_mask.d32, 0);
  54786. + } else {
  54787. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->doepmsk,
  54788. + intr_mask.d32, 0);
  54789. + }
  54790. +
  54791. + return 1;
  54792. +}
  54793. +
  54794. +/**
  54795. + * Handler for the OUT EP NAK interrupt.
  54796. + */
  54797. +static inline int32_t handle_out_ep_nak_intr(dwc_otg_pcd_t * pcd,
  54798. + const uint32_t epnum)
  54799. +{
  54800. + /** @todo implement ISR */
  54801. + dwc_otg_core_if_t *core_if;
  54802. + doepmsk_data_t intr_mask = {.d32 = 0 };
  54803. +
  54804. + DWC_DEBUGPL(DBG_ANY, "INTERRUPT Handler not implemented for %s\n", "OUT EP NAK");
  54805. + core_if = GET_CORE_IF(pcd);
  54806. + intr_mask.b.nak = 1;
  54807. +
  54808. + if (core_if->multiproc_int_enable) {
  54809. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  54810. + doepeachintmsk[epnum], intr_mask.d32, 0);
  54811. + } else {
  54812. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->doepmsk,
  54813. + intr_mask.d32, 0);
  54814. + }
  54815. +
  54816. + return 1;
  54817. +}
  54818. +
  54819. +/**
  54820. + * Handler for the OUT EP NYET interrupt.
  54821. + */
  54822. +static inline int32_t handle_out_ep_nyet_intr(dwc_otg_pcd_t * pcd,
  54823. + const uint32_t epnum)
  54824. +{
  54825. + /** @todo implement ISR */
  54826. + dwc_otg_core_if_t *core_if;
  54827. + doepmsk_data_t intr_mask = {.d32 = 0 };
  54828. +
  54829. + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n", "OUT EP NYET");
  54830. + core_if = GET_CORE_IF(pcd);
  54831. + intr_mask.b.nyet = 1;
  54832. +
  54833. + if (core_if->multiproc_int_enable) {
  54834. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->
  54835. + doepeachintmsk[epnum], intr_mask.d32, 0);
  54836. + } else {
  54837. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->doepmsk,
  54838. + intr_mask.d32, 0);
  54839. + }
  54840. +
  54841. + return 1;
  54842. +}
  54843. +
  54844. +/**
  54845. + * This interrupt indicates that an IN EP has a pending Interrupt.
  54846. + * The sequence for handling the IN EP interrupt is shown below:
  54847. + * -# Read the Device All Endpoint Interrupt register
  54848. + * -# Repeat the following for each IN EP interrupt bit set (from
  54849. + * LSB to MSB).
  54850. + * -# Read the Device Endpoint Interrupt (DIEPINTn) register
  54851. + * -# If "Transfer Complete" call the request complete function
  54852. + * -# If "Endpoint Disabled" complete the EP disable procedure.
  54853. + * -# If "AHB Error Interrupt" log error
  54854. + * -# If "Time-out Handshake" log error
  54855. + * -# If "IN Token Received when TxFIFO Empty" write packet to Tx
  54856. + * FIFO.
  54857. + * -# If "IN Token EP Mismatch" (disable, this is handled by EP
  54858. + * Mismatch Interrupt)
  54859. + */
  54860. +static int32_t dwc_otg_pcd_handle_in_ep_intr(dwc_otg_pcd_t * pcd)
  54861. +{
  54862. +#define CLEAR_IN_EP_INTR(__core_if,__epnum,__intr) \
  54863. +do { \
  54864. + diepint_data_t diepint = {.d32=0}; \
  54865. + diepint.b.__intr = 1; \
  54866. + DWC_WRITE_REG32(&__core_if->dev_if->in_ep_regs[__epnum]->diepint, \
  54867. + diepint.d32); \
  54868. +} while (0)
  54869. +
  54870. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  54871. + dwc_otg_dev_if_t *dev_if = core_if->dev_if;
  54872. + diepint_data_t diepint = {.d32 = 0 };
  54873. + depctl_data_t depctl = {.d32 = 0 };
  54874. + uint32_t ep_intr;
  54875. + uint32_t epnum = 0;
  54876. + dwc_otg_pcd_ep_t *ep;
  54877. + dwc_ep_t *dwc_ep;
  54878. + gintmsk_data_t intr_mask = {.d32 = 0 };
  54879. +
  54880. + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, pcd);
  54881. +
  54882. + /* Read in the device interrupt bits */
  54883. + ep_intr = dwc_otg_read_dev_all_in_ep_intr(core_if);
  54884. +
  54885. + /* Service the Device IN interrupts for each endpoint */
  54886. + while (ep_intr) {
  54887. + if (ep_intr & 0x1) {
  54888. + uint32_t empty_msk;
  54889. + /* Get EP pointer */
  54890. + ep = get_in_ep(pcd, epnum);
  54891. + dwc_ep = &ep->dwc_ep;
  54892. +
  54893. + depctl.d32 =
  54894. + DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->diepctl);
  54895. + empty_msk =
  54896. + DWC_READ_REG32(&dev_if->
  54897. + dev_global_regs->dtknqr4_fifoemptymsk);
  54898. +
  54899. + DWC_DEBUGPL(DBG_PCDV,
  54900. + "IN EP INTERRUPT - %d\nepmty_msk - %8x diepctl - %8x\n",
  54901. + epnum, empty_msk, depctl.d32);
  54902. +
  54903. + DWC_DEBUGPL(DBG_PCD,
  54904. + "EP%d-%s: type=%d, mps=%d\n",
  54905. + dwc_ep->num, (dwc_ep->is_in ? "IN" : "OUT"),
  54906. + dwc_ep->type, dwc_ep->maxpacket);
  54907. +
  54908. + diepint.d32 =
  54909. + dwc_otg_read_dev_in_ep_intr(core_if, dwc_ep);
  54910. +
  54911. + DWC_DEBUGPL(DBG_PCDV,
  54912. + "EP %d Interrupt Register - 0x%x\n", epnum,
  54913. + diepint.d32);
  54914. + /* Transfer complete */
  54915. + if (diepint.b.xfercompl) {
  54916. + /* Disable the NP Tx FIFO Empty
  54917. + * Interrupt */
  54918. + if (core_if->en_multiple_tx_fifo == 0) {
  54919. + intr_mask.b.nptxfempty = 1;
  54920. + DWC_MODIFY_REG32
  54921. + (&core_if->core_global_regs->gintmsk,
  54922. + intr_mask.d32, 0);
  54923. + } else {
  54924. + /* Disable the Tx FIFO Empty Interrupt for this EP */
  54925. + uint32_t fifoemptymsk =
  54926. + 0x1 << dwc_ep->num;
  54927. + DWC_MODIFY_REG32(&core_if->
  54928. + dev_if->dev_global_regs->dtknqr4_fifoemptymsk,
  54929. + fifoemptymsk, 0);
  54930. + }
  54931. + /* Clear the bit in DIEPINTn for this interrupt */
  54932. + CLEAR_IN_EP_INTR(core_if, epnum, xfercompl);
  54933. +
  54934. + /* Complete the transfer */
  54935. + if (epnum == 0) {
  54936. + handle_ep0(pcd);
  54937. + }
  54938. +#ifdef DWC_EN_ISOC
  54939. + else if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
  54940. + if (!ep->stopped)
  54941. + complete_iso_ep(pcd, ep);
  54942. + }
  54943. +#endif /* DWC_EN_ISOC */
  54944. +#ifdef DWC_UTE_PER_IO
  54945. + else if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
  54946. + if (!ep->stopped)
  54947. + complete_xiso_ep(ep);
  54948. + }
  54949. +#endif /* DWC_UTE_PER_IO */
  54950. + else {
  54951. + if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC &&
  54952. + dwc_ep->bInterval > 1) {
  54953. + dwc_ep->frame_num += dwc_ep->bInterval;
  54954. + if (dwc_ep->frame_num > 0x3FFF)
  54955. + {
  54956. + dwc_ep->frm_overrun = 1;
  54957. + dwc_ep->frame_num &= 0x3FFF;
  54958. + } else
  54959. + dwc_ep->frm_overrun = 0;
  54960. + }
  54961. + complete_ep(ep);
  54962. + if(diepint.b.nak)
  54963. + CLEAR_IN_EP_INTR(core_if, epnum, nak);
  54964. + }
  54965. + }
  54966. + /* Endpoint disable */
  54967. + if (diepint.b.epdisabled) {
  54968. + DWC_DEBUGPL(DBG_ANY, "EP%d IN disabled\n",
  54969. + epnum);
  54970. + handle_in_ep_disable_intr(pcd, epnum);
  54971. +
  54972. + /* Clear the bit in DIEPINTn for this interrupt */
  54973. + CLEAR_IN_EP_INTR(core_if, epnum, epdisabled);
  54974. + }
  54975. + /* AHB Error */
  54976. + if (diepint.b.ahberr) {
  54977. + DWC_ERROR("EP%d IN AHB Error\n", epnum);
  54978. + /* Clear the bit in DIEPINTn for this interrupt */
  54979. + CLEAR_IN_EP_INTR(core_if, epnum, ahberr);
  54980. + }
  54981. + /* TimeOUT Handshake (non-ISOC IN EPs) */
  54982. + if (diepint.b.timeout) {
  54983. + DWC_ERROR("EP%d IN Time-out\n", epnum);
  54984. + handle_in_ep_timeout_intr(pcd, epnum);
  54985. +
  54986. + CLEAR_IN_EP_INTR(core_if, epnum, timeout);
  54987. + }
  54988. + /** IN Token received with TxF Empty */
  54989. + if (diepint.b.intktxfemp) {
  54990. + DWC_DEBUGPL(DBG_ANY,
  54991. + "EP%d IN TKN TxFifo Empty\n",
  54992. + epnum);
  54993. + if (!ep->stopped && epnum != 0) {
  54994. +
  54995. + diepmsk_data_t diepmsk = {.d32 = 0 };
  54996. + diepmsk.b.intktxfemp = 1;
  54997. +
  54998. + if (core_if->multiproc_int_enable) {
  54999. + DWC_MODIFY_REG32
  55000. + (&dev_if->dev_global_regs->diepeachintmsk
  55001. + [epnum], diepmsk.d32, 0);
  55002. + } else {
  55003. + DWC_MODIFY_REG32
  55004. + (&dev_if->dev_global_regs->diepmsk,
  55005. + diepmsk.d32, 0);
  55006. + }
  55007. + } else if (core_if->dma_desc_enable
  55008. + && epnum == 0
  55009. + && pcd->ep0state ==
  55010. + EP0_OUT_STATUS_PHASE) {
  55011. + // EP0 IN set STALL
  55012. + depctl.d32 =
  55013. + DWC_READ_REG32(&dev_if->in_ep_regs
  55014. + [epnum]->diepctl);
  55015. +
  55016. + /* set the disable and stall bits */
  55017. + if (depctl.b.epena) {
  55018. + depctl.b.epdis = 1;
  55019. + }
  55020. + depctl.b.stall = 1;
  55021. + DWC_WRITE_REG32(&dev_if->in_ep_regs
  55022. + [epnum]->diepctl,
  55023. + depctl.d32);
  55024. + }
  55025. + CLEAR_IN_EP_INTR(core_if, epnum, intktxfemp);
  55026. + }
  55027. + /** IN Token Received with EP mismatch */
  55028. + if (diepint.b.intknepmis) {
  55029. + DWC_DEBUGPL(DBG_ANY,
  55030. + "EP%d IN TKN EP Mismatch\n", epnum);
  55031. + CLEAR_IN_EP_INTR(core_if, epnum, intknepmis);
  55032. + }
  55033. + /** IN Endpoint NAK Effective */
  55034. + if (diepint.b.inepnakeff) {
  55035. + DWC_DEBUGPL(DBG_ANY,
  55036. + "EP%d IN EP NAK Effective\n",
  55037. + epnum);
  55038. + /* Periodic EP */
  55039. + if (ep->disabling) {
  55040. + depctl.d32 = 0;
  55041. + depctl.b.snak = 1;
  55042. + depctl.b.epdis = 1;
  55043. + DWC_MODIFY_REG32(&dev_if->in_ep_regs
  55044. + [epnum]->diepctl,
  55045. + depctl.d32,
  55046. + depctl.d32);
  55047. + }
  55048. + CLEAR_IN_EP_INTR(core_if, epnum, inepnakeff);
  55049. +
  55050. + }
  55051. +
  55052. + /** IN EP Tx FIFO Empty Intr */
  55053. + if (diepint.b.emptyintr) {
  55054. + DWC_DEBUGPL(DBG_ANY,
  55055. + "EP%d Tx FIFO Empty Intr \n",
  55056. + epnum);
  55057. + write_empty_tx_fifo(pcd, epnum);
  55058. +
  55059. + CLEAR_IN_EP_INTR(core_if, epnum, emptyintr);
  55060. +
  55061. + }
  55062. +
  55063. + /** IN EP BNA Intr */
  55064. + if (diepint.b.bna) {
  55065. + CLEAR_IN_EP_INTR(core_if, epnum, bna);
  55066. + if (core_if->dma_desc_enable) {
  55067. +#ifdef DWC_EN_ISOC
  55068. + if (dwc_ep->type ==
  55069. + DWC_OTG_EP_TYPE_ISOC) {
  55070. + /*
  55071. + * This checking is performed to prevent first "false" BNA
  55072. + * handling occuring right after reconnect
  55073. + */
  55074. + if (dwc_ep->next_frame !=
  55075. + 0xffffffff)
  55076. + dwc_otg_pcd_handle_iso_bna(ep);
  55077. + } else
  55078. +#endif /* DWC_EN_ISOC */
  55079. + {
  55080. + dwc_otg_pcd_handle_noniso_bna(ep);
  55081. + }
  55082. + }
  55083. + }
  55084. + /* NAK Interrutp */
  55085. + if (diepint.b.nak) {
  55086. + DWC_DEBUGPL(DBG_ANY, "EP%d IN NAK Interrupt\n",
  55087. + epnum);
  55088. + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
  55089. + depctl_data_t depctl;
  55090. + if (ep->dwc_ep.frame_num == 0xFFFFFFFF) {
  55091. + ep->dwc_ep.frame_num = core_if->frame_num;
  55092. + if (ep->dwc_ep.bInterval > 1) {
  55093. + depctl.d32 = 0;
  55094. + depctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[epnum]->diepctl);
  55095. + if (ep->dwc_ep.frame_num & 0x1) {
  55096. + depctl.b.setd1pid = 1;
  55097. + depctl.b.setd0pid = 0;
  55098. + } else {
  55099. + depctl.b.setd0pid = 1;
  55100. + depctl.b.setd1pid = 0;
  55101. + }
  55102. + DWC_WRITE_REG32(&dev_if->in_ep_regs[epnum]->diepctl, depctl.d32);
  55103. + }
  55104. + start_next_request(ep);
  55105. + }
  55106. + ep->dwc_ep.frame_num += ep->dwc_ep.bInterval;
  55107. + if (dwc_ep->frame_num > 0x3FFF) {
  55108. + dwc_ep->frm_overrun = 1;
  55109. + dwc_ep->frame_num &= 0x3FFF;
  55110. + } else
  55111. + dwc_ep->frm_overrun = 0;
  55112. + }
  55113. +
  55114. + CLEAR_IN_EP_INTR(core_if, epnum, nak);
  55115. + }
  55116. + }
  55117. + epnum++;
  55118. + ep_intr >>= 1;
  55119. + }
  55120. +
  55121. + return 1;
  55122. +#undef CLEAR_IN_EP_INTR
  55123. +}
  55124. +
  55125. +/**
  55126. + * This interrupt indicates that an OUT EP has a pending Interrupt.
  55127. + * The sequence for handling the OUT EP interrupt is shown below:
  55128. + * -# Read the Device All Endpoint Interrupt register
  55129. + * -# Repeat the following for each OUT EP interrupt bit set (from
  55130. + * LSB to MSB).
  55131. + * -# Read the Device Endpoint Interrupt (DOEPINTn) register
  55132. + * -# If "Transfer Complete" call the request complete function
  55133. + * -# If "Endpoint Disabled" complete the EP disable procedure.
  55134. + * -# If "AHB Error Interrupt" log error
  55135. + * -# If "Setup Phase Done" process Setup Packet (See Standard USB
  55136. + * Command Processing)
  55137. + */
  55138. +static int32_t dwc_otg_pcd_handle_out_ep_intr(dwc_otg_pcd_t * pcd)
  55139. +{
  55140. +#define CLEAR_OUT_EP_INTR(__core_if,__epnum,__intr) \
  55141. +do { \
  55142. + doepint_data_t doepint = {.d32=0}; \
  55143. + doepint.b.__intr = 1; \
  55144. + DWC_WRITE_REG32(&__core_if->dev_if->out_ep_regs[__epnum]->doepint, \
  55145. + doepint.d32); \
  55146. +} while (0)
  55147. +
  55148. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  55149. + uint32_t ep_intr;
  55150. + doepint_data_t doepint = {.d32 = 0 };
  55151. + uint32_t epnum = 0;
  55152. + dwc_otg_pcd_ep_t *ep;
  55153. + dwc_ep_t *dwc_ep;
  55154. + dctl_data_t dctl = {.d32 = 0 };
  55155. + gintmsk_data_t gintmsk = {.d32 = 0 };
  55156. +
  55157. +
  55158. + DWC_DEBUGPL(DBG_PCDV, "%s()\n", __func__);
  55159. +
  55160. + /* Read in the device interrupt bits */
  55161. + ep_intr = dwc_otg_read_dev_all_out_ep_intr(core_if);
  55162. +
  55163. + while (ep_intr) {
  55164. + if (ep_intr & 0x1) {
  55165. + /* Get EP pointer */
  55166. + ep = get_out_ep(pcd, epnum);
  55167. + dwc_ep = &ep->dwc_ep;
  55168. +
  55169. +#ifdef VERBOSE
  55170. + DWC_DEBUGPL(DBG_PCDV,
  55171. + "EP%d-%s: type=%d, mps=%d\n",
  55172. + dwc_ep->num, (dwc_ep->is_in ? "IN" : "OUT"),
  55173. + dwc_ep->type, dwc_ep->maxpacket);
  55174. +#endif
  55175. + doepint.d32 =
  55176. + dwc_otg_read_dev_out_ep_intr(core_if, dwc_ep);
  55177. + /* Moved this interrupt upper due to core deffect of asserting
  55178. + * OUT EP 0 xfercompl along with stsphsrcvd in BDMA */
  55179. + if (doepint.b.stsphsercvd) {
  55180. + deptsiz0_data_t deptsiz;
  55181. + CLEAR_OUT_EP_INTR(core_if, epnum, stsphsercvd);
  55182. + deptsiz.d32 =
  55183. + DWC_READ_REG32(&core_if->dev_if->
  55184. + out_ep_regs[0]->doeptsiz);
  55185. + if (core_if->snpsid >= OTG_CORE_REV_3_00a
  55186. + && core_if->dma_enable
  55187. + && core_if->dma_desc_enable == 0
  55188. + && doepint.b.xfercompl
  55189. + && deptsiz.b.xfersize == 24) {
  55190. + CLEAR_OUT_EP_INTR(core_if, epnum,
  55191. + xfercompl);
  55192. + doepint.b.xfercompl = 0;
  55193. + ep0_out_start(core_if, pcd);
  55194. + }
  55195. + if ((core_if->dma_desc_enable) ||
  55196. + (core_if->dma_enable
  55197. + && core_if->snpsid >=
  55198. + OTG_CORE_REV_3_00a)) {
  55199. + do_setup_in_status_phase(pcd);
  55200. + }
  55201. + }
  55202. + /* Transfer complete */
  55203. + if (doepint.b.xfercompl) {
  55204. +
  55205. + if (epnum == 0) {
  55206. + /* Clear the bit in DOEPINTn for this interrupt */
  55207. + CLEAR_OUT_EP_INTR(core_if, epnum, xfercompl);
  55208. + if (core_if->snpsid >= OTG_CORE_REV_3_00a) {
  55209. + DWC_DEBUGPL(DBG_PCDV, "DOEPINT=%x doepint=%x\n",
  55210. + DWC_READ_REG32(&core_if->dev_if->out_ep_regs[0]->doepint),
  55211. + doepint.d32);
  55212. + DWC_DEBUGPL(DBG_PCDV, "DOEPCTL=%x \n",
  55213. + DWC_READ_REG32(&core_if->dev_if->out_ep_regs[0]->doepctl));
  55214. +
  55215. + if (core_if->snpsid >= OTG_CORE_REV_3_00a
  55216. + && core_if->dma_enable == 0) {
  55217. + doepint_data_t doepint;
  55218. + doepint.d32 = DWC_READ_REG32(&core_if->dev_if->
  55219. + out_ep_regs[0]->doepint);
  55220. + if (pcd->ep0state == EP0_IDLE && doepint.b.sr) {
  55221. + CLEAR_OUT_EP_INTR(core_if, epnum, sr);
  55222. + goto exit_xfercompl;
  55223. + }
  55224. + }
  55225. + /* In case of DDMA look at SR bit to go to the Data Stage */
  55226. + if (core_if->dma_desc_enable) {
  55227. + dev_dma_desc_sts_t status = {.d32 = 0};
  55228. + if (pcd->ep0state == EP0_IDLE) {
  55229. + status.d32 = core_if->dev_if->setup_desc_addr[core_if->
  55230. + dev_if->setup_desc_index]->status.d32;
  55231. + if(pcd->data_terminated) {
  55232. + pcd->data_terminated = 0;
  55233. + status.d32 = core_if->dev_if->out_desc_addr->status.d32;
  55234. + dwc_memcpy(&pcd->setup_pkt->req, pcd->backup_buf, 8);
  55235. + }
  55236. + if (status.b.sr) {
  55237. + if (doepint.b.setup) {
  55238. + DWC_DEBUGPL(DBG_PCDV, "DMA DESC EP0_IDLE SR=1 setup=1\n");
  55239. + /* Already started data stage, clear setup */
  55240. + CLEAR_OUT_EP_INTR(core_if, epnum, setup);
  55241. + doepint.b.setup = 0;
  55242. + handle_ep0(pcd);
  55243. + /* Prepare for more setup packets */
  55244. + if (pcd->ep0state == EP0_IN_STATUS_PHASE ||
  55245. + pcd->ep0state == EP0_IN_DATA_PHASE) {
  55246. + ep0_out_start(core_if, pcd);
  55247. + }
  55248. +
  55249. + goto exit_xfercompl;
  55250. + } else {
  55251. + /* Prepare for more setup packets */
  55252. + DWC_DEBUGPL(DBG_PCDV,
  55253. + "EP0_IDLE SR=1 setup=0 new setup comes\n");
  55254. + ep0_out_start(core_if, pcd);
  55255. + }
  55256. + }
  55257. + } else {
  55258. + dwc_otg_pcd_request_t *req;
  55259. + dev_dma_desc_sts_t status = {.d32 = 0};
  55260. + diepint_data_t diepint0;
  55261. + diepint0.d32 = DWC_READ_REG32(&core_if->dev_if->
  55262. + in_ep_regs[0]->diepint);
  55263. +
  55264. + if (pcd->ep0state == EP0_STALL || pcd->ep0state == EP0_DISCONNECT) {
  55265. + DWC_ERROR("EP0 is stalled/disconnected\n");
  55266. + }
  55267. +
  55268. + /* Clear IN xfercompl if set */
  55269. + if (diepint0.b.xfercompl && (pcd->ep0state == EP0_IN_STATUS_PHASE
  55270. + || pcd->ep0state == EP0_IN_DATA_PHASE)) {
  55271. + DWC_WRITE_REG32(&core_if->dev_if->
  55272. + in_ep_regs[0]->diepint, diepint0.d32);
  55273. + }
  55274. +
  55275. + status.d32 = core_if->dev_if->setup_desc_addr[core_if->
  55276. + dev_if->setup_desc_index]->status.d32;
  55277. +
  55278. + if (ep->dwc_ep.xfer_count != ep->dwc_ep.total_len
  55279. + && (pcd->ep0state == EP0_OUT_DATA_PHASE))
  55280. + status.d32 = core_if->dev_if->out_desc_addr->status.d32;
  55281. + if (pcd->ep0state == EP0_OUT_STATUS_PHASE)
  55282. + status.d32 = core_if->dev_if->
  55283. + out_desc_addr->status.d32;
  55284. +
  55285. + if (status.b.sr) {
  55286. + if (DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  55287. + DWC_DEBUGPL(DBG_PCDV, "Request queue empty!!\n");
  55288. + } else {
  55289. + DWC_DEBUGPL(DBG_PCDV, "complete req!!\n");
  55290. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  55291. + if (ep->dwc_ep.xfer_count != ep->dwc_ep.total_len &&
  55292. + pcd->ep0state == EP0_OUT_DATA_PHASE) {
  55293. + /* Read arrived setup packet from req->buf */
  55294. + dwc_memcpy(&pcd->setup_pkt->req,
  55295. + req->buf + ep->dwc_ep.xfer_count, 8);
  55296. + }
  55297. + req->actual = ep->dwc_ep.xfer_count;
  55298. + dwc_otg_request_done(ep, req, -ECONNRESET);
  55299. + ep->dwc_ep.start_xfer_buff = 0;
  55300. + ep->dwc_ep.xfer_buff = 0;
  55301. + ep->dwc_ep.xfer_len = 0;
  55302. + }
  55303. + pcd->ep0state = EP0_IDLE;
  55304. + if (doepint.b.setup) {
  55305. + DWC_DEBUGPL(DBG_PCDV, "EP0_IDLE SR=1 setup=1\n");
  55306. + /* Data stage started, clear setup */
  55307. + CLEAR_OUT_EP_INTR(core_if, epnum, setup);
  55308. + doepint.b.setup = 0;
  55309. + handle_ep0(pcd);
  55310. + /* Prepare for setup packets if ep0in was enabled*/
  55311. + if (pcd->ep0state == EP0_IN_STATUS_PHASE) {
  55312. + ep0_out_start(core_if, pcd);
  55313. + }
  55314. +
  55315. + goto exit_xfercompl;
  55316. + } else {
  55317. + /* Prepare for more setup packets */
  55318. + DWC_DEBUGPL(DBG_PCDV,
  55319. + "EP0_IDLE SR=1 setup=0 new setup comes 2\n");
  55320. + ep0_out_start(core_if, pcd);
  55321. + }
  55322. + }
  55323. + }
  55324. + }
  55325. + if (core_if->snpsid >= OTG_CORE_REV_2_94a && core_if->dma_enable
  55326. + && core_if->dma_desc_enable == 0) {
  55327. + doepint_data_t doepint_temp = {.d32 = 0};
  55328. + deptsiz0_data_t doeptsize0 = {.d32 = 0 };
  55329. + doepint_temp.d32 = DWC_READ_REG32(&core_if->dev_if->
  55330. + out_ep_regs[ep->dwc_ep.num]->doepint);
  55331. + doeptsize0.d32 = DWC_READ_REG32(&core_if->dev_if->
  55332. + out_ep_regs[ep->dwc_ep.num]->doeptsiz);
  55333. + if (pcd->ep0state == EP0_IDLE) {
  55334. + if (doepint_temp.b.sr) {
  55335. + CLEAR_OUT_EP_INTR(core_if, epnum, sr);
  55336. + }
  55337. + doepint.d32 = DWC_READ_REG32(&core_if->dev_if->
  55338. + out_ep_regs[0]->doepint);
  55339. + if (doeptsize0.b.supcnt == 3) {
  55340. + DWC_DEBUGPL(DBG_ANY, "Rolling over!!!!!!!\n");
  55341. + ep->dwc_ep.stp_rollover = 1;
  55342. + }
  55343. + if (doepint.b.setup) {
  55344. +retry:
  55345. + /* Already started data stage, clear setup */
  55346. + CLEAR_OUT_EP_INTR(core_if, epnum, setup);
  55347. + doepint.b.setup = 0;
  55348. + handle_ep0(pcd);
  55349. + ep->dwc_ep.stp_rollover = 0;
  55350. + /* Prepare for more setup packets */
  55351. + if (pcd->ep0state == EP0_IN_STATUS_PHASE ||
  55352. + pcd->ep0state == EP0_IN_DATA_PHASE) {
  55353. + ep0_out_start(core_if, pcd);
  55354. + }
  55355. + goto exit_xfercompl;
  55356. + } else {
  55357. + /* Prepare for more setup packets */
  55358. + DWC_DEBUGPL(DBG_ANY,
  55359. + "EP0_IDLE SR=1 setup=0 new setup comes\n");
  55360. + doepint.d32 = DWC_READ_REG32(&core_if->dev_if->
  55361. + out_ep_regs[0]->doepint);
  55362. + if(doepint.b.setup)
  55363. + goto retry;
  55364. + ep0_out_start(core_if, pcd);
  55365. + }
  55366. + } else {
  55367. + dwc_otg_pcd_request_t *req;
  55368. + diepint_data_t diepint0 = {.d32 = 0};
  55369. + doepint_data_t doepint_temp = {.d32 = 0};
  55370. + depctl_data_t diepctl0;
  55371. + diepint0.d32 = DWC_READ_REG32(&core_if->dev_if->
  55372. + in_ep_regs[0]->diepint);
  55373. + diepctl0.d32 = DWC_READ_REG32(&core_if->dev_if->
  55374. + in_ep_regs[0]->diepctl);
  55375. +
  55376. + if (pcd->ep0state == EP0_IN_DATA_PHASE
  55377. + || pcd->ep0state == EP0_IN_STATUS_PHASE) {
  55378. + if (diepint0.b.xfercompl) {
  55379. + DWC_WRITE_REG32(&core_if->dev_if->
  55380. + in_ep_regs[0]->diepint, diepint0.d32);
  55381. + }
  55382. + if (diepctl0.b.epena) {
  55383. + diepint_data_t diepint = {.d32 = 0};
  55384. + diepctl0.b.snak = 1;
  55385. + DWC_WRITE_REG32(&core_if->dev_if->
  55386. + in_ep_regs[0]->diepctl, diepctl0.d32);
  55387. + do {
  55388. + dwc_udelay(10);
  55389. + diepint.d32 = DWC_READ_REG32(&core_if->dev_if->
  55390. + in_ep_regs[0]->diepint);
  55391. + } while (!diepint.b.inepnakeff);
  55392. + diepint.b.inepnakeff = 1;
  55393. + DWC_WRITE_REG32(&core_if->dev_if->
  55394. + in_ep_regs[0]->diepint, diepint.d32);
  55395. + diepctl0.d32 = 0;
  55396. + diepctl0.b.epdis = 1;
  55397. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[0]->diepctl,
  55398. + diepctl0.d32);
  55399. + do {
  55400. + dwc_udelay(10);
  55401. + diepint.d32 = DWC_READ_REG32(&core_if->dev_if->
  55402. + in_ep_regs[0]->diepint);
  55403. + } while (!diepint.b.epdisabled);
  55404. + diepint.b.epdisabled = 1;
  55405. + DWC_WRITE_REG32(&core_if->dev_if->in_ep_regs[0]->diepint,
  55406. + diepint.d32);
  55407. + }
  55408. + }
  55409. + doepint_temp.d32 = DWC_READ_REG32(&core_if->dev_if->
  55410. + out_ep_regs[ep->dwc_ep.num]->doepint);
  55411. + if (doepint_temp.b.sr) {
  55412. + CLEAR_OUT_EP_INTR(core_if, epnum, sr);
  55413. + if (DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  55414. + DWC_DEBUGPL(DBG_PCDV, "Request queue empty!!\n");
  55415. + } else {
  55416. + DWC_DEBUGPL(DBG_PCDV, "complete req!!\n");
  55417. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  55418. + if (ep->dwc_ep.xfer_count != ep->dwc_ep.total_len &&
  55419. + pcd->ep0state == EP0_OUT_DATA_PHASE) {
  55420. + /* Read arrived setup packet from req->buf */
  55421. + dwc_memcpy(&pcd->setup_pkt->req,
  55422. + req->buf + ep->dwc_ep.xfer_count, 8);
  55423. + }
  55424. + req->actual = ep->dwc_ep.xfer_count;
  55425. + dwc_otg_request_done(ep, req, -ECONNRESET);
  55426. + ep->dwc_ep.start_xfer_buff = 0;
  55427. + ep->dwc_ep.xfer_buff = 0;
  55428. + ep->dwc_ep.xfer_len = 0;
  55429. + }
  55430. + pcd->ep0state = EP0_IDLE;
  55431. + if (doepint.b.setup) {
  55432. + DWC_DEBUGPL(DBG_PCDV, "EP0_IDLE SR=1 setup=1\n");
  55433. + /* Data stage started, clear setup */
  55434. + CLEAR_OUT_EP_INTR(core_if, epnum, setup);
  55435. + doepint.b.setup = 0;
  55436. + handle_ep0(pcd);
  55437. + /* Prepare for setup packets if ep0in was enabled*/
  55438. + if (pcd->ep0state == EP0_IN_STATUS_PHASE) {
  55439. + ep0_out_start(core_if, pcd);
  55440. + }
  55441. + goto exit_xfercompl;
  55442. + } else {
  55443. + /* Prepare for more setup packets */
  55444. + DWC_DEBUGPL(DBG_PCDV,
  55445. + "EP0_IDLE SR=1 setup=0 new setup comes 2\n");
  55446. + ep0_out_start(core_if, pcd);
  55447. + }
  55448. + }
  55449. + }
  55450. + }
  55451. + if (core_if->dma_enable == 0 || pcd->ep0state != EP0_IDLE)
  55452. + handle_ep0(pcd);
  55453. +exit_xfercompl:
  55454. + DWC_DEBUGPL(DBG_PCDV, "DOEPINT=%x doepint=%x\n",
  55455. + dwc_otg_read_dev_out_ep_intr(core_if, dwc_ep), doepint.d32);
  55456. + } else {
  55457. + if (core_if->dma_desc_enable == 0
  55458. + || pcd->ep0state != EP0_IDLE)
  55459. + handle_ep0(pcd);
  55460. + }
  55461. +#ifdef DWC_EN_ISOC
  55462. + } else if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
  55463. + if (doepint.b.pktdrpsts == 0) {
  55464. + /* Clear the bit in DOEPINTn for this interrupt */
  55465. + CLEAR_OUT_EP_INTR(core_if,
  55466. + epnum,
  55467. + xfercompl);
  55468. + complete_iso_ep(pcd, ep);
  55469. + } else {
  55470. +
  55471. + doepint_data_t doepint = {.d32 = 0 };
  55472. + doepint.b.xfercompl = 1;
  55473. + doepint.b.pktdrpsts = 1;
  55474. + DWC_WRITE_REG32
  55475. + (&core_if->dev_if->out_ep_regs
  55476. + [epnum]->doepint,
  55477. + doepint.d32);
  55478. + if (handle_iso_out_pkt_dropped
  55479. + (core_if, dwc_ep)) {
  55480. + complete_iso_ep(pcd,
  55481. + ep);
  55482. + }
  55483. + }
  55484. +#endif /* DWC_EN_ISOC */
  55485. +#ifdef DWC_UTE_PER_IO
  55486. + } else if (dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
  55487. + CLEAR_OUT_EP_INTR(core_if, epnum, xfercompl);
  55488. + if (!ep->stopped)
  55489. + complete_xiso_ep(ep);
  55490. +#endif /* DWC_UTE_PER_IO */
  55491. + } else {
  55492. + /* Clear the bit in DOEPINTn for this interrupt */
  55493. + CLEAR_OUT_EP_INTR(core_if, epnum,
  55494. + xfercompl);
  55495. +
  55496. + if (core_if->core_params->dev_out_nak) {
  55497. + DWC_TIMER_CANCEL(pcd->core_if->ep_xfer_timer[epnum]);
  55498. + pcd->core_if->ep_xfer_info[epnum].state = 0;
  55499. +#ifdef DEBUG
  55500. + print_memory_payload(pcd, dwc_ep);
  55501. +#endif
  55502. + }
  55503. + complete_ep(ep);
  55504. + }
  55505. +
  55506. + }
  55507. +
  55508. + /* Endpoint disable */
  55509. + if (doepint.b.epdisabled) {
  55510. +
  55511. + /* Clear the bit in DOEPINTn for this interrupt */
  55512. + CLEAR_OUT_EP_INTR(core_if, epnum, epdisabled);
  55513. + if (core_if->core_params->dev_out_nak) {
  55514. +#ifdef DEBUG
  55515. + print_memory_payload(pcd, dwc_ep);
  55516. +#endif
  55517. + /* In case of timeout condition */
  55518. + if (core_if->ep_xfer_info[epnum].state == 2) {
  55519. + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->
  55520. + dev_global_regs->dctl);
  55521. + dctl.b.cgoutnak = 1;
  55522. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl,
  55523. + dctl.d32);
  55524. + /* Unmask goutnakeff interrupt which was masked
  55525. + * during handle nak out interrupt */
  55526. + gintmsk.b.goutnakeff = 1;
  55527. + DWC_MODIFY_REG32(&core_if->core_global_regs->gintmsk,
  55528. + 0, gintmsk.d32);
  55529. +
  55530. + complete_ep(ep);
  55531. + }
  55532. + }
  55533. + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC)
  55534. + {
  55535. + dctl_data_t dctl;
  55536. + gintmsk_data_t intr_mask = {.d32 = 0};
  55537. + dwc_otg_pcd_request_t *req = 0;
  55538. +
  55539. + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->
  55540. + dev_global_regs->dctl);
  55541. + dctl.b.cgoutnak = 1;
  55542. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl,
  55543. + dctl.d32);
  55544. +
  55545. + intr_mask.d32 = 0;
  55546. + intr_mask.b.incomplisoout = 1;
  55547. +
  55548. + /* Get any pending requests */
  55549. + if (!DWC_CIRCLEQ_EMPTY(&ep->queue)) {
  55550. + req = DWC_CIRCLEQ_FIRST(&ep->queue);
  55551. + if (!req) {
  55552. + DWC_PRINTF("complete_ep 0x%p, req = NULL!\n", ep);
  55553. + } else {
  55554. + dwc_otg_request_done(ep, req, 0);
  55555. + start_next_request(ep);
  55556. + }
  55557. + } else {
  55558. + DWC_PRINTF("complete_ep 0x%p, ep->queue empty!\n", ep);
  55559. + }
  55560. + }
  55561. + }
  55562. + /* AHB Error */
  55563. + if (doepint.b.ahberr) {
  55564. + DWC_ERROR("EP%d OUT AHB Error\n", epnum);
  55565. + DWC_ERROR("EP%d DEPDMA=0x%08x \n",
  55566. + epnum, core_if->dev_if->out_ep_regs[epnum]->doepdma);
  55567. + CLEAR_OUT_EP_INTR(core_if, epnum, ahberr);
  55568. + }
  55569. + /* Setup Phase Done (contorl EPs) */
  55570. + if (doepint.b.setup) {
  55571. +#ifdef DEBUG_EP0
  55572. + DWC_DEBUGPL(DBG_PCD, "EP%d SETUP Done\n", epnum);
  55573. +#endif
  55574. + CLEAR_OUT_EP_INTR(core_if, epnum, setup);
  55575. +
  55576. + handle_ep0(pcd);
  55577. + }
  55578. +
  55579. + /** OUT EP BNA Intr */
  55580. + if (doepint.b.bna) {
  55581. + CLEAR_OUT_EP_INTR(core_if, epnum, bna);
  55582. + if (core_if->dma_desc_enable) {
  55583. +#ifdef DWC_EN_ISOC
  55584. + if (dwc_ep->type ==
  55585. + DWC_OTG_EP_TYPE_ISOC) {
  55586. + /*
  55587. + * This checking is performed to prevent first "false" BNA
  55588. + * handling occuring right after reconnect
  55589. + */
  55590. + if (dwc_ep->next_frame !=
  55591. + 0xffffffff)
  55592. + dwc_otg_pcd_handle_iso_bna(ep);
  55593. + } else
  55594. +#endif /* DWC_EN_ISOC */
  55595. + {
  55596. + dwc_otg_pcd_handle_noniso_bna(ep);
  55597. + }
  55598. + }
  55599. + }
  55600. + /* Babble Interrupt */
  55601. + if (doepint.b.babble) {
  55602. + DWC_DEBUGPL(DBG_ANY, "EP%d OUT Babble\n",
  55603. + epnum);
  55604. + handle_out_ep_babble_intr(pcd, epnum);
  55605. +
  55606. + CLEAR_OUT_EP_INTR(core_if, epnum, babble);
  55607. + }
  55608. + if (doepint.b.outtknepdis) {
  55609. + DWC_DEBUGPL(DBG_ANY, "EP%d OUT Token received when EP is \
  55610. + disabled\n",epnum);
  55611. + if (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
  55612. + doepmsk_data_t doepmsk = {.d32 = 0};
  55613. + ep->dwc_ep.frame_num = core_if->frame_num;
  55614. + if (ep->dwc_ep.bInterval > 1) {
  55615. + depctl_data_t depctl;
  55616. + depctl.d32 = DWC_READ_REG32(&core_if->dev_if->
  55617. + out_ep_regs[epnum]->doepctl);
  55618. + if (ep->dwc_ep.frame_num & 0x1) {
  55619. + depctl.b.setd1pid = 1;
  55620. + depctl.b.setd0pid = 0;
  55621. + } else {
  55622. + depctl.b.setd0pid = 1;
  55623. + depctl.b.setd1pid = 0;
  55624. + }
  55625. + DWC_WRITE_REG32(&core_if->dev_if->
  55626. + out_ep_regs[epnum]->doepctl, depctl.d32);
  55627. + }
  55628. + start_next_request(ep);
  55629. + doepmsk.b.outtknepdis = 1;
  55630. + DWC_MODIFY_REG32(&core_if->dev_if->dev_global_regs->doepmsk,
  55631. + doepmsk.d32, 0);
  55632. + }
  55633. + CLEAR_OUT_EP_INTR(core_if, epnum, outtknepdis);
  55634. + }
  55635. +
  55636. + /* NAK Interrutp */
  55637. + if (doepint.b.nak) {
  55638. + DWC_DEBUGPL(DBG_ANY, "EP%d OUT NAK\n", epnum);
  55639. + handle_out_ep_nak_intr(pcd, epnum);
  55640. +
  55641. + CLEAR_OUT_EP_INTR(core_if, epnum, nak);
  55642. + }
  55643. + /* NYET Interrutp */
  55644. + if (doepint.b.nyet) {
  55645. + DWC_DEBUGPL(DBG_ANY, "EP%d OUT NYET\n", epnum);
  55646. + handle_out_ep_nyet_intr(pcd, epnum);
  55647. +
  55648. + CLEAR_OUT_EP_INTR(core_if, epnum, nyet);
  55649. + }
  55650. + }
  55651. +
  55652. + epnum++;
  55653. + ep_intr >>= 1;
  55654. + }
  55655. +
  55656. + return 1;
  55657. +
  55658. +#undef CLEAR_OUT_EP_INTR
  55659. +}
  55660. +static int drop_transfer(uint32_t trgt_fr, uint32_t curr_fr, uint8_t frm_overrun)
  55661. +{
  55662. + int retval = 0;
  55663. + if(!frm_overrun && curr_fr >= trgt_fr)
  55664. + retval = 1;
  55665. + else if (frm_overrun
  55666. + && (curr_fr >= trgt_fr && ((curr_fr - trgt_fr) < 0x3FFF / 2)))
  55667. + retval = 1;
  55668. + return retval;
  55669. +}
  55670. +/**
  55671. + * Incomplete ISO IN Transfer Interrupt.
  55672. + * This interrupt indicates one of the following conditions occurred
  55673. + * while transmitting an ISOC transaction.
  55674. + * - Corrupted IN Token for ISOC EP.
  55675. + * - Packet not complete in FIFO.
  55676. + * The follow actions will be taken:
  55677. + * -# Determine the EP
  55678. + * -# Set incomplete flag in dwc_ep structure
  55679. + * -# Disable EP; when "Endpoint Disabled" interrupt is received
  55680. + * Flush FIFO
  55681. + */
  55682. +int32_t dwc_otg_pcd_handle_incomplete_isoc_in_intr(dwc_otg_pcd_t * pcd)
  55683. +{
  55684. + gintsts_data_t gintsts;
  55685. +
  55686. +#ifdef DWC_EN_ISOC
  55687. + dwc_otg_dev_if_t *dev_if;
  55688. + deptsiz_data_t deptsiz = {.d32 = 0 };
  55689. + depctl_data_t depctl = {.d32 = 0 };
  55690. + dsts_data_t dsts = {.d32 = 0 };
  55691. + dwc_ep_t *dwc_ep;
  55692. + int i;
  55693. +
  55694. + dev_if = GET_CORE_IF(pcd)->dev_if;
  55695. +
  55696. + for (i = 1; i <= dev_if->num_in_eps; ++i) {
  55697. + dwc_ep = &pcd->in_ep[i].dwc_ep;
  55698. + if (dwc_ep->active && dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
  55699. + deptsiz.d32 =
  55700. + DWC_READ_REG32(&dev_if->in_ep_regs[i]->dieptsiz);
  55701. + depctl.d32 =
  55702. + DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  55703. +
  55704. + if (depctl.b.epdis && deptsiz.d32) {
  55705. + set_current_pkt_info(GET_CORE_IF(pcd), dwc_ep);
  55706. + if (dwc_ep->cur_pkt >= dwc_ep->pkt_cnt) {
  55707. + dwc_ep->cur_pkt = 0;
  55708. + dwc_ep->proc_buf_num =
  55709. + (dwc_ep->proc_buf_num ^ 1) & 0x1;
  55710. +
  55711. + if (dwc_ep->proc_buf_num) {
  55712. + dwc_ep->cur_pkt_addr =
  55713. + dwc_ep->xfer_buff1;
  55714. + dwc_ep->cur_pkt_dma_addr =
  55715. + dwc_ep->dma_addr1;
  55716. + } else {
  55717. + dwc_ep->cur_pkt_addr =
  55718. + dwc_ep->xfer_buff0;
  55719. + dwc_ep->cur_pkt_dma_addr =
  55720. + dwc_ep->dma_addr0;
  55721. + }
  55722. +
  55723. + }
  55724. +
  55725. + dsts.d32 =
  55726. + DWC_READ_REG32(&GET_CORE_IF(pcd)->dev_if->
  55727. + dev_global_regs->dsts);
  55728. + dwc_ep->next_frame = dsts.b.soffn;
  55729. +
  55730. + dwc_otg_iso_ep_start_frm_transfer(GET_CORE_IF
  55731. + (pcd),
  55732. + dwc_ep);
  55733. + }
  55734. + }
  55735. + }
  55736. +
  55737. +#else
  55738. + depctl_data_t depctl = {.d32 = 0 };
  55739. + dwc_ep_t *dwc_ep;
  55740. + dwc_otg_dev_if_t *dev_if;
  55741. + int i;
  55742. + dev_if = GET_CORE_IF(pcd)->dev_if;
  55743. +
  55744. + DWC_DEBUGPL(DBG_PCD,"Incomplete ISO IN \n");
  55745. +
  55746. + for (i = 1; i <= dev_if->num_in_eps; ++i) {
  55747. + dwc_ep = &pcd->in_ep[i-1].dwc_ep;
  55748. + depctl.d32 =
  55749. + DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  55750. + if (depctl.b.epena && dwc_ep->type == DWC_OTG_EP_TYPE_ISOC) {
  55751. + if (drop_transfer(dwc_ep->frame_num, GET_CORE_IF(pcd)->frame_num,
  55752. + dwc_ep->frm_overrun))
  55753. + {
  55754. + depctl.d32 =
  55755. + DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  55756. + depctl.b.snak = 1;
  55757. + depctl.b.epdis = 1;
  55758. + DWC_MODIFY_REG32(&dev_if->in_ep_regs[i]->diepctl, depctl.d32, depctl.d32);
  55759. + }
  55760. + }
  55761. + }
  55762. +
  55763. + /*intr_mask.b.incomplisoin = 1;
  55764. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
  55765. + intr_mask.d32, 0); */
  55766. +#endif //DWC_EN_ISOC
  55767. +
  55768. + /* Clear interrupt */
  55769. + gintsts.d32 = 0;
  55770. + gintsts.b.incomplisoin = 1;
  55771. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  55772. + gintsts.d32);
  55773. +
  55774. + return 1;
  55775. +}
  55776. +
  55777. +/**
  55778. + * Incomplete ISO OUT Transfer Interrupt.
  55779. + *
  55780. + * This interrupt indicates that the core has dropped an ISO OUT
  55781. + * packet. The following conditions can be the cause:
  55782. + * - FIFO Full, the entire packet would not fit in the FIFO.
  55783. + * - CRC Error
  55784. + * - Corrupted Token
  55785. + * The follow actions will be taken:
  55786. + * -# Determine the EP
  55787. + * -# Set incomplete flag in dwc_ep structure
  55788. + * -# Read any data from the FIFO
  55789. + * -# Disable EP. When "Endpoint Disabled" interrupt is received
  55790. + * re-enable EP.
  55791. + */
  55792. +int32_t dwc_otg_pcd_handle_incomplete_isoc_out_intr(dwc_otg_pcd_t * pcd)
  55793. +{
  55794. +
  55795. + gintsts_data_t gintsts;
  55796. +
  55797. +#ifdef DWC_EN_ISOC
  55798. + dwc_otg_dev_if_t *dev_if;
  55799. + deptsiz_data_t deptsiz = {.d32 = 0 };
  55800. + depctl_data_t depctl = {.d32 = 0 };
  55801. + dsts_data_t dsts = {.d32 = 0 };
  55802. + dwc_ep_t *dwc_ep;
  55803. + int i;
  55804. +
  55805. + dev_if = GET_CORE_IF(pcd)->dev_if;
  55806. +
  55807. + for (i = 1; i <= dev_if->num_out_eps; ++i) {
  55808. + dwc_ep = &pcd->in_ep[i].dwc_ep;
  55809. + if (pcd->out_ep[i].dwc_ep.active &&
  55810. + pcd->out_ep[i].dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) {
  55811. + deptsiz.d32 =
  55812. + DWC_READ_REG32(&dev_if->out_ep_regs[i]->doeptsiz);
  55813. + depctl.d32 =
  55814. + DWC_READ_REG32(&dev_if->out_ep_regs[i]->doepctl);
  55815. +
  55816. + if (depctl.b.epdis && deptsiz.d32) {
  55817. + set_current_pkt_info(GET_CORE_IF(pcd),
  55818. + &pcd->out_ep[i].dwc_ep);
  55819. + if (dwc_ep->cur_pkt >= dwc_ep->pkt_cnt) {
  55820. + dwc_ep->cur_pkt = 0;
  55821. + dwc_ep->proc_buf_num =
  55822. + (dwc_ep->proc_buf_num ^ 1) & 0x1;
  55823. +
  55824. + if (dwc_ep->proc_buf_num) {
  55825. + dwc_ep->cur_pkt_addr =
  55826. + dwc_ep->xfer_buff1;
  55827. + dwc_ep->cur_pkt_dma_addr =
  55828. + dwc_ep->dma_addr1;
  55829. + } else {
  55830. + dwc_ep->cur_pkt_addr =
  55831. + dwc_ep->xfer_buff0;
  55832. + dwc_ep->cur_pkt_dma_addr =
  55833. + dwc_ep->dma_addr0;
  55834. + }
  55835. +
  55836. + }
  55837. +
  55838. + dsts.d32 =
  55839. + DWC_READ_REG32(&GET_CORE_IF(pcd)->dev_if->
  55840. + dev_global_regs->dsts);
  55841. + dwc_ep->next_frame = dsts.b.soffn;
  55842. +
  55843. + dwc_otg_iso_ep_start_frm_transfer(GET_CORE_IF
  55844. + (pcd),
  55845. + dwc_ep);
  55846. + }
  55847. + }
  55848. + }
  55849. +#else
  55850. + /** @todo implement ISR */
  55851. + gintmsk_data_t intr_mask = {.d32 = 0 };
  55852. + dwc_otg_core_if_t *core_if;
  55853. + deptsiz_data_t deptsiz = {.d32 = 0 };
  55854. + depctl_data_t depctl = {.d32 = 0 };
  55855. + dctl_data_t dctl = {.d32 = 0 };
  55856. + dwc_ep_t *dwc_ep = NULL;
  55857. + int i;
  55858. + core_if = GET_CORE_IF(pcd);
  55859. +
  55860. + for (i = 0; i < core_if->dev_if->num_out_eps; ++i) {
  55861. + dwc_ep = &pcd->out_ep[i].dwc_ep;
  55862. + depctl.d32 =
  55863. + DWC_READ_REG32(&core_if->dev_if->out_ep_regs[dwc_ep->num]->doepctl);
  55864. + if (depctl.b.epena && depctl.b.dpid == (core_if->frame_num & 0x1)) {
  55865. + core_if->dev_if->isoc_ep = dwc_ep;
  55866. + deptsiz.d32 =
  55867. + DWC_READ_REG32(&core_if->dev_if->out_ep_regs[dwc_ep->num]->doeptsiz);
  55868. + break;
  55869. + }
  55870. + }
  55871. + dctl.d32 = DWC_READ_REG32(&core_if->dev_if->dev_global_regs->dctl);
  55872. + gintsts.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintsts);
  55873. + intr_mask.d32 = DWC_READ_REG32(&core_if->core_global_regs->gintmsk);
  55874. +
  55875. + if (!intr_mask.b.goutnakeff) {
  55876. + /* Unmask it */
  55877. + intr_mask.b.goutnakeff = 1;
  55878. + DWC_WRITE_REG32(&core_if->core_global_regs->gintmsk, intr_mask.d32);
  55879. + }
  55880. + if (!gintsts.b.goutnakeff) {
  55881. + dctl.b.sgoutnak = 1;
  55882. + }
  55883. + DWC_WRITE_REG32(&core_if->dev_if->dev_global_regs->dctl, dctl.d32);
  55884. +
  55885. + depctl.d32 = DWC_READ_REG32(&core_if->dev_if->out_ep_regs[dwc_ep->num]->doepctl);
  55886. + if (depctl.b.epena) {
  55887. + depctl.b.epdis = 1;
  55888. + depctl.b.snak = 1;
  55889. + }
  55890. + DWC_WRITE_REG32(&core_if->dev_if->out_ep_regs[dwc_ep->num]->doepctl, depctl.d32);
  55891. +
  55892. + intr_mask.d32 = 0;
  55893. + intr_mask.b.incomplisoout = 1;
  55894. +
  55895. +#endif /* DWC_EN_ISOC */
  55896. +
  55897. + /* Clear interrupt */
  55898. + gintsts.d32 = 0;
  55899. + gintsts.b.incomplisoout = 1;
  55900. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  55901. + gintsts.d32);
  55902. +
  55903. + return 1;
  55904. +}
  55905. +
  55906. +/**
  55907. + * This function handles the Global IN NAK Effective interrupt.
  55908. + *
  55909. + */
  55910. +int32_t dwc_otg_pcd_handle_in_nak_effective(dwc_otg_pcd_t * pcd)
  55911. +{
  55912. + dwc_otg_dev_if_t *dev_if = GET_CORE_IF(pcd)->dev_if;
  55913. + depctl_data_t diepctl = {.d32 = 0 };
  55914. + gintmsk_data_t intr_mask = {.d32 = 0 };
  55915. + gintsts_data_t gintsts;
  55916. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  55917. + int i;
  55918. +
  55919. + DWC_DEBUGPL(DBG_PCD, "Global IN NAK Effective\n");
  55920. +
  55921. + /* Disable all active IN EPs */
  55922. + for (i = 0; i <= dev_if->num_in_eps; i++) {
  55923. + diepctl.d32 = DWC_READ_REG32(&dev_if->in_ep_regs[i]->diepctl);
  55924. + if (!(diepctl.b.eptype & 1) && diepctl.b.epena) {
  55925. + if (core_if->start_predict > 0)
  55926. + core_if->start_predict++;
  55927. + diepctl.b.epdis = 1;
  55928. + diepctl.b.snak = 1;
  55929. + DWC_WRITE_REG32(&dev_if->in_ep_regs[i]->diepctl, diepctl.d32);
  55930. + }
  55931. + }
  55932. +
  55933. +
  55934. + /* Disable the Global IN NAK Effective Interrupt */
  55935. + intr_mask.b.ginnakeff = 1;
  55936. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
  55937. + intr_mask.d32, 0);
  55938. +
  55939. + /* Clear interrupt */
  55940. + gintsts.d32 = 0;
  55941. + gintsts.b.ginnakeff = 1;
  55942. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  55943. + gintsts.d32);
  55944. +
  55945. + return 1;
  55946. +}
  55947. +
  55948. +/**
  55949. + * OUT NAK Effective.
  55950. + *
  55951. + */
  55952. +int32_t dwc_otg_pcd_handle_out_nak_effective(dwc_otg_pcd_t * pcd)
  55953. +{
  55954. + dwc_otg_dev_if_t *dev_if = GET_CORE_IF(pcd)->dev_if;
  55955. + gintmsk_data_t intr_mask = {.d32 = 0 };
  55956. + gintsts_data_t gintsts;
  55957. + depctl_data_t doepctl;
  55958. + int i;
  55959. +
  55960. + /* Disable the Global OUT NAK Effective Interrupt */
  55961. + intr_mask.b.goutnakeff = 1;
  55962. + DWC_MODIFY_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintmsk,
  55963. + intr_mask.d32, 0);
  55964. +
  55965. + /* If DEV OUT NAK enabled*/
  55966. + if (pcd->core_if->core_params->dev_out_nak) {
  55967. + /* Run over all out endpoints to determine the ep number on
  55968. + * which the timeout has happened
  55969. + */
  55970. + for (i = 0; i <= dev_if->num_out_eps; i++) {
  55971. + if ( pcd->core_if->ep_xfer_info[i].state == 2 )
  55972. + break;
  55973. + }
  55974. + if (i > dev_if->num_out_eps) {
  55975. + dctl_data_t dctl;
  55976. + dctl.d32 =
  55977. + DWC_READ_REG32(&dev_if->dev_global_regs->dctl);
  55978. + dctl.b.cgoutnak = 1;
  55979. + DWC_WRITE_REG32(&dev_if->dev_global_regs->dctl,
  55980. + dctl.d32);
  55981. + goto out;
  55982. + }
  55983. +
  55984. + /* Disable the endpoint */
  55985. + doepctl.d32 = DWC_READ_REG32(&dev_if->out_ep_regs[i]->doepctl);
  55986. + if (doepctl.b.epena) {
  55987. + doepctl.b.epdis = 1;
  55988. + doepctl.b.snak = 1;
  55989. + }
  55990. + DWC_WRITE_REG32(&dev_if->out_ep_regs[i]->doepctl, doepctl.d32);
  55991. + return 1;
  55992. + }
  55993. + /* We come here from Incomplete ISO OUT handler */
  55994. + if (dev_if->isoc_ep) {
  55995. + dwc_ep_t *dwc_ep = (dwc_ep_t *)dev_if->isoc_ep;
  55996. + uint32_t epnum = dwc_ep->num;
  55997. + doepint_data_t doepint;
  55998. + doepint.d32 =
  55999. + DWC_READ_REG32(&dev_if->out_ep_regs[dwc_ep->num]->doepint);
  56000. + dev_if->isoc_ep = NULL;
  56001. + doepctl.d32 =
  56002. + DWC_READ_REG32(&dev_if->out_ep_regs[epnum]->doepctl);
  56003. + DWC_PRINTF("Before disable DOEPCTL = %08x\n", doepctl.d32);
  56004. + if (doepctl.b.epena) {
  56005. + doepctl.b.epdis = 1;
  56006. + doepctl.b.snak = 1;
  56007. + }
  56008. + DWC_WRITE_REG32(&dev_if->out_ep_regs[epnum]->doepctl,
  56009. + doepctl.d32);
  56010. + return 1;
  56011. + } else
  56012. + DWC_PRINTF("INTERRUPT Handler not implemented for %s\n",
  56013. + "Global OUT NAK Effective\n");
  56014. +
  56015. +out:
  56016. + /* Clear interrupt */
  56017. + gintsts.d32 = 0;
  56018. + gintsts.b.goutnakeff = 1;
  56019. + DWC_WRITE_REG32(&GET_CORE_IF(pcd)->core_global_regs->gintsts,
  56020. + gintsts.d32);
  56021. +
  56022. + return 1;
  56023. +}
  56024. +
  56025. +/**
  56026. + * PCD interrupt handler.
  56027. + *
  56028. + * The PCD handles the device interrupts. Many conditions can cause a
  56029. + * device interrupt. When an interrupt occurs, the device interrupt
  56030. + * service routine determines the cause of the interrupt and
  56031. + * dispatches handling to the appropriate function. These interrupt
  56032. + * handling functions are described below.
  56033. + *
  56034. + * All interrupt registers are processed from LSB to MSB.
  56035. + *
  56036. + */
  56037. +int32_t dwc_otg_pcd_handle_intr(dwc_otg_pcd_t * pcd)
  56038. +{
  56039. + dwc_otg_core_if_t *core_if = GET_CORE_IF(pcd);
  56040. +#ifdef VERBOSE
  56041. + dwc_otg_core_global_regs_t *global_regs = core_if->core_global_regs;
  56042. +#endif
  56043. + gintsts_data_t gintr_status;
  56044. + int32_t retval = 0;
  56045. +
  56046. + /* Exit from ISR if core is hibernated */
  56047. + if (core_if->hibernation_suspend == 1) {
  56048. + return retval;
  56049. + }
  56050. +#ifdef VERBOSE
  56051. + DWC_DEBUGPL(DBG_ANY, "%s() gintsts=%08x gintmsk=%08x\n",
  56052. + __func__,
  56053. + DWC_READ_REG32(&global_regs->gintsts),
  56054. + DWC_READ_REG32(&global_regs->gintmsk));
  56055. +#endif
  56056. +
  56057. + if (dwc_otg_is_device_mode(core_if)) {
  56058. + DWC_SPINLOCK(pcd->lock);
  56059. +#ifdef VERBOSE
  56060. + DWC_DEBUGPL(DBG_PCDV, "%s() gintsts=%08x gintmsk=%08x\n",
  56061. + __func__,
  56062. + DWC_READ_REG32(&global_regs->gintsts),
  56063. + DWC_READ_REG32(&global_regs->gintmsk));
  56064. +#endif
  56065. +
  56066. + gintr_status.d32 = dwc_otg_read_core_intr(core_if);
  56067. +
  56068. + DWC_DEBUGPL(DBG_PCDV, "%s: gintsts&gintmsk=%08x\n",
  56069. + __func__, gintr_status.d32);
  56070. +
  56071. + if (gintr_status.b.sofintr) {
  56072. + retval |= dwc_otg_pcd_handle_sof_intr(pcd);
  56073. + }
  56074. + if (gintr_status.b.rxstsqlvl) {
  56075. + retval |=
  56076. + dwc_otg_pcd_handle_rx_status_q_level_intr(pcd);
  56077. + }
  56078. + if (gintr_status.b.nptxfempty) {
  56079. + retval |= dwc_otg_pcd_handle_np_tx_fifo_empty_intr(pcd);
  56080. + }
  56081. + if (gintr_status.b.goutnakeff) {
  56082. + retval |= dwc_otg_pcd_handle_out_nak_effective(pcd);
  56083. + }
  56084. + if (gintr_status.b.i2cintr) {
  56085. + retval |= dwc_otg_pcd_handle_i2c_intr(pcd);
  56086. + }
  56087. + if (gintr_status.b.erlysuspend) {
  56088. + retval |= dwc_otg_pcd_handle_early_suspend_intr(pcd);
  56089. + }
  56090. + if (gintr_status.b.usbreset) {
  56091. + retval |= dwc_otg_pcd_handle_usb_reset_intr(pcd);
  56092. + }
  56093. + if (gintr_status.b.enumdone) {
  56094. + retval |= dwc_otg_pcd_handle_enum_done_intr(pcd);
  56095. + }
  56096. + if (gintr_status.b.isooutdrop) {
  56097. + retval |=
  56098. + dwc_otg_pcd_handle_isoc_out_packet_dropped_intr
  56099. + (pcd);
  56100. + }
  56101. + if (gintr_status.b.eopframe) {
  56102. + retval |=
  56103. + dwc_otg_pcd_handle_end_periodic_frame_intr(pcd);
  56104. + }
  56105. + if (gintr_status.b.inepint) {
  56106. + if (!core_if->multiproc_int_enable) {
  56107. + retval |= dwc_otg_pcd_handle_in_ep_intr(pcd);
  56108. + }
  56109. + }
  56110. + if (gintr_status.b.outepintr) {
  56111. + if (!core_if->multiproc_int_enable) {
  56112. + retval |= dwc_otg_pcd_handle_out_ep_intr(pcd);
  56113. + }
  56114. + }
  56115. + if (gintr_status.b.epmismatch) {
  56116. + retval |= dwc_otg_pcd_handle_ep_mismatch_intr(pcd);
  56117. + }
  56118. + if (gintr_status.b.fetsusp) {
  56119. + retval |= dwc_otg_pcd_handle_ep_fetsusp_intr(pcd);
  56120. + }
  56121. + if (gintr_status.b.ginnakeff) {
  56122. + retval |= dwc_otg_pcd_handle_in_nak_effective(pcd);
  56123. + }
  56124. + if (gintr_status.b.incomplisoin) {
  56125. + retval |=
  56126. + dwc_otg_pcd_handle_incomplete_isoc_in_intr(pcd);
  56127. + }
  56128. + if (gintr_status.b.incomplisoout) {
  56129. + retval |=
  56130. + dwc_otg_pcd_handle_incomplete_isoc_out_intr(pcd);
  56131. + }
  56132. +
  56133. + /* In MPI mode Device Endpoints interrupts are asserted
  56134. + * without setting outepintr and inepint bits set, so these
  56135. + * Interrupt handlers are called without checking these bit-fields
  56136. + */
  56137. + if (core_if->multiproc_int_enable) {
  56138. + retval |= dwc_otg_pcd_handle_in_ep_intr(pcd);
  56139. + retval |= dwc_otg_pcd_handle_out_ep_intr(pcd);
  56140. + }
  56141. +#ifdef VERBOSE
  56142. + DWC_DEBUGPL(DBG_PCDV, "%s() gintsts=%0x\n", __func__,
  56143. + DWC_READ_REG32(&global_regs->gintsts));
  56144. +#endif
  56145. + DWC_SPINUNLOCK(pcd->lock);
  56146. + }
  56147. + return retval;
  56148. +}
  56149. +
  56150. +#endif /* DWC_HOST_ONLY */
  56151. --- /dev/null
  56152. +++ b/drivers/usb/host/dwc_otg/dwc_otg_pcd_linux.c
  56153. @@ -0,0 +1,1280 @@
  56154. + /* ==========================================================================
  56155. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_pcd_linux.c $
  56156. + * $Revision: #21 $
  56157. + * $Date: 2012/08/10 $
  56158. + * $Change: 2047372 $
  56159. + *
  56160. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  56161. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  56162. + * otherwise expressly agreed to in writing between Synopsys and you.
  56163. + *
  56164. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  56165. + * any End User Software License Agreement or Agreement for Licensed Product
  56166. + * with Synopsys or any supplement thereto. You are permitted to use and
  56167. + * redistribute this Software in source and binary forms, with or without
  56168. + * modification, provided that redistributions of source code must retain this
  56169. + * notice. You may not view, use, disclose, copy or distribute this file or
  56170. + * any information contained herein except pursuant to this license grant from
  56171. + * Synopsys. If you do not agree with this notice, including the disclaimer
  56172. + * below, then you are not authorized to use the Software.
  56173. + *
  56174. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  56175. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  56176. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  56177. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  56178. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  56179. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  56180. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  56181. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  56182. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  56183. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  56184. + * DAMAGE.
  56185. + * ========================================================================== */
  56186. +#ifndef DWC_HOST_ONLY
  56187. +
  56188. +/** @file
  56189. + * This file implements the Peripheral Controller Driver.
  56190. + *
  56191. + * The Peripheral Controller Driver (PCD) is responsible for
  56192. + * translating requests from the Function Driver into the appropriate
  56193. + * actions on the DWC_otg controller. It isolates the Function Driver
  56194. + * from the specifics of the controller by providing an API to the
  56195. + * Function Driver.
  56196. + *
  56197. + * The Peripheral Controller Driver for Linux will implement the
  56198. + * Gadget API, so that the existing Gadget drivers can be used.
  56199. + * (Gadget Driver is the Linux terminology for a Function Driver.)
  56200. + *
  56201. + * The Linux Gadget API is defined in the header file
  56202. + * <code><linux/usb_gadget.h></code>. The USB EP operations API is
  56203. + * defined in the structure <code>usb_ep_ops</code> and the USB
  56204. + * Controller API is defined in the structure
  56205. + * <code>usb_gadget_ops</code>.
  56206. + *
  56207. + */
  56208. +
  56209. +#include "dwc_otg_os_dep.h"
  56210. +#include "dwc_otg_pcd_if.h"
  56211. +#include "dwc_otg_pcd.h"
  56212. +#include "dwc_otg_driver.h"
  56213. +#include "dwc_otg_dbg.h"
  56214. +
  56215. +extern bool fiq_enable;
  56216. +
  56217. +static struct gadget_wrapper {
  56218. + dwc_otg_pcd_t *pcd;
  56219. +
  56220. + struct usb_gadget gadget;
  56221. + struct usb_gadget_driver *driver;
  56222. +
  56223. + struct usb_ep ep0;
  56224. + struct usb_ep in_ep[16];
  56225. + struct usb_ep out_ep[16];
  56226. +
  56227. +} *gadget_wrapper;
  56228. +
  56229. +/* Display the contents of the buffer */
  56230. +extern void dump_msg(const u8 * buf, unsigned int length);
  56231. +/**
  56232. + * Get the dwc_otg_pcd_ep_t* from usb_ep* pointer - NULL in case
  56233. + * if the endpoint is not found
  56234. + */
  56235. +static struct dwc_otg_pcd_ep *ep_from_handle(dwc_otg_pcd_t * pcd, void *handle)
  56236. +{
  56237. + int i;
  56238. + if (pcd->ep0.priv == handle) {
  56239. + return &pcd->ep0;
  56240. + }
  56241. +
  56242. + for (i = 0; i < MAX_EPS_CHANNELS - 1; i++) {
  56243. + if (pcd->in_ep[i].priv == handle)
  56244. + return &pcd->in_ep[i];
  56245. + if (pcd->out_ep[i].priv == handle)
  56246. + return &pcd->out_ep[i];
  56247. + }
  56248. +
  56249. + return NULL;
  56250. +}
  56251. +
  56252. +/* USB Endpoint Operations */
  56253. +/*
  56254. + * The following sections briefly describe the behavior of the Gadget
  56255. + * API endpoint operations implemented in the DWC_otg driver
  56256. + * software. Detailed descriptions of the generic behavior of each of
  56257. + * these functions can be found in the Linux header file
  56258. + * include/linux/usb_gadget.h.
  56259. + *
  56260. + * The Gadget API provides wrapper functions for each of the function
  56261. + * pointers defined in usb_ep_ops. The Gadget Driver calls the wrapper
  56262. + * function, which then calls the underlying PCD function. The
  56263. + * following sections are named according to the wrapper
  56264. + * functions. Within each section, the corresponding DWC_otg PCD
  56265. + * function name is specified.
  56266. + *
  56267. + */
  56268. +
  56269. +/**
  56270. + * This function is called by the Gadget Driver for each EP to be
  56271. + * configured for the current configuration (SET_CONFIGURATION).
  56272. + *
  56273. + * This function initializes the dwc_otg_ep_t data structure, and then
  56274. + * calls dwc_otg_ep_activate.
  56275. + */
  56276. +static int ep_enable(struct usb_ep *usb_ep,
  56277. + const struct usb_endpoint_descriptor *ep_desc)
  56278. +{
  56279. + int retval;
  56280. +
  56281. + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%p)\n", __func__, usb_ep, ep_desc);
  56282. +
  56283. + if (!usb_ep || !ep_desc || ep_desc->bDescriptorType != USB_DT_ENDPOINT) {
  56284. + DWC_WARN("%s, bad ep or descriptor\n", __func__);
  56285. + return -EINVAL;
  56286. + }
  56287. + if (usb_ep == &gadget_wrapper->ep0) {
  56288. + DWC_WARN("%s, bad ep(0)\n", __func__);
  56289. + return -EINVAL;
  56290. + }
  56291. +
  56292. + /* Check FIFO size? */
  56293. + if (!ep_desc->wMaxPacketSize) {
  56294. + DWC_WARN("%s, bad %s maxpacket\n", __func__, usb_ep->name);
  56295. + return -ERANGE;
  56296. + }
  56297. +
  56298. + if (!gadget_wrapper->driver ||
  56299. + gadget_wrapper->gadget.speed == USB_SPEED_UNKNOWN) {
  56300. + DWC_WARN("%s, bogus device state\n", __func__);
  56301. + return -ESHUTDOWN;
  56302. + }
  56303. +
  56304. + /* Delete after check - MAS */
  56305. +#if 0
  56306. + nat = (uint32_t) ep_desc->wMaxPacketSize;
  56307. + printk(KERN_ALERT "%s: nat (before) =%d\n", __func__, nat);
  56308. + nat = (nat >> 11) & 0x03;
  56309. + printk(KERN_ALERT "%s: nat (after) =%d\n", __func__, nat);
  56310. +#endif
  56311. + retval = dwc_otg_pcd_ep_enable(gadget_wrapper->pcd,
  56312. + (const uint8_t *)ep_desc,
  56313. + (void *)usb_ep);
  56314. + if (retval) {
  56315. + DWC_WARN("dwc_otg_pcd_ep_enable failed\n");
  56316. + return -EINVAL;
  56317. + }
  56318. +
  56319. + usb_ep->maxpacket = le16_to_cpu(ep_desc->wMaxPacketSize);
  56320. +
  56321. + return 0;
  56322. +}
  56323. +
  56324. +/**
  56325. + * This function is called when an EP is disabled due to disconnect or
  56326. + * change in configuration. Any pending requests will terminate with a
  56327. + * status of -ESHUTDOWN.
  56328. + *
  56329. + * This function modifies the dwc_otg_ep_t data structure for this EP,
  56330. + * and then calls dwc_otg_ep_deactivate.
  56331. + */
  56332. +static int ep_disable(struct usb_ep *usb_ep)
  56333. +{
  56334. + int retval;
  56335. +
  56336. + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, usb_ep);
  56337. + if (!usb_ep) {
  56338. + DWC_DEBUGPL(DBG_PCD, "%s, %s not enabled\n", __func__,
  56339. + usb_ep ? usb_ep->name : NULL);
  56340. + return -EINVAL;
  56341. + }
  56342. +
  56343. + retval = dwc_otg_pcd_ep_disable(gadget_wrapper->pcd, usb_ep);
  56344. + if (retval) {
  56345. + retval = -EINVAL;
  56346. + }
  56347. +
  56348. + return retval;
  56349. +}
  56350. +
  56351. +/**
  56352. + * This function allocates a request object to use with the specified
  56353. + * endpoint.
  56354. + *
  56355. + * @param ep The endpoint to be used with with the request
  56356. + * @param gfp_flags the GFP_* flags to use.
  56357. + */
  56358. +static struct usb_request *dwc_otg_pcd_alloc_request(struct usb_ep *ep,
  56359. + gfp_t gfp_flags)
  56360. +{
  56361. + struct usb_request *usb_req;
  56362. +
  56363. + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%d)\n", __func__, ep, gfp_flags);
  56364. + if (0 == ep) {
  56365. + DWC_WARN("%s() %s\n", __func__, "Invalid EP!\n");
  56366. + return 0;
  56367. + }
  56368. + usb_req = kzalloc(sizeof(*usb_req), gfp_flags);
  56369. + if (0 == usb_req) {
  56370. + DWC_WARN("%s() %s\n", __func__, "request allocation failed!\n");
  56371. + return 0;
  56372. + }
  56373. + usb_req->dma = DWC_DMA_ADDR_INVALID;
  56374. +
  56375. + return usb_req;
  56376. +}
  56377. +
  56378. +/**
  56379. + * This function frees a request object.
  56380. + *
  56381. + * @param ep The endpoint associated with the request
  56382. + * @param req The request being freed
  56383. + */
  56384. +static void dwc_otg_pcd_free_request(struct usb_ep *ep, struct usb_request *req)
  56385. +{
  56386. + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%p)\n", __func__, ep, req);
  56387. +
  56388. + if (0 == ep || 0 == req) {
  56389. + DWC_WARN("%s() %s\n", __func__,
  56390. + "Invalid ep or req argument!\n");
  56391. + return;
  56392. + }
  56393. +
  56394. + kfree(req);
  56395. +}
  56396. +
  56397. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  56398. +/**
  56399. + * This function allocates an I/O buffer to be used for a transfer
  56400. + * to/from the specified endpoint.
  56401. + *
  56402. + * @param usb_ep The endpoint to be used with with the request
  56403. + * @param bytes The desired number of bytes for the buffer
  56404. + * @param dma Pointer to the buffer's DMA address; must be valid
  56405. + * @param gfp_flags the GFP_* flags to use.
  56406. + * @return address of a new buffer or null is buffer could not be allocated.
  56407. + */
  56408. +static void *dwc_otg_pcd_alloc_buffer(struct usb_ep *usb_ep, unsigned bytes,
  56409. + dma_addr_t * dma, gfp_t gfp_flags)
  56410. +{
  56411. + void *buf;
  56412. + dwc_otg_pcd_t *pcd = 0;
  56413. +
  56414. + pcd = gadget_wrapper->pcd;
  56415. +
  56416. + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%d,%p,%0x)\n", __func__, usb_ep, bytes,
  56417. + dma, gfp_flags);
  56418. +
  56419. + /* Check dword alignment */
  56420. + if ((bytes & 0x3UL) != 0) {
  56421. + DWC_WARN("%s() Buffer size is not a multiple of"
  56422. + "DWORD size (%d)", __func__, bytes);
  56423. + }
  56424. +
  56425. + buf = dma_alloc_coherent(NULL, bytes, dma, gfp_flags);
  56426. + WARN_ON(!buf);
  56427. +
  56428. + /* Check dword alignment */
  56429. + if (((int)buf & 0x3UL) != 0) {
  56430. + DWC_WARN("%s() Buffer is not DWORD aligned (%p)",
  56431. + __func__, buf);
  56432. + }
  56433. +
  56434. + return buf;
  56435. +}
  56436. +
  56437. +/**
  56438. + * This function frees an I/O buffer that was allocated by alloc_buffer.
  56439. + *
  56440. + * @param usb_ep the endpoint associated with the buffer
  56441. + * @param buf address of the buffer
  56442. + * @param dma The buffer's DMA address
  56443. + * @param bytes The number of bytes of the buffer
  56444. + */
  56445. +static void dwc_otg_pcd_free_buffer(struct usb_ep *usb_ep, void *buf,
  56446. + dma_addr_t dma, unsigned bytes)
  56447. +{
  56448. + dwc_otg_pcd_t *pcd = 0;
  56449. +
  56450. + pcd = gadget_wrapper->pcd;
  56451. +
  56452. + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%0x,%d)\n", __func__, buf, dma, bytes);
  56453. +
  56454. + dma_free_coherent(NULL, bytes, buf, dma);
  56455. +}
  56456. +#endif
  56457. +
  56458. +/**
  56459. + * This function is used to submit an I/O Request to an EP.
  56460. + *
  56461. + * - When the request completes the request's completion callback
  56462. + * is called to return the request to the driver.
  56463. + * - An EP, except control EPs, may have multiple requests
  56464. + * pending.
  56465. + * - Once submitted the request cannot be examined or modified.
  56466. + * - Each request is turned into one or more packets.
  56467. + * - A BULK EP can queue any amount of data; the transfer is
  56468. + * packetized.
  56469. + * - Zero length Packets are specified with the request 'zero'
  56470. + * flag.
  56471. + */
  56472. +static int ep_queue(struct usb_ep *usb_ep, struct usb_request *usb_req,
  56473. + gfp_t gfp_flags)
  56474. +{
  56475. + dwc_otg_pcd_t *pcd;
  56476. + struct dwc_otg_pcd_ep *ep = NULL;
  56477. + int retval = 0, is_isoc_ep = 0;
  56478. + dma_addr_t dma_addr = DWC_DMA_ADDR_INVALID;
  56479. +
  56480. + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%p,%d)\n",
  56481. + __func__, usb_ep, usb_req, gfp_flags);
  56482. +
  56483. + if (!usb_req || !usb_req->complete || !usb_req->buf) {
  56484. + DWC_WARN("bad params\n");
  56485. + return -EINVAL;
  56486. + }
  56487. +
  56488. + if (!usb_ep) {
  56489. + DWC_WARN("bad ep\n");
  56490. + return -EINVAL;
  56491. + }
  56492. +
  56493. + pcd = gadget_wrapper->pcd;
  56494. + if (!gadget_wrapper->driver ||
  56495. + gadget_wrapper->gadget.speed == USB_SPEED_UNKNOWN) {
  56496. + DWC_DEBUGPL(DBG_PCDV, "gadget.speed=%d\n",
  56497. + gadget_wrapper->gadget.speed);
  56498. + DWC_WARN("bogus device state\n");
  56499. + return -ESHUTDOWN;
  56500. + }
  56501. +
  56502. + DWC_DEBUGPL(DBG_PCD, "%s queue req %p, len %d buf %p\n",
  56503. + usb_ep->name, usb_req, usb_req->length, usb_req->buf);
  56504. +
  56505. + usb_req->status = -EINPROGRESS;
  56506. + usb_req->actual = 0;
  56507. +
  56508. + ep = ep_from_handle(pcd, usb_ep);
  56509. + if (ep == NULL)
  56510. + is_isoc_ep = 0;
  56511. + else
  56512. + is_isoc_ep = (ep->dwc_ep.type == DWC_OTG_EP_TYPE_ISOC) ? 1 : 0;
  56513. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  56514. + dma_addr = usb_req->dma;
  56515. +#else
  56516. + if (GET_CORE_IF(pcd)->dma_enable) {
  56517. + dwc_otg_device_t *otg_dev = gadget_wrapper->pcd->otg_dev;
  56518. + struct device *dev = NULL;
  56519. +
  56520. + if (otg_dev != NULL)
  56521. + dev = DWC_OTG_OS_GETDEV(otg_dev->os_dep);
  56522. +
  56523. + if (usb_req->length != 0 &&
  56524. + usb_req->dma == DWC_DMA_ADDR_INVALID) {
  56525. + dma_addr = dma_map_single(dev, usb_req->buf,
  56526. + usb_req->length,
  56527. + ep->dwc_ep.is_in ?
  56528. + DMA_TO_DEVICE:
  56529. + DMA_FROM_DEVICE);
  56530. + }
  56531. + }
  56532. +#endif
  56533. +
  56534. +#ifdef DWC_UTE_PER_IO
  56535. + if (is_isoc_ep == 1) {
  56536. + retval = dwc_otg_pcd_xiso_ep_queue(pcd, usb_ep, usb_req->buf, dma_addr,
  56537. + usb_req->length, usb_req->zero, usb_req,
  56538. + gfp_flags == GFP_ATOMIC ? 1 : 0, &usb_req->ext_req);
  56539. + if (retval)
  56540. + return -EINVAL;
  56541. +
  56542. + return 0;
  56543. + }
  56544. +#endif
  56545. + retval = dwc_otg_pcd_ep_queue(pcd, usb_ep, usb_req->buf, dma_addr,
  56546. + usb_req->length, usb_req->zero, usb_req,
  56547. + gfp_flags == GFP_ATOMIC ? 1 : 0);
  56548. + if (retval) {
  56549. + return -EINVAL;
  56550. + }
  56551. +
  56552. + return 0;
  56553. +}
  56554. +
  56555. +/**
  56556. + * This function cancels an I/O request from an EP.
  56557. + */
  56558. +static int ep_dequeue(struct usb_ep *usb_ep, struct usb_request *usb_req)
  56559. +{
  56560. + DWC_DEBUGPL(DBG_PCDV, "%s(%p,%p)\n", __func__, usb_ep, usb_req);
  56561. +
  56562. + if (!usb_ep || !usb_req) {
  56563. + DWC_WARN("bad argument\n");
  56564. + return -EINVAL;
  56565. + }
  56566. + if (!gadget_wrapper->driver ||
  56567. + gadget_wrapper->gadget.speed == USB_SPEED_UNKNOWN) {
  56568. + DWC_WARN("bogus device state\n");
  56569. + return -ESHUTDOWN;
  56570. + }
  56571. + if (dwc_otg_pcd_ep_dequeue(gadget_wrapper->pcd, usb_ep, usb_req)) {
  56572. + return -EINVAL;
  56573. + }
  56574. +
  56575. + return 0;
  56576. +}
  56577. +
  56578. +/**
  56579. + * usb_ep_set_halt stalls an endpoint.
  56580. + *
  56581. + * usb_ep_clear_halt clears an endpoint halt and resets its data
  56582. + * toggle.
  56583. + *
  56584. + * Both of these functions are implemented with the same underlying
  56585. + * function. The behavior depends on the value argument.
  56586. + *
  56587. + * @param[in] usb_ep the Endpoint to halt or clear halt.
  56588. + * @param[in] value
  56589. + * - 0 means clear_halt.
  56590. + * - 1 means set_halt,
  56591. + * - 2 means clear stall lock flag.
  56592. + * - 3 means set stall lock flag.
  56593. + */
  56594. +static int ep_halt(struct usb_ep *usb_ep, int value)
  56595. +{
  56596. + int retval = 0;
  56597. +
  56598. + DWC_DEBUGPL(DBG_PCD, "HALT %s %d\n", usb_ep->name, value);
  56599. +
  56600. + if (!usb_ep) {
  56601. + DWC_WARN("bad ep\n");
  56602. + return -EINVAL;
  56603. + }
  56604. +
  56605. + retval = dwc_otg_pcd_ep_halt(gadget_wrapper->pcd, usb_ep, value);
  56606. + if (retval == -DWC_E_AGAIN) {
  56607. + return -EAGAIN;
  56608. + } else if (retval) {
  56609. + retval = -EINVAL;
  56610. + }
  56611. +
  56612. + return retval;
  56613. +}
  56614. +
  56615. +//#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,30))
  56616. +#if 0
  56617. +/**
  56618. + * ep_wedge: sets the halt feature and ignores clear requests
  56619. + *
  56620. + * @usb_ep: the endpoint being wedged
  56621. + *
  56622. + * Use this to stall an endpoint and ignore CLEAR_FEATURE(HALT_ENDPOINT)
  56623. + * requests. If the gadget driver clears the halt status, it will
  56624. + * automatically unwedge the endpoint.
  56625. + *
  56626. + * Returns zero on success, else negative errno. *
  56627. + * Check usb_ep_set_wedge() at "usb_gadget.h" for details
  56628. + */
  56629. +static int ep_wedge(struct usb_ep *usb_ep)
  56630. +{
  56631. + int retval = 0;
  56632. +
  56633. + DWC_DEBUGPL(DBG_PCD, "WEDGE %s\n", usb_ep->name);
  56634. +
  56635. + if (!usb_ep) {
  56636. + DWC_WARN("bad ep\n");
  56637. + return -EINVAL;
  56638. + }
  56639. +
  56640. + retval = dwc_otg_pcd_ep_wedge(gadget_wrapper->pcd, usb_ep);
  56641. + if (retval == -DWC_E_AGAIN) {
  56642. + retval = -EAGAIN;
  56643. + } else if (retval) {
  56644. + retval = -EINVAL;
  56645. + }
  56646. +
  56647. + return retval;
  56648. +}
  56649. +#endif
  56650. +
  56651. +#ifdef DWC_EN_ISOC
  56652. +/**
  56653. + * This function is used to submit an ISOC Transfer Request to an EP.
  56654. + *
  56655. + * - Every time a sync period completes the request's completion callback
  56656. + * is called to provide data to the gadget driver.
  56657. + * - Once submitted the request cannot be modified.
  56658. + * - Each request is turned into periodic data packets untill ISO
  56659. + * Transfer is stopped..
  56660. + */
  56661. +static int iso_ep_start(struct usb_ep *usb_ep, struct usb_iso_request *req,
  56662. + gfp_t gfp_flags)
  56663. +{
  56664. + int retval = 0;
  56665. +
  56666. + if (!req || !req->process_buffer || !req->buf0 || !req->buf1) {
  56667. + DWC_WARN("bad params\n");
  56668. + return -EINVAL;
  56669. + }
  56670. +
  56671. + if (!usb_ep) {
  56672. + DWC_PRINTF("bad params\n");
  56673. + return -EINVAL;
  56674. + }
  56675. +
  56676. + req->status = -EINPROGRESS;
  56677. +
  56678. + retval =
  56679. + dwc_otg_pcd_iso_ep_start(gadget_wrapper->pcd, usb_ep, req->buf0,
  56680. + req->buf1, req->dma0, req->dma1,
  56681. + req->sync_frame, req->data_pattern_frame,
  56682. + req->data_per_frame,
  56683. + req->
  56684. + flags & USB_REQ_ISO_ASAP ? -1 :
  56685. + req->start_frame, req->buf_proc_intrvl,
  56686. + req, gfp_flags == GFP_ATOMIC ? 1 : 0);
  56687. +
  56688. + if (retval) {
  56689. + return -EINVAL;
  56690. + }
  56691. +
  56692. + return retval;
  56693. +}
  56694. +
  56695. +/**
  56696. + * This function stops ISO EP Periodic Data Transfer.
  56697. + */
  56698. +static int iso_ep_stop(struct usb_ep *usb_ep, struct usb_iso_request *req)
  56699. +{
  56700. + int retval = 0;
  56701. + if (!usb_ep) {
  56702. + DWC_WARN("bad ep\n");
  56703. + }
  56704. +
  56705. + if (!gadget_wrapper->driver ||
  56706. + gadget_wrapper->gadget.speed == USB_SPEED_UNKNOWN) {
  56707. + DWC_DEBUGPL(DBG_PCDV, "gadget.speed=%d\n",
  56708. + gadget_wrapper->gadget.speed);
  56709. + DWC_WARN("bogus device state\n");
  56710. + }
  56711. +
  56712. + dwc_otg_pcd_iso_ep_stop(gadget_wrapper->pcd, usb_ep, req);
  56713. + if (retval) {
  56714. + retval = -EINVAL;
  56715. + }
  56716. +
  56717. + return retval;
  56718. +}
  56719. +
  56720. +static struct usb_iso_request *alloc_iso_request(struct usb_ep *ep,
  56721. + int packets, gfp_t gfp_flags)
  56722. +{
  56723. + struct usb_iso_request *pReq = NULL;
  56724. + uint32_t req_size;
  56725. +
  56726. + req_size = sizeof(struct usb_iso_request);
  56727. + req_size +=
  56728. + (2 * packets * (sizeof(struct usb_gadget_iso_packet_descriptor)));
  56729. +
  56730. + pReq = kmalloc(req_size, gfp_flags);
  56731. + if (!pReq) {
  56732. + DWC_WARN("Can't allocate Iso Request\n");
  56733. + return 0;
  56734. + }
  56735. + pReq->iso_packet_desc0 = (void *)(pReq + 1);
  56736. +
  56737. + pReq->iso_packet_desc1 = pReq->iso_packet_desc0 + packets;
  56738. +
  56739. + return pReq;
  56740. +}
  56741. +
  56742. +static void free_iso_request(struct usb_ep *ep, struct usb_iso_request *req)
  56743. +{
  56744. + kfree(req);
  56745. +}
  56746. +
  56747. +static struct usb_isoc_ep_ops dwc_otg_pcd_ep_ops = {
  56748. + .ep_ops = {
  56749. + .enable = ep_enable,
  56750. + .disable = ep_disable,
  56751. +
  56752. + .alloc_request = dwc_otg_pcd_alloc_request,
  56753. + .free_request = dwc_otg_pcd_free_request,
  56754. +
  56755. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  56756. + .alloc_buffer = dwc_otg_pcd_alloc_buffer,
  56757. + .free_buffer = dwc_otg_pcd_free_buffer,
  56758. +#endif
  56759. +
  56760. + .queue = ep_queue,
  56761. + .dequeue = ep_dequeue,
  56762. +
  56763. + .set_halt = ep_halt,
  56764. + .fifo_status = 0,
  56765. + .fifo_flush = 0,
  56766. + },
  56767. + .iso_ep_start = iso_ep_start,
  56768. + .iso_ep_stop = iso_ep_stop,
  56769. + .alloc_iso_request = alloc_iso_request,
  56770. + .free_iso_request = free_iso_request,
  56771. +};
  56772. +
  56773. +#else
  56774. +
  56775. + int (*enable) (struct usb_ep *ep,
  56776. + const struct usb_endpoint_descriptor *desc);
  56777. + int (*disable) (struct usb_ep *ep);
  56778. +
  56779. + struct usb_request *(*alloc_request) (struct usb_ep *ep,
  56780. + gfp_t gfp_flags);
  56781. + void (*free_request) (struct usb_ep *ep, struct usb_request *req);
  56782. +
  56783. + int (*queue) (struct usb_ep *ep, struct usb_request *req,
  56784. + gfp_t gfp_flags);
  56785. + int (*dequeue) (struct usb_ep *ep, struct usb_request *req);
  56786. +
  56787. + int (*set_halt) (struct usb_ep *ep, int value);
  56788. + int (*set_wedge) (struct usb_ep *ep);
  56789. +
  56790. + int (*fifo_status) (struct usb_ep *ep);
  56791. + void (*fifo_flush) (struct usb_ep *ep);
  56792. +static struct usb_ep_ops dwc_otg_pcd_ep_ops = {
  56793. + .enable = ep_enable,
  56794. + .disable = ep_disable,
  56795. +
  56796. + .alloc_request = dwc_otg_pcd_alloc_request,
  56797. + .free_request = dwc_otg_pcd_free_request,
  56798. +
  56799. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
  56800. + .alloc_buffer = dwc_otg_pcd_alloc_buffer,
  56801. + .free_buffer = dwc_otg_pcd_free_buffer,
  56802. +#else
  56803. + /* .set_wedge = ep_wedge, */
  56804. + .set_wedge = NULL, /* uses set_halt instead */
  56805. +#endif
  56806. +
  56807. + .queue = ep_queue,
  56808. + .dequeue = ep_dequeue,
  56809. +
  56810. + .set_halt = ep_halt,
  56811. + .fifo_status = 0,
  56812. + .fifo_flush = 0,
  56813. +
  56814. +};
  56815. +
  56816. +#endif /* _EN_ISOC_ */
  56817. +/* Gadget Operations */
  56818. +/**
  56819. + * The following gadget operations will be implemented in the DWC_otg
  56820. + * PCD. Functions in the API that are not described below are not
  56821. + * implemented.
  56822. + *
  56823. + * The Gadget API provides wrapper functions for each of the function
  56824. + * pointers defined in usb_gadget_ops. The Gadget Driver calls the
  56825. + * wrapper function, which then calls the underlying PCD function. The
  56826. + * following sections are named according to the wrapper functions
  56827. + * (except for ioctl, which doesn't have a wrapper function). Within
  56828. + * each section, the corresponding DWC_otg PCD function name is
  56829. + * specified.
  56830. + *
  56831. + */
  56832. +
  56833. +/**
  56834. + *Gets the USB Frame number of the last SOF.
  56835. + */
  56836. +static int get_frame_number(struct usb_gadget *gadget)
  56837. +{
  56838. + struct gadget_wrapper *d;
  56839. +
  56840. + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, gadget);
  56841. +
  56842. + if (gadget == 0) {
  56843. + return -ENODEV;
  56844. + }
  56845. +
  56846. + d = container_of(gadget, struct gadget_wrapper, gadget);
  56847. + return dwc_otg_pcd_get_frame_number(d->pcd);
  56848. +}
  56849. +
  56850. +#ifdef CONFIG_USB_DWC_OTG_LPM
  56851. +static int test_lpm_enabled(struct usb_gadget *gadget)
  56852. +{
  56853. + struct gadget_wrapper *d;
  56854. +
  56855. + d = container_of(gadget, struct gadget_wrapper, gadget);
  56856. +
  56857. + return dwc_otg_pcd_is_lpm_enabled(d->pcd);
  56858. +}
  56859. +#endif
  56860. +
  56861. +/**
  56862. + * Initiates Session Request Protocol (SRP) to wakeup the host if no
  56863. + * session is in progress. If a session is already in progress, but
  56864. + * the device is suspended, remote wakeup signaling is started.
  56865. + *
  56866. + */
  56867. +static int wakeup(struct usb_gadget *gadget)
  56868. +{
  56869. + struct gadget_wrapper *d;
  56870. +
  56871. + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, gadget);
  56872. +
  56873. + if (gadget == 0) {
  56874. + return -ENODEV;
  56875. + } else {
  56876. + d = container_of(gadget, struct gadget_wrapper, gadget);
  56877. + }
  56878. + dwc_otg_pcd_wakeup(d->pcd);
  56879. + return 0;
  56880. +}
  56881. +
  56882. +static const struct usb_gadget_ops dwc_otg_pcd_ops = {
  56883. + .get_frame = get_frame_number,
  56884. + .wakeup = wakeup,
  56885. +#ifdef CONFIG_USB_DWC_OTG_LPM
  56886. + .lpm_support = test_lpm_enabled,
  56887. +#endif
  56888. + // current versions must always be self-powered
  56889. +};
  56890. +
  56891. +static int _setup(dwc_otg_pcd_t * pcd, uint8_t * bytes)
  56892. +{
  56893. + int retval = -DWC_E_NOT_SUPPORTED;
  56894. + if (gadget_wrapper->driver && gadget_wrapper->driver->setup) {
  56895. + retval = gadget_wrapper->driver->setup(&gadget_wrapper->gadget,
  56896. + (struct usb_ctrlrequest
  56897. + *)bytes);
  56898. + }
  56899. +
  56900. + if (retval == -ENOTSUPP) {
  56901. + retval = -DWC_E_NOT_SUPPORTED;
  56902. + } else if (retval < 0) {
  56903. + retval = -DWC_E_INVALID;
  56904. + }
  56905. +
  56906. + return retval;
  56907. +}
  56908. +
  56909. +#ifdef DWC_EN_ISOC
  56910. +static int _isoc_complete(dwc_otg_pcd_t * pcd, void *ep_handle,
  56911. + void *req_handle, int proc_buf_num)
  56912. +{
  56913. + int i, packet_count;
  56914. + struct usb_gadget_iso_packet_descriptor *iso_packet = 0;
  56915. + struct usb_iso_request *iso_req = req_handle;
  56916. +
  56917. + if (proc_buf_num) {
  56918. + iso_packet = iso_req->iso_packet_desc1;
  56919. + } else {
  56920. + iso_packet = iso_req->iso_packet_desc0;
  56921. + }
  56922. + packet_count =
  56923. + dwc_otg_pcd_get_iso_packet_count(pcd, ep_handle, req_handle);
  56924. + for (i = 0; i < packet_count; ++i) {
  56925. + int status;
  56926. + int actual;
  56927. + int offset;
  56928. + dwc_otg_pcd_get_iso_packet_params(pcd, ep_handle, req_handle,
  56929. + i, &status, &actual, &offset);
  56930. + switch (status) {
  56931. + case -DWC_E_NO_DATA:
  56932. + status = -ENODATA;
  56933. + break;
  56934. + default:
  56935. + if (status) {
  56936. + DWC_PRINTF("unknown status in isoc packet\n");
  56937. + }
  56938. +
  56939. + }
  56940. + iso_packet[i].status = status;
  56941. + iso_packet[i].offset = offset;
  56942. + iso_packet[i].actual_length = actual;
  56943. + }
  56944. +
  56945. + iso_req->status = 0;
  56946. + iso_req->process_buffer(ep_handle, iso_req);
  56947. +
  56948. + return 0;
  56949. +}
  56950. +#endif /* DWC_EN_ISOC */
  56951. +
  56952. +#ifdef DWC_UTE_PER_IO
  56953. +/**
  56954. + * Copy the contents of the extended request to the Linux usb_request's
  56955. + * extended part and call the gadget's completion.
  56956. + *
  56957. + * @param pcd Pointer to the pcd structure
  56958. + * @param ep_handle Void pointer to the usb_ep structure
  56959. + * @param req_handle Void pointer to the usb_request structure
  56960. + * @param status Request status returned from the portable logic
  56961. + * @param ereq_port Void pointer to the extended request structure
  56962. + * created in the the portable part that contains the
  56963. + * results of the processed iso packets.
  56964. + */
  56965. +static int _xisoc_complete(dwc_otg_pcd_t * pcd, void *ep_handle,
  56966. + void *req_handle, int32_t status, void *ereq_port)
  56967. +{
  56968. + struct dwc_ute_iso_req_ext *ereqorg = NULL;
  56969. + struct dwc_iso_xreq_port *ereqport = NULL;
  56970. + struct dwc_ute_iso_packet_descriptor *desc_org = NULL;
  56971. + int i;
  56972. + struct usb_request *req;
  56973. + //struct dwc_ute_iso_packet_descriptor *
  56974. + //int status = 0;
  56975. +
  56976. + req = (struct usb_request *)req_handle;
  56977. + ereqorg = &req->ext_req;
  56978. + ereqport = (struct dwc_iso_xreq_port *)ereq_port;
  56979. + desc_org = ereqorg->per_io_frame_descs;
  56980. +
  56981. + if (req && req->complete) {
  56982. + /* Copy the request data from the portable logic to our request */
  56983. + for (i = 0; i < ereqport->pio_pkt_count; i++) {
  56984. + desc_org[i].actual_length =
  56985. + ereqport->per_io_frame_descs[i].actual_length;
  56986. + desc_org[i].status =
  56987. + ereqport->per_io_frame_descs[i].status;
  56988. + }
  56989. +
  56990. + switch (status) {
  56991. + case -DWC_E_SHUTDOWN:
  56992. + req->status = -ESHUTDOWN;
  56993. + break;
  56994. + case -DWC_E_RESTART:
  56995. + req->status = -ECONNRESET;
  56996. + break;
  56997. + case -DWC_E_INVALID:
  56998. + req->status = -EINVAL;
  56999. + break;
  57000. + case -DWC_E_TIMEOUT:
  57001. + req->status = -ETIMEDOUT;
  57002. + break;
  57003. + default:
  57004. + req->status = status;
  57005. + }
  57006. +
  57007. + /* And call the gadget's completion */
  57008. + req->complete(ep_handle, req);
  57009. + }
  57010. +
  57011. + return 0;
  57012. +}
  57013. +#endif /* DWC_UTE_PER_IO */
  57014. +
  57015. +static int _complete(dwc_otg_pcd_t * pcd, void *ep_handle,
  57016. + void *req_handle, int32_t status, uint32_t actual)
  57017. +{
  57018. + struct usb_request *req = (struct usb_request *)req_handle;
  57019. +#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,27)
  57020. + struct dwc_otg_pcd_ep *ep = NULL;
  57021. +#endif
  57022. +
  57023. + if (req && req->complete) {
  57024. + switch (status) {
  57025. + case -DWC_E_SHUTDOWN:
  57026. + req->status = -ESHUTDOWN;
  57027. + break;
  57028. + case -DWC_E_RESTART:
  57029. + req->status = -ECONNRESET;
  57030. + break;
  57031. + case -DWC_E_INVALID:
  57032. + req->status = -EINVAL;
  57033. + break;
  57034. + case -DWC_E_TIMEOUT:
  57035. + req->status = -ETIMEDOUT;
  57036. + break;
  57037. + default:
  57038. + req->status = status;
  57039. +
  57040. + }
  57041. +
  57042. + req->actual = actual;
  57043. + DWC_SPINUNLOCK(pcd->lock);
  57044. + req->complete(ep_handle, req);
  57045. + DWC_SPINLOCK(pcd->lock);
  57046. + }
  57047. +#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,27)
  57048. + ep = ep_from_handle(pcd, ep_handle);
  57049. + if (GET_CORE_IF(pcd)->dma_enable) {
  57050. + if (req->length != 0) {
  57051. + dwc_otg_device_t *otg_dev = gadget_wrapper->pcd->otg_dev;
  57052. + struct device *dev = NULL;
  57053. +
  57054. + if (otg_dev != NULL)
  57055. + dev = DWC_OTG_OS_GETDEV(otg_dev->os_dep);
  57056. +
  57057. + dma_unmap_single(dev, req->dma, req->length,
  57058. + ep->dwc_ep.is_in ?
  57059. + DMA_TO_DEVICE: DMA_FROM_DEVICE);
  57060. + }
  57061. + }
  57062. +#endif
  57063. +
  57064. + return 0;
  57065. +}
  57066. +
  57067. +static int _connect(dwc_otg_pcd_t * pcd, int speed)
  57068. +{
  57069. + gadget_wrapper->gadget.speed = speed;
  57070. + return 0;
  57071. +}
  57072. +
  57073. +static int _disconnect(dwc_otg_pcd_t * pcd)
  57074. +{
  57075. + if (gadget_wrapper->driver && gadget_wrapper->driver->disconnect) {
  57076. + gadget_wrapper->driver->disconnect(&gadget_wrapper->gadget);
  57077. + }
  57078. + return 0;
  57079. +}
  57080. +
  57081. +static int _resume(dwc_otg_pcd_t * pcd)
  57082. +{
  57083. + if (gadget_wrapper->driver && gadget_wrapper->driver->resume) {
  57084. + gadget_wrapper->driver->resume(&gadget_wrapper->gadget);
  57085. + }
  57086. +
  57087. + return 0;
  57088. +}
  57089. +
  57090. +static int _suspend(dwc_otg_pcd_t * pcd)
  57091. +{
  57092. + if (gadget_wrapper->driver && gadget_wrapper->driver->suspend) {
  57093. + gadget_wrapper->driver->suspend(&gadget_wrapper->gadget);
  57094. + }
  57095. + return 0;
  57096. +}
  57097. +
  57098. +/**
  57099. + * This function updates the otg values in the gadget structure.
  57100. + */
  57101. +static int _hnp_changed(dwc_otg_pcd_t * pcd)
  57102. +{
  57103. +
  57104. + if (!gadget_wrapper->gadget.is_otg)
  57105. + return 0;
  57106. +
  57107. + gadget_wrapper->gadget.b_hnp_enable = get_b_hnp_enable(pcd);
  57108. + gadget_wrapper->gadget.a_hnp_support = get_a_hnp_support(pcd);
  57109. + gadget_wrapper->gadget.a_alt_hnp_support = get_a_alt_hnp_support(pcd);
  57110. + return 0;
  57111. +}
  57112. +
  57113. +static int _reset(dwc_otg_pcd_t * pcd)
  57114. +{
  57115. + return 0;
  57116. +}
  57117. +
  57118. +#ifdef DWC_UTE_CFI
  57119. +static int _cfi_setup(dwc_otg_pcd_t * pcd, void *cfi_req)
  57120. +{
  57121. + int retval = -DWC_E_INVALID;
  57122. + if (gadget_wrapper->driver->cfi_feature_setup) {
  57123. + retval =
  57124. + gadget_wrapper->driver->
  57125. + cfi_feature_setup(&gadget_wrapper->gadget,
  57126. + (struct cfi_usb_ctrlrequest *)cfi_req);
  57127. + }
  57128. +
  57129. + return retval;
  57130. +}
  57131. +#endif
  57132. +
  57133. +static const struct dwc_otg_pcd_function_ops fops = {
  57134. + .complete = _complete,
  57135. +#ifdef DWC_EN_ISOC
  57136. + .isoc_complete = _isoc_complete,
  57137. +#endif
  57138. + .setup = _setup,
  57139. + .disconnect = _disconnect,
  57140. + .connect = _connect,
  57141. + .resume = _resume,
  57142. + .suspend = _suspend,
  57143. + .hnp_changed = _hnp_changed,
  57144. + .reset = _reset,
  57145. +#ifdef DWC_UTE_CFI
  57146. + .cfi_setup = _cfi_setup,
  57147. +#endif
  57148. +#ifdef DWC_UTE_PER_IO
  57149. + .xisoc_complete = _xisoc_complete,
  57150. +#endif
  57151. +};
  57152. +
  57153. +/**
  57154. + * This function is the top level PCD interrupt handler.
  57155. + */
  57156. +static irqreturn_t dwc_otg_pcd_irq(int irq, void *dev)
  57157. +{
  57158. + dwc_otg_pcd_t *pcd = dev;
  57159. + int32_t retval = IRQ_NONE;
  57160. +
  57161. + retval = dwc_otg_pcd_handle_intr(pcd);
  57162. + if (retval != 0) {
  57163. + S3C2410X_CLEAR_EINTPEND();
  57164. + }
  57165. + return IRQ_RETVAL(retval);
  57166. +}
  57167. +
  57168. +/**
  57169. + * This function initialized the usb_ep structures to there default
  57170. + * state.
  57171. + *
  57172. + * @param d Pointer on gadget_wrapper.
  57173. + */
  57174. +void gadget_add_eps(struct gadget_wrapper *d)
  57175. +{
  57176. + static const char *names[] = {
  57177. +
  57178. + "ep0",
  57179. + "ep1in",
  57180. + "ep2in",
  57181. + "ep3in",
  57182. + "ep4in",
  57183. + "ep5in",
  57184. + "ep6in",
  57185. + "ep7in",
  57186. + "ep8in",
  57187. + "ep9in",
  57188. + "ep10in",
  57189. + "ep11in",
  57190. + "ep12in",
  57191. + "ep13in",
  57192. + "ep14in",
  57193. + "ep15in",
  57194. + "ep1out",
  57195. + "ep2out",
  57196. + "ep3out",
  57197. + "ep4out",
  57198. + "ep5out",
  57199. + "ep6out",
  57200. + "ep7out",
  57201. + "ep8out",
  57202. + "ep9out",
  57203. + "ep10out",
  57204. + "ep11out",
  57205. + "ep12out",
  57206. + "ep13out",
  57207. + "ep14out",
  57208. + "ep15out"
  57209. + };
  57210. +
  57211. + int i;
  57212. + struct usb_ep *ep;
  57213. + int8_t dev_endpoints;
  57214. +
  57215. + DWC_DEBUGPL(DBG_PCDV, "%s\n", __func__);
  57216. +
  57217. + INIT_LIST_HEAD(&d->gadget.ep_list);
  57218. + d->gadget.ep0 = &d->ep0;
  57219. + d->gadget.speed = USB_SPEED_UNKNOWN;
  57220. +
  57221. + INIT_LIST_HEAD(&d->gadget.ep0->ep_list);
  57222. +
  57223. + /**
  57224. + * Initialize the EP0 structure.
  57225. + */
  57226. + ep = &d->ep0;
  57227. +
  57228. + /* Init the usb_ep structure. */
  57229. + ep->name = names[0];
  57230. + ep->ops = (struct usb_ep_ops *)&dwc_otg_pcd_ep_ops;
  57231. +
  57232. + /**
  57233. + * @todo NGS: What should the max packet size be set to
  57234. + * here? Before EP type is set?
  57235. + */
  57236. + ep->maxpacket = MAX_PACKET_SIZE;
  57237. + dwc_otg_pcd_ep_enable(d->pcd, NULL, ep);
  57238. +
  57239. + list_add_tail(&ep->ep_list, &d->gadget.ep_list);
  57240. +
  57241. + /**
  57242. + * Initialize the EP structures.
  57243. + */
  57244. + dev_endpoints = d->pcd->core_if->dev_if->num_in_eps;
  57245. +
  57246. + for (i = 0; i < dev_endpoints; i++) {
  57247. + ep = &d->in_ep[i];
  57248. +
  57249. + /* Init the usb_ep structure. */
  57250. + ep->name = names[d->pcd->in_ep[i].dwc_ep.num];
  57251. + ep->ops = (struct usb_ep_ops *)&dwc_otg_pcd_ep_ops;
  57252. +
  57253. + /**
  57254. + * @todo NGS: What should the max packet size be set to
  57255. + * here? Before EP type is set?
  57256. + */
  57257. + ep->maxpacket = MAX_PACKET_SIZE;
  57258. + list_add_tail(&ep->ep_list, &d->gadget.ep_list);
  57259. + }
  57260. +
  57261. + dev_endpoints = d->pcd->core_if->dev_if->num_out_eps;
  57262. +
  57263. + for (i = 0; i < dev_endpoints; i++) {
  57264. + ep = &d->out_ep[i];
  57265. +
  57266. + /* Init the usb_ep structure. */
  57267. + ep->name = names[15 + d->pcd->out_ep[i].dwc_ep.num];
  57268. + ep->ops = (struct usb_ep_ops *)&dwc_otg_pcd_ep_ops;
  57269. +
  57270. + /**
  57271. + * @todo NGS: What should the max packet size be set to
  57272. + * here? Before EP type is set?
  57273. + */
  57274. + ep->maxpacket = MAX_PACKET_SIZE;
  57275. +
  57276. + list_add_tail(&ep->ep_list, &d->gadget.ep_list);
  57277. + }
  57278. +
  57279. + /* remove ep0 from the list. There is a ep0 pointer. */
  57280. + list_del_init(&d->ep0.ep_list);
  57281. +
  57282. + d->ep0.maxpacket = MAX_EP0_SIZE;
  57283. +}
  57284. +
  57285. +/**
  57286. + * This function releases the Gadget device.
  57287. + * required by device_unregister().
  57288. + *
  57289. + * @todo Should this do something? Should it free the PCD?
  57290. + */
  57291. +static void dwc_otg_pcd_gadget_release(struct device *dev)
  57292. +{
  57293. + DWC_DEBUGPL(DBG_PCDV, "%s(%p)\n", __func__, dev);
  57294. +}
  57295. +
  57296. +static struct gadget_wrapper *alloc_wrapper(dwc_bus_dev_t *_dev)
  57297. +{
  57298. + static char pcd_name[] = "dwc_otg_pcd";
  57299. + dwc_otg_device_t *otg_dev = DWC_OTG_BUSDRVDATA(_dev);
  57300. + struct gadget_wrapper *d;
  57301. + int retval;
  57302. +
  57303. + d = DWC_ALLOC(sizeof(*d));
  57304. + if (d == NULL) {
  57305. + return NULL;
  57306. + }
  57307. +
  57308. + memset(d, 0, sizeof(*d));
  57309. +
  57310. + d->gadget.name = pcd_name;
  57311. + d->pcd = otg_dev->pcd;
  57312. +
  57313. +#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30)
  57314. + strcpy(d->gadget.dev.bus_id, "gadget");
  57315. +#else
  57316. + dev_set_name(&d->gadget.dev, "%s", "gadget");
  57317. +#endif
  57318. +
  57319. + d->gadget.dev.parent = &_dev->dev;
  57320. + d->gadget.dev.release = dwc_otg_pcd_gadget_release;
  57321. + d->gadget.ops = &dwc_otg_pcd_ops;
  57322. + d->gadget.max_speed = dwc_otg_pcd_is_dualspeed(otg_dev->pcd) ? USB_SPEED_HIGH:USB_SPEED_FULL;
  57323. + d->gadget.is_otg = dwc_otg_pcd_is_otg(otg_dev->pcd);
  57324. +
  57325. + d->driver = 0;
  57326. + /* Register the gadget device */
  57327. + retval = device_register(&d->gadget.dev);
  57328. + if (retval != 0) {
  57329. + DWC_ERROR("device_register failed\n");
  57330. + DWC_FREE(d);
  57331. + return NULL;
  57332. + }
  57333. +
  57334. + return d;
  57335. +}
  57336. +
  57337. +static void free_wrapper(struct gadget_wrapper *d)
  57338. +{
  57339. + if (d->driver) {
  57340. + /* should have been done already by driver model core */
  57341. + DWC_WARN("driver '%s' is still registered\n",
  57342. + d->driver->driver.name);
  57343. +#ifdef CONFIG_USB_GADGET
  57344. + usb_gadget_unregister_driver(d->driver);
  57345. +#endif
  57346. + }
  57347. +
  57348. + device_unregister(&d->gadget.dev);
  57349. + DWC_FREE(d);
  57350. +}
  57351. +
  57352. +/**
  57353. + * This function initialized the PCD portion of the driver.
  57354. + *
  57355. + */
  57356. +int pcd_init(dwc_bus_dev_t *_dev)
  57357. +{
  57358. + dwc_otg_device_t *otg_dev = DWC_OTG_BUSDRVDATA(_dev);
  57359. + int retval = 0;
  57360. +
  57361. + DWC_DEBUGPL(DBG_PCDV, "%s(%p) otg_dev=%p\n", __func__, _dev, otg_dev);
  57362. +
  57363. + otg_dev->pcd = dwc_otg_pcd_init(otg_dev);
  57364. +
  57365. + if (!otg_dev->pcd) {
  57366. + DWC_ERROR("dwc_otg_pcd_init failed\n");
  57367. + return -ENOMEM;
  57368. + }
  57369. +
  57370. + otg_dev->pcd->otg_dev = otg_dev;
  57371. + gadget_wrapper = alloc_wrapper(_dev);
  57372. +
  57373. + /*
  57374. + * Initialize EP structures
  57375. + */
  57376. + gadget_add_eps(gadget_wrapper);
  57377. + /*
  57378. + * Setup interupt handler
  57379. + */
  57380. +#ifdef PLATFORM_INTERFACE
  57381. + DWC_DEBUGPL(DBG_ANY, "registering handler for irq%d\n",
  57382. + platform_get_irq(_dev, fiq_enable ? 0 : 1));
  57383. + retval = request_irq(platform_get_irq(_dev, fiq_enable ? 0 : 1), dwc_otg_pcd_irq,
  57384. + IRQF_SHARED, gadget_wrapper->gadget.name,
  57385. + otg_dev->pcd);
  57386. + if (retval != 0) {
  57387. + DWC_ERROR("request of irq%d failed\n",
  57388. + platform_get_irq(_dev, fiq_enable ? 0 : 1));
  57389. + free_wrapper(gadget_wrapper);
  57390. + return -EBUSY;
  57391. + }
  57392. +#else
  57393. + DWC_DEBUGPL(DBG_ANY, "registering handler for irq%d\n",
  57394. + _dev->irq);
  57395. + retval = request_irq(_dev->irq, dwc_otg_pcd_irq,
  57396. + IRQF_SHARED | IRQF_DISABLED,
  57397. + gadget_wrapper->gadget.name, otg_dev->pcd);
  57398. + if (retval != 0) {
  57399. + DWC_ERROR("request of irq%d failed\n", _dev->irq);
  57400. + free_wrapper(gadget_wrapper);
  57401. + return -EBUSY;
  57402. + }
  57403. +#endif
  57404. +
  57405. + dwc_otg_pcd_start(gadget_wrapper->pcd, &fops);
  57406. +
  57407. + return retval;
  57408. +}
  57409. +
  57410. +/**
  57411. + * Cleanup the PCD.
  57412. + */
  57413. +void pcd_remove(dwc_bus_dev_t *_dev)
  57414. +{
  57415. + dwc_otg_device_t *otg_dev = DWC_OTG_BUSDRVDATA(_dev);
  57416. + dwc_otg_pcd_t *pcd = otg_dev->pcd;
  57417. +
  57418. + DWC_DEBUGPL(DBG_PCDV, "%s(%p) otg_dev %p\n", __func__, _dev, otg_dev);
  57419. +
  57420. + /*
  57421. + * Free the IRQ
  57422. + */
  57423. +#ifdef PLATFORM_INTERFACE
  57424. + free_irq(platform_get_irq(_dev, 0), pcd);
  57425. +#else
  57426. + free_irq(_dev->irq, pcd);
  57427. +#endif
  57428. + dwc_otg_pcd_remove(otg_dev->pcd);
  57429. + free_wrapper(gadget_wrapper);
  57430. + otg_dev->pcd = 0;
  57431. +}
  57432. +
  57433. +#endif /* DWC_HOST_ONLY */
  57434. --- /dev/null
  57435. +++ b/drivers/usb/host/dwc_otg/dwc_otg_regs.h
  57436. @@ -0,0 +1,2550 @@
  57437. +/* ==========================================================================
  57438. + * $File: //dwh/usb_iip/dev/software/otg/linux/drivers/dwc_otg_regs.h $
  57439. + * $Revision: #98 $
  57440. + * $Date: 2012/08/10 $
  57441. + * $Change: 2047372 $
  57442. + *
  57443. + * Synopsys HS OTG Linux Software Driver and documentation (hereinafter,
  57444. + * "Software") is an Unsupported proprietary work of Synopsys, Inc. unless
  57445. + * otherwise expressly agreed to in writing between Synopsys and you.
  57446. + *
  57447. + * The Software IS NOT an item of Licensed Software or Licensed Product under
  57448. + * any End User Software License Agreement or Agreement for Licensed Product
  57449. + * with Synopsys or any supplement thereto. You are permitted to use and
  57450. + * redistribute this Software in source and binary forms, with or without
  57451. + * modification, provided that redistributions of source code must retain this
  57452. + * notice. You may not view, use, disclose, copy or distribute this file or
  57453. + * any information contained herein except pursuant to this license grant from
  57454. + * Synopsys. If you do not agree with this notice, including the disclaimer
  57455. + * below, then you are not authorized to use the Software.
  57456. + *
  57457. + * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" BASIS
  57458. + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  57459. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  57460. + * ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS BE LIABLE FOR ANY DIRECT,
  57461. + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  57462. + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  57463. + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  57464. + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  57465. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  57466. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  57467. + * DAMAGE.
  57468. + * ========================================================================== */
  57469. +
  57470. +#ifndef __DWC_OTG_REGS_H__
  57471. +#define __DWC_OTG_REGS_H__
  57472. +
  57473. +#include "dwc_otg_core_if.h"
  57474. +
  57475. +/**
  57476. + * @file
  57477. + *
  57478. + * This file contains the data structures for accessing the DWC_otg core registers.
  57479. + *
  57480. + * The application interfaces with the HS OTG core by reading from and
  57481. + * writing to the Control and Status Register (CSR) space through the
  57482. + * AHB Slave interface. These registers are 32 bits wide, and the
  57483. + * addresses are 32-bit-block aligned.
  57484. + * CSRs are classified as follows:
  57485. + * - Core Global Registers
  57486. + * - Device Mode Registers
  57487. + * - Device Global Registers
  57488. + * - Device Endpoint Specific Registers
  57489. + * - Host Mode Registers
  57490. + * - Host Global Registers
  57491. + * - Host Port CSRs
  57492. + * - Host Channel Specific Registers
  57493. + *
  57494. + * Only the Core Global registers can be accessed in both Device and
  57495. + * Host modes. When the HS OTG core is operating in one mode, either
  57496. + * Device or Host, the application must not access registers from the
  57497. + * other mode. When the core switches from one mode to another, the
  57498. + * registers in the new mode of operation must be reprogrammed as they
  57499. + * would be after a power-on reset.
  57500. + */
  57501. +
  57502. +/****************************************************************************/
  57503. +/** DWC_otg Core registers .
  57504. + * The dwc_otg_core_global_regs structure defines the size
  57505. + * and relative field offsets for the Core Global registers.
  57506. + */
  57507. +typedef struct dwc_otg_core_global_regs {
  57508. + /** OTG Control and Status Register. <i>Offset: 000h</i> */
  57509. + volatile uint32_t gotgctl;
  57510. + /** OTG Interrupt Register. <i>Offset: 004h</i> */
  57511. + volatile uint32_t gotgint;
  57512. + /**Core AHB Configuration Register. <i>Offset: 008h</i> */
  57513. + volatile uint32_t gahbcfg;
  57514. +
  57515. +#define DWC_GLBINTRMASK 0x0001
  57516. +#define DWC_DMAENABLE 0x0020
  57517. +#define DWC_NPTXEMPTYLVL_EMPTY 0x0080
  57518. +#define DWC_NPTXEMPTYLVL_HALFEMPTY 0x0000
  57519. +#define DWC_PTXEMPTYLVL_EMPTY 0x0100
  57520. +#define DWC_PTXEMPTYLVL_HALFEMPTY 0x0000
  57521. +
  57522. + /**Core USB Configuration Register. <i>Offset: 00Ch</i> */
  57523. + volatile uint32_t gusbcfg;
  57524. + /**Core Reset Register. <i>Offset: 010h</i> */
  57525. + volatile uint32_t grstctl;
  57526. + /**Core Interrupt Register. <i>Offset: 014h</i> */
  57527. + volatile uint32_t gintsts;
  57528. + /**Core Interrupt Mask Register. <i>Offset: 018h</i> */
  57529. + volatile uint32_t gintmsk;
  57530. + /**Receive Status Queue Read Register (Read Only). <i>Offset: 01Ch</i> */
  57531. + volatile uint32_t grxstsr;
  57532. + /**Receive Status Queue Read & POP Register (Read Only). <i>Offset: 020h</i>*/
  57533. + volatile uint32_t grxstsp;
  57534. + /**Receive FIFO Size Register. <i>Offset: 024h</i> */
  57535. + volatile uint32_t grxfsiz;
  57536. + /**Non Periodic Transmit FIFO Size Register. <i>Offset: 028h</i> */
  57537. + volatile uint32_t gnptxfsiz;
  57538. + /**Non Periodic Transmit FIFO/Queue Status Register (Read
  57539. + * Only). <i>Offset: 02Ch</i> */
  57540. + volatile uint32_t gnptxsts;
  57541. + /**I2C Access Register. <i>Offset: 030h</i> */
  57542. + volatile uint32_t gi2cctl;
  57543. + /**PHY Vendor Control Register. <i>Offset: 034h</i> */
  57544. + volatile uint32_t gpvndctl;
  57545. + /**General Purpose Input/Output Register. <i>Offset: 038h</i> */
  57546. + volatile uint32_t ggpio;
  57547. + /**User ID Register. <i>Offset: 03Ch</i> */
  57548. + volatile uint32_t guid;
  57549. + /**Synopsys ID Register (Read Only). <i>Offset: 040h</i> */
  57550. + volatile uint32_t gsnpsid;
  57551. + /**User HW Config1 Register (Read Only). <i>Offset: 044h</i> */
  57552. + volatile uint32_t ghwcfg1;
  57553. + /**User HW Config2 Register (Read Only). <i>Offset: 048h</i> */
  57554. + volatile uint32_t ghwcfg2;
  57555. +#define DWC_SLAVE_ONLY_ARCH 0
  57556. +#define DWC_EXT_DMA_ARCH 1
  57557. +#define DWC_INT_DMA_ARCH 2
  57558. +
  57559. +#define DWC_MODE_HNP_SRP_CAPABLE 0
  57560. +#define DWC_MODE_SRP_ONLY_CAPABLE 1
  57561. +#define DWC_MODE_NO_HNP_SRP_CAPABLE 2
  57562. +#define DWC_MODE_SRP_CAPABLE_DEVICE 3
  57563. +#define DWC_MODE_NO_SRP_CAPABLE_DEVICE 4
  57564. +#define DWC_MODE_SRP_CAPABLE_HOST 5
  57565. +#define DWC_MODE_NO_SRP_CAPABLE_HOST 6
  57566. +
  57567. + /**User HW Config3 Register (Read Only). <i>Offset: 04Ch</i> */
  57568. + volatile uint32_t ghwcfg3;
  57569. + /**User HW Config4 Register (Read Only). <i>Offset: 050h</i>*/
  57570. + volatile uint32_t ghwcfg4;
  57571. + /** Core LPM Configuration register <i>Offset: 054h</i>*/
  57572. + volatile uint32_t glpmcfg;
  57573. + /** Global PowerDn Register <i>Offset: 058h</i> */
  57574. + volatile uint32_t gpwrdn;
  57575. + /** Global DFIFO SW Config Register <i>Offset: 05Ch</i> */
  57576. + volatile uint32_t gdfifocfg;
  57577. + /** ADP Control Register <i>Offset: 060h</i> */
  57578. + volatile uint32_t adpctl;
  57579. + /** Reserved <i>Offset: 064h-0FFh</i> */
  57580. + volatile uint32_t reserved39[39];
  57581. + /** Host Periodic Transmit FIFO Size Register. <i>Offset: 100h</i> */
  57582. + volatile uint32_t hptxfsiz;
  57583. + /** Device Periodic Transmit FIFO#n Register if dedicated fifos are disabled,
  57584. + otherwise Device Transmit FIFO#n Register.
  57585. + * <i>Offset: 104h + (FIFO_Number-1)*04h, 1 <= FIFO Number <= 15 (1<=n<=15).</i> */
  57586. + volatile uint32_t dtxfsiz[15];
  57587. +} dwc_otg_core_global_regs_t;
  57588. +
  57589. +/**
  57590. + * This union represents the bit fields of the Core OTG Control
  57591. + * and Status Register (GOTGCTL). Set the bits using the bit
  57592. + * fields then write the <i>d32</i> value to the register.
  57593. + */
  57594. +typedef union gotgctl_data {
  57595. + /** raw register data */
  57596. + uint32_t d32;
  57597. + /** register bits */
  57598. + struct {
  57599. + unsigned sesreqscs:1;
  57600. + unsigned sesreq:1;
  57601. + unsigned vbvalidoven:1;
  57602. + unsigned vbvalidovval:1;
  57603. + unsigned avalidoven:1;
  57604. + unsigned avalidovval:1;
  57605. + unsigned bvalidoven:1;
  57606. + unsigned bvalidovval:1;
  57607. + unsigned hstnegscs:1;
  57608. + unsigned hnpreq:1;
  57609. + unsigned hstsethnpen:1;
  57610. + unsigned devhnpen:1;
  57611. + unsigned reserved12_15:4;
  57612. + unsigned conidsts:1;
  57613. + unsigned dbnctime:1;
  57614. + unsigned asesvld:1;
  57615. + unsigned bsesvld:1;
  57616. + unsigned otgver:1;
  57617. + unsigned reserved1:1;
  57618. + unsigned multvalidbc:5;
  57619. + unsigned chirpen:1;
  57620. + unsigned reserved28_31:4;
  57621. + } b;
  57622. +} gotgctl_data_t;
  57623. +
  57624. +/**
  57625. + * This union represents the bit fields of the Core OTG Interrupt Register
  57626. + * (GOTGINT). Set/clear the bits using the bit fields then write the <i>d32</i>
  57627. + * value to the register.
  57628. + */
  57629. +typedef union gotgint_data {
  57630. + /** raw register data */
  57631. + uint32_t d32;
  57632. + /** register bits */
  57633. + struct {
  57634. + /** Current Mode */
  57635. + unsigned reserved0_1:2;
  57636. +
  57637. + /** Session End Detected */
  57638. + unsigned sesenddet:1;
  57639. +
  57640. + unsigned reserved3_7:5;
  57641. +
  57642. + /** Session Request Success Status Change */
  57643. + unsigned sesreqsucstschng:1;
  57644. + /** Host Negotiation Success Status Change */
  57645. + unsigned hstnegsucstschng:1;
  57646. +
  57647. + unsigned reserved10_16:7;
  57648. +
  57649. + /** Host Negotiation Detected */
  57650. + unsigned hstnegdet:1;
  57651. + /** A-Device Timeout Change */
  57652. + unsigned adevtoutchng:1;
  57653. + /** Debounce Done */
  57654. + unsigned debdone:1;
  57655. + /** Multi-Valued input changed */
  57656. + unsigned mvic:1;
  57657. +
  57658. + unsigned reserved31_21:11;
  57659. +
  57660. + } b;
  57661. +} gotgint_data_t;
  57662. +
  57663. +/**
  57664. + * This union represents the bit fields of the Core AHB Configuration
  57665. + * Register (GAHBCFG). Set/clear the bits using the bit fields then
  57666. + * write the <i>d32</i> value to the register.
  57667. + */
  57668. +typedef union gahbcfg_data {
  57669. + /** raw register data */
  57670. + uint32_t d32;
  57671. + /** register bits */
  57672. + struct {
  57673. + unsigned glblintrmsk:1;
  57674. +#define DWC_GAHBCFG_GLBINT_ENABLE 1
  57675. +
  57676. + unsigned hburstlen:4;
  57677. +#define DWC_GAHBCFG_INT_DMA_BURST_SINGLE 0
  57678. +#define DWC_GAHBCFG_INT_DMA_BURST_INCR 1
  57679. +#define DWC_GAHBCFG_INT_DMA_BURST_INCR4 3
  57680. +#define DWC_GAHBCFG_INT_DMA_BURST_INCR8 5
  57681. +#define DWC_GAHBCFG_INT_DMA_BURST_INCR16 7
  57682. +
  57683. + unsigned dmaenable:1;
  57684. +#define DWC_GAHBCFG_DMAENABLE 1
  57685. + unsigned reserved:1;
  57686. + unsigned nptxfemplvl_txfemplvl:1;
  57687. + unsigned ptxfemplvl:1;
  57688. +#define DWC_GAHBCFG_TXFEMPTYLVL_EMPTY 1
  57689. +#define DWC_GAHBCFG_TXFEMPTYLVL_HALFEMPTY 0
  57690. + unsigned reserved9_20:12;
  57691. + unsigned remmemsupp:1;
  57692. + unsigned notialldmawrit:1;
  57693. + unsigned ahbsingle:1;
  57694. + unsigned reserved24_31:8;
  57695. + } b;
  57696. +} gahbcfg_data_t;
  57697. +
  57698. +/**
  57699. + * This union represents the bit fields of the Core USB Configuration
  57700. + * Register (GUSBCFG). Set the bits using the bit fields then write
  57701. + * the <i>d32</i> value to the register.
  57702. + */
  57703. +typedef union gusbcfg_data {
  57704. + /** raw register data */
  57705. + uint32_t d32;
  57706. + /** register bits */
  57707. + struct {
  57708. + unsigned toutcal:3;
  57709. + unsigned phyif:1;
  57710. + unsigned ulpi_utmi_sel:1;
  57711. + unsigned fsintf:1;
  57712. + unsigned physel:1;
  57713. + unsigned ddrsel:1;
  57714. + unsigned srpcap:1;
  57715. + unsigned hnpcap:1;
  57716. + unsigned usbtrdtim:4;
  57717. + unsigned reserved1:1;
  57718. + unsigned phylpwrclksel:1;
  57719. + unsigned otgutmifssel:1;
  57720. + unsigned ulpi_fsls:1;
  57721. + unsigned ulpi_auto_res:1;
  57722. + unsigned ulpi_clk_sus_m:1;
  57723. + unsigned ulpi_ext_vbus_drv:1;
  57724. + unsigned ulpi_int_vbus_indicator:1;
  57725. + unsigned term_sel_dl_pulse:1;
  57726. + unsigned indicator_complement:1;
  57727. + unsigned indicator_pass_through:1;
  57728. + unsigned ulpi_int_prot_dis:1;
  57729. + unsigned ic_usb_cap:1;
  57730. + unsigned ic_traffic_pull_remove:1;
  57731. + unsigned tx_end_delay:1;
  57732. + unsigned force_host_mode:1;
  57733. + unsigned force_dev_mode:1;
  57734. + unsigned reserved31:1;
  57735. + } b;
  57736. +} gusbcfg_data_t;
  57737. +
  57738. +/**
  57739. + * This union represents the bit fields of the Core Reset Register
  57740. + * (GRSTCTL). Set/clear the bits using the bit fields then write the
  57741. + * <i>d32</i> value to the register.
  57742. + */
  57743. +typedef union grstctl_data {
  57744. + /** raw register data */
  57745. + uint32_t d32;
  57746. + /** register bits */
  57747. + struct {
  57748. + /** Core Soft Reset (CSftRst) (Device and Host)
  57749. + *
  57750. + * The application can flush the control logic in the
  57751. + * entire core using this bit. This bit resets the
  57752. + * pipelines in the AHB Clock domain as well as the
  57753. + * PHY Clock domain.
  57754. + *
  57755. + * The state machines are reset to an IDLE state, the
  57756. + * control bits in the CSRs are cleared, all the
  57757. + * transmit FIFOs and the receive FIFO are flushed.
  57758. + *
  57759. + * The status mask bits that control the generation of
  57760. + * the interrupt, are cleared, to clear the
  57761. + * interrupt. The interrupt status bits are not
  57762. + * cleared, so the application can get the status of
  57763. + * any events that occurred in the core after it has
  57764. + * set this bit.
  57765. + *
  57766. + * Any transactions on the AHB are terminated as soon
  57767. + * as possible following the protocol. Any
  57768. + * transactions on the USB are terminated immediately.
  57769. + *
  57770. + * The configuration settings in the CSRs are
  57771. + * unchanged, so the software doesn't have to
  57772. + * reprogram these registers (Device
  57773. + * Configuration/Host Configuration/Core System
  57774. + * Configuration/Core PHY Configuration).
  57775. + *
  57776. + * The application can write to this bit, any time it
  57777. + * wants to reset the core. This is a self clearing
  57778. + * bit and the core clears this bit after all the
  57779. + * necessary logic is reset in the core, which may
  57780. + * take several clocks, depending on the current state
  57781. + * of the core.
  57782. + */
  57783. + unsigned csftrst:1;
  57784. + /** Hclk Soft Reset
  57785. + *
  57786. + * The application uses this bit to reset the control logic in
  57787. + * the AHB clock domain. Only AHB clock domain pipelines are
  57788. + * reset.
  57789. + */
  57790. + unsigned hsftrst:1;
  57791. + /** Host Frame Counter Reset (Host Only)<br>
  57792. + *
  57793. + * The application can reset the (micro)frame number
  57794. + * counter inside the core, using this bit. When the
  57795. + * (micro)frame counter is reset, the subsequent SOF
  57796. + * sent out by the core, will have a (micro)frame
  57797. + * number of 0.
  57798. + */
  57799. + unsigned hstfrm:1;
  57800. + /** In Token Sequence Learning Queue Flush
  57801. + * (INTknQFlsh) (Device Only)
  57802. + */
  57803. + unsigned intknqflsh:1;
  57804. + /** RxFIFO Flush (RxFFlsh) (Device and Host)
  57805. + *
  57806. + * The application can flush the entire Receive FIFO
  57807. + * using this bit. The application must first
  57808. + * ensure that the core is not in the middle of a
  57809. + * transaction. The application should write into
  57810. + * this bit, only after making sure that neither the
  57811. + * DMA engine is reading from the RxFIFO nor the MAC
  57812. + * is writing the data in to the FIFO. The
  57813. + * application should wait until the bit is cleared
  57814. + * before performing any other operations. This bit
  57815. + * will takes 8 clocks (slowest of PHY or AHB clock)
  57816. + * to clear.
  57817. + */
  57818. + unsigned rxfflsh:1;
  57819. + /** TxFIFO Flush (TxFFlsh) (Device and Host).
  57820. + *
  57821. + * This bit is used to selectively flush a single or
  57822. + * all transmit FIFOs. The application must first
  57823. + * ensure that the core is not in the middle of a
  57824. + * transaction. The application should write into
  57825. + * this bit, only after making sure that neither the
  57826. + * DMA engine is writing into the TxFIFO nor the MAC
  57827. + * is reading the data out of the FIFO. The
  57828. + * application should wait until the core clears this
  57829. + * bit, before performing any operations. This bit
  57830. + * will takes 8 clocks (slowest of PHY or AHB clock)
  57831. + * to clear.
  57832. + */
  57833. + unsigned txfflsh:1;
  57834. +
  57835. + /** TxFIFO Number (TxFNum) (Device and Host).
  57836. + *
  57837. + * This is the FIFO number which needs to be flushed,
  57838. + * using the TxFIFO Flush bit. This field should not
  57839. + * be changed until the TxFIFO Flush bit is cleared by
  57840. + * the core.
  57841. + * - 0x0 : Non Periodic TxFIFO Flush
  57842. + * - 0x1 : Periodic TxFIFO #1 Flush in device mode
  57843. + * or Periodic TxFIFO in host mode
  57844. + * - 0x2 : Periodic TxFIFO #2 Flush in device mode.
  57845. + * - ...
  57846. + * - 0xF : Periodic TxFIFO #15 Flush in device mode
  57847. + * - 0x10: Flush all the Transmit NonPeriodic and
  57848. + * Transmit Periodic FIFOs in the core
  57849. + */
  57850. + unsigned txfnum:5;
  57851. + /** Reserved */
  57852. + unsigned reserved11_29:19;
  57853. + /** DMA Request Signal. Indicated DMA request is in
  57854. + * probress. Used for debug purpose. */
  57855. + unsigned dmareq:1;
  57856. + /** AHB Master Idle. Indicates the AHB Master State
  57857. + * Machine is in IDLE condition. */
  57858. + unsigned ahbidle:1;
  57859. + } b;
  57860. +} grstctl_t;
  57861. +
  57862. +/**
  57863. + * This union represents the bit fields of the Core Interrupt Mask
  57864. + * Register (GINTMSK). Set/clear the bits using the bit fields then
  57865. + * write the <i>d32</i> value to the register.
  57866. + */
  57867. +typedef union gintmsk_data {
  57868. + /** raw register data */
  57869. + uint32_t d32;
  57870. + /** register bits */
  57871. + struct {
  57872. + unsigned reserved0:1;
  57873. + unsigned modemismatch:1;
  57874. + unsigned otgintr:1;
  57875. + unsigned sofintr:1;
  57876. + unsigned rxstsqlvl:1;
  57877. + unsigned nptxfempty:1;
  57878. + unsigned ginnakeff:1;
  57879. + unsigned goutnakeff:1;
  57880. + unsigned ulpickint:1;
  57881. + unsigned i2cintr:1;
  57882. + unsigned erlysuspend:1;
  57883. + unsigned usbsuspend:1;
  57884. + unsigned usbreset:1;
  57885. + unsigned enumdone:1;
  57886. + unsigned isooutdrop:1;
  57887. + unsigned eopframe:1;
  57888. + unsigned restoredone:1;
  57889. + unsigned epmismatch:1;
  57890. + unsigned inepintr:1;
  57891. + unsigned outepintr:1;
  57892. + unsigned incomplisoin:1;
  57893. + unsigned incomplisoout:1;
  57894. + unsigned fetsusp:1;
  57895. + unsigned resetdet:1;
  57896. + unsigned portintr:1;
  57897. + unsigned hcintr:1;
  57898. + unsigned ptxfempty:1;
  57899. + unsigned lpmtranrcvd:1;
  57900. + unsigned conidstschng:1;
  57901. + unsigned disconnect:1;
  57902. + unsigned sessreqintr:1;
  57903. + unsigned wkupintr:1;
  57904. + } b;
  57905. +} gintmsk_data_t;
  57906. +/**
  57907. + * This union represents the bit fields of the Core Interrupt Register
  57908. + * (GINTSTS). Set/clear the bits using the bit fields then write the
  57909. + * <i>d32</i> value to the register.
  57910. + */
  57911. +typedef union gintsts_data {
  57912. + /** raw register data */
  57913. + uint32_t d32;
  57914. +#define DWC_SOF_INTR_MASK 0x0008
  57915. + /** register bits */
  57916. + struct {
  57917. +#define DWC_HOST_MODE 1
  57918. + unsigned curmode:1;
  57919. + unsigned modemismatch:1;
  57920. + unsigned otgintr:1;
  57921. + unsigned sofintr:1;
  57922. + unsigned rxstsqlvl:1;
  57923. + unsigned nptxfempty:1;
  57924. + unsigned ginnakeff:1;
  57925. + unsigned goutnakeff:1;
  57926. + unsigned ulpickint:1;
  57927. + unsigned i2cintr:1;
  57928. + unsigned erlysuspend:1;
  57929. + unsigned usbsuspend:1;
  57930. + unsigned usbreset:1;
  57931. + unsigned enumdone:1;
  57932. + unsigned isooutdrop:1;
  57933. + unsigned eopframe:1;
  57934. + unsigned restoredone:1;
  57935. + unsigned epmismatch:1;
  57936. + unsigned inepint:1;
  57937. + unsigned outepintr:1;
  57938. + unsigned incomplisoin:1;
  57939. + unsigned incomplisoout:1;
  57940. + unsigned fetsusp:1;
  57941. + unsigned resetdet:1;
  57942. + unsigned portintr:1;
  57943. + unsigned hcintr:1;
  57944. + unsigned ptxfempty:1;
  57945. + unsigned lpmtranrcvd:1;
  57946. + unsigned conidstschng:1;
  57947. + unsigned disconnect:1;
  57948. + unsigned sessreqintr:1;
  57949. + unsigned wkupintr:1;
  57950. + } b;
  57951. +} gintsts_data_t;
  57952. +
  57953. +/**
  57954. + * This union represents the bit fields in the Device Receive Status Read and
  57955. + * Pop Registers (GRXSTSR, GRXSTSP) Read the register into the <i>d32</i>
  57956. + * element then read out the bits using the <i>b</i>it elements.
  57957. + */
  57958. +typedef union device_grxsts_data {
  57959. + /** raw register data */
  57960. + uint32_t d32;
  57961. + /** register bits */
  57962. + struct {
  57963. + unsigned epnum:4;
  57964. + unsigned bcnt:11;
  57965. + unsigned dpid:2;
  57966. +
  57967. +#define DWC_STS_DATA_UPDT 0x2 // OUT Data Packet
  57968. +#define DWC_STS_XFER_COMP 0x3 // OUT Data Transfer Complete
  57969. +
  57970. +#define DWC_DSTS_GOUT_NAK 0x1 // Global OUT NAK
  57971. +#define DWC_DSTS_SETUP_COMP 0x4 // Setup Phase Complete
  57972. +#define DWC_DSTS_SETUP_UPDT 0x6 // SETUP Packet
  57973. + unsigned pktsts:4;
  57974. + unsigned fn:4;
  57975. + unsigned reserved25_31:7;
  57976. + } b;
  57977. +} device_grxsts_data_t;
  57978. +
  57979. +/**
  57980. + * This union represents the bit fields in the Host Receive Status Read and
  57981. + * Pop Registers (GRXSTSR, GRXSTSP) Read the register into the <i>d32</i>
  57982. + * element then read out the bits using the <i>b</i>it elements.
  57983. + */
  57984. +typedef union host_grxsts_data {
  57985. + /** raw register data */
  57986. + uint32_t d32;
  57987. + /** register bits */
  57988. + struct {
  57989. + unsigned chnum:4;
  57990. + unsigned bcnt:11;
  57991. + unsigned dpid:2;
  57992. +
  57993. + unsigned pktsts:4;
  57994. +#define DWC_GRXSTS_PKTSTS_IN 0x2
  57995. +#define DWC_GRXSTS_PKTSTS_IN_XFER_COMP 0x3
  57996. +#define DWC_GRXSTS_PKTSTS_DATA_TOGGLE_ERR 0x5
  57997. +#define DWC_GRXSTS_PKTSTS_CH_HALTED 0x7
  57998. +
  57999. + unsigned reserved21_31:11;
  58000. + } b;
  58001. +} host_grxsts_data_t;
  58002. +
  58003. +/**
  58004. + * This union represents the bit fields in the FIFO Size Registers (HPTXFSIZ,
  58005. + * GNPTXFSIZ, DPTXFSIZn, DIEPTXFn). Read the register into the <i>d32</i> element
  58006. + * then read out the bits using the <i>b</i>it elements.
  58007. + */
  58008. +typedef union fifosize_data {
  58009. + /** raw register data */
  58010. + uint32_t d32;
  58011. + /** register bits */
  58012. + struct {
  58013. + unsigned startaddr:16;
  58014. + unsigned depth:16;
  58015. + } b;
  58016. +} fifosize_data_t;
  58017. +
  58018. +/**
  58019. + * This union represents the bit fields in the Non-Periodic Transmit
  58020. + * FIFO/Queue Status Register (GNPTXSTS). Read the register into the
  58021. + * <i>d32</i> element then read out the bits using the <i>b</i>it
  58022. + * elements.
  58023. + */
  58024. +typedef union gnptxsts_data {
  58025. + /** raw register data */
  58026. + uint32_t d32;
  58027. + /** register bits */
  58028. + struct {
  58029. + unsigned nptxfspcavail:16;
  58030. + unsigned nptxqspcavail:8;
  58031. + /** Top of the Non-Periodic Transmit Request Queue
  58032. + * - bit 24 - Terminate (Last entry for the selected
  58033. + * channel/EP)
  58034. + * - bits 26:25 - Token Type
  58035. + * - 2'b00 - IN/OUT
  58036. + * - 2'b01 - Zero Length OUT
  58037. + * - 2'b10 - PING/Complete Split
  58038. + * - 2'b11 - Channel Halt
  58039. + * - bits 30:27 - Channel/EP Number
  58040. + */
  58041. + unsigned nptxqtop_terminate:1;
  58042. + unsigned nptxqtop_token:2;
  58043. + unsigned nptxqtop_chnep:4;
  58044. + unsigned reserved:1;
  58045. + } b;
  58046. +} gnptxsts_data_t;
  58047. +
  58048. +/**
  58049. + * This union represents the bit fields in the Transmit
  58050. + * FIFO Status Register (DTXFSTS). Read the register into the
  58051. + * <i>d32</i> element then read out the bits using the <i>b</i>it
  58052. + * elements.
  58053. + */
  58054. +typedef union dtxfsts_data {
  58055. + /** raw register data */
  58056. + uint32_t d32;
  58057. + /** register bits */
  58058. + struct {
  58059. + unsigned txfspcavail:16;
  58060. + unsigned reserved:16;
  58061. + } b;
  58062. +} dtxfsts_data_t;
  58063. +
  58064. +/**
  58065. + * This union represents the bit fields in the I2C Control Register
  58066. + * (I2CCTL). Read the register into the <i>d32</i> element then read out the
  58067. + * bits using the <i>b</i>it elements.
  58068. + */
  58069. +typedef union gi2cctl_data {
  58070. + /** raw register data */
  58071. + uint32_t d32;
  58072. + /** register bits */
  58073. + struct {
  58074. + unsigned rwdata:8;
  58075. + unsigned regaddr:8;
  58076. + unsigned addr:7;
  58077. + unsigned i2cen:1;
  58078. + unsigned ack:1;
  58079. + unsigned i2csuspctl:1;
  58080. + unsigned i2cdevaddr:2;
  58081. + unsigned i2cdatse0:1;
  58082. + unsigned reserved:1;
  58083. + unsigned rw:1;
  58084. + unsigned bsydne:1;
  58085. + } b;
  58086. +} gi2cctl_data_t;
  58087. +
  58088. +/**
  58089. + * This union represents the bit fields in the PHY Vendor Control Register
  58090. + * (GPVNDCTL). Read the register into the <i>d32</i> element then read out the
  58091. + * bits using the <i>b</i>it elements.
  58092. + */
  58093. +typedef union gpvndctl_data {
  58094. + /** raw register data */
  58095. + uint32_t d32;
  58096. + /** register bits */
  58097. + struct {
  58098. + unsigned regdata:8;
  58099. + unsigned vctrl:8;
  58100. + unsigned regaddr16_21:6;
  58101. + unsigned regwr:1;
  58102. + unsigned reserved23_24:2;
  58103. + unsigned newregreq:1;
  58104. + unsigned vstsbsy:1;
  58105. + unsigned vstsdone:1;
  58106. + unsigned reserved28_30:3;
  58107. + unsigned disulpidrvr:1;
  58108. + } b;
  58109. +} gpvndctl_data_t;
  58110. +
  58111. +/**
  58112. + * This union represents the bit fields in the General Purpose
  58113. + * Input/Output Register (GGPIO).
  58114. + * Read the register into the <i>d32</i> element then read out the
  58115. + * bits using the <i>b</i>it elements.
  58116. + */
  58117. +typedef union ggpio_data {
  58118. + /** raw register data */
  58119. + uint32_t d32;
  58120. + /** register bits */
  58121. + struct {
  58122. + unsigned gpi:16;
  58123. + unsigned gpo:16;
  58124. + } b;
  58125. +} ggpio_data_t;
  58126. +
  58127. +/**
  58128. + * This union represents the bit fields in the User ID Register
  58129. + * (GUID). Read the register into the <i>d32</i> element then read out the
  58130. + * bits using the <i>b</i>it elements.
  58131. + */
  58132. +typedef union guid_data {
  58133. + /** raw register data */
  58134. + uint32_t d32;
  58135. + /** register bits */
  58136. + struct {
  58137. + unsigned rwdata:32;
  58138. + } b;
  58139. +} guid_data_t;
  58140. +
  58141. +/**
  58142. + * This union represents the bit fields in the Synopsys ID Register
  58143. + * (GSNPSID). Read the register into the <i>d32</i> element then read out the
  58144. + * bits using the <i>b</i>it elements.
  58145. + */
  58146. +typedef union gsnpsid_data {
  58147. + /** raw register data */
  58148. + uint32_t d32;
  58149. + /** register bits */
  58150. + struct {
  58151. + unsigned rwdata:32;
  58152. + } b;
  58153. +} gsnpsid_data_t;
  58154. +
  58155. +/**
  58156. + * This union represents the bit fields in the User HW Config1
  58157. + * Register. Read the register into the <i>d32</i> element then read
  58158. + * out the bits using the <i>b</i>it elements.
  58159. + */
  58160. +typedef union hwcfg1_data {
  58161. + /** raw register data */
  58162. + uint32_t d32;
  58163. + /** register bits */
  58164. + struct {
  58165. + unsigned ep_dir0:2;
  58166. + unsigned ep_dir1:2;
  58167. + unsigned ep_dir2:2;
  58168. + unsigned ep_dir3:2;
  58169. + unsigned ep_dir4:2;
  58170. + unsigned ep_dir5:2;
  58171. + unsigned ep_dir6:2;
  58172. + unsigned ep_dir7:2;
  58173. + unsigned ep_dir8:2;
  58174. + unsigned ep_dir9:2;
  58175. + unsigned ep_dir10:2;
  58176. + unsigned ep_dir11:2;
  58177. + unsigned ep_dir12:2;
  58178. + unsigned ep_dir13:2;
  58179. + unsigned ep_dir14:2;
  58180. + unsigned ep_dir15:2;
  58181. + } b;
  58182. +} hwcfg1_data_t;
  58183. +
  58184. +/**
  58185. + * This union represents the bit fields in the User HW Config2
  58186. + * Register. Read the register into the <i>d32</i> element then read
  58187. + * out the bits using the <i>b</i>it elements.
  58188. + */
  58189. +typedef union hwcfg2_data {
  58190. + /** raw register data */
  58191. + uint32_t d32;
  58192. + /** register bits */
  58193. + struct {
  58194. + /* GHWCFG2 */
  58195. + unsigned op_mode:3;
  58196. +#define DWC_HWCFG2_OP_MODE_HNP_SRP_CAPABLE_OTG 0
  58197. +#define DWC_HWCFG2_OP_MODE_SRP_ONLY_CAPABLE_OTG 1
  58198. +#define DWC_HWCFG2_OP_MODE_NO_HNP_SRP_CAPABLE_OTG 2
  58199. +#define DWC_HWCFG2_OP_MODE_SRP_CAPABLE_DEVICE 3
  58200. +#define DWC_HWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE 4
  58201. +#define DWC_HWCFG2_OP_MODE_SRP_CAPABLE_HOST 5
  58202. +#define DWC_HWCFG2_OP_MODE_NO_SRP_CAPABLE_HOST 6
  58203. +
  58204. + unsigned architecture:2;
  58205. + unsigned point2point:1;
  58206. + unsigned hs_phy_type:2;
  58207. +#define DWC_HWCFG2_HS_PHY_TYPE_NOT_SUPPORTED 0
  58208. +#define DWC_HWCFG2_HS_PHY_TYPE_UTMI 1
  58209. +#define DWC_HWCFG2_HS_PHY_TYPE_ULPI 2
  58210. +#define DWC_HWCFG2_HS_PHY_TYPE_UTMI_ULPI 3
  58211. +
  58212. + unsigned fs_phy_type:2;
  58213. + unsigned num_dev_ep:4;
  58214. + unsigned num_host_chan:4;
  58215. + unsigned perio_ep_supported:1;
  58216. + unsigned dynamic_fifo:1;
  58217. + unsigned multi_proc_int:1;
  58218. + unsigned reserved21:1;
  58219. + unsigned nonperio_tx_q_depth:2;
  58220. + unsigned host_perio_tx_q_depth:2;
  58221. + unsigned dev_token_q_depth:5;
  58222. + unsigned otg_enable_ic_usb:1;
  58223. + } b;
  58224. +} hwcfg2_data_t;
  58225. +
  58226. +/**
  58227. + * This union represents the bit fields in the User HW Config3
  58228. + * Register. Read the register into the <i>d32</i> element then read
  58229. + * out the bits using the <i>b</i>it elements.
  58230. + */
  58231. +typedef union hwcfg3_data {
  58232. + /** raw register data */
  58233. + uint32_t d32;
  58234. + /** register bits */
  58235. + struct {
  58236. + /* GHWCFG3 */
  58237. + unsigned xfer_size_cntr_width:4;
  58238. + unsigned packet_size_cntr_width:3;
  58239. + unsigned otg_func:1;
  58240. + unsigned i2c:1;
  58241. + unsigned vendor_ctrl_if:1;
  58242. + unsigned optional_features:1;
  58243. + unsigned synch_reset_type:1;
  58244. + unsigned adp_supp:1;
  58245. + unsigned otg_enable_hsic:1;
  58246. + unsigned bc_support:1;
  58247. + unsigned otg_lpm_en:1;
  58248. + unsigned dfifo_depth:16;
  58249. + } b;
  58250. +} hwcfg3_data_t;
  58251. +
  58252. +/**
  58253. + * This union represents the bit fields in the User HW Config4
  58254. + * Register. Read the register into the <i>d32</i> element then read
  58255. + * out the bits using the <i>b</i>it elements.
  58256. + */
  58257. +typedef union hwcfg4_data {
  58258. + /** raw register data */
  58259. + uint32_t d32;
  58260. + /** register bits */
  58261. + struct {
  58262. + unsigned num_dev_perio_in_ep:4;
  58263. + unsigned power_optimiz:1;
  58264. + unsigned min_ahb_freq:1;
  58265. + unsigned hiber:1;
  58266. + unsigned xhiber:1;
  58267. + unsigned reserved:6;
  58268. + unsigned utmi_phy_data_width:2;
  58269. + unsigned num_dev_mode_ctrl_ep:4;
  58270. + unsigned iddig_filt_en:1;
  58271. + unsigned vbus_valid_filt_en:1;
  58272. + unsigned a_valid_filt_en:1;
  58273. + unsigned b_valid_filt_en:1;
  58274. + unsigned session_end_filt_en:1;
  58275. + unsigned ded_fifo_en:1;
  58276. + unsigned num_in_eps:4;
  58277. + unsigned desc_dma:1;
  58278. + unsigned desc_dma_dyn:1;
  58279. + } b;
  58280. +} hwcfg4_data_t;
  58281. +
  58282. +/**
  58283. + * This union represents the bit fields of the Core LPM Configuration
  58284. + * Register (GLPMCFG). Set the bits using bit fields then write
  58285. + * the <i>d32</i> value to the register.
  58286. + */
  58287. +typedef union glpmctl_data {
  58288. + /** raw register data */
  58289. + uint32_t d32;
  58290. + /** register bits */
  58291. + struct {
  58292. + /** LPM-Capable (LPMCap) (Device and Host)
  58293. + * The application uses this bit to control
  58294. + * the DWC_otg core LPM capabilities.
  58295. + */
  58296. + unsigned lpm_cap_en:1;
  58297. + /** LPM response programmed by application (AppL1Res) (Device)
  58298. + * Handshake response to LPM token pre-programmed
  58299. + * by device application software.
  58300. + */
  58301. + unsigned appl_resp:1;
  58302. + /** Host Initiated Resume Duration (HIRD) (Device and Host)
  58303. + * In Host mode this field indicates the value of HIRD
  58304. + * to be sent in an LPM transaction.
  58305. + * In Device mode this field is updated with the
  58306. + * Received LPM Token HIRD bmAttribute
  58307. + * when an ACK/NYET/STALL response is sent
  58308. + * to an LPM transaction.
  58309. + */
  58310. + unsigned hird:4;
  58311. + /** RemoteWakeEnable (bRemoteWake) (Device and Host)
  58312. + * In Host mode this bit indicates the value of remote
  58313. + * wake up to be sent in wIndex field of LPM transaction.
  58314. + * In Device mode this field is updated with the
  58315. + * Received LPM Token bRemoteWake bmAttribute
  58316. + * when an ACK/NYET/STALL response is sent
  58317. + * to an LPM transaction.
  58318. + */
  58319. + unsigned rem_wkup_en:1;
  58320. + /** Enable utmi_sleep_n (EnblSlpM) (Device and Host)
  58321. + * The application uses this bit to control
  58322. + * the utmi_sleep_n assertion to the PHY when in L1 state.
  58323. + */
  58324. + unsigned en_utmi_sleep:1;
  58325. + /** HIRD Threshold (HIRD_Thres) (Device and Host)
  58326. + */
  58327. + unsigned hird_thres:5;
  58328. + /** LPM Response (CoreL1Res) (Device and Host)
  58329. + * In Host mode this bit contains handsake response to
  58330. + * LPM transaction.
  58331. + * In Device mode the response of the core to
  58332. + * LPM transaction received is reflected in these two bits.
  58333. + - 0x0 : ERROR (No handshake response)
  58334. + - 0x1 : STALL
  58335. + - 0x2 : NYET
  58336. + - 0x3 : ACK
  58337. + */
  58338. + unsigned lpm_resp:2;
  58339. + /** Port Sleep Status (SlpSts) (Device and Host)
  58340. + * This bit is set as long as a Sleep condition
  58341. + * is present on the USB bus.
  58342. + */
  58343. + unsigned prt_sleep_sts:1;
  58344. + /** Sleep State Resume OK (L1ResumeOK) (Device and Host)
  58345. + * Indicates that the application or host
  58346. + * can start resume from Sleep state.
  58347. + */
  58348. + unsigned sleep_state_resumeok:1;
  58349. + /** LPM channel Index (LPM_Chnl_Indx) (Host)
  58350. + * The channel number on which the LPM transaction
  58351. + * has to be applied while sending
  58352. + * an LPM transaction to the local device.
  58353. + */
  58354. + unsigned lpm_chan_index:4;
  58355. + /** LPM Retry Count (LPM_Retry_Cnt) (Host)
  58356. + * Number host retries that would be performed
  58357. + * if the device response was not valid response.
  58358. + */
  58359. + unsigned retry_count:3;
  58360. + /** Send LPM Transaction (SndLPM) (Host)
  58361. + * When set by application software,
  58362. + * an LPM transaction containing two tokens
  58363. + * is sent.
  58364. + */
  58365. + unsigned send_lpm:1;
  58366. + /** LPM Retry status (LPM_RetryCnt_Sts) (Host)
  58367. + * Number of LPM Host Retries still remaining
  58368. + * to be transmitted for the current LPM sequence
  58369. + */
  58370. + unsigned retry_count_sts:3;
  58371. + unsigned reserved28_29:2;
  58372. + /** In host mode once this bit is set, the host
  58373. + * configures to drive the HSIC Idle state on the bus.
  58374. + * It then waits for the device to initiate the Connect sequence.
  58375. + * In device mode once this bit is set, the device waits for
  58376. + * the HSIC Idle line state on the bus. Upon receving the Idle
  58377. + * line state, it initiates the HSIC Connect sequence.
  58378. + */
  58379. + unsigned hsic_connect:1;
  58380. + /** This bit overrides and functionally inverts
  58381. + * the if_select_hsic input port signal.
  58382. + */
  58383. + unsigned inv_sel_hsic:1;
  58384. + } b;
  58385. +} glpmcfg_data_t;
  58386. +
  58387. +/**
  58388. + * This union represents the bit fields of the Core ADP Timer, Control and
  58389. + * Status Register (ADPTIMCTLSTS). Set the bits using bit fields then write
  58390. + * the <i>d32</i> value to the register.
  58391. + */
  58392. +typedef union adpctl_data {
  58393. + /** raw register data */
  58394. + uint32_t d32;
  58395. + /** register bits */
  58396. + struct {
  58397. + /** Probe Discharge (PRB_DSCHG)
  58398. + * These bits set the times for TADP_DSCHG.
  58399. + * These bits are defined as follows:
  58400. + * 2'b00 - 4 msec
  58401. + * 2'b01 - 8 msec
  58402. + * 2'b10 - 16 msec
  58403. + * 2'b11 - 32 msec
  58404. + */
  58405. + unsigned prb_dschg:2;
  58406. + /** Probe Delta (PRB_DELTA)
  58407. + * These bits set the resolution for RTIM value.
  58408. + * The bits are defined in units of 32 kHz clock cycles as follows:
  58409. + * 2'b00 - 1 cycles
  58410. + * 2'b01 - 2 cycles
  58411. + * 2'b10 - 3 cycles
  58412. + * 2'b11 - 4 cycles
  58413. + * For example if this value is chosen to 2'b01, it means that RTIM
  58414. + * increments for every 3(three) 32Khz clock cycles.
  58415. + */
  58416. + unsigned prb_delta:2;
  58417. + /** Probe Period (PRB_PER)
  58418. + * These bits sets the TADP_PRD as shown in Figure 4 as follows:
  58419. + * 2'b00 - 0.625 to 0.925 sec (typical 0.775 sec)
  58420. + * 2'b01 - 1.25 to 1.85 sec (typical 1.55 sec)
  58421. + * 2'b10 - 1.9 to 2.6 sec (typical 2.275 sec)
  58422. + * 2'b11 - Reserved
  58423. + */
  58424. + unsigned prb_per:2;
  58425. + /** These bits capture the latest time it took for VBUS to ramp from
  58426. + * VADP_SINK to VADP_PRB.
  58427. + * 0x000 - 1 cycles
  58428. + * 0x001 - 2 cycles
  58429. + * 0x002 - 3 cycles
  58430. + * etc
  58431. + * 0x7FF - 2048 cycles
  58432. + * A time of 1024 cycles at 32 kHz corresponds to a time of 32 msec.
  58433. + */
  58434. + unsigned rtim:11;
  58435. + /** Enable Probe (EnaPrb)
  58436. + * When programmed to 1'b1, the core performs a probe operation.
  58437. + * This bit is valid only if OTG_Ver = 1'b1.
  58438. + */
  58439. + unsigned enaprb:1;
  58440. + /** Enable Sense (EnaSns)
  58441. + * When programmed to 1'b1, the core performs a Sense operation.
  58442. + * This bit is valid only if OTG_Ver = 1'b1.
  58443. + */
  58444. + unsigned enasns:1;
  58445. + /** ADP Reset (ADPRes)
  58446. + * When set, ADP controller is reset.
  58447. + * This bit is valid only if OTG_Ver = 1'b1.
  58448. + */
  58449. + unsigned adpres:1;
  58450. + /** ADP Enable (ADPEn)
  58451. + * When set, the core performs either ADP probing or sensing
  58452. + * based on EnaPrb or EnaSns.
  58453. + * This bit is valid only if OTG_Ver = 1'b1.
  58454. + */
  58455. + unsigned adpen:1;
  58456. + /** ADP Probe Interrupt (ADP_PRB_INT)
  58457. + * When this bit is set, it means that the VBUS
  58458. + * voltage is greater than VADP_PRB or VADP_PRB is reached.
  58459. + * This bit is valid only if OTG_Ver = 1'b1.
  58460. + */
  58461. + unsigned adp_prb_int:1;
  58462. + /**
  58463. + * ADP Sense Interrupt (ADP_SNS_INT)
  58464. + * When this bit is set, it means that the VBUS voltage is greater than
  58465. + * VADP_SNS value or VADP_SNS is reached.
  58466. + * This bit is valid only if OTG_Ver = 1'b1.
  58467. + */
  58468. + unsigned adp_sns_int:1;
  58469. + /** ADP Tomeout Interrupt (ADP_TMOUT_INT)
  58470. + * This bit is relevant only for an ADP probe.
  58471. + * When this bit is set, it means that the ramp time has
  58472. + * completed ie ADPCTL.RTIM has reached its terminal value
  58473. + * of 0x7FF. This is a debug feature that allows software
  58474. + * to read the ramp time after each cycle.
  58475. + * This bit is valid only if OTG_Ver = 1'b1.
  58476. + */
  58477. + unsigned adp_tmout_int:1;
  58478. + /** ADP Probe Interrupt Mask (ADP_PRB_INT_MSK)
  58479. + * When this bit is set, it unmasks the interrupt due to ADP_PRB_INT.
  58480. + * This bit is valid only if OTG_Ver = 1'b1.
  58481. + */
  58482. + unsigned adp_prb_int_msk:1;
  58483. + /** ADP Sense Interrupt Mask (ADP_SNS_INT_MSK)
  58484. + * When this bit is set, it unmasks the interrupt due to ADP_SNS_INT.
  58485. + * This bit is valid only if OTG_Ver = 1'b1.
  58486. + */
  58487. + unsigned adp_sns_int_msk:1;
  58488. + /** ADP Timoeout Interrupt Mask (ADP_TMOUT_MSK)
  58489. + * When this bit is set, it unmasks the interrupt due to ADP_TMOUT_INT.
  58490. + * This bit is valid only if OTG_Ver = 1'b1.
  58491. + */
  58492. + unsigned adp_tmout_int_msk:1;
  58493. + /** Access Request
  58494. + * 2'b00 - Read/Write Valid (updated by the core)
  58495. + * 2'b01 - Read
  58496. + * 2'b00 - Write
  58497. + * 2'b00 - Reserved
  58498. + */
  58499. + unsigned ar:2;
  58500. + /** Reserved */
  58501. + unsigned reserved29_31:3;
  58502. + } b;
  58503. +} adpctl_data_t;
  58504. +
  58505. +////////////////////////////////////////////
  58506. +// Device Registers
  58507. +/**
  58508. + * Device Global Registers. <i>Offsets 800h-BFFh</i>
  58509. + *
  58510. + * The following structures define the size and relative field offsets
  58511. + * for the Device Mode Registers.
  58512. + *
  58513. + * <i>These registers are visible only in Device mode and must not be
  58514. + * accessed in Host mode, as the results are unknown.</i>
  58515. + */
  58516. +typedef struct dwc_otg_dev_global_regs {
  58517. + /** Device Configuration Register. <i>Offset 800h</i> */
  58518. + volatile uint32_t dcfg;
  58519. + /** Device Control Register. <i>Offset: 804h</i> */
  58520. + volatile uint32_t dctl;
  58521. + /** Device Status Register (Read Only). <i>Offset: 808h</i> */
  58522. + volatile uint32_t dsts;
  58523. + /** Reserved. <i>Offset: 80Ch</i> */
  58524. + uint32_t unused;
  58525. + /** Device IN Endpoint Common Interrupt Mask
  58526. + * Register. <i>Offset: 810h</i> */
  58527. + volatile uint32_t diepmsk;
  58528. + /** Device OUT Endpoint Common Interrupt Mask
  58529. + * Register. <i>Offset: 814h</i> */
  58530. + volatile uint32_t doepmsk;
  58531. + /** Device All Endpoints Interrupt Register. <i>Offset: 818h</i> */
  58532. + volatile uint32_t daint;
  58533. + /** Device All Endpoints Interrupt Mask Register. <i>Offset:
  58534. + * 81Ch</i> */
  58535. + volatile uint32_t daintmsk;
  58536. + /** Device IN Token Queue Read Register-1 (Read Only).
  58537. + * <i>Offset: 820h</i> */
  58538. + volatile uint32_t dtknqr1;
  58539. + /** Device IN Token Queue Read Register-2 (Read Only).
  58540. + * <i>Offset: 824h</i> */
  58541. + volatile uint32_t dtknqr2;
  58542. + /** Device VBUS discharge Register. <i>Offset: 828h</i> */
  58543. + volatile uint32_t dvbusdis;
  58544. + /** Device VBUS Pulse Register. <i>Offset: 82Ch</i> */
  58545. + volatile uint32_t dvbuspulse;
  58546. + /** Device IN Token Queue Read Register-3 (Read Only). /
  58547. + * Device Thresholding control register (Read/Write)
  58548. + * <i>Offset: 830h</i> */
  58549. + volatile uint32_t dtknqr3_dthrctl;
  58550. + /** Device IN Token Queue Read Register-4 (Read Only). /
  58551. + * Device IN EPs empty Inr. Mask Register (Read/Write)
  58552. + * <i>Offset: 834h</i> */
  58553. + volatile uint32_t dtknqr4_fifoemptymsk;
  58554. + /** Device Each Endpoint Interrupt Register (Read Only). /
  58555. + * <i>Offset: 838h</i> */
  58556. + volatile uint32_t deachint;
  58557. + /** Device Each Endpoint Interrupt mask Register (Read/Write). /
  58558. + * <i>Offset: 83Ch</i> */
  58559. + volatile uint32_t deachintmsk;
  58560. + /** Device Each In Endpoint Interrupt mask Register (Read/Write). /
  58561. + * <i>Offset: 840h</i> */
  58562. + volatile uint32_t diepeachintmsk[MAX_EPS_CHANNELS];
  58563. + /** Device Each Out Endpoint Interrupt mask Register (Read/Write). /
  58564. + * <i>Offset: 880h</i> */
  58565. + volatile uint32_t doepeachintmsk[MAX_EPS_CHANNELS];
  58566. +} dwc_otg_device_global_regs_t;
  58567. +
  58568. +/**
  58569. + * This union represents the bit fields in the Device Configuration
  58570. + * Register. Read the register into the <i>d32</i> member then
  58571. + * set/clear the bits using the <i>b</i>it elements. Write the
  58572. + * <i>d32</i> member to the dcfg register.
  58573. + */
  58574. +typedef union dcfg_data {
  58575. + /** raw register data */
  58576. + uint32_t d32;
  58577. + /** register bits */
  58578. + struct {
  58579. + /** Device Speed */
  58580. + unsigned devspd:2;
  58581. + /** Non Zero Length Status OUT Handshake */
  58582. + unsigned nzstsouthshk:1;
  58583. +#define DWC_DCFG_SEND_STALL 1
  58584. +
  58585. + unsigned ena32khzs:1;
  58586. + /** Device Addresses */
  58587. + unsigned devaddr:7;
  58588. + /** Periodic Frame Interval */
  58589. + unsigned perfrint:2;
  58590. +#define DWC_DCFG_FRAME_INTERVAL_80 0
  58591. +#define DWC_DCFG_FRAME_INTERVAL_85 1
  58592. +#define DWC_DCFG_FRAME_INTERVAL_90 2
  58593. +#define DWC_DCFG_FRAME_INTERVAL_95 3
  58594. +
  58595. + /** Enable Device OUT NAK for bulk in DDMA mode */
  58596. + unsigned endevoutnak:1;
  58597. +
  58598. + unsigned reserved14_17:4;
  58599. + /** In Endpoint Mis-match count */
  58600. + unsigned epmscnt:5;
  58601. + /** Enable Descriptor DMA in Device mode */
  58602. + unsigned descdma:1;
  58603. + unsigned perschintvl:2;
  58604. + unsigned resvalid:6;
  58605. + } b;
  58606. +} dcfg_data_t;
  58607. +
  58608. +/**
  58609. + * This union represents the bit fields in the Device Control
  58610. + * Register. Read the register into the <i>d32</i> member then
  58611. + * set/clear the bits using the <i>b</i>it elements.
  58612. + */
  58613. +typedef union dctl_data {
  58614. + /** raw register data */
  58615. + uint32_t d32;
  58616. + /** register bits */
  58617. + struct {
  58618. + /** Remote Wakeup */
  58619. + unsigned rmtwkupsig:1;
  58620. + /** Soft Disconnect */
  58621. + unsigned sftdiscon:1;
  58622. + /** Global Non-Periodic IN NAK Status */
  58623. + unsigned gnpinnaksts:1;
  58624. + /** Global OUT NAK Status */
  58625. + unsigned goutnaksts:1;
  58626. + /** Test Control */
  58627. + unsigned tstctl:3;
  58628. + /** Set Global Non-Periodic IN NAK */
  58629. + unsigned sgnpinnak:1;
  58630. + /** Clear Global Non-Periodic IN NAK */
  58631. + unsigned cgnpinnak:1;
  58632. + /** Set Global OUT NAK */
  58633. + unsigned sgoutnak:1;
  58634. + /** Clear Global OUT NAK */
  58635. + unsigned cgoutnak:1;
  58636. + /** Power-On Programming Done */
  58637. + unsigned pwronprgdone:1;
  58638. + /** Reserved */
  58639. + unsigned reserved:1;
  58640. + /** Global Multi Count */
  58641. + unsigned gmc:2;
  58642. + /** Ignore Frame Number for ISOC EPs */
  58643. + unsigned ifrmnum:1;
  58644. + /** NAK on Babble */
  58645. + unsigned nakonbble:1;
  58646. + /** Enable Continue on BNA */
  58647. + unsigned encontonbna:1;
  58648. +
  58649. + unsigned reserved18_31:14;
  58650. + } b;
  58651. +} dctl_data_t;
  58652. +
  58653. +/**
  58654. + * This union represents the bit fields in the Device Status
  58655. + * Register. Read the register into the <i>d32</i> member then
  58656. + * set/clear the bits using the <i>b</i>it elements.
  58657. + */
  58658. +typedef union dsts_data {
  58659. + /** raw register data */
  58660. + uint32_t d32;
  58661. + /** register bits */
  58662. + struct {
  58663. + /** Suspend Status */
  58664. + unsigned suspsts:1;
  58665. + /** Enumerated Speed */
  58666. + unsigned enumspd:2;
  58667. +#define DWC_DSTS_ENUMSPD_HS_PHY_30MHZ_OR_60MHZ 0
  58668. +#define DWC_DSTS_ENUMSPD_FS_PHY_30MHZ_OR_60MHZ 1
  58669. +#define DWC_DSTS_ENUMSPD_LS_PHY_6MHZ 2
  58670. +#define DWC_DSTS_ENUMSPD_FS_PHY_48MHZ 3
  58671. + /** Erratic Error */
  58672. + unsigned errticerr:1;
  58673. + unsigned reserved4_7:4;
  58674. + /** Frame or Microframe Number of the received SOF */
  58675. + unsigned soffn:14;
  58676. + unsigned reserved22_31:10;
  58677. + } b;
  58678. +} dsts_data_t;
  58679. +
  58680. +/**
  58681. + * This union represents the bit fields in the Device IN EP Interrupt
  58682. + * Register and the Device IN EP Common Mask Register.
  58683. + *
  58684. + * - Read the register into the <i>d32</i> member then set/clear the
  58685. + * bits using the <i>b</i>it elements.
  58686. + */
  58687. +typedef union diepint_data {
  58688. + /** raw register data */
  58689. + uint32_t d32;
  58690. + /** register bits */
  58691. + struct {
  58692. + /** Transfer complete mask */
  58693. + unsigned xfercompl:1;
  58694. + /** Endpoint disable mask */
  58695. + unsigned epdisabled:1;
  58696. + /** AHB Error mask */
  58697. + unsigned ahberr:1;
  58698. + /** TimeOUT Handshake mask (non-ISOC EPs) */
  58699. + unsigned timeout:1;
  58700. + /** IN Token received with TxF Empty mask */
  58701. + unsigned intktxfemp:1;
  58702. + /** IN Token Received with EP mismatch mask */
  58703. + unsigned intknepmis:1;
  58704. + /** IN Endpoint NAK Effective mask */
  58705. + unsigned inepnakeff:1;
  58706. + /** Reserved */
  58707. + unsigned emptyintr:1;
  58708. +
  58709. + unsigned txfifoundrn:1;
  58710. +
  58711. + /** BNA Interrupt mask */
  58712. + unsigned bna:1;
  58713. +
  58714. + unsigned reserved10_12:3;
  58715. + /** BNA Interrupt mask */
  58716. + unsigned nak:1;
  58717. +
  58718. + unsigned reserved14_31:18;
  58719. + } b;
  58720. +} diepint_data_t;
  58721. +
  58722. +/**
  58723. + * This union represents the bit fields in the Device IN EP
  58724. + * Common/Dedicated Interrupt Mask Register.
  58725. + */
  58726. +typedef union diepint_data diepmsk_data_t;
  58727. +
  58728. +/**
  58729. + * This union represents the bit fields in the Device OUT EP Interrupt
  58730. + * Registerand Device OUT EP Common Interrupt Mask Register.
  58731. + *
  58732. + * - Read the register into the <i>d32</i> member then set/clear the
  58733. + * bits using the <i>b</i>it elements.
  58734. + */
  58735. +typedef union doepint_data {
  58736. + /** raw register data */
  58737. + uint32_t d32;
  58738. + /** register bits */
  58739. + struct {
  58740. + /** Transfer complete */
  58741. + unsigned xfercompl:1;
  58742. + /** Endpoint disable */
  58743. + unsigned epdisabled:1;
  58744. + /** AHB Error */
  58745. + unsigned ahberr:1;
  58746. + /** Setup Phase Done (contorl EPs) */
  58747. + unsigned setup:1;
  58748. + /** OUT Token Received when Endpoint Disabled */
  58749. + unsigned outtknepdis:1;
  58750. +
  58751. + unsigned stsphsercvd:1;
  58752. + /** Back-to-Back SETUP Packets Received */
  58753. + unsigned back2backsetup:1;
  58754. +
  58755. + unsigned reserved7:1;
  58756. + /** OUT packet Error */
  58757. + unsigned outpkterr:1;
  58758. + /** BNA Interrupt */
  58759. + unsigned bna:1;
  58760. +
  58761. + unsigned reserved10:1;
  58762. + /** Packet Drop Status */
  58763. + unsigned pktdrpsts:1;
  58764. + /** Babble Interrupt */
  58765. + unsigned babble:1;
  58766. + /** NAK Interrupt */
  58767. + unsigned nak:1;
  58768. + /** NYET Interrupt */
  58769. + unsigned nyet:1;
  58770. + /** Bit indicating setup packet received */
  58771. + unsigned sr:1;
  58772. +
  58773. + unsigned reserved16_31:16;
  58774. + } b;
  58775. +} doepint_data_t;
  58776. +
  58777. +/**
  58778. + * This union represents the bit fields in the Device OUT EP
  58779. + * Common/Dedicated Interrupt Mask Register.
  58780. + */
  58781. +typedef union doepint_data doepmsk_data_t;
  58782. +
  58783. +/**
  58784. + * This union represents the bit fields in the Device All EP Interrupt
  58785. + * and Mask Registers.
  58786. + * - Read the register into the <i>d32</i> member then set/clear the
  58787. + * bits using the <i>b</i>it elements.
  58788. + */
  58789. +typedef union daint_data {
  58790. + /** raw register data */
  58791. + uint32_t d32;
  58792. + /** register bits */
  58793. + struct {
  58794. + /** IN Endpoint bits */
  58795. + unsigned in:16;
  58796. + /** OUT Endpoint bits */
  58797. + unsigned out:16;
  58798. + } ep;
  58799. + struct {
  58800. + /** IN Endpoint bits */
  58801. + unsigned inep0:1;
  58802. + unsigned inep1:1;
  58803. + unsigned inep2:1;
  58804. + unsigned inep3:1;
  58805. + unsigned inep4:1;
  58806. + unsigned inep5:1;
  58807. + unsigned inep6:1;
  58808. + unsigned inep7:1;
  58809. + unsigned inep8:1;
  58810. + unsigned inep9:1;
  58811. + unsigned inep10:1;
  58812. + unsigned inep11:1;
  58813. + unsigned inep12:1;
  58814. + unsigned inep13:1;
  58815. + unsigned inep14:1;
  58816. + unsigned inep15:1;
  58817. + /** OUT Endpoint bits */
  58818. + unsigned outep0:1;
  58819. + unsigned outep1:1;
  58820. + unsigned outep2:1;
  58821. + unsigned outep3:1;
  58822. + unsigned outep4:1;
  58823. + unsigned outep5:1;
  58824. + unsigned outep6:1;
  58825. + unsigned outep7:1;
  58826. + unsigned outep8:1;
  58827. + unsigned outep9:1;
  58828. + unsigned outep10:1;
  58829. + unsigned outep11:1;
  58830. + unsigned outep12:1;
  58831. + unsigned outep13:1;
  58832. + unsigned outep14:1;
  58833. + unsigned outep15:1;
  58834. + } b;
  58835. +} daint_data_t;
  58836. +
  58837. +/**
  58838. + * This union represents the bit fields in the Device IN Token Queue
  58839. + * Read Registers.
  58840. + * - Read the register into the <i>d32</i> member.
  58841. + * - READ-ONLY Register
  58842. + */
  58843. +typedef union dtknq1_data {
  58844. + /** raw register data */
  58845. + uint32_t d32;
  58846. + /** register bits */
  58847. + struct {
  58848. + /** In Token Queue Write Pointer */
  58849. + unsigned intknwptr:5;
  58850. + /** Reserved */
  58851. + unsigned reserved05_06:2;
  58852. + /** write pointer has wrapped. */
  58853. + unsigned wrap_bit:1;
  58854. + /** EP Numbers of IN Tokens 0 ... 4 */
  58855. + unsigned epnums0_5:24;
  58856. + } b;
  58857. +} dtknq1_data_t;
  58858. +
  58859. +/**
  58860. + * This union represents Threshold control Register
  58861. + * - Read and write the register into the <i>d32</i> member.
  58862. + * - READ-WRITABLE Register
  58863. + */
  58864. +typedef union dthrctl_data {
  58865. + /** raw register data */
  58866. + uint32_t d32;
  58867. + /** register bits */
  58868. + struct {
  58869. + /** non ISO Tx Thr. Enable */
  58870. + unsigned non_iso_thr_en:1;
  58871. + /** ISO Tx Thr. Enable */
  58872. + unsigned iso_thr_en:1;
  58873. + /** Tx Thr. Length */
  58874. + unsigned tx_thr_len:9;
  58875. + /** AHB Threshold ratio */
  58876. + unsigned ahb_thr_ratio:2;
  58877. + /** Reserved */
  58878. + unsigned reserved13_15:3;
  58879. + /** Rx Thr. Enable */
  58880. + unsigned rx_thr_en:1;
  58881. + /** Rx Thr. Length */
  58882. + unsigned rx_thr_len:9;
  58883. + unsigned reserved26:1;
  58884. + /** Arbiter Parking Enable*/
  58885. + unsigned arbprken:1;
  58886. + /** Reserved */
  58887. + unsigned reserved28_31:4;
  58888. + } b;
  58889. +} dthrctl_data_t;
  58890. +
  58891. +/**
  58892. + * Device Logical IN Endpoint-Specific Registers. <i>Offsets
  58893. + * 900h-AFCh</i>
  58894. + *
  58895. + * There will be one set of endpoint registers per logical endpoint
  58896. + * implemented.
  58897. + *
  58898. + * <i>These registers are visible only in Device mode and must not be
  58899. + * accessed in Host mode, as the results are unknown.</i>
  58900. + */
  58901. +typedef struct dwc_otg_dev_in_ep_regs {
  58902. + /** Device IN Endpoint Control Register. <i>Offset:900h +
  58903. + * (ep_num * 20h) + 00h</i> */
  58904. + volatile uint32_t diepctl;
  58905. + /** Reserved. <i>Offset:900h + (ep_num * 20h) + 04h</i> */
  58906. + uint32_t reserved04;
  58907. + /** Device IN Endpoint Interrupt Register. <i>Offset:900h +
  58908. + * (ep_num * 20h) + 08h</i> */
  58909. + volatile uint32_t diepint;
  58910. + /** Reserved. <i>Offset:900h + (ep_num * 20h) + 0Ch</i> */
  58911. + uint32_t reserved0C;
  58912. + /** Device IN Endpoint Transfer Size
  58913. + * Register. <i>Offset:900h + (ep_num * 20h) + 10h</i> */
  58914. + volatile uint32_t dieptsiz;
  58915. + /** Device IN Endpoint DMA Address Register. <i>Offset:900h +
  58916. + * (ep_num * 20h) + 14h</i> */
  58917. + volatile uint32_t diepdma;
  58918. + /** Device IN Endpoint Transmit FIFO Status Register. <i>Offset:900h +
  58919. + * (ep_num * 20h) + 18h</i> */
  58920. + volatile uint32_t dtxfsts;
  58921. + /** Device IN Endpoint DMA Buffer Register. <i>Offset:900h +
  58922. + * (ep_num * 20h) + 1Ch</i> */
  58923. + volatile uint32_t diepdmab;
  58924. +} dwc_otg_dev_in_ep_regs_t;
  58925. +
  58926. +/**
  58927. + * Device Logical OUT Endpoint-Specific Registers. <i>Offsets:
  58928. + * B00h-CFCh</i>
  58929. + *
  58930. + * There will be one set of endpoint registers per logical endpoint
  58931. + * implemented.
  58932. + *
  58933. + * <i>These registers are visible only in Device mode and must not be
  58934. + * accessed in Host mode, as the results are unknown.</i>
  58935. + */
  58936. +typedef struct dwc_otg_dev_out_ep_regs {
  58937. + /** Device OUT Endpoint Control Register. <i>Offset:B00h +
  58938. + * (ep_num * 20h) + 00h</i> */
  58939. + volatile uint32_t doepctl;
  58940. + /** Reserved. <i>Offset:B00h + (ep_num * 20h) + 04h</i> */
  58941. + uint32_t reserved04;
  58942. + /** Device OUT Endpoint Interrupt Register. <i>Offset:B00h +
  58943. + * (ep_num * 20h) + 08h</i> */
  58944. + volatile uint32_t doepint;
  58945. + /** Reserved. <i>Offset:B00h + (ep_num * 20h) + 0Ch</i> */
  58946. + uint32_t reserved0C;
  58947. + /** Device OUT Endpoint Transfer Size Register. <i>Offset:
  58948. + * B00h + (ep_num * 20h) + 10h</i> */
  58949. + volatile uint32_t doeptsiz;
  58950. + /** Device OUT Endpoint DMA Address Register. <i>Offset:B00h
  58951. + * + (ep_num * 20h) + 14h</i> */
  58952. + volatile uint32_t doepdma;
  58953. + /** Reserved. <i>Offset:B00h + * (ep_num * 20h) + 18h</i> */
  58954. + uint32_t unused;
  58955. + /** Device OUT Endpoint DMA Buffer Register. <i>Offset:B00h
  58956. + * + (ep_num * 20h) + 1Ch</i> */
  58957. + uint32_t doepdmab;
  58958. +} dwc_otg_dev_out_ep_regs_t;
  58959. +
  58960. +/**
  58961. + * This union represents the bit fields in the Device EP Control
  58962. + * Register. Read the register into the <i>d32</i> member then
  58963. + * set/clear the bits using the <i>b</i>it elements.
  58964. + */
  58965. +typedef union depctl_data {
  58966. + /** raw register data */
  58967. + uint32_t d32;
  58968. + /** register bits */
  58969. + struct {
  58970. + /** Maximum Packet Size
  58971. + * IN/OUT EPn
  58972. + * IN/OUT EP0 - 2 bits
  58973. + * 2'b00: 64 Bytes
  58974. + * 2'b01: 32
  58975. + * 2'b10: 16
  58976. + * 2'b11: 8 */
  58977. + unsigned mps:11;
  58978. +#define DWC_DEP0CTL_MPS_64 0
  58979. +#define DWC_DEP0CTL_MPS_32 1
  58980. +#define DWC_DEP0CTL_MPS_16 2
  58981. +#define DWC_DEP0CTL_MPS_8 3
  58982. +
  58983. + /** Next Endpoint
  58984. + * IN EPn/IN EP0
  58985. + * OUT EPn/OUT EP0 - reserved */
  58986. + unsigned nextep:4;
  58987. +
  58988. + /** USB Active Endpoint */
  58989. + unsigned usbactep:1;
  58990. +
  58991. + /** Endpoint DPID (INTR/Bulk IN and OUT endpoints)
  58992. + * This field contains the PID of the packet going to
  58993. + * be received or transmitted on this endpoint. The
  58994. + * application should program the PID of the first
  58995. + * packet going to be received or transmitted on this
  58996. + * endpoint , after the endpoint is
  58997. + * activated. Application use the SetD1PID and
  58998. + * SetD0PID fields of this register to program either
  58999. + * D0 or D1 PID.
  59000. + *
  59001. + * The encoding for this field is
  59002. + * - 0: D0
  59003. + * - 1: D1
  59004. + */
  59005. + unsigned dpid:1;
  59006. +
  59007. + /** NAK Status */
  59008. + unsigned naksts:1;
  59009. +
  59010. + /** Endpoint Type
  59011. + * 2'b00: Control
  59012. + * 2'b01: Isochronous
  59013. + * 2'b10: Bulk
  59014. + * 2'b11: Interrupt */
  59015. + unsigned eptype:2;
  59016. +
  59017. + /** Snoop Mode
  59018. + * OUT EPn/OUT EP0
  59019. + * IN EPn/IN EP0 - reserved */
  59020. + unsigned snp:1;
  59021. +
  59022. + /** Stall Handshake */
  59023. + unsigned stall:1;
  59024. +
  59025. + /** Tx Fifo Number
  59026. + * IN EPn/IN EP0
  59027. + * OUT EPn/OUT EP0 - reserved */
  59028. + unsigned txfnum:4;
  59029. +
  59030. + /** Clear NAK */
  59031. + unsigned cnak:1;
  59032. + /** Set NAK */
  59033. + unsigned snak:1;
  59034. + /** Set DATA0 PID (INTR/Bulk IN and OUT endpoints)
  59035. + * Writing to this field sets the Endpoint DPID (DPID)
  59036. + * field in this register to DATA0. Set Even
  59037. + * (micro)frame (SetEvenFr) (ISO IN and OUT Endpoints)
  59038. + * Writing to this field sets the Even/Odd
  59039. + * (micro)frame (EO_FrNum) field to even (micro)
  59040. + * frame.
  59041. + */
  59042. + unsigned setd0pid:1;
  59043. + /** Set DATA1 PID (INTR/Bulk IN and OUT endpoints)
  59044. + * Writing to this field sets the Endpoint DPID (DPID)
  59045. + * field in this register to DATA1 Set Odd
  59046. + * (micro)frame (SetOddFr) (ISO IN and OUT Endpoints)
  59047. + * Writing to this field sets the Even/Odd
  59048. + * (micro)frame (EO_FrNum) field to odd (micro) frame.
  59049. + */
  59050. + unsigned setd1pid:1;
  59051. +
  59052. + /** Endpoint Disable */
  59053. + unsigned epdis:1;
  59054. + /** Endpoint Enable */
  59055. + unsigned epena:1;
  59056. + } b;
  59057. +} depctl_data_t;
  59058. +
  59059. +/**
  59060. + * This union represents the bit fields in the Device EP Transfer
  59061. + * Size Register. Read the register into the <i>d32</i> member then
  59062. + * set/clear the bits using the <i>b</i>it elements.
  59063. + */
  59064. +typedef union deptsiz_data {
  59065. + /** raw register data */
  59066. + uint32_t d32;
  59067. + /** register bits */
  59068. + struct {
  59069. + /** Transfer size */
  59070. + unsigned xfersize:19;
  59071. +/** Max packet count for EP (pow(2,10)-1) */
  59072. +#define MAX_PKT_CNT 1023
  59073. + /** Packet Count */
  59074. + unsigned pktcnt:10;
  59075. + /** Multi Count - Periodic IN endpoints */
  59076. + unsigned mc:2;
  59077. + unsigned reserved:1;
  59078. + } b;
  59079. +} deptsiz_data_t;
  59080. +
  59081. +/**
  59082. + * This union represents the bit fields in the Device EP 0 Transfer
  59083. + * Size Register. Read the register into the <i>d32</i> member then
  59084. + * set/clear the bits using the <i>b</i>it elements.
  59085. + */
  59086. +typedef union deptsiz0_data {
  59087. + /** raw register data */
  59088. + uint32_t d32;
  59089. + /** register bits */
  59090. + struct {
  59091. + /** Transfer size */
  59092. + unsigned xfersize:7;
  59093. + /** Reserved */
  59094. + unsigned reserved7_18:12;
  59095. + /** Packet Count */
  59096. + unsigned pktcnt:2;
  59097. + /** Reserved */
  59098. + unsigned reserved21_28:8;
  59099. + /**Setup Packet Count (DOEPTSIZ0 Only) */
  59100. + unsigned supcnt:2;
  59101. + unsigned reserved31;
  59102. + } b;
  59103. +} deptsiz0_data_t;
  59104. +
  59105. +/////////////////////////////////////////////////
  59106. +// DMA Descriptor Specific Structures
  59107. +//
  59108. +
  59109. +/** Buffer status definitions */
  59110. +
  59111. +#define BS_HOST_READY 0x0
  59112. +#define BS_DMA_BUSY 0x1
  59113. +#define BS_DMA_DONE 0x2
  59114. +#define BS_HOST_BUSY 0x3
  59115. +
  59116. +/** Receive/Transmit status definitions */
  59117. +
  59118. +#define RTS_SUCCESS 0x0
  59119. +#define RTS_BUFFLUSH 0x1
  59120. +#define RTS_RESERVED 0x2
  59121. +#define RTS_BUFERR 0x3
  59122. +
  59123. +/**
  59124. + * This union represents the bit fields in the DMA Descriptor
  59125. + * status quadlet. Read the quadlet into the <i>d32</i> member then
  59126. + * set/clear the bits using the <i>b</i>it, <i>b_iso_out</i> and
  59127. + * <i>b_iso_in</i> elements.
  59128. + */
  59129. +typedef union dev_dma_desc_sts {
  59130. + /** raw register data */
  59131. + uint32_t d32;
  59132. + /** quadlet bits */
  59133. + struct {
  59134. + /** Received number of bytes */
  59135. + unsigned bytes:16;
  59136. + /** NAK bit - only for OUT EPs */
  59137. + unsigned nak:1;
  59138. + unsigned reserved17_22:6;
  59139. + /** Multiple Transfer - only for OUT EPs */
  59140. + unsigned mtrf:1;
  59141. + /** Setup Packet received - only for OUT EPs */
  59142. + unsigned sr:1;
  59143. + /** Interrupt On Complete */
  59144. + unsigned ioc:1;
  59145. + /** Short Packet */
  59146. + unsigned sp:1;
  59147. + /** Last */
  59148. + unsigned l:1;
  59149. + /** Receive Status */
  59150. + unsigned sts:2;
  59151. + /** Buffer Status */
  59152. + unsigned bs:2;
  59153. + } b;
  59154. +
  59155. +//#ifdef DWC_EN_ISOC
  59156. + /** iso out quadlet bits */
  59157. + struct {
  59158. + /** Received number of bytes */
  59159. + unsigned rxbytes:11;
  59160. +
  59161. + unsigned reserved11:1;
  59162. + /** Frame Number */
  59163. + unsigned framenum:11;
  59164. + /** Received ISO Data PID */
  59165. + unsigned pid:2;
  59166. + /** Interrupt On Complete */
  59167. + unsigned ioc:1;
  59168. + /** Short Packet */
  59169. + unsigned sp:1;
  59170. + /** Last */
  59171. + unsigned l:1;
  59172. + /** Receive Status */
  59173. + unsigned rxsts:2;
  59174. + /** Buffer Status */
  59175. + unsigned bs:2;
  59176. + } b_iso_out;
  59177. +
  59178. + /** iso in quadlet bits */
  59179. + struct {
  59180. + /** Transmited number of bytes */
  59181. + unsigned txbytes:12;
  59182. + /** Frame Number */
  59183. + unsigned framenum:11;
  59184. + /** Transmited ISO Data PID */
  59185. + unsigned pid:2;
  59186. + /** Interrupt On Complete */
  59187. + unsigned ioc:1;
  59188. + /** Short Packet */
  59189. + unsigned sp:1;
  59190. + /** Last */
  59191. + unsigned l:1;
  59192. + /** Transmit Status */
  59193. + unsigned txsts:2;
  59194. + /** Buffer Status */
  59195. + unsigned bs:2;
  59196. + } b_iso_in;
  59197. +//#endif /* DWC_EN_ISOC */
  59198. +} dev_dma_desc_sts_t;
  59199. +
  59200. +/**
  59201. + * DMA Descriptor structure
  59202. + *
  59203. + * DMA Descriptor structure contains two quadlets:
  59204. + * Status quadlet and Data buffer pointer.
  59205. + */
  59206. +typedef struct dwc_otg_dev_dma_desc {
  59207. + /** DMA Descriptor status quadlet */
  59208. + dev_dma_desc_sts_t status;
  59209. + /** DMA Descriptor data buffer pointer */
  59210. + uint32_t buf;
  59211. +} dwc_otg_dev_dma_desc_t;
  59212. +
  59213. +/**
  59214. + * The dwc_otg_dev_if structure contains information needed to manage
  59215. + * the DWC_otg controller acting in device mode. It represents the
  59216. + * programming view of the device-specific aspects of the controller.
  59217. + */
  59218. +typedef struct dwc_otg_dev_if {
  59219. + /** Pointer to device Global registers.
  59220. + * Device Global Registers starting at offset 800h
  59221. + */
  59222. + dwc_otg_device_global_regs_t *dev_global_regs;
  59223. +#define DWC_DEV_GLOBAL_REG_OFFSET 0x800
  59224. +
  59225. + /**
  59226. + * Device Logical IN Endpoint-Specific Registers 900h-AFCh
  59227. + */
  59228. + dwc_otg_dev_in_ep_regs_t *in_ep_regs[MAX_EPS_CHANNELS];
  59229. +#define DWC_DEV_IN_EP_REG_OFFSET 0x900
  59230. +#define DWC_EP_REG_OFFSET 0x20
  59231. +
  59232. + /** Device Logical OUT Endpoint-Specific Registers B00h-CFCh */
  59233. + dwc_otg_dev_out_ep_regs_t *out_ep_regs[MAX_EPS_CHANNELS];
  59234. +#define DWC_DEV_OUT_EP_REG_OFFSET 0xB00
  59235. +
  59236. + /* Device configuration information */
  59237. + uint8_t speed; /**< Device Speed 0: Unknown, 1: LS, 2:FS, 3: HS */
  59238. + uint8_t num_in_eps; /**< Number # of Tx EP range: 0-15 exept ep0 */
  59239. + uint8_t num_out_eps; /**< Number # of Rx EP range: 0-15 exept ep 0*/
  59240. +
  59241. + /** Size of periodic FIFOs (Bytes) */
  59242. + uint16_t perio_tx_fifo_size[MAX_PERIO_FIFOS];
  59243. +
  59244. + /** Size of Tx FIFOs (Bytes) */
  59245. + uint16_t tx_fifo_size[MAX_TX_FIFOS];
  59246. +
  59247. + /** Thresholding enable flags and length varaiables **/
  59248. + uint16_t rx_thr_en;
  59249. + uint16_t iso_tx_thr_en;
  59250. + uint16_t non_iso_tx_thr_en;
  59251. +
  59252. + uint16_t rx_thr_length;
  59253. + uint16_t tx_thr_length;
  59254. +
  59255. + /**
  59256. + * Pointers to the DMA Descriptors for EP0 Control
  59257. + * transfers (virtual and physical)
  59258. + */
  59259. +
  59260. + /** 2 descriptors for SETUP packets */
  59261. + dwc_dma_t dma_setup_desc_addr[2];
  59262. + dwc_otg_dev_dma_desc_t *setup_desc_addr[2];
  59263. +
  59264. + /** Pointer to Descriptor with latest SETUP packet */
  59265. + dwc_otg_dev_dma_desc_t *psetup;
  59266. +
  59267. + /** Index of current SETUP handler descriptor */
  59268. + uint32_t setup_desc_index;
  59269. +
  59270. + /** Descriptor for Data In or Status In phases */
  59271. + dwc_dma_t dma_in_desc_addr;
  59272. + dwc_otg_dev_dma_desc_t *in_desc_addr;
  59273. +
  59274. + /** Descriptor for Data Out or Status Out phases */
  59275. + dwc_dma_t dma_out_desc_addr;
  59276. + dwc_otg_dev_dma_desc_t *out_desc_addr;
  59277. +
  59278. + /** Setup Packet Detected - if set clear NAK when queueing */
  59279. + uint32_t spd;
  59280. + /** Isoc ep pointer on which incomplete happens */
  59281. + void *isoc_ep;
  59282. +
  59283. +} dwc_otg_dev_if_t;
  59284. +
  59285. +/////////////////////////////////////////////////
  59286. +// Host Mode Register Structures
  59287. +//
  59288. +/**
  59289. + * The Host Global Registers structure defines the size and relative
  59290. + * field offsets for the Host Mode Global Registers. Host Global
  59291. + * Registers offsets 400h-7FFh.
  59292. +*/
  59293. +typedef struct dwc_otg_host_global_regs {
  59294. + /** Host Configuration Register. <i>Offset: 400h</i> */
  59295. + volatile uint32_t hcfg;
  59296. + /** Host Frame Interval Register. <i>Offset: 404h</i> */
  59297. + volatile uint32_t hfir;
  59298. + /** Host Frame Number / Frame Remaining Register. <i>Offset: 408h</i> */
  59299. + volatile uint32_t hfnum;
  59300. + /** Reserved. <i>Offset: 40Ch</i> */
  59301. + uint32_t reserved40C;
  59302. + /** Host Periodic Transmit FIFO/ Queue Status Register. <i>Offset: 410h</i> */
  59303. + volatile uint32_t hptxsts;
  59304. + /** Host All Channels Interrupt Register. <i>Offset: 414h</i> */
  59305. + volatile uint32_t haint;
  59306. + /** Host All Channels Interrupt Mask Register. <i>Offset: 418h</i> */
  59307. + volatile uint32_t haintmsk;
  59308. + /** Host Frame List Base Address Register . <i>Offset: 41Ch</i> */
  59309. + volatile uint32_t hflbaddr;
  59310. +} dwc_otg_host_global_regs_t;
  59311. +
  59312. +/**
  59313. + * This union represents the bit fields in the Host Configuration Register.
  59314. + * Read the register into the <i>d32</i> member then set/clear the bits using
  59315. + * the <i>b</i>it elements. Write the <i>d32</i> member to the hcfg register.
  59316. + */
  59317. +typedef union hcfg_data {
  59318. + /** raw register data */
  59319. + uint32_t d32;
  59320. +
  59321. + /** register bits */
  59322. + struct {
  59323. + /** FS/LS Phy Clock Select */
  59324. + unsigned fslspclksel:2;
  59325. +#define DWC_HCFG_30_60_MHZ 0
  59326. +#define DWC_HCFG_48_MHZ 1
  59327. +#define DWC_HCFG_6_MHZ 2
  59328. +
  59329. + /** FS/LS Only Support */
  59330. + unsigned fslssupp:1;
  59331. + unsigned reserved3_6:4;
  59332. + /** Enable 32-KHz Suspend Mode */
  59333. + unsigned ena32khzs:1;
  59334. + /** Resume Validation Periiod */
  59335. + unsigned resvalid:8;
  59336. + unsigned reserved16_22:7;
  59337. + /** Enable Scatter/gather DMA in Host mode */
  59338. + unsigned descdma:1;
  59339. + /** Frame List Entries */
  59340. + unsigned frlisten:2;
  59341. + /** Enable Periodic Scheduling */
  59342. + unsigned perschedena:1;
  59343. + unsigned reserved27_30:4;
  59344. + unsigned modechtimen:1;
  59345. + } b;
  59346. +} hcfg_data_t;
  59347. +
  59348. +/**
  59349. + * This union represents the bit fields in the Host Frame Remaing/Number
  59350. + * Register.
  59351. + */
  59352. +typedef union hfir_data {
  59353. + /** raw register data */
  59354. + uint32_t d32;
  59355. +
  59356. + /** register bits */
  59357. + struct {
  59358. + unsigned frint:16;
  59359. + unsigned hfirrldctrl:1;
  59360. + unsigned reserved:15;
  59361. + } b;
  59362. +} hfir_data_t;
  59363. +
  59364. +/**
  59365. + * This union represents the bit fields in the Host Frame Remaing/Number
  59366. + * Register.
  59367. + */
  59368. +typedef union hfnum_data {
  59369. + /** raw register data */
  59370. + uint32_t d32;
  59371. +
  59372. + /** register bits */
  59373. + struct {
  59374. + unsigned frnum:16;
  59375. +#define DWC_HFNUM_MAX_FRNUM 0x3FFF
  59376. + unsigned frrem:16;
  59377. + } b;
  59378. +} hfnum_data_t;
  59379. +
  59380. +typedef union hptxsts_data {
  59381. + /** raw register data */
  59382. + uint32_t d32;
  59383. +
  59384. + /** register bits */
  59385. + struct {
  59386. + unsigned ptxfspcavail:16;
  59387. + unsigned ptxqspcavail:8;
  59388. + /** Top of the Periodic Transmit Request Queue
  59389. + * - bit 24 - Terminate (last entry for the selected channel)
  59390. + * - bits 26:25 - Token Type
  59391. + * - 2'b00 - Zero length
  59392. + * - 2'b01 - Ping
  59393. + * - 2'b10 - Disable
  59394. + * - bits 30:27 - Channel Number
  59395. + * - bit 31 - Odd/even microframe
  59396. + */
  59397. + unsigned ptxqtop_terminate:1;
  59398. + unsigned ptxqtop_token:2;
  59399. + unsigned ptxqtop_chnum:4;
  59400. + unsigned ptxqtop_odd:1;
  59401. + } b;
  59402. +} hptxsts_data_t;
  59403. +
  59404. +/**
  59405. + * This union represents the bit fields in the Host Port Control and Status
  59406. + * Register. Read the register into the <i>d32</i> member then set/clear the
  59407. + * bits using the <i>b</i>it elements. Write the <i>d32</i> member to the
  59408. + * hprt0 register.
  59409. + */
  59410. +typedef union hprt0_data {
  59411. + /** raw register data */
  59412. + uint32_t d32;
  59413. + /** register bits */
  59414. + struct {
  59415. + unsigned prtconnsts:1;
  59416. + unsigned prtconndet:1;
  59417. + unsigned prtena:1;
  59418. + unsigned prtenchng:1;
  59419. + unsigned prtovrcurract:1;
  59420. + unsigned prtovrcurrchng:1;
  59421. + unsigned prtres:1;
  59422. + unsigned prtsusp:1;
  59423. + unsigned prtrst:1;
  59424. + unsigned reserved9:1;
  59425. + unsigned prtlnsts:2;
  59426. + unsigned prtpwr:1;
  59427. + unsigned prttstctl:4;
  59428. + unsigned prtspd:2;
  59429. +#define DWC_HPRT0_PRTSPD_HIGH_SPEED 0
  59430. +#define DWC_HPRT0_PRTSPD_FULL_SPEED 1
  59431. +#define DWC_HPRT0_PRTSPD_LOW_SPEED 2
  59432. + unsigned reserved19_31:13;
  59433. + } b;
  59434. +} hprt0_data_t;
  59435. +
  59436. +/**
  59437. + * This union represents the bit fields in the Host All Interrupt
  59438. + * Register.
  59439. + */
  59440. +typedef union haint_data {
  59441. + /** raw register data */
  59442. + uint32_t d32;
  59443. + /** register bits */
  59444. + struct {
  59445. + unsigned ch0:1;
  59446. + unsigned ch1:1;
  59447. + unsigned ch2:1;
  59448. + unsigned ch3:1;
  59449. + unsigned ch4:1;
  59450. + unsigned ch5:1;
  59451. + unsigned ch6:1;
  59452. + unsigned ch7:1;
  59453. + unsigned ch8:1;
  59454. + unsigned ch9:1;
  59455. + unsigned ch10:1;
  59456. + unsigned ch11:1;
  59457. + unsigned ch12:1;
  59458. + unsigned ch13:1;
  59459. + unsigned ch14:1;
  59460. + unsigned ch15:1;
  59461. + unsigned reserved:16;
  59462. + } b;
  59463. +
  59464. + struct {
  59465. + unsigned chint:16;
  59466. + unsigned reserved:16;
  59467. + } b2;
  59468. +} haint_data_t;
  59469. +
  59470. +/**
  59471. + * This union represents the bit fields in the Host All Interrupt
  59472. + * Register.
  59473. + */
  59474. +typedef union haintmsk_data {
  59475. + /** raw register data */
  59476. + uint32_t d32;
  59477. + /** register bits */
  59478. + struct {
  59479. + unsigned ch0:1;
  59480. + unsigned ch1:1;
  59481. + unsigned ch2:1;
  59482. + unsigned ch3:1;
  59483. + unsigned ch4:1;
  59484. + unsigned ch5:1;
  59485. + unsigned ch6:1;
  59486. + unsigned ch7:1;
  59487. + unsigned ch8:1;
  59488. + unsigned ch9:1;
  59489. + unsigned ch10:1;
  59490. + unsigned ch11:1;
  59491. + unsigned ch12:1;
  59492. + unsigned ch13:1;
  59493. + unsigned ch14:1;
  59494. + unsigned ch15:1;
  59495. + unsigned reserved:16;
  59496. + } b;
  59497. +
  59498. + struct {
  59499. + unsigned chint:16;
  59500. + unsigned reserved:16;
  59501. + } b2;
  59502. +} haintmsk_data_t;
  59503. +
  59504. +/**
  59505. + * Host Channel Specific Registers. <i>500h-5FCh</i>
  59506. + */
  59507. +typedef struct dwc_otg_hc_regs {
  59508. + /** Host Channel 0 Characteristic Register. <i>Offset: 500h + (chan_num * 20h) + 00h</i> */
  59509. + volatile uint32_t hcchar;
  59510. + /** Host Channel 0 Split Control Register. <i>Offset: 500h + (chan_num * 20h) + 04h</i> */
  59511. + volatile uint32_t hcsplt;
  59512. + /** Host Channel 0 Interrupt Register. <i>Offset: 500h + (chan_num * 20h) + 08h</i> */
  59513. + volatile uint32_t hcint;
  59514. + /** Host Channel 0 Interrupt Mask Register. <i>Offset: 500h + (chan_num * 20h) + 0Ch</i> */
  59515. + volatile uint32_t hcintmsk;
  59516. + /** Host Channel 0 Transfer Size Register. <i>Offset: 500h + (chan_num * 20h) + 10h</i> */
  59517. + volatile uint32_t hctsiz;
  59518. + /** Host Channel 0 DMA Address Register. <i>Offset: 500h + (chan_num * 20h) + 14h</i> */
  59519. + volatile uint32_t hcdma;
  59520. + volatile uint32_t reserved;
  59521. + /** Host Channel 0 DMA Buffer Address Register. <i>Offset: 500h + (chan_num * 20h) + 1Ch</i> */
  59522. + volatile uint32_t hcdmab;
  59523. +} dwc_otg_hc_regs_t;
  59524. +
  59525. +/**
  59526. + * This union represents the bit fields in the Host Channel Characteristics
  59527. + * Register. Read the register into the <i>d32</i> member then set/clear the
  59528. + * bits using the <i>b</i>it elements. Write the <i>d32</i> member to the
  59529. + * hcchar register.
  59530. + */
  59531. +typedef union hcchar_data {
  59532. + /** raw register data */
  59533. + uint32_t d32;
  59534. +
  59535. + /** register bits */
  59536. + struct {
  59537. + /** Maximum packet size in bytes */
  59538. + unsigned mps:11;
  59539. +
  59540. + /** Endpoint number */
  59541. + unsigned epnum:4;
  59542. +
  59543. + /** 0: OUT, 1: IN */
  59544. + unsigned epdir:1;
  59545. +
  59546. + unsigned reserved:1;
  59547. +
  59548. + /** 0: Full/high speed device, 1: Low speed device */
  59549. + unsigned lspddev:1;
  59550. +
  59551. + /** 0: Control, 1: Isoc, 2: Bulk, 3: Intr */
  59552. + unsigned eptype:2;
  59553. +
  59554. + /** Packets per frame for periodic transfers. 0 is reserved. */
  59555. + unsigned multicnt:2;
  59556. +
  59557. + /** Device address */
  59558. + unsigned devaddr:7;
  59559. +
  59560. + /**
  59561. + * Frame to transmit periodic transaction.
  59562. + * 0: even, 1: odd
  59563. + */
  59564. + unsigned oddfrm:1;
  59565. +
  59566. + /** Channel disable */
  59567. + unsigned chdis:1;
  59568. +
  59569. + /** Channel enable */
  59570. + unsigned chen:1;
  59571. + } b;
  59572. +} hcchar_data_t;
  59573. +
  59574. +typedef union hcsplt_data {
  59575. + /** raw register data */
  59576. + uint32_t d32;
  59577. +
  59578. + /** register bits */
  59579. + struct {
  59580. + /** Port Address */
  59581. + unsigned prtaddr:7;
  59582. +
  59583. + /** Hub Address */
  59584. + unsigned hubaddr:7;
  59585. +
  59586. + /** Transaction Position */
  59587. + unsigned xactpos:2;
  59588. +#define DWC_HCSPLIT_XACTPOS_MID 0
  59589. +#define DWC_HCSPLIT_XACTPOS_END 1
  59590. +#define DWC_HCSPLIT_XACTPOS_BEGIN 2
  59591. +#define DWC_HCSPLIT_XACTPOS_ALL 3
  59592. +
  59593. + /** Do Complete Split */
  59594. + unsigned compsplt:1;
  59595. +
  59596. + /** Reserved */
  59597. + unsigned reserved:14;
  59598. +
  59599. + /** Split Enble */
  59600. + unsigned spltena:1;
  59601. + } b;
  59602. +} hcsplt_data_t;
  59603. +
  59604. +/**
  59605. + * This union represents the bit fields in the Host All Interrupt
  59606. + * Register.
  59607. + */
  59608. +typedef union hcint_data {
  59609. + /** raw register data */
  59610. + uint32_t d32;
  59611. + /** register bits */
  59612. + struct {
  59613. + /** Transfer Complete */
  59614. + unsigned xfercomp:1;
  59615. + /** Channel Halted */
  59616. + unsigned chhltd:1;
  59617. + /** AHB Error */
  59618. + unsigned ahberr:1;
  59619. + /** STALL Response Received */
  59620. + unsigned stall:1;
  59621. + /** NAK Response Received */
  59622. + unsigned nak:1;
  59623. + /** ACK Response Received */
  59624. + unsigned ack:1;
  59625. + /** NYET Response Received */
  59626. + unsigned nyet:1;
  59627. + /** Transaction Err */
  59628. + unsigned xacterr:1;
  59629. + /** Babble Error */
  59630. + unsigned bblerr:1;
  59631. + /** Frame Overrun */
  59632. + unsigned frmovrun:1;
  59633. + /** Data Toggle Error */
  59634. + unsigned datatglerr:1;
  59635. + /** Buffer Not Available (only for DDMA mode) */
  59636. + unsigned bna:1;
  59637. + /** Exessive transaction error (only for DDMA mode) */
  59638. + unsigned xcs_xact:1;
  59639. + /** Frame List Rollover interrupt */
  59640. + unsigned frm_list_roll:1;
  59641. + /** Reserved */
  59642. + unsigned reserved14_31:18;
  59643. + } b;
  59644. +} hcint_data_t;
  59645. +
  59646. +/**
  59647. + * This union represents the bit fields in the Host Channel Interrupt Mask
  59648. + * Register. Read the register into the <i>d32</i> member then set/clear the
  59649. + * bits using the <i>b</i>it elements. Write the <i>d32</i> member to the
  59650. + * hcintmsk register.
  59651. + */
  59652. +typedef union hcintmsk_data {
  59653. + /** raw register data */
  59654. + uint32_t d32;
  59655. +
  59656. + /** register bits */
  59657. + struct {
  59658. + unsigned xfercompl:1;
  59659. + unsigned chhltd:1;
  59660. + unsigned ahberr:1;
  59661. + unsigned stall:1;
  59662. + unsigned nak:1;
  59663. + unsigned ack:1;
  59664. + unsigned nyet:1;
  59665. + unsigned xacterr:1;
  59666. + unsigned bblerr:1;
  59667. + unsigned frmovrun:1;
  59668. + unsigned datatglerr:1;
  59669. + unsigned bna:1;
  59670. + unsigned xcs_xact:1;
  59671. + unsigned frm_list_roll:1;
  59672. + unsigned reserved14_31:18;
  59673. + } b;
  59674. +} hcintmsk_data_t;
  59675. +
  59676. +/**
  59677. + * This union represents the bit fields in the Host Channel Transfer Size
  59678. + * Register. Read the register into the <i>d32</i> member then set/clear the
  59679. + * bits using the <i>b</i>it elements. Write the <i>d32</i> member to the
  59680. + * hcchar register.
  59681. + */
  59682. +
  59683. +typedef union hctsiz_data {
  59684. + /** raw register data */
  59685. + uint32_t d32;
  59686. +
  59687. + /** register bits */
  59688. + struct {
  59689. + /** Total transfer size in bytes */
  59690. + unsigned xfersize:19;
  59691. +
  59692. + /** Data packets to transfer */
  59693. + unsigned pktcnt:10;
  59694. +
  59695. + /**
  59696. + * Packet ID for next data packet
  59697. + * 0: DATA0
  59698. + * 1: DATA2
  59699. + * 2: DATA1
  59700. + * 3: MDATA (non-Control), SETUP (Control)
  59701. + */
  59702. + unsigned pid:2;
  59703. +#define DWC_HCTSIZ_DATA0 0
  59704. +#define DWC_HCTSIZ_DATA1 2
  59705. +#define DWC_HCTSIZ_DATA2 1
  59706. +#define DWC_HCTSIZ_MDATA 3
  59707. +#define DWC_HCTSIZ_SETUP 3
  59708. +
  59709. + /** Do PING protocol when 1 */
  59710. + unsigned dopng:1;
  59711. + } b;
  59712. +
  59713. + /** register bits */
  59714. + struct {
  59715. + /** Scheduling information */
  59716. + unsigned schinfo:8;
  59717. +
  59718. + /** Number of transfer descriptors.
  59719. + * Max value:
  59720. + * 64 in general,
  59721. + * 256 only for HS isochronous endpoint.
  59722. + */
  59723. + unsigned ntd:8;
  59724. +
  59725. + /** Data packets to transfer */
  59726. + unsigned reserved16_28:13;
  59727. +
  59728. + /**
  59729. + * Packet ID for next data packet
  59730. + * 0: DATA0
  59731. + * 1: DATA2
  59732. + * 2: DATA1
  59733. + * 3: MDATA (non-Control)
  59734. + */
  59735. + unsigned pid:2;
  59736. +
  59737. + /** Do PING protocol when 1 */
  59738. + unsigned dopng:1;
  59739. + } b_ddma;
  59740. +} hctsiz_data_t;
  59741. +
  59742. +/**
  59743. + * This union represents the bit fields in the Host DMA Address
  59744. + * Register used in Descriptor DMA mode.
  59745. + */
  59746. +typedef union hcdma_data {
  59747. + /** raw register data */
  59748. + uint32_t d32;
  59749. + /** register bits */
  59750. + struct {
  59751. + unsigned reserved0_2:3;
  59752. + /** Current Transfer Descriptor. Not used for ISOC */
  59753. + unsigned ctd:8;
  59754. + /** Start Address of Descriptor List */
  59755. + unsigned dma_addr:21;
  59756. + } b;
  59757. +} hcdma_data_t;
  59758. +
  59759. +/**
  59760. + * This union represents the bit fields in the DMA Descriptor
  59761. + * status quadlet for host mode. Read the quadlet into the <i>d32</i> member then
  59762. + * set/clear the bits using the <i>b</i>it elements.
  59763. + */
  59764. +typedef union host_dma_desc_sts {
  59765. + /** raw register data */
  59766. + uint32_t d32;
  59767. + /** quadlet bits */
  59768. +
  59769. + /* for non-isochronous */
  59770. + struct {
  59771. + /** Number of bytes */
  59772. + unsigned n_bytes:17;
  59773. + /** QTD offset to jump when Short Packet received - only for IN EPs */
  59774. + unsigned qtd_offset:6;
  59775. + /**
  59776. + * Set to request the core to jump to alternate QTD if
  59777. + * Short Packet received - only for IN EPs
  59778. + */
  59779. + unsigned a_qtd:1;
  59780. + /**
  59781. + * Setup Packet bit. When set indicates that buffer contains
  59782. + * setup packet.
  59783. + */
  59784. + unsigned sup:1;
  59785. + /** Interrupt On Complete */
  59786. + unsigned ioc:1;
  59787. + /** End of List */
  59788. + unsigned eol:1;
  59789. + unsigned reserved27:1;
  59790. + /** Rx/Tx Status */
  59791. + unsigned sts:2;
  59792. +#define DMA_DESC_STS_PKTERR 1
  59793. + unsigned reserved30:1;
  59794. + /** Active Bit */
  59795. + unsigned a:1;
  59796. + } b;
  59797. + /* for isochronous */
  59798. + struct {
  59799. + /** Number of bytes */
  59800. + unsigned n_bytes:12;
  59801. + unsigned reserved12_24:13;
  59802. + /** Interrupt On Complete */
  59803. + unsigned ioc:1;
  59804. + unsigned reserved26_27:2;
  59805. + /** Rx/Tx Status */
  59806. + unsigned sts:2;
  59807. + unsigned reserved30:1;
  59808. + /** Active Bit */
  59809. + unsigned a:1;
  59810. + } b_isoc;
  59811. +} host_dma_desc_sts_t;
  59812. +
  59813. +#define MAX_DMA_DESC_SIZE 131071
  59814. +#define MAX_DMA_DESC_NUM_GENERIC 64
  59815. +#define MAX_DMA_DESC_NUM_HS_ISOC 256
  59816. +#define MAX_FRLIST_EN_NUM 64
  59817. +/**
  59818. + * Host-mode DMA Descriptor structure
  59819. + *
  59820. + * DMA Descriptor structure contains two quadlets:
  59821. + * Status quadlet and Data buffer pointer.
  59822. + */
  59823. +typedef struct dwc_otg_host_dma_desc {
  59824. + /** DMA Descriptor status quadlet */
  59825. + host_dma_desc_sts_t status;
  59826. + /** DMA Descriptor data buffer pointer */
  59827. + uint32_t buf;
  59828. +} dwc_otg_host_dma_desc_t;
  59829. +
  59830. +/** OTG Host Interface Structure.
  59831. + *
  59832. + * The OTG Host Interface Structure structure contains information
  59833. + * needed to manage the DWC_otg controller acting in host mode. It
  59834. + * represents the programming view of the host-specific aspects of the
  59835. + * controller.
  59836. + */
  59837. +typedef struct dwc_otg_host_if {
  59838. + /** Host Global Registers starting at offset 400h.*/
  59839. + dwc_otg_host_global_regs_t *host_global_regs;
  59840. +#define DWC_OTG_HOST_GLOBAL_REG_OFFSET 0x400
  59841. +
  59842. + /** Host Port 0 Control and Status Register */
  59843. + volatile uint32_t *hprt0;
  59844. +#define DWC_OTG_HOST_PORT_REGS_OFFSET 0x440
  59845. +
  59846. + /** Host Channel Specific Registers at offsets 500h-5FCh. */
  59847. + dwc_otg_hc_regs_t *hc_regs[MAX_EPS_CHANNELS];
  59848. +#define DWC_OTG_HOST_CHAN_REGS_OFFSET 0x500
  59849. +#define DWC_OTG_CHAN_REGS_OFFSET 0x20
  59850. +
  59851. + /* Host configuration information */
  59852. + /** Number of Host Channels (range: 1-16) */
  59853. + uint8_t num_host_channels;
  59854. + /** Periodic EPs supported (0: no, 1: yes) */
  59855. + uint8_t perio_eps_supported;
  59856. + /** Periodic Tx FIFO Size (Only 1 host periodic Tx FIFO) */
  59857. + uint16_t perio_tx_fifo_size;
  59858. +
  59859. +} dwc_otg_host_if_t;
  59860. +
  59861. +/**
  59862. + * This union represents the bit fields in the Power and Clock Gating Control
  59863. + * Register. Read the register into the <i>d32</i> member then set/clear the
  59864. + * bits using the <i>b</i>it elements.
  59865. + */
  59866. +typedef union pcgcctl_data {
  59867. + /** raw register data */
  59868. + uint32_t d32;
  59869. +
  59870. + /** register bits */
  59871. + struct {
  59872. + /** Stop Pclk */
  59873. + unsigned stoppclk:1;
  59874. + /** Gate Hclk */
  59875. + unsigned gatehclk:1;
  59876. + /** Power Clamp */
  59877. + unsigned pwrclmp:1;
  59878. + /** Reset Power Down Modules */
  59879. + unsigned rstpdwnmodule:1;
  59880. + /** Reserved */
  59881. + unsigned reserved:1;
  59882. + /** Enable Sleep Clock Gating (Enbl_L1Gating) */
  59883. + unsigned enbl_sleep_gating:1;
  59884. + /** PHY In Sleep (PhySleep) */
  59885. + unsigned phy_in_sleep:1;
  59886. + /** Deep Sleep*/
  59887. + unsigned deep_sleep:1;
  59888. + unsigned resetaftsusp:1;
  59889. + unsigned restoremode:1;
  59890. + unsigned enbl_extnd_hiber:1;
  59891. + unsigned extnd_hiber_pwrclmp:1;
  59892. + unsigned extnd_hiber_switch:1;
  59893. + unsigned ess_reg_restored:1;
  59894. + unsigned prt_clk_sel:2;
  59895. + unsigned port_power:1;
  59896. + unsigned max_xcvrselect:2;
  59897. + unsigned max_termsel:1;
  59898. + unsigned mac_dev_addr:7;
  59899. + unsigned p2hd_dev_enum_spd:2;
  59900. + unsigned p2hd_prt_spd:2;
  59901. + unsigned if_dev_mode:1;
  59902. + } b;
  59903. +} pcgcctl_data_t;
  59904. +
  59905. +/**
  59906. + * This union represents the bit fields in the Global Data FIFO Software
  59907. + * Configuration Register. Read the register into the <i>d32</i> member then
  59908. + * set/clear the bits using the <i>b</i>it elements.
  59909. + */
  59910. +typedef union gdfifocfg_data {
  59911. + /* raw register data */
  59912. + uint32_t d32;
  59913. + /** register bits */
  59914. + struct {
  59915. + /** OTG Data FIFO depth */
  59916. + unsigned gdfifocfg:16;
  59917. + /** Start address of EP info controller */
  59918. + unsigned epinfobase:16;
  59919. + } b;
  59920. +} gdfifocfg_data_t;
  59921. +
  59922. +/**
  59923. + * This union represents the bit fields in the Global Power Down Register
  59924. + * Register. Read the register into the <i>d32</i> member then set/clear the
  59925. + * bits using the <i>b</i>it elements.
  59926. + */
  59927. +typedef union gpwrdn_data {
  59928. + /* raw register data */
  59929. + uint32_t d32;
  59930. +
  59931. + /** register bits */
  59932. + struct {
  59933. + /** PMU Interrupt Select */
  59934. + unsigned pmuintsel:1;
  59935. + /** PMU Active */
  59936. + unsigned pmuactv:1;
  59937. + /** Restore */
  59938. + unsigned restore:1;
  59939. + /** Power Down Clamp */
  59940. + unsigned pwrdnclmp:1;
  59941. + /** Power Down Reset */
  59942. + unsigned pwrdnrstn:1;
  59943. + /** Power Down Switch */
  59944. + unsigned pwrdnswtch:1;
  59945. + /** Disable VBUS */
  59946. + unsigned dis_vbus:1;
  59947. + /** Line State Change */
  59948. + unsigned lnstschng:1;
  59949. + /** Line state change mask */
  59950. + unsigned lnstchng_msk:1;
  59951. + /** Reset Detected */
  59952. + unsigned rst_det:1;
  59953. + /** Reset Detect mask */
  59954. + unsigned rst_det_msk:1;
  59955. + /** Disconnect Detected */
  59956. + unsigned disconn_det:1;
  59957. + /** Disconnect Detect mask */
  59958. + unsigned disconn_det_msk:1;
  59959. + /** Connect Detected*/
  59960. + unsigned connect_det:1;
  59961. + /** Connect Detected Mask*/
  59962. + unsigned connect_det_msk:1;
  59963. + /** SRP Detected */
  59964. + unsigned srp_det:1;
  59965. + /** SRP Detect mask */
  59966. + unsigned srp_det_msk:1;
  59967. + /** Status Change Interrupt */
  59968. + unsigned sts_chngint:1;
  59969. + /** Status Change Interrupt Mask */
  59970. + unsigned sts_chngint_msk:1;
  59971. + /** Line State */
  59972. + unsigned linestate:2;
  59973. + /** Indicates current mode(status of IDDIG signal) */
  59974. + unsigned idsts:1;
  59975. + /** B Session Valid signal status*/
  59976. + unsigned bsessvld:1;
  59977. + /** ADP Event Detected */
  59978. + unsigned adp_int:1;
  59979. + /** Multi Valued ID pin */
  59980. + unsigned mult_val_id_bc:5;
  59981. + /** Reserved 24_31 */
  59982. + unsigned reserved29_31:3;
  59983. + } b;
  59984. +} gpwrdn_data_t;
  59985. +
  59986. +#endif
  59987. --- /dev/null
  59988. +++ b/drivers/usb/host/dwc_otg/test/Makefile
  59989. @@ -0,0 +1,16 @@
  59990. +
  59991. +PERL=/usr/bin/perl
  59992. +PL_TESTS=test_sysfs.pl test_mod_param.pl
  59993. +
  59994. +.PHONY : test
  59995. +test : perl_tests
  59996. +
  59997. +perl_tests :
  59998. + @echo
  59999. + @echo Running perl tests
  60000. + @for test in $(PL_TESTS); do \
  60001. + if $(PERL) ./$$test ; then \
  60002. + echo "=======> $$test, PASSED" ; \
  60003. + else echo "=======> $$test, FAILED" ; \
  60004. + fi \
  60005. + done
  60006. --- /dev/null
  60007. +++ b/drivers/usb/host/dwc_otg/test/dwc_otg_test.pm
  60008. @@ -0,0 +1,337 @@
  60009. +package dwc_otg_test;
  60010. +
  60011. +use strict;
  60012. +use Exporter ();
  60013. +
  60014. +use vars qw(@ISA @EXPORT
  60015. +$sysfsdir $paramdir $errors $params
  60016. +);
  60017. +
  60018. +@ISA = qw(Exporter);
  60019. +
  60020. +#
  60021. +# Globals
  60022. +#
  60023. +$sysfsdir = "/sys/devices/lm0";
  60024. +$paramdir = "/sys/module/dwc_otg";
  60025. +$errors = 0;
  60026. +
  60027. +$params = [
  60028. + {
  60029. + NAME => "otg_cap",
  60030. + DEFAULT => 0,
  60031. + ENUM => [],
  60032. + LOW => 0,
  60033. + HIGH => 2
  60034. + },
  60035. + {
  60036. + NAME => "dma_enable",
  60037. + DEFAULT => 0,
  60038. + ENUM => [],
  60039. + LOW => 0,
  60040. + HIGH => 1
  60041. + },
  60042. + {
  60043. + NAME => "dma_burst_size",
  60044. + DEFAULT => 32,
  60045. + ENUM => [1, 4, 8, 16, 32, 64, 128, 256],
  60046. + LOW => 1,
  60047. + HIGH => 256
  60048. + },
  60049. + {
  60050. + NAME => "host_speed",
  60051. + DEFAULT => 0,
  60052. + ENUM => [],
  60053. + LOW => 0,
  60054. + HIGH => 1
  60055. + },
  60056. + {
  60057. + NAME => "host_support_fs_ls_low_power",
  60058. + DEFAULT => 0,
  60059. + ENUM => [],
  60060. + LOW => 0,
  60061. + HIGH => 1
  60062. + },
  60063. + {
  60064. + NAME => "host_ls_low_power_phy_clk",
  60065. + DEFAULT => 0,
  60066. + ENUM => [],
  60067. + LOW => 0,
  60068. + HIGH => 1
  60069. + },
  60070. + {
  60071. + NAME => "dev_speed",
  60072. + DEFAULT => 0,
  60073. + ENUM => [],
  60074. + LOW => 0,
  60075. + HIGH => 1
  60076. + },
  60077. + {
  60078. + NAME => "enable_dynamic_fifo",
  60079. + DEFAULT => 1,
  60080. + ENUM => [],
  60081. + LOW => 0,
  60082. + HIGH => 1
  60083. + },
  60084. + {
  60085. + NAME => "data_fifo_size",
  60086. + DEFAULT => 8192,
  60087. + ENUM => [],
  60088. + LOW => 32,
  60089. + HIGH => 32768
  60090. + },
  60091. + {
  60092. + NAME => "dev_rx_fifo_size",
  60093. + DEFAULT => 1064,
  60094. + ENUM => [],
  60095. + LOW => 16,
  60096. + HIGH => 32768
  60097. + },
  60098. + {
  60099. + NAME => "dev_nperio_tx_fifo_size",
  60100. + DEFAULT => 1024,
  60101. + ENUM => [],
  60102. + LOW => 16,
  60103. + HIGH => 32768
  60104. + },
  60105. + {
  60106. + NAME => "dev_perio_tx_fifo_size_1",
  60107. + DEFAULT => 256,
  60108. + ENUM => [],
  60109. + LOW => 4,
  60110. + HIGH => 768
  60111. + },
  60112. + {
  60113. + NAME => "dev_perio_tx_fifo_size_2",
  60114. + DEFAULT => 256,
  60115. + ENUM => [],
  60116. + LOW => 4,
  60117. + HIGH => 768
  60118. + },
  60119. + {
  60120. + NAME => "dev_perio_tx_fifo_size_3",
  60121. + DEFAULT => 256,
  60122. + ENUM => [],
  60123. + LOW => 4,
  60124. + HIGH => 768
  60125. + },
  60126. + {
  60127. + NAME => "dev_perio_tx_fifo_size_4",
  60128. + DEFAULT => 256,
  60129. + ENUM => [],
  60130. + LOW => 4,
  60131. + HIGH => 768
  60132. + },
  60133. + {
  60134. + NAME => "dev_perio_tx_fifo_size_5",
  60135. + DEFAULT => 256,
  60136. + ENUM => [],
  60137. + LOW => 4,
  60138. + HIGH => 768
  60139. + },
  60140. + {
  60141. + NAME => "dev_perio_tx_fifo_size_6",
  60142. + DEFAULT => 256,
  60143. + ENUM => [],
  60144. + LOW => 4,
  60145. + HIGH => 768
  60146. + },
  60147. + {
  60148. + NAME => "dev_perio_tx_fifo_size_7",
  60149. + DEFAULT => 256,
  60150. + ENUM => [],
  60151. + LOW => 4,
  60152. + HIGH => 768
  60153. + },
  60154. + {
  60155. + NAME => "dev_perio_tx_fifo_size_8",
  60156. + DEFAULT => 256,
  60157. + ENUM => [],
  60158. + LOW => 4,
  60159. + HIGH => 768
  60160. + },
  60161. + {
  60162. + NAME => "dev_perio_tx_fifo_size_9",
  60163. + DEFAULT => 256,
  60164. + ENUM => [],
  60165. + LOW => 4,
  60166. + HIGH => 768
  60167. + },
  60168. + {
  60169. + NAME => "dev_perio_tx_fifo_size_10",
  60170. + DEFAULT => 256,
  60171. + ENUM => [],
  60172. + LOW => 4,
  60173. + HIGH => 768
  60174. + },
  60175. + {
  60176. + NAME => "dev_perio_tx_fifo_size_11",
  60177. + DEFAULT => 256,
  60178. + ENUM => [],
  60179. + LOW => 4,
  60180. + HIGH => 768
  60181. + },
  60182. + {
  60183. + NAME => "dev_perio_tx_fifo_size_12",
  60184. + DEFAULT => 256,
  60185. + ENUM => [],
  60186. + LOW => 4,
  60187. + HIGH => 768
  60188. + },
  60189. + {
  60190. + NAME => "dev_perio_tx_fifo_size_13",
  60191. + DEFAULT => 256,
  60192. + ENUM => [],
  60193. + LOW => 4,
  60194. + HIGH => 768
  60195. + },
  60196. + {
  60197. + NAME => "dev_perio_tx_fifo_size_14",
  60198. + DEFAULT => 256,
  60199. + ENUM => [],
  60200. + LOW => 4,
  60201. + HIGH => 768
  60202. + },
  60203. + {
  60204. + NAME => "dev_perio_tx_fifo_size_15",
  60205. + DEFAULT => 256,
  60206. + ENUM => [],
  60207. + LOW => 4,
  60208. + HIGH => 768
  60209. + },
  60210. + {
  60211. + NAME => "host_rx_fifo_size",
  60212. + DEFAULT => 1024,
  60213. + ENUM => [],
  60214. + LOW => 16,
  60215. + HIGH => 32768
  60216. + },
  60217. + {
  60218. + NAME => "host_nperio_tx_fifo_size",
  60219. + DEFAULT => 1024,
  60220. + ENUM => [],
  60221. + LOW => 16,
  60222. + HIGH => 32768
  60223. + },
  60224. + {
  60225. + NAME => "host_perio_tx_fifo_size",
  60226. + DEFAULT => 1024,
  60227. + ENUM => [],
  60228. + LOW => 16,
  60229. + HIGH => 32768
  60230. + },
  60231. + {
  60232. + NAME => "max_transfer_size",
  60233. + DEFAULT => 65535,
  60234. + ENUM => [],
  60235. + LOW => 2047,
  60236. + HIGH => 65535
  60237. + },
  60238. + {
  60239. + NAME => "max_packet_count",
  60240. + DEFAULT => 511,
  60241. + ENUM => [],
  60242. + LOW => 15,
  60243. + HIGH => 511
  60244. + },
  60245. + {
  60246. + NAME => "host_channels",
  60247. + DEFAULT => 12,
  60248. + ENUM => [],
  60249. + LOW => 1,
  60250. + HIGH => 16
  60251. + },
  60252. + {
  60253. + NAME => "dev_endpoints",
  60254. + DEFAULT => 6,
  60255. + ENUM => [],
  60256. + LOW => 1,
  60257. + HIGH => 15
  60258. + },
  60259. + {
  60260. + NAME => "phy_type",
  60261. + DEFAULT => 1,
  60262. + ENUM => [],
  60263. + LOW => 0,
  60264. + HIGH => 2
  60265. + },
  60266. + {
  60267. + NAME => "phy_utmi_width",
  60268. + DEFAULT => 16,
  60269. + ENUM => [8, 16],
  60270. + LOW => 8,
  60271. + HIGH => 16
  60272. + },
  60273. + {
  60274. + NAME => "phy_ulpi_ddr",
  60275. + DEFAULT => 0,
  60276. + ENUM => [],
  60277. + LOW => 0,
  60278. + HIGH => 1
  60279. + },
  60280. + ];
  60281. +
  60282. +
  60283. +#
  60284. +#
  60285. +sub check_arch {
  60286. + $_ = `uname -m`;
  60287. + chomp;
  60288. + unless (m/armv4tl/) {
  60289. + warn "# \n# Can't execute on $_. Run on integrator platform.\n# \n";
  60290. + return 0;
  60291. + }
  60292. + return 1;
  60293. +}
  60294. +
  60295. +#
  60296. +#
  60297. +sub load_module {
  60298. + my $params = shift;
  60299. + print "\nRemoving Module\n";
  60300. + system "rmmod dwc_otg";
  60301. + print "Loading Module\n";
  60302. + if ($params ne "") {
  60303. + print "Module Parameters: $params\n";
  60304. + }
  60305. + if (system("modprobe dwc_otg $params")) {
  60306. + warn "Unable to load module\n";
  60307. + return 0;
  60308. + }
  60309. + return 1;
  60310. +}
  60311. +
  60312. +#
  60313. +#
  60314. +sub test_status {
  60315. + my $arg = shift;
  60316. +
  60317. + print "\n";
  60318. +
  60319. + if (defined $arg) {
  60320. + warn "WARNING: $arg\n";
  60321. + }
  60322. +
  60323. + if ($errors > 0) {
  60324. + warn "TEST FAILED with $errors errors\n";
  60325. + return 0;
  60326. + } else {
  60327. + print "TEST PASSED\n";
  60328. + return 0 if (defined $arg);
  60329. + }
  60330. + return 1;
  60331. +}
  60332. +
  60333. +#
  60334. +#
  60335. +@EXPORT = qw(
  60336. +$sysfsdir
  60337. +$paramdir
  60338. +$params
  60339. +$errors
  60340. +check_arch
  60341. +load_module
  60342. +test_status
  60343. +);
  60344. +
  60345. +1;
  60346. --- /dev/null
  60347. +++ b/drivers/usb/host/dwc_otg/test/test_mod_param.pl
  60348. @@ -0,0 +1,133 @@
  60349. +#!/usr/bin/perl -w
  60350. +#
  60351. +# Run this program on the integrator.
  60352. +#
  60353. +# - Tests module parameter default values.
  60354. +# - Tests setting of valid module parameter values via modprobe.
  60355. +# - Tests invalid module parameter values.
  60356. +# -----------------------------------------------------------------------------
  60357. +use strict;
  60358. +use dwc_otg_test;
  60359. +
  60360. +check_arch() or die;
  60361. +
  60362. +#
  60363. +#
  60364. +sub test {
  60365. + my ($param,$expected) = @_;
  60366. + my $value = get($param);
  60367. +
  60368. + if ($value == $expected) {
  60369. + print "$param = $value, okay\n";
  60370. + }
  60371. +
  60372. + else {
  60373. + warn "ERROR: value of $param != $expected, $value\n";
  60374. + $errors ++;
  60375. + }
  60376. +}
  60377. +
  60378. +#
  60379. +#
  60380. +sub get {
  60381. + my $param = shift;
  60382. + my $tmp = `cat $paramdir/$param`;
  60383. + chomp $tmp;
  60384. + return $tmp;
  60385. +}
  60386. +
  60387. +#
  60388. +#
  60389. +sub test_main {
  60390. +
  60391. + print "\nTesting Module Parameters\n";
  60392. +
  60393. + load_module("") or die;
  60394. +
  60395. + # Test initial values
  60396. + print "\nTesting Default Values\n";
  60397. + foreach (@{$params}) {
  60398. + test ($_->{NAME}, $_->{DEFAULT});
  60399. + }
  60400. +
  60401. + # Test low value
  60402. + print "\nTesting Low Value\n";
  60403. + my $cmd_params = "";
  60404. + foreach (@{$params}) {
  60405. + $cmd_params = $cmd_params . "$_->{NAME}=$_->{LOW} ";
  60406. + }
  60407. + load_module($cmd_params) or die;
  60408. +
  60409. + foreach (@{$params}) {
  60410. + test ($_->{NAME}, $_->{LOW});
  60411. + }
  60412. +
  60413. + # Test high value
  60414. + print "\nTesting High Value\n";
  60415. + $cmd_params = "";
  60416. + foreach (@{$params}) {
  60417. + $cmd_params = $cmd_params . "$_->{NAME}=$_->{HIGH} ";
  60418. + }
  60419. + load_module($cmd_params) or die;
  60420. +
  60421. + foreach (@{$params}) {
  60422. + test ($_->{NAME}, $_->{HIGH});
  60423. + }
  60424. +
  60425. + # Test Enum
  60426. + print "\nTesting Enumerated\n";
  60427. + foreach (@{$params}) {
  60428. + if (defined $_->{ENUM}) {
  60429. + my $value;
  60430. + foreach $value (@{$_->{ENUM}}) {
  60431. + $cmd_params = "$_->{NAME}=$value";
  60432. + load_module($cmd_params) or die;
  60433. + test ($_->{NAME}, $value);
  60434. + }
  60435. + }
  60436. + }
  60437. +
  60438. + # Test Invalid Values
  60439. + print "\nTesting Invalid Values\n";
  60440. + $cmd_params = "";
  60441. + foreach (@{$params}) {
  60442. + $cmd_params = $cmd_params . sprintf "$_->{NAME}=%d ", $_->{LOW}-1;
  60443. + }
  60444. + load_module($cmd_params) or die;
  60445. +
  60446. + foreach (@{$params}) {
  60447. + test ($_->{NAME}, $_->{DEFAULT});
  60448. + }
  60449. +
  60450. + $cmd_params = "";
  60451. + foreach (@{$params}) {
  60452. + $cmd_params = $cmd_params . sprintf "$_->{NAME}=%d ", $_->{HIGH}+1;
  60453. + }
  60454. + load_module($cmd_params) or die;
  60455. +
  60456. + foreach (@{$params}) {
  60457. + test ($_->{NAME}, $_->{DEFAULT});
  60458. + }
  60459. +
  60460. + print "\nTesting Enumerated\n";
  60461. + foreach (@{$params}) {
  60462. + if (defined $_->{ENUM}) {
  60463. + my $value;
  60464. + foreach $value (@{$_->{ENUM}}) {
  60465. + $value = $value + 1;
  60466. + $cmd_params = "$_->{NAME}=$value";
  60467. + load_module($cmd_params) or die;
  60468. + test ($_->{NAME}, $_->{DEFAULT});
  60469. + $value = $value - 2;
  60470. + $cmd_params = "$_->{NAME}=$value";
  60471. + load_module($cmd_params) or die;
  60472. + test ($_->{NAME}, $_->{DEFAULT});
  60473. + }
  60474. + }
  60475. + }
  60476. +
  60477. + test_status() or die;
  60478. +}
  60479. +
  60480. +test_main();
  60481. +0;
  60482. --- /dev/null
  60483. +++ b/drivers/usb/host/dwc_otg/test/test_sysfs.pl
  60484. @@ -0,0 +1,193 @@
  60485. +#!/usr/bin/perl -w
  60486. +#
  60487. +# Run this program on the integrator
  60488. +# - Tests select sysfs attributes.
  60489. +# - Todo ... test more attributes, hnp/srp, buspower/bussuspend, etc.
  60490. +# -----------------------------------------------------------------------------
  60491. +use strict;
  60492. +use dwc_otg_test;
  60493. +
  60494. +check_arch() or die;
  60495. +
  60496. +#
  60497. +#
  60498. +sub test {
  60499. + my ($attr,$expected) = @_;
  60500. + my $string = get($attr);
  60501. +
  60502. + if ($string eq $expected) {
  60503. + printf("$attr = $string, okay\n");
  60504. + }
  60505. + else {
  60506. + warn "ERROR: value of $attr != $expected, $string\n";
  60507. + $errors ++;
  60508. + }
  60509. +}
  60510. +
  60511. +#
  60512. +#
  60513. +sub set {
  60514. + my ($reg, $value) = @_;
  60515. + system "echo $value > $sysfsdir/$reg";
  60516. +}
  60517. +
  60518. +#
  60519. +#
  60520. +sub get {
  60521. + my $attr = shift;
  60522. + my $string = `cat $sysfsdir/$attr`;
  60523. + chomp $string;
  60524. + if ($string =~ m/\s\=\s/) {
  60525. + my $tmp;
  60526. + ($tmp, $string) = split /\s=\s/, $string;
  60527. + }
  60528. + return $string;
  60529. +}
  60530. +
  60531. +#
  60532. +#
  60533. +sub test_main {
  60534. + print("\nTesting Sysfs Attributes\n");
  60535. +
  60536. + load_module("") or die;
  60537. +
  60538. + # Test initial values of regoffset/regvalue/guid/gsnpsid
  60539. + print("\nTesting Default Values\n");
  60540. +
  60541. + test("regoffset", "0xffffffff");
  60542. + test("regvalue", "invalid offset");
  60543. + test("guid", "0x12345678"); # this will fail if it has been changed
  60544. + test("gsnpsid", "0x4f54200a");
  60545. +
  60546. + # Test operation of regoffset/regvalue
  60547. + print("\nTesting regoffset\n");
  60548. + set('regoffset', '5a5a5a5a');
  60549. + test("regoffset", "0xffffffff");
  60550. +
  60551. + set('regoffset', '0');
  60552. + test("regoffset", "0x00000000");
  60553. +
  60554. + set('regoffset', '40000');
  60555. + test("regoffset", "0x00000000");
  60556. +
  60557. + set('regoffset', '3ffff');
  60558. + test("regoffset", "0x0003ffff");
  60559. +
  60560. + set('regoffset', '1');
  60561. + test("regoffset", "0x00000001");
  60562. +
  60563. + print("\nTesting regvalue\n");
  60564. + set('regoffset', '3c');
  60565. + test("regvalue", "0x12345678");
  60566. + set('regvalue', '5a5a5a5a');
  60567. + test("regvalue", "0x5a5a5a5a");
  60568. + set('regvalue','a5a5a5a5');
  60569. + test("regvalue", "0xa5a5a5a5");
  60570. + set('guid','12345678');
  60571. +
  60572. + # Test HNP Capable
  60573. + print("\nTesting HNP Capable bit\n");
  60574. + set('hnpcapable', '1');
  60575. + test("hnpcapable", "0x1");
  60576. + set('hnpcapable','0');
  60577. + test("hnpcapable", "0x0");
  60578. +
  60579. + set('regoffset','0c');
  60580. +
  60581. + my $old = get('gusbcfg');
  60582. + print("setting hnpcapable\n");
  60583. + set('hnpcapable', '1');
  60584. + test("hnpcapable", "0x1");
  60585. + test('gusbcfg', sprintf "0x%08x", (oct ($old) | (1<<9)));
  60586. + test('regvalue', sprintf "0x%08x", (oct ($old) | (1<<9)));
  60587. +
  60588. + $old = get('gusbcfg');
  60589. + print("clearing hnpcapable\n");
  60590. + set('hnpcapable', '0');
  60591. + test("hnpcapable", "0x0");
  60592. + test ('gusbcfg', sprintf "0x%08x", oct ($old) & (~(1<<9)));
  60593. + test ('regvalue', sprintf "0x%08x", oct ($old) & (~(1<<9)));
  60594. +
  60595. + # Test SRP Capable
  60596. + print("\nTesting SRP Capable bit\n");
  60597. + set('srpcapable', '1');
  60598. + test("srpcapable", "0x1");
  60599. + set('srpcapable','0');
  60600. + test("srpcapable", "0x0");
  60601. +
  60602. + set('regoffset','0c');
  60603. +
  60604. + $old = get('gusbcfg');
  60605. + print("setting srpcapable\n");
  60606. + set('srpcapable', '1');
  60607. + test("srpcapable", "0x1");
  60608. + test('gusbcfg', sprintf "0x%08x", (oct ($old) | (1<<8)));
  60609. + test('regvalue', sprintf "0x%08x", (oct ($old) | (1<<8)));
  60610. +
  60611. + $old = get('gusbcfg');
  60612. + print("clearing srpcapable\n");
  60613. + set('srpcapable', '0');
  60614. + test("srpcapable", "0x0");
  60615. + test('gusbcfg', sprintf "0x%08x", oct ($old) & (~(1<<8)));
  60616. + test('regvalue', sprintf "0x%08x", oct ($old) & (~(1<<8)));
  60617. +
  60618. + # Test GGPIO
  60619. + print("\nTesting GGPIO\n");
  60620. + set('ggpio','5a5a5a5a');
  60621. + test('ggpio','0x5a5a0000');
  60622. + set('ggpio','a5a5a5a5');
  60623. + test('ggpio','0xa5a50000');
  60624. + set('ggpio','11110000');
  60625. + test('ggpio','0x11110000');
  60626. + set('ggpio','00001111');
  60627. + test('ggpio','0x00000000');
  60628. +
  60629. + # Test DEVSPEED
  60630. + print("\nTesting DEVSPEED\n");
  60631. + set('regoffset','800');
  60632. + $old = get('regvalue');
  60633. + set('devspeed','0');
  60634. + test('devspeed','0x0');
  60635. + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3)));
  60636. + set('devspeed','1');
  60637. + test('devspeed','0x1');
  60638. + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3) | 1));
  60639. + set('devspeed','2');
  60640. + test('devspeed','0x2');
  60641. + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3) | 2));
  60642. + set('devspeed','3');
  60643. + test('devspeed','0x3');
  60644. + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3) | 3));
  60645. + set('devspeed','4');
  60646. + test('devspeed','0x0');
  60647. + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3)));
  60648. + set('devspeed','5');
  60649. + test('devspeed','0x1');
  60650. + test('regvalue',sprintf("0x%08x", oct($old) & ~(0x3) | 1));
  60651. +
  60652. +
  60653. + # mode Returns the current mode:0 for device mode1 for host mode Read
  60654. + # hnp Initiate the Host Negotiation Protocol. Read returns the status. Read/Write
  60655. + # srp Initiate the Session Request Protocol. Read returns the status. Read/Write
  60656. + # buspower Get or Set the Power State of the bus (0 - Off or 1 - On) Read/Write
  60657. + # bussuspend Suspend the USB bus. Read/Write
  60658. + # busconnected Get the connection status of the bus Read
  60659. +
  60660. + # gotgctl Get or set the Core Control Status Register. Read/Write
  60661. + ## gusbcfg Get or set the Core USB Configuration Register Read/Write
  60662. + # grxfsiz Get or set the Receive FIFO Size Register Read/Write
  60663. + # gnptxfsiz Get or set the non-periodic Transmit Size Register Read/Write
  60664. + # gpvndctl Get or set the PHY Vendor Control Register Read/Write
  60665. + ## ggpio Get the value in the lower 16-bits of the General Purpose IO Register or Set the upper 16 bits. Read/Write
  60666. + ## guid Get or set the value of the User ID Register Read/Write
  60667. + ## gsnpsid Get the value of the Synopsys ID Regester Read
  60668. + ## devspeed Get or set the device speed setting in the DCFG register Read/Write
  60669. + # enumspeed Gets the device enumeration Speed. Read
  60670. + # hptxfsiz Get the value of the Host Periodic Transmit FIFO Read
  60671. + # hprt0 Get or Set the value in the Host Port Control and Status Register Read/Write
  60672. +
  60673. + test_status("TEST NYI") or die;
  60674. +}
  60675. +
  60676. +test_main();
  60677. +0;